Skip to content

Commit

Permalink
TX-15590: Links localization for custom post types
Browse files Browse the repository at this point in the history
  • Loading branch information
foteinigk committed Aug 5, 2024
1 parent 2052696 commit 439335b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion includes/lib/transifex-live-integration-picker.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ function render() {
// Otherwise remove the part from url string until the language prefix
// e.g el/sample_page =>sample_page
if (strpos($url_path, $lang ) !== false && $url_path !== $lang && strpos($url_path, $lang .'/' ) === false) {
$source_url_path = '/' . $url_path;
$source_url_path = '/' . ltrim( $url_path, '/' );
} else {
$source_url_path = (substr($url_path, 0, strlen($lang)) === $lang) ? substr($url_path, strlen($lang)) : $url_path;
}
Expand Down
27 changes: 25 additions & 2 deletions includes/lib/transifex-live-integration-rewrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,29 @@ function post_link_hook( $permalink, $post, $leavename ) {
return $retlink;
}

/**
* Filters and processes the given field value to handle URLs and localize content.
*
* This function determines whether the provided field value is a valid URL. If it is a URL,
* it localizes the URL by calling the `reverse_hard_link` method. If the field value is not a URL,
* the method `the_content_hook` is invoked to localize all anchor (`<a>`) href links within the text content.
*
* @param string $field_value The field value to be processed, which can be a URL or custom content.
* @return string The processed field value after handling URLs or applying content hooks.
*/
function custom_field_link_hook($field_value) {
if (filter_var($field_value, FILTER_VALIDATE_URL)) {
if ( !Transifex_Live_Integration_Validators::is_hard_link_ok( $field_value ) ) {
return $field_value;
}
$field_value = $this->reverse_hard_link( $this->lang, $field_value, $this->languages_map, $this->source_language, $this->rewrite_pattern );
} else {
$field_value = $this->the_content_hook($field_value);
}

return $field_value;
}

/*
* WP post_type_archive_link filter, filters archive links
* @param string $link The link to filter
Expand Down Expand Up @@ -372,11 +395,11 @@ function the_content_hook( $string) {
}

/*
* WP comment_form_field_comment filter, to add a hidden field to the comment form
* WP comment_form_field_comment filter, to add a hidden field to the comment form
* that will be used to redirect the user to the same page after submitting the comment.
* We append the the comment field HTML with the hidden field.
* @param string $comment_form_field_comment The comment field HTML
* @return string The filtered comment field HTML
* @return string The filtered comment field HTML
*/
function add_redirect_to_comments_form_hook( $comment_form_field_comment) {
Plugin_Debug::logTrace();
Expand Down
10 changes: 7 additions & 3 deletions transifex-live-integration-main.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ static function do_plugin( $is_admin, $version ) {

$settings = Transifex_Live_Integration_Defaults::settings();
}
if (!isset($settings['is_subdirectory_install'])) {
$settings['is_subdirectory_install'] = 0;
}
$live_settings = Transifex_Live_Integration_Defaults::transifex_settings();
$debug_mode = ($settings['debug']) ? true : false;

Expand Down Expand Up @@ -160,6 +157,13 @@ static function do_plugin( $is_admin, $version ) {
// Add filter for custom content that is not triggered by any other hook
add_filter('tx_link', [ $rewrite,'the_content_hook'], 10 ,1);
add_filter( 'comment_form_field_comment', [ $rewrite, 'add_redirect_to_comments_form_hook'], 10, 1);

// Add filters for custom post types
add_filter( 'post_type_link', [$rewrite, 'pre_post_link_hook'], 10, 3 );
add_filter( 'post_type_link', [$rewrite, 'post_link_hook'], 10, 3 );

// Add filters for acf fields values
add_filter('acf/format_value', [$rewrite, 'custom_field_link_hook'], 10, 3 );
}
}
$subdirectory = Transifex_Live_Integration_Static_Factory::create_subdirectory( $settings, $rewrite_options );
Expand Down

0 comments on commit 439335b

Please sign in to comment.