Skip to content

Commit

Permalink
Merge pull request #278 from transifex/TX-15503_toggle_canonical_urls
Browse files Browse the repository at this point in the history
Toggle canonical URLs
  • Loading branch information
svaloumas authored Jun 21, 2024
2 parents a22a5c9 + 55e8fc9 commit 7cfc4a2
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
7 changes: 6 additions & 1 deletion includes/admin/transifex-live-integration-admin-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,15 @@
</td>
</tr>
<tr>
<td style="padding-right:0px">
<td style="padding-right:0px; padding-bottom: 1px;">
<input name="transifex_live_settings[translate_urls]" type="checkbox" id="transifex_live_settings_translate_urls" value="1" <?php echo $checked_translate_urls ?>><?php _e( 'Make images and links translatable' ); ?>&nbsp;<a target="_blank" href="<?php echo $settings['urls']['translate_urls']; ?>"><b><?php _e( 'Learn more' ) ?></b></a>.
</td>
</tr>
<tr>
<td style="padding-right:0px">
<input name="transifex_live_settings[canonical_urls]" type="checkbox" id="transifex_live_settings_canonical_urls" value="1" <?php echo $checked_canonical_urls ?>><?php _e( 'Disable rendering canonical URLs' ); ?>.
</td>
</tr>
<tr>
<td>
<label for="transifex_live_settings_url_options">
Expand Down
9 changes: 9 additions & 0 deletions includes/admin/transifex-live-integration-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ static function options_page() {
checked( $settings['translate_urls'], 1 );
$checked_translate_urls = ob_get_clean();

ob_start();
checked( $settings['canonical_urls'], 1 );
$checked_canonical_urls = ob_get_clean();

ob_start();
checked( $settings['rewrite_option_all'], 1 );
$checked_rewrite_option_all = ob_get_clean();
Expand Down Expand Up @@ -212,9 +216,13 @@ static public function update_settings( $settings ) {
$transifex_languages = json_decode( stripslashes( $settings['transifex_live_settings']['transifex_languages'] ), true );

$is_subdirectory_install = false;
$disable_canonical_urls = false;
if ( isset($settings['transifex_live_settings']['is_subdirectory_install'])) {
$is_subdirectory_install = $settings['transifex_live_settings']['is_subdirectory_install'];
}
if ( isset($settings['transifex_live_settings']['disable_canonical_urls'])) {
$disable_canonical_urls = $settings['transifex_live_settings']['disable_canonical_urls'];
}
$site_url = (new Transifex_Live_Integration_WP_Services($settings['transifex_live_settings']))->get_site_url();
$tokenized_url = Transifex_Live_Integration_Admin_Util::generate_tokenized_url( $site_url, $settings['transifex_live_settings']['url_options'] );
$settings['transifex_live_settings']['tokenized_url'] = $tokenized_url;
Expand Down Expand Up @@ -255,6 +263,7 @@ static public function update_settings( $settings ) {
$settings['transifex_live_settings']['rewrite_pattern'] = $rewrite_pattern;
$settings['transifex_live_settings']['languages_regex'] = $languages_regex;
$settings['transifex_live_settings']['languages'] = $languages;
$settings['transifex_live_settings']['disable_canonical_urls'] = $disable_canonical_urls;
if ( isset( $settings['transifex_live_settings'] ) ) {
update_option( 'transifex_live_settings', $settings['transifex_live_settings'] );
}
Expand Down
14 changes: 14 additions & 0 deletions includes/lib/transifex-live-integration-hreflang.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ public function render_hreflang() {
return false;
}
global $wp;
$disable_canonical_urls = $this->settings['canonical_urls'];
$lang = get_query_var( 'lang' );
$url_path = add_query_arg( array(), $wp->request );
$source_url_path = (substr( $url_path, 0, strlen( $lang ) ) === $lang) ? substr( $url_path, strlen( $lang ), strlen( $url_path ) ) : $url_path;
Expand Down Expand Up @@ -164,6 +165,19 @@ public function render_hreflang() {
$hreflang_out .= <<<XDEFAULT
<link rel="alternate" href="$source_url" hreflang="x-default"/>\n
XDEFAULT;
if (!$disable_canonical_urls) {
$canonical_url = home_url('/');
foreach ($hreflangs as $hreflang) {
if (!empty($url_path) && strpos($hreflang['href'], $url_path) !== false) {
$canonical_url = $hreflang['href'];
break;
} else {}
}

$hreflang_out .= <<<CANONICAL
<link rel="canonical" href="$canonical_url"/>\n
CANONICAL;
}
echo $hreflang_out;
return true;
}
Expand Down
1 change: 1 addition & 0 deletions includes/transifex-live-integration-defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ static function settings() {
// Wordpress is installed in a subdirectory like www.mydomain.com/cms but the site is accessible from www.mydomain.com
'is_subdirectory_install' => 0,
'translate_urls' => false,
'canonical_urls' => false,
'previous_api_key' => null,
'raw_transifex_languages' => null,
'transifex_languages' => null,
Expand Down

0 comments on commit 7cfc4a2

Please sign in to comment.