From 203d1ad5bd6e703241af2bf95cdbd7b6ef687fd6 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 7 Mar 2016 10:30:10 -0800 Subject: [PATCH 01/18] refactored admin ids --- ...fex-live-integration-settings-template.php | 12 +++---- ...ransifex-live-integration-settings-page.js | 36 +++++++++---------- 2 files changed, 24 insertions(+), 24 deletions(-) diff --git a/includes/admin/transifex-live-integration-settings-template.php b/includes/admin/transifex-live-integration-settings-template.php index 5271ddbe..31323108 100644 --- a/includes/admin/transifex-live-integration-settings-template.php +++ b/includes/admin/transifex-live-integration-settings-template.php @@ -1,7 +1,7 @@

-
+

  @@ -42,16 +42,16 @@

- - +
LanguageCode
+
LanguageCode
Your languages can't be loaded. Please re-check your API key.

-

-

Tweak your localized urls.

+

+

Tweak your localized urls.

@@ -76,7 +76,7 @@ -

+

| diff --git a/javascript/transifex-live-integration-settings-page.js b/javascript/transifex-live-integration-settings-page.js index ccae9524..b7a36a15 100755 --- a/javascript/transifex-live-integration-settings-page.js +++ b/javascript/transifex-live-integration-settings-page.js @@ -98,14 +98,14 @@ function addTransifexLanguages(obj) { jQuery.each(obj, function (i, o) { myName = 'transifex-integration-live-' + o.code; myId = 'transifex-integration-live-' + o.code; - jQuery('#language_map_table').append(jQuery('#language_map_template').clone().show().addClass('cloned-language-map').each(function (ii, oo) { + jQuery('#transifex_live_language_map_table').append(jQuery('#transifex_live_language_map_template').clone().show().addClass('cloned-language-map').each(function (ii, oo) { jQuery(oo).find('span.tx-language').text(o.tx_name); jQuery(oo).find('input.tx-code').attr('id', myId).attr('name', myName).val(lm[0][o.code]); })); }); jQuery.each(obj, function (i, o) { - jQuery('#transifex-integration-live-' + o.code).machine({defaultState: {onEnter: function () { + jQuery('#transifex_live_transifex-integration-live-' + o.code).machine({defaultState: {onEnter: function () { transifex_live_integration_map_update(tl); }, events: {change: 'defaultState'}}, }); @@ -114,7 +114,7 @@ function addTransifexLanguages(obj) { } else { jQuery.each(transifex_language_fields['html'], function (i, o) { - jQuery('#language_map_table').append(jQuery('#language_map_template').clone().show().addClass('cloned-language-map').each(function () { + jQuery('#transifex_live_language_map_table').append(jQuery('#transifex_live_language_map_template').clone().show().addClass('cloned-language-map').each(function () { jQuery(this).find('span.tx-language').text(o.caption); jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(o.value); jQuery(this).machine({defaultState: {onEnter: function () { @@ -222,7 +222,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_none').attr('disabled', true); $('#transifex_live_settings_url_options_subdirectory').attr('disabled', true); $('#transifex_live_settings_url_options_subdomain').attr('disabled', true); - $('input#submit').trigger('disable'); + $('input#transifex_live_submit').trigger('disable'); transifexLanguages(); }, events: {success: 'valid', blank: 'blank', error: 'error', notranslation: 'missing', change: 'validating'} @@ -239,7 +239,7 @@ function addTransifexLanguages(obj) { languages_override = false; } $('#transifex_live_settings_api_key_message').text('Success! Advanced SEO settings enabled.'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {success: 'valid', change: 'validating', validating: 'validating'} }, @@ -342,7 +342,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_subdirectory').prop("checked", false); $('#transifex_live_settings_url_options_subdomain').prop("checked", false); this.val('1'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {none: 'none', subdomain: 'subdomain', subdirectory: 'subdirectory'} }, @@ -357,7 +357,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_none').prop("checked", false); $('#transifex_live_settings_url_options_subdomain').prop("checked", false); this.val('3'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {none: 'none', subdomain: 'subdomain', subdirectory: 'subdirectory'} }, @@ -372,7 +372,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_subdirectory').prop("checked", false); $('#transifex_live_settings_url_options_none').prop("checked", false); this.val('2'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {none: 'none', subdomain: 'subdomain', subdirectory: 'subdirectory'} } @@ -406,7 +406,7 @@ function addTransifexLanguages(obj) { $.log.debug('transifex_live_settings_rewrite_option_all::off::onEnter'); this.prop('checked', false); $('.all_selector').trigger('off'); - $('input#submit').trigger('disable'); + $('input#transifex_live_submit').trigger('disable'); }, events: {click: 'on'} }, @@ -437,7 +437,7 @@ function addTransifexLanguages(obj) { onEnter: function () { $.log.debug('all_selector::on::onEnter'); this.prop("checked", true); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {click: 'off', off: 'off'} }, @@ -446,7 +446,7 @@ function addTransifexLanguages(obj) { $.log.debug('all_selector::off::onEnter'); this.prop("checked", false); $('#transifex_live_settings_rewrite_option_all').trigger('singleoff'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {click: 'on', on: 'on'} } @@ -454,10 +454,10 @@ function addTransifexLanguages(obj) { })(jQuery); (function ($) { - $('input#submit').machine({ + $('input#transifex_live_submit').machine({ defaultState: { onEnter: function () { - $.log.debug('transifex_live_settings_url_options_subdomain::input#submit::onEnter'); + $.log.debug('transifex_live_settings_url_options_subdomain::input#transifex_live_submit::onEnter'); this.trigger('disable'); }, events: {disable: 'disable'} @@ -491,30 +491,30 @@ function addTransifexLanguages(obj) { })(jQuery); (function ($) { - $('input#sync').machine({ + $('input#transifex_live_sync').machine({ defaultState: { onEnter: function () { - $.log.debug('input#sync::defaultState::onEnter'); + $.log.debug('input#transifex_live_sync::defaultState::onEnter'); this.trigger('wait'); }, events: {wait: 'wait'} }, wait: { onEnter: function () { - $.log.debug('input#sync::wait::onEnter'); + $.log.debug('input#transifex_live_sync::wait::onEnter'); }, events: {click: 'confirm'} }, confirm: { onEnter: function () { - $.log.debug('input#sync::confirm::onEnter'); + $.log.debug('input#transifex_live_sync::confirm::onEnter'); (confirm('Refreshing languages will replace your current codes with those from Transifex Live. Continue?')) ? this.trigger('refresh') : this.trigger('wait'); }, events: {refresh: 'refresh', wait: 'wait'} }, refresh: { onEnter: function () { - $.log.debug('input#sync::refresh::onEnter'); + $.log.debug('input#transifex_live_sync::refresh::onEnter'); languages_override = true; jQuery('#transifex_live_settings_api_key').trigger('validating'); From 67246f975754f052ec471fefc9e7063c99c5a58f Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 7 Mar 2016 10:30:10 -0800 Subject: [PATCH 02/18] fixed ids to all be prefixed with 'transifex' --- ...fex-live-integration-settings-template.php | 12 +++---- ...ransifex-live-integration-settings-page.js | 34 +++++++++---------- 2 files changed, 23 insertions(+), 23 deletions(-) diff --git a/includes/admin/transifex-live-integration-settings-template.php b/includes/admin/transifex-live-integration-settings-template.php index 5271ddbe..31323108 100644 --- a/includes/admin/transifex-live-integration-settings-template.php +++ b/includes/admin/transifex-live-integration-settings-template.php @@ -1,7 +1,7 @@

-
+

  @@ -42,16 +42,16 @@

- - +
LanguageCode
+
LanguageCode
Your languages can't be loaded. Please re-check your API key.

-

-

Tweak your localized urls.

+

+

Tweak your localized urls.

@@ -76,7 +76,7 @@ -

+

| diff --git a/javascript/transifex-live-integration-settings-page.js b/javascript/transifex-live-integration-settings-page.js index ccae9524..92912674 100755 --- a/javascript/transifex-live-integration-settings-page.js +++ b/javascript/transifex-live-integration-settings-page.js @@ -98,7 +98,7 @@ function addTransifexLanguages(obj) { jQuery.each(obj, function (i, o) { myName = 'transifex-integration-live-' + o.code; myId = 'transifex-integration-live-' + o.code; - jQuery('#language_map_table').append(jQuery('#language_map_template').clone().show().addClass('cloned-language-map').each(function (ii, oo) { + jQuery('#transifex_live_language_map_table').append(jQuery('#transifex_live_language_map_template').clone().show().addClass('cloned-language-map').each(function (ii, oo) { jQuery(oo).find('span.tx-language').text(o.tx_name); jQuery(oo).find('input.tx-code').attr('id', myId).attr('name', myName).val(lm[0][o.code]); })); @@ -114,7 +114,7 @@ function addTransifexLanguages(obj) { } else { jQuery.each(transifex_language_fields['html'], function (i, o) { - jQuery('#language_map_table').append(jQuery('#language_map_template').clone().show().addClass('cloned-language-map').each(function () { + jQuery('#transifex_live_language_map_table').append(jQuery('#transifex_live_language_map_template').clone().show().addClass('cloned-language-map').each(function () { jQuery(this).find('span.tx-language').text(o.caption); jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(o.value); jQuery(this).machine({defaultState: {onEnter: function () { @@ -222,7 +222,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_none').attr('disabled', true); $('#transifex_live_settings_url_options_subdirectory').attr('disabled', true); $('#transifex_live_settings_url_options_subdomain').attr('disabled', true); - $('input#submit').trigger('disable'); + $('input#transifex_live_submit').trigger('disable'); transifexLanguages(); }, events: {success: 'valid', blank: 'blank', error: 'error', notranslation: 'missing', change: 'validating'} @@ -239,7 +239,7 @@ function addTransifexLanguages(obj) { languages_override = false; } $('#transifex_live_settings_api_key_message').text('Success! Advanced SEO settings enabled.'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {success: 'valid', change: 'validating', validating: 'validating'} }, @@ -342,7 +342,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_subdirectory').prop("checked", false); $('#transifex_live_settings_url_options_subdomain').prop("checked", false); this.val('1'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {none: 'none', subdomain: 'subdomain', subdirectory: 'subdirectory'} }, @@ -357,7 +357,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_none').prop("checked", false); $('#transifex_live_settings_url_options_subdomain').prop("checked", false); this.val('3'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {none: 'none', subdomain: 'subdomain', subdirectory: 'subdirectory'} }, @@ -372,7 +372,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_subdirectory').prop("checked", false); $('#transifex_live_settings_url_options_none').prop("checked", false); this.val('2'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {none: 'none', subdomain: 'subdomain', subdirectory: 'subdirectory'} } @@ -406,7 +406,7 @@ function addTransifexLanguages(obj) { $.log.debug('transifex_live_settings_rewrite_option_all::off::onEnter'); this.prop('checked', false); $('.all_selector').trigger('off'); - $('input#submit').trigger('disable'); + $('input#transifex_live_submit').trigger('disable'); }, events: {click: 'on'} }, @@ -437,7 +437,7 @@ function addTransifexLanguages(obj) { onEnter: function () { $.log.debug('all_selector::on::onEnter'); this.prop("checked", true); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {click: 'off', off: 'off'} }, @@ -446,7 +446,7 @@ function addTransifexLanguages(obj) { $.log.debug('all_selector::off::onEnter'); this.prop("checked", false); $('#transifex_live_settings_rewrite_option_all').trigger('singleoff'); - $('input#submit').trigger('enable'); + $('input#transifex_live_submit').trigger('enable'); }, events: {click: 'on', on: 'on'} } @@ -454,10 +454,10 @@ function addTransifexLanguages(obj) { })(jQuery); (function ($) { - $('input#submit').machine({ + $('input#transifex_live_submit').machine({ defaultState: { onEnter: function () { - $.log.debug('transifex_live_settings_url_options_subdomain::input#submit::onEnter'); + $.log.debug('transifex_live_settings_url_options_subdomain::input#transifex_live_submit::onEnter'); this.trigger('disable'); }, events: {disable: 'disable'} @@ -491,30 +491,30 @@ function addTransifexLanguages(obj) { })(jQuery); (function ($) { - $('input#sync').machine({ + $('input#transifex_live_sync').machine({ defaultState: { onEnter: function () { - $.log.debug('input#sync::defaultState::onEnter'); + $.log.debug('input#transifex_live_sync::defaultState::onEnter'); this.trigger('wait'); }, events: {wait: 'wait'} }, wait: { onEnter: function () { - $.log.debug('input#sync::wait::onEnter'); + $.log.debug('input#transifex_live_sync::wait::onEnter'); }, events: {click: 'confirm'} }, confirm: { onEnter: function () { - $.log.debug('input#sync::confirm::onEnter'); + $.log.debug('input#transifex_live_sync::confirm::onEnter'); (confirm('Refreshing languages will replace your current codes with those from Transifex Live. Continue?')) ? this.trigger('refresh') : this.trigger('wait'); }, events: {refresh: 'refresh', wait: 'wait'} }, refresh: { onEnter: function () { - $.log.debug('input#sync::refresh::onEnter'); + $.log.debug('input#transifex_live_sync::refresh::onEnter'); languages_override = true; jQuery('#transifex_live_settings_api_key').trigger('validating'); From e3307e24273d918bd2e198dd0f4de586eafbe352 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 7 Mar 2016 13:48:27 -0800 Subject: [PATCH 03/18] Updates for issues Mapping fix for #119 Outer notranslate div added for #118 --- ...ansifex-live-integration-settings-page.php | 19 +++++-- ...ansifex-live-integration-settings-util.php | 10 ++++ includes/plugin-debug.php | 2 +- .../transifex-live-integration-defaults.php | 3 +- ...ransifex-live-integration-language-map.php | 52 +++++++++++++++++++ .../transifex-live-integration-rewrite.php | 6 +-- transifex-live-integration.php | 19 ++++++- 7 files changed, 101 insertions(+), 10 deletions(-) create mode 100644 includes/transifex-live-integration-language-map.php diff --git a/includes/admin/transifex-live-integration-settings-page.php b/includes/admin/transifex-live-integration-settings-page.php index b254e1c8..0287de67 100644 --- a/includes/admin/transifex-live-integration-settings-page.php +++ b/includes/admin/transifex-live-integration-settings-page.php @@ -115,8 +115,6 @@ static public function update_settings( $settings ) { $languages_map = (array) json_decode( stripslashes( $languages_map ), true ); - Plugin_Debug::logTrace( $transifex_languages ); - Plugin_Debug::logTrace( $languages_map ); $trim = false; $languages = ''; @@ -128,8 +126,7 @@ static public function update_settings( $settings ) { $languages_regex .= $languages_map[0][$lang]; $languages_regex .= "|"; } - Plugin_Debug::logTrace( $languages ); - Plugin_Debug::logTrace( $languages_regex ); + $languages = ($trim) ? rtrim( $languages, ',' ) : ''; $languages_regex = ($trim) ? rtrim( $languages_regex, '|' ) : ''; $languages_regex = '(' . $languages_regex . ')'; @@ -142,6 +139,20 @@ static public function update_settings( $settings ) { $array_url[2] = implode( '.', $array_domain ); $subdomain_pattern = implode( '/', $array_url ); } + + $url_regex = $settings['transifex_live_settings']['url_regex']; + if ($settings['transifex_live_settings']['url_options'] == '3') { //subdirectory + Plugin_Debug::logTrace(site_url()); + Plugin_Debug::logTrace($url_regex); + + + } + if ($settings['transifex_live_settings']['url_options'] == '2') { //subdomain + $subdomain_pattern = $settings['transifex_live_settings']['subdomain_pattern']; + Plugin_Debug::logTrace(site_url()); + Plugin_Debug::logTrace($subdomain_pattern); + Plugin_Debug::logTrace($url_regex); + } $settings['transifex_live_settings']['subdomain_pattern'] = $subdomain_pattern; $settings['transifex_live_settings']['language_map'] = $languages_map_string; diff --git a/includes/admin/transifex-live-integration-settings-util.php b/includes/admin/transifex-live-integration-settings-util.php index 25e72c20..293fe3f1 100644 --- a/includes/admin/transifex-live-integration-settings-util.php +++ b/includes/admin/transifex-live-integration-settings-util.php @@ -2,6 +2,16 @@ class Transifex_Live_Integration_Settings_Util { + static function wp_before_admin_bar_render_hook() { + Plugin_Debug::logTrace(); + echo ('

'); + } + + static function wp_after_admin_bar_render_hook() { + Plugin_Debug::logTrace(); + echo ('
'); + } + static function render_url_options( $options ) { $html = ''; $row = ''; diff --git a/includes/plugin-debug.php b/includes/plugin-debug.php index 6b3fd5cb..e137f7a2 100644 --- a/includes/plugin-debug.php +++ b/includes/plugin-debug.php @@ -18,7 +18,7 @@ class Plugin_Debug { * Define the core functionality of the plugin. */ public function __construct() { - self::$debug_mode = false; + self::$debug_mode = true; $this->logTrace(); // Check to see if plugin is in debug mode diff --git a/includes/transifex-live-integration-defaults.php b/includes/transifex-live-integration-defaults.php index a8f59216..e1c2d47e 100644 --- a/includes/transifex-live-integration-defaults.php +++ b/includes/transifex-live-integration-defaults.php @@ -58,8 +58,9 @@ static function settings() { 'language_lookup' => null, 'language_map' => null, 'languages_regex' => null, + 'url_regex' => null, 'rewrite_option_all' => 0, - + 'enable_language_picker' => 0, 'enable_custom_urls' => 0, 'urls' => [ 'rate_us' => 'https://wordpress.org/support/view/plugin-reviews/transifex-live-integration?rate=5#postform', diff --git a/includes/transifex-live-integration-language-map.php b/includes/transifex-live-integration-language-map.php new file mode 100644 index 00000000..f1c7b163 --- /dev/null +++ b/includes/transifex-live-integration-language-map.php @@ -0,0 +1,52 @@ +language_map = $language_map; + } + + static function create_language_maps ( $settings ) { + Plugin_Debug::logTrace(); + if ( !isset( $settings['enabled_language_picker'] ) ) { + Plugin_Debug::logTrace( 'settings[enabled_language_picker] not set' ); + return false; + } + if ( !($settings['enabled_language_picker'] )) { + Plugin_Debug::logTrace( 'settings[enabled_language_picker] not truthy' ); + return false; + } + return new Transifex_Live_Integration_Language_Map( $settings['language_map'] ); + } + + static function generate_language_url_map( $raw_url, $languages, $lang, + $language_map ) { + Plugin_Debug::logTrace(); + $ret = [ ]; + $tokenized_url = str_replace( $lang, "%lang%", $raw_url, $count ); + if ( $count !== 0 ) { + foreach ($languages as $language) { + $arr = [ ]; + $hreflang_code = $language_map[$language]; + $language_url = str_replace( '%lang%', $hreflang_code, $tokenized_url ); + $arr['href'] = $language_url; + $arr['hreflang'] = $hreflang_code; + array_push( $ret, $arr ); + } + } + return $ret; + } + + function render() { + Plugin_Debug::logTrace(); + $language_map = $this->language_map; + $include = <<function transifex_language_map() { return $language_map;}; +JSONP; + echo $include; + } + +} diff --git a/includes/transifex-live-integration-rewrite.php b/includes/transifex-live-integration-rewrite.php index d6cf5a65..f85dbba0 100644 --- a/includes/transifex-live-integration-rewrite.php +++ b/includes/transifex-live-integration-rewrite.php @@ -112,7 +112,7 @@ function init_hook() { * @param array $query WP query object. */ function parse_query_hook( $query ) { - if ( !Transifex_Live_Integration_Validators::is_query_ok( $query ) ) { + if ( !Transifex_Live_Integration_Validators::is_query_ok( $query ) ) { return $query; } $qv = &$query->query_vars; @@ -157,13 +157,13 @@ static function reverse_hard_link( $lang, $link, $languages_map, $source_lang ) if ( !empty( $lang ) ) { $reverse_url = ($reverse_url) ? (!strpos( $modified_link, $lang )) : false; } - $reverse_url = ($reverse_url) ? (array_key_exists( $lang, $languages_map )) : false; + $reverse_url = ($reverse_url) ? (in_array( $lang, $languages_map )) : false; $reverse_url = ($reverse_url) ? (!($source_lang == $lang)) : false; //TODO This can be dep'd if ( $reverse_url && (3 <= substr_count( $link, '/' )) ) { $array_url = explode( '/', $link ); - $array_url[3] = $languages_map[$lang] . '/' . $array_url[3]; + $array_url[3] = $lang . '/' . $array_url[3]; $modified_link = implode( '/', $array_url ); } return $modified_link; diff --git a/transifex-live-integration.php b/transifex-live-integration.php index f05d36fd..438103ee 100755 --- a/transifex-live-integration.php +++ b/transifex-live-integration.php @@ -92,6 +92,8 @@ static function do_plugin( $is_admin, $version ) { } add_filter( 'query_vars', array( 'Transifex_Live_Integration', 'query_vars_hook' ) ); + + include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/transifex-live-integration-subdomain.php'; $subdomain = Transifex_Live_Integration_Subdomain::create_subdomains( $settings ); ($subdomain) ? Plugin_Debug::logTrace( 'subdomains created' ) : Plugin_Debug::logTrace( 'subdomains skipped' ); @@ -115,7 +117,9 @@ static function do_plugin( $is_admin, $version ) { add_filter( 'day_link', [$rewrite, 'day_link_hook' ], 10, 4 ); add_filter( 'month_link', [$rewrite, 'month_link_hook' ], 10, 3 ); add_filter( 'year_link', [$rewrite, 'year_link_hook' ], 10, 2 ); - add_filter( 'home_url', [$rewrite, 'home_url_hook' ] ); +/* Turned off for compatibility with Yoast SEO - Mjj 3-7 + * add_filter( 'home_url', [$rewrite, 'home_url_hook' ] ); + */ } foreach ($rewrite->rewrite_options as $option) { Plugin_Debug::logTrace( $option ); @@ -163,6 +167,12 @@ static function do_plugin( $is_admin, $version ) { } } } + + Plugin_Debug::logTrace( 'adding admin bar actions' ); + include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/admin/transifex-live-integration-settings-util.php'; + add_action( 'wp_before_admin_bar_render', [ 'Transifex_Live_Integration_Settings_Util', 'wp_before_admin_bar_render_hook' ] ); + add_action( 'wp_after_admin_bar_render', [ 'Transifex_Live_Integration_Settings_Util', 'wp_after_admin_bar_render_hook' ] ); + if ( $is_admin ) { include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/admin/transifex-live-integration-action-links.php'; @@ -199,6 +209,13 @@ static function do_plugin( $is_admin, $version ) { include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/transifex-live-integration-javascript.php'; $javascript = new Transifex_Live_Integration_Javascript( $settings, $rewrite ? true : false ); add_action( 'wp_head', [ $javascript, 'render' ], 1 ); + + include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/transifex-live-integration-language-map.php'; + $language_map = Transifex_Live_Integration_Language_Map::create_language_maps($settings); + ($language_map) ? Plugin_Debug::logTrace( 'jsonp language map created' ) : Plugin_Debug::logTrace( 'jsonp language map skipped' ); + if ( $language_map ) { + add_action( 'wp_head', [ $language_map, 'render' ], 1 ); + } } } From 3e665f9e94d3c95dbe1fb209a21909d9fe57991e Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Tue, 8 Mar 2016 15:41:02 -0800 Subject: [PATCH 04/18] Fixing tests - lang will always be mapped val --- tests/unit/ReverseHardLinkTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/unit/ReverseHardLinkTest.php b/tests/unit/ReverseHardLinkTest.php index d43d4c08..02cf1ef9 100644 --- a/tests/unit/ReverseHardLinkTest.php +++ b/tests/unit/ReverseHardLinkTest.php @@ -30,7 +30,7 @@ protected function setUp() 'result'=> 'http://192.168.99.100:32777/page-markup-and-formatting' ], [ - 'lang' => 'zh_CN', + 'lang' => 'cn', 'link' => 'http://192.168.99.100:32777/page-markup-and-formatting', 'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE"], 'souce_lang' => 'en', From 5176ce344f095beff21757805003a6030dd95387 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Tue, 8 Mar 2016 17:34:49 -0800 Subject: [PATCH 05/18] Updates to enable language picker Added js module to read transifex_settings Added DRYd tokenized url and test --- ...ansifex-live-integration-settings-page.php | 52 +++++++++---- ...fex-live-integration-settings-template.php | 4 +- ...ansifex-live-integration-settings-util.php | 45 ++++++++++++ .../transifex-live-integration-defaults.php | 73 ++++++++++--------- ...ransifex-live-integration-settings-page.js | 51 ++++++++++--- ...fex-live-integration-transifex-settings.js | 37 ++++++++++ tests/unit/GenerateTokenizedUrlTest.php | 40 ++++++++++ transifex-live-integration.php | 1 + uninstall.php | 4 + 9 files changed, 245 insertions(+), 62 deletions(-) create mode 100755 javascript/transifex-live-integration-transifex-settings.js create mode 100644 tests/unit/GenerateTokenizedUrlTest.php diff --git a/includes/admin/transifex-live-integration-settings-page.php b/includes/admin/transifex-live-integration-settings-page.php index 0287de67..48491304 100644 --- a/includes/admin/transifex-live-integration-settings-page.php +++ b/includes/admin/transifex-live-integration-settings-page.php @@ -24,6 +24,18 @@ static function load_rewrite_options() { return array_merge( Transifex_Live_Integration_Defaults::options_values(), $db_opt_settings ); } + + static function load_transifex_settings() { + + $db_settings = get_option( 'transifex_live_transifex_settings', array() ); + if ( !$db_settings ) { + + $db_settings = Transifex_Live_Integration_Defaults::transifex_settings(); + } + Plugin_Debug::logTrace(array_merge( Transifex_Live_Integration_Defaults::transifex_settings(), $db_settings )); + return array_merge( Transifex_Live_Integration_Defaults::transifex_settings(), $db_settings ); + } + static function options_page() { @@ -39,7 +51,20 @@ static function options_page() { $arr['name'] = 'transifex_live_options[' . $key . ']'; array_push( $rewrite_options_array, $arr ); } - + + $transifex_settings = self::load_transifex_settings(); + $transifex_settings_settings = $transifex_settings['settings']; + /* $transifex_settings_array = [ ]; + $transifex_settings_settings = ''; + foreach ($transifex_settings as $key => $value) { + $arr = [ ]; + $arr['value'] = $value; + $arr['id'] = 'transifex_live_transifex_settings_' . $key; + $arr['name'] = 'transifex_live_transifex_settings[' . $key . ']'; + array_push( $transifex_settings_array, $arr ); + } +*/ + Plugin_Debug::logTrace($settings['language_map']); ob_start(); checked( $settings['rewrite_option_all'], 1 ); $checked_rewrite_option_all = ob_get_clean(); @@ -108,8 +133,12 @@ public function admin_init_hook() { */ static public function update_settings( $settings ) { Plugin_Debug::logTrace(); - + + Plugin_Debug::logTrace($settings['transifex_live_settings']['language_map']); $transifex_languages = json_decode( stripslashes( $settings['transifex_live_settings']['transifex_languages'] ), true ); + $tokenized_url = Transifex_Live_Integration_Settings_Util::generate_tokenized_url(site_url(),$settings['transifex_live_settings']['url_options']); + $settings['transifex_live_settings']['tokenized_url'] = $tokenized_url; + $languages_map = $settings['transifex_live_settings']['language_map']; $languages_map_string = $languages_map; // TODO: Switch to wp_json_encode. @@ -140,22 +169,8 @@ static public function update_settings( $settings ) { $subdomain_pattern = implode( '/', $array_url ); } - $url_regex = $settings['transifex_live_settings']['url_regex']; - if ($settings['transifex_live_settings']['url_options'] == '3') { //subdirectory - Plugin_Debug::logTrace(site_url()); - Plugin_Debug::logTrace($url_regex); - - - } - if ($settings['transifex_live_settings']['url_options'] == '2') { //subdomain - $subdomain_pattern = $settings['transifex_live_settings']['subdomain_pattern']; - Plugin_Debug::logTrace(site_url()); - Plugin_Debug::logTrace($subdomain_pattern); - Plugin_Debug::logTrace($url_regex); - } $settings['transifex_live_settings']['subdomain_pattern'] = $subdomain_pattern; - $settings['transifex_live_settings']['language_map'] = $languages_map_string; $settings['transifex_live_settings']['languages_regex'] = $languages_regex; $settings['transifex_live_settings']['languages'] = $languages; if ( isset( $settings['transifex_live_settings'] ) ) { @@ -166,6 +181,11 @@ static public function update_settings( $settings ) { Plugin_Debug::logTrace( $settings['transifex_live_options'] ); update_option( 'transifex_live_options', $settings['transifex_live_options'] ); } + + if ( isset( $settings['transifex_live_transifex_settings'] ) ) { + Plugin_Debug::logTrace( $settings['transifex_live_transifex_settings'] ); + update_option( 'transifex_live_transifex_settings', $settings['transifex_live_transifex_settings'] ); + } } /** diff --git a/includes/admin/transifex-live-integration-settings-template.php b/includes/admin/transifex-live-integration-settings-template.php index 31323108..9fcf7031 100644 --- a/includes/admin/transifex-live-integration-settings-template.php +++ b/includes/admin/transifex-live-integration-settings-template.php @@ -70,12 +70,14 @@ + + + -

diff --git a/includes/admin/transifex-live-integration-settings-util.php b/includes/admin/transifex-live-integration-settings-util.php index 293fe3f1..f4af9023 100644 --- a/includes/admin/transifex-live-integration-settings-util.php +++ b/includes/admin/transifex-live-integration-settings-util.php @@ -12,6 +12,38 @@ static function wp_after_admin_bar_render_hook() { echo ('

'); } + static function generate_tokenized_url ($site_url, $url_option_setting) { + Plugin_Debug::logTrace(); + $tokenized_url = false; + + if ($url_option_setting !== '2' && $url_option_setting != '3') { + Plugin_Debug::logTrace('No URL option, skipping tokenization'); + return false; + } + + if (!($site_url)) { + Plugin_Debug::logTrace('Failed site URL truthiness, skipping tokenization'); + return false; + } + + $slashes = []; + $slashes = explode( "/", $site_url ); + if ( $url_option_setting === '3' ) { // Subdirectory option + array_push( $slashes, '%lang%' ); + array_push( $slashes, '' ); + } + if ( $url_option_setting === '2' ) { // Subdomain option + $dots = explode( ".", $slashes[2] ); + $dots[0] = '%lang%'; + $slashes[2] = implode( '.', $dots ); + array_push( $slashes, '' ); + } + $tokenized_url = implode('/', $slashes); + + return $tokenized_url; + } + + static function render_url_options( $options ) { $html = ''; $row = ''; @@ -34,5 +66,18 @@ static function render_url_options( $options ) { } echo $html; } + + static function render_transifex_settings( $settings ) { + $html = ''; + foreach ($settings as $setting) { + $text = $setting['value']; + $id = $setting['id']; + $name = $setting['name']; + $html .= << +HTML; + } + echo $html; + } } diff --git a/includes/transifex-live-integration-defaults.php b/includes/transifex-live-integration-defaults.php index e1c2d47e..19d3e39f 100644 --- a/includes/transifex-live-integration-defaults.php +++ b/includes/transifex-live-integration-defaults.php @@ -10,46 +10,56 @@ */ class Transifex_Live_Integration_Defaults { - static function options_values() { - Plugin_Debug::logTrace(); return [ - 'add_rewrites_post' => 0, - 'add_rewrites_page' => 0, - 'add_rewrites_author' => 0, - 'add_rewrites_date' => 0, - 'add_rewrites_tag' => 0, - 'add_rewrites_category' => 0, - 'add_rewrites_search' => 0, - 'add_rewrites_root' => 0, - 'add_rewrites_reverse_template_links' => 0, - 'add_rewrites_permalink_tag' => 0 - ]; + 'add_rewrites_post' => 0, + 'add_rewrites_page' => 0, + 'add_rewrites_author' => 0, + 'add_rewrites_date' => 0, + 'add_rewrites_tag' => 0, + 'add_rewrites_category' => 0, + 'add_rewrites_search' => 0, + 'add_rewrites_root' => 0, + 'add_rewrites_reverse_template_links' => 0, + 'add_rewrites_permalink_tag' => 0 + ]; } - - static function get_options_text($key) { - Plugin_Debug::logTrace(); + + static function get_options_text( $key ) { $arr = [ - 'add_rewrites_post' => 'Posts', - 'add_rewrites_page' => 'Pages', - 'add_rewrites_author' => 'Authors', - 'add_rewrites_date' => 'Date', - 'add_rewrites_tag' => 'Tags', - 'add_rewrites_category' => 'Categories', - 'add_rewrites_search' => 'Search', - 'add_rewrites_root' => 'Root', - 'add_rewrites_reverse_template_links' => 'Reverse Template Links', - 'add_rewrites_permalink_tag' => 'Permalink Tag' - ]; - return $arr[$key]; - + 'add_rewrites_post' => 'Posts', + 'add_rewrites_page' => 'Pages', + 'add_rewrites_author' => 'Authors', + 'add_rewrites_date' => 'Date', + 'add_rewrites_tag' => 'Tags', + 'add_rewrites_category' => 'Categories', + 'add_rewrites_search' => 'Search', + 'add_rewrites_root' => 'Root', + 'add_rewrites_reverse_template_links' => 'Reverse Template Links', + 'add_rewrites_permalink_tag' => 'Permalink Tag' + ]; + return $arr[$key]; } + + static function transifex_settings() { + return [ + 'settings' => null, + 'picker' => 'no-picker', + 'domain' => 'wpliveplugin.onpressidium.com', + 'ignore_tags' => [ ], + 'autocollect' => false, + 'parse_attr' => [ ], + 'dynamic' => false, + 'detectlang' => false, + 'ignore_class' => [ ] + ]; + } + /** * Static function for settings defaults * @return array Returns the settings array */ static function settings() { - Plugin_Debug::logTrace(); return array( 'api_key' => null, // This is the only required field and needs to be copied from Live 'previous_api_key' => null, @@ -58,9 +68,7 @@ static function settings() { 'language_lookup' => null, 'language_map' => null, 'languages_regex' => null, - 'url_regex' => null, 'rewrite_option_all' => 0, - 'enable_language_picker' => 0, 'enable_custom_urls' => 0, 'urls' => [ 'rate_us' => 'https://wordpress.org/support/view/plugin-reviews/transifex-live-integration?rate=5#postform', @@ -73,7 +81,6 @@ static function settings() { 'hreflang' => false, 'url_options' => 1, 'subdomain_pattern' => '' - ); } diff --git a/javascript/transifex-live-integration-settings-page.js b/javascript/transifex-live-integration-settings-page.js index 92912674..7f82e3ea 100755 --- a/javascript/transifex-live-integration-settings-page.js +++ b/javascript/transifex-live-integration-settings-page.js @@ -88,8 +88,8 @@ function transifexLanguages() { } function addTransifexLanguages(obj) { - - if ( typeof(obj) !== 'undefined' && obj !== null) { + + if (typeof (obj) !== 'undefined' && obj !== null) { lm = jQuery.parseJSON(jQuery('#transifex_live_settings_language_map').val()); globalobj = obj; var myName = ''; @@ -103,15 +103,15 @@ function addTransifexLanguages(obj) { jQuery(oo).find('input.tx-code').attr('id', myId).attr('name', myName).val(lm[0][o.code]); })); }); - + jQuery.each(obj, function (i, o) { jQuery('#transifex-integration-live-' + o.code).machine({defaultState: {onEnter: function () { - transifex_live_integration_map_update(tl); - }, - events: {change: 'defaultState'}}, }); + transifex_live_integration_map_update(tl); + }, + events: {change: 'defaultState'}}, }); }); - - + + } else { jQuery.each(transifex_language_fields['html'], function (i, o) { jQuery('#transifex_live_language_map_table').append(jQuery('#transifex_live_language_map_template').clone().show().addClass('cloned-language-map').each(function () { @@ -130,6 +130,10 @@ function addTransifexLanguages(obj) { } } +function updateTransifexSettingsFields(obj) { + jQuery('#transifex_live_transifex_settings_settings').val(JSON.stringify(obj)); +} + (function ($) { $('#transifex_live_languages').machine({ defaultState: { @@ -205,7 +209,7 @@ function addTransifexLanguages(obj) { })(jQuery); -(function ($) { +(function (Transifex, $) { $('#transifex_live_settings_api_key').machine({ defaultState: { onEnter: function () { @@ -224,6 +228,29 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_subdomain').attr('disabled', true); $('input#transifex_live_submit').trigger('disable'); transifexLanguages(); + transifex_settings_params = { + url: "https://cdn.transifex.com/" + this.val() + "/latest/settings.jsonp", + done: function (data) { + if (data) { + console.log(data); + updateTransifexSettingsFields(data); + $('#transifex_live_settings_api_key').trigger('success'); + } else { + $('#transifex_live_settings_api_key').trigger('error'); + } + }, + fail: function () { + $('#transifex_live_settings_api_key').trigger('error'); + } + }; + + Transifex.httpGet(transifex_settings_params); + }, + events: {success: 'validating2', blank: 'blank', error: 'error', notranslation: 'missing', change: 'validating'} + }, + validating2: { + onEnter: function () { + $.log.debug('transifex_live_settings_api_key:validating2:onEnter'); }, events: {success: 'valid', blank: 'blank', error: 'error', notranslation: 'missing', change: 'validating'} }, @@ -234,7 +261,7 @@ function addTransifexLanguages(obj) { $('#transifex_live_settings_url_options_none').attr('disabled', false); $('#transifex_live_settings_url_options_subdirectory').attr('disabled', false); $('#transifex_live_settings_url_options_subdomain').attr('disabled', false); - if (jQuery('#transifex_live_settings_language_map').val()=='' || languages_override) { + if (jQuery('#transifex_live_settings_language_map').val() == '' || languages_override) { $('#transifex_live_languages').trigger('load'); languages_override = false; } @@ -268,7 +295,7 @@ function addTransifexLanguages(obj) { events: {validating: 'validating'} } }, {setClass: true}); -})(jQuery); +})(window.Transifex, jQuery); (function ($) { $('#transifex_live_settings_url_options_none').machine({ @@ -517,7 +544,7 @@ function addTransifexLanguages(obj) { $.log.debug('input#transifex_live_sync::refresh::onEnter'); languages_override = true; jQuery('#transifex_live_settings_api_key').trigger('validating'); - + this.trigger('wait'); }, events: {wait: 'wait'} diff --git a/javascript/transifex-live-integration-transifex-settings.js b/javascript/transifex-live-integration-transifex-settings.js new file mode 100755 index 00000000..bd8fa4da --- /dev/null +++ b/javascript/transifex-live-integration-transifex-settings.js @@ -0,0 +1,37 @@ +window.Transifex = window.Transifex || {}; + +(function (exports, $) { + function self() {} + + exports.httpGet = function (params) { + $.ajax({ + url: params.url || "BADURL", + jsonpCallback: params.jsonpCallback || "transifex_settings", + jsonp: params.jsonp || true, + dataType: params.dataType || "jsonp", + timeout: params.timeout || 3000 + }).done(function (data) { + params.done(self.validate(data)); + }).fail(function () { + params.fail(); + }); + }; + + + self.validate = function (obj) { + var ret = false; + var expected_keys = ['picker', 'domain']; + var keys = Object.keys(obj); + var diff = []; + $.grep(expected_keys, function (e) { + if ($.inArray(e, keys) === -1) + diff.push(e); + }); + + if (diff.length === 0) { + ret = obj; + } + return ret; + }; + +})(Transifex, jQuery); \ No newline at end of file diff --git a/tests/unit/GenerateTokenizedUrlTest.php b/tests/unit/GenerateTokenizedUrlTest.php new file mode 100644 index 00000000..c1743e65 --- /dev/null +++ b/tests/unit/GenerateTokenizedUrlTest.php @@ -0,0 +1,40 @@ +data = [[ + 'site_url' => 'http://192.168.99.100:32777', + 'url_option_setting' => '2', // Subdomain option + 'result' => 'http://%lang%.168.99.100:32777/' + ], + [ + 'site_url' => 'http://192.168.99.100:32777', + 'url_option_setting' => '3', // Subdirectory option + 'result' => 'http://192.168.99.100:32777/%lang%/' + ], + ]; + // negative options tests + $neg_options = [1,'1','',' ',null]; + foreach ($neg_options as $o) { + array_push($this->data,['site_url'=>'http://192.168.99.100:32777','url_option_setting'=>$o,'result'=>false]); + } + + } + + public function testMe() + { + foreach ($this->data as $d) { + $result = Transifex_Live_Integration_Settings_Util::generate_tokenized_url($d['site_url'], $d['url_option_setting']); + + // eval(\Psy\sh()); + $this->assertEquals($d['result'], $result); + } + + } +} diff --git a/transifex-live-integration.php b/transifex-live-integration.php index 438103ee..b9d961b2 100755 --- a/transifex-live-integration.php +++ b/transifex-live-integration.php @@ -190,6 +190,7 @@ static function do_plugin( $is_admin, $version ) { $handler->add_js_file( $version, TRANSIFEX_LIVE_INTEGRATION_JAVASCRIPT . '/jquery.jloggins.1.0.1.js', 'jloggins' ); $handler->add_js_file( $version, TRANSIFEX_LIVE_INTEGRATION_JAVASCRIPT . '/jquery-machine.1.0.1.min.js', 'jquery-machine' ); + $handler->add_js_file( $version, TRANSIFEX_LIVE_INTEGRATION_JAVASCRIPT . '/transifex-live-integration-transifex-settings.js' ); $handler->add_js_file( $version, TRANSIFEX_LIVE_INTEGRATION_JAVASCRIPT . '/transifex-live-integration-settings-page.js' ); add_action( 'admin_enqueue_scripts', [ $handler, 'render_css' ] ); diff --git a/uninstall.php b/uninstall.php index dd9006a6..878d32e7 100755 --- a/uninstall.php +++ b/uninstall.php @@ -38,3 +38,7 @@ $options_option_name = 'transifex_live_options'; delete_option( $options_option_name ); + +$transifex_settings_option_name = 'transifex_live_transifex_settings'; + +delete_option( $transifex_settings_option_name ); From 906bd981ee9374877cce0053d1ebdcfd3a7a60f3 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Thu, 10 Mar 2016 16:02:05 -0800 Subject: [PATCH 06/18] Updated picker support --- ...ansifex-live-integration-settings-page.php | 9 ++- .../transifex-live-integration-defaults.php | 1 + ...ransifex-live-integration-language-map.php | 52 ------------- .../transifex-live-integration-picker.php | 74 +++++++++++++++++++ tests/unit/GenerateLanguageUrlMapTest.php | 36 +++++++++ transifex-live-integration.php | 10 +-- 6 files changed, 124 insertions(+), 58 deletions(-) delete mode 100644 includes/transifex-live-integration-language-map.php create mode 100644 includes/transifex-live-integration-picker.php create mode 100644 tests/unit/GenerateLanguageUrlMapTest.php diff --git a/includes/admin/transifex-live-integration-settings-page.php b/includes/admin/transifex-live-integration-settings-page.php index c88fb1b8..b4bb5fde 100644 --- a/includes/admin/transifex-live-integration-settings-page.php +++ b/includes/admin/transifex-live-integration-settings-page.php @@ -133,7 +133,11 @@ public function admin_init_hook() { static public function update_settings( $settings ) { Plugin_Debug::logTrace(); - Plugin_Debug::logTrace( $settings['transifex_live_settings']['language_map'] ); + if ( isset($settings['transifex_live_transifex_settings']['settings'])) { + $p = json_decode( $settings['transifex_live_transifex_settings']['settings'], true )['picker']; + $settings['transifex_live_settings']['enable_picker'] = ($p !== 'no-picker')?true:false; + } + $transifex_languages = json_decode( stripslashes( $settings['transifex_live_settings']['transifex_languages'] ), true ); $tokenized_url = Transifex_Live_Integration_Settings_Util::generate_tokenized_url( site_url(), $settings['transifex_live_settings']['url_options'] ); $settings['transifex_live_settings']['tokenized_url'] = $tokenized_url; @@ -237,6 +241,9 @@ static public function sanitize_settings( $settings ) { $settings['transifex_live_settings']['languages_regex'] = ( isset( $settings['transifex_live_settings']['languages_regex'] )) ? sanitize_text_field( $settings['transifex_live_settings']['languages_regex'] ) : ''; $settings['transifex_live_settings']['transifex_languages'] = ( isset( $settings['transifex_live_settings']['transifex_languages'] )) ? sanitize_text_field( stripslashes( $settings['transifex_live_settings']['transifex_languages'] ) ) : ''; $settings['transifex_live_settings']['subdomain_pattern'] = ( isset( $settings['transifex_live_settings']['language_map'] )) ? sanitize_text_field( $settings['transifex_live_settings']['language_map'] ) : ''; + + + $settings['transifex_live_transifex_settings']['settings'] = ( isset( $settings['transifex_live_transifex_settings']['settings'] )) ? sanitize_text_field( stripslashes( $settings['transifex_live_transifex_settings']['settings'] ) ) : ''; return $settings; } diff --git a/includes/transifex-live-integration-defaults.php b/includes/transifex-live-integration-defaults.php index 19d3e39f..78f911c5 100644 --- a/includes/transifex-live-integration-defaults.php +++ b/includes/transifex-live-integration-defaults.php @@ -75,6 +75,7 @@ static function settings() { 'api_key_landing_page' => 'https://www.transifex.com/signup/?utm_source=liveplugin', ], 'enable_language_urls' => false, + 'enable_picker' => false, 'add_language_rewrites' => 'none selected', 'source_language' => '', 'languages' => '', diff --git a/includes/transifex-live-integration-language-map.php b/includes/transifex-live-integration-language-map.php deleted file mode 100644 index f1c7b163..00000000 --- a/includes/transifex-live-integration-language-map.php +++ /dev/null @@ -1,52 +0,0 @@ -language_map = $language_map; - } - - static function create_language_maps ( $settings ) { - Plugin_Debug::logTrace(); - if ( !isset( $settings['enabled_language_picker'] ) ) { - Plugin_Debug::logTrace( 'settings[enabled_language_picker] not set' ); - return false; - } - if ( !($settings['enabled_language_picker'] )) { - Plugin_Debug::logTrace( 'settings[enabled_language_picker] not truthy' ); - return false; - } - return new Transifex_Live_Integration_Language_Map( $settings['language_map'] ); - } - - static function generate_language_url_map( $raw_url, $languages, $lang, - $language_map ) { - Plugin_Debug::logTrace(); - $ret = [ ]; - $tokenized_url = str_replace( $lang, "%lang%", $raw_url, $count ); - if ( $count !== 0 ) { - foreach ($languages as $language) { - $arr = [ ]; - $hreflang_code = $language_map[$language]; - $language_url = str_replace( '%lang%', $hreflang_code, $tokenized_url ); - $arr['href'] = $language_url; - $arr['hreflang'] = $hreflang_code; - array_push( $ret, $arr ); - } - } - return $ret; - } - - function render() { - Plugin_Debug::logTrace(); - $language_map = $this->language_map; - $include = <<function transifex_language_map() { return $language_map;}; -JSONP; - echo $include; - } - -} diff --git a/includes/transifex-live-integration-picker.php b/includes/transifex-live-integration-picker.php new file mode 100644 index 00000000..908b602e --- /dev/null +++ b/includes/transifex-live-integration-picker.php @@ -0,0 +1,74 @@ +language_map = json_decode( $language_map, true )[0]; + $this->tokenized_url = $tokenized_url; + $this->enable_picker = $enable_picker; + Plugin_Debug::logTrace($this->language_map); + Plugin_Debug::logTrace($this->tokenized_url); + Plugin_Debug::logTrace($this->enable_picker); + } + + static function create_picker ( $settings ) { + Plugin_Debug::logTrace(); + if ( !isset( $settings['enable_picker'] ) ) { + Plugin_Debug::logTrace( 'settings[enable_picker] not set' ); + return false; + } + if ( !($settings['enable_picker']) ) { + Plugin_Debug::logTrace( 'settings[enable_picker] not truthy' ); + return false; + } + if ( !isset( $settings['tokenized_url'] ) || !( $settings['tokenized_url'] ) ) { + Plugin_Debug::logTrace( 'settings[tokenized_url] not set and not truthy' ); + return false; + } + return new Transifex_Live_Integration_Picker( $settings['language_map'], $settings['tokenized_url'], $settings['enable_picker'] ); + } + + static function generate_language_url_map( $raw_url, $tokenized_url, $language_map ) { + Plugin_Debug::logTrace(); + $trimmed_url = ltrim($raw_url,"/"); + $trimmed_tokenized_url = rtrim($tokenized_url,"/"); + + $ret = [ ]; + foreach ($language_map as $k => $v) { + $ret[$k] = str_replace('%lang%',$v,$trimmed_tokenized_url) . "/". $trimmed_url; + } + + return $ret; + } + + function render() { + Plugin_Debug::logTrace(); + global $wp; + $raw_url = home_url( $wp->request ); + Plugin_Debug::logTrace(home_url( $wp->request )); + $raw_url = add_query_arg(array(), $wp->request); + Plugin_Debug::logTrace(add_query_arg(array(), $wp->request)); + Plugin_Debug::logTrace(home_url(add_query_arg(array(),$wp->request))); + $url_map = json_encode($this->generate_language_url_map($raw_url, $this->tokenized_url, $this->language_map), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ; + $language_map = $this->language_map; + $include = << + Transifex.live.onBeforeTranslatePage(function(params) { + + var locale_urls = $url_map; + + params.noop = true; + window.location.href = locale_urls[params.lang_code]; + +}); + +JSONP; + echo $include; + } + +} diff --git a/tests/unit/GenerateLanguageUrlMapTest.php b/tests/unit/GenerateLanguageUrlMapTest.php new file mode 100644 index 00000000..aa114776 --- /dev/null +++ b/tests/unit/GenerateLanguageUrlMapTest.php @@ -0,0 +1,36 @@ +data = [ + [ + 'raw_url' => '/about', + 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', + 'language_map' => [ 'zh_CN' => 'cn'], + 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/about'] + ], [ + 'raw_url' => '2015/12/17/hello-world/', + 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', + 'language_map' => [ 'zh_CN' => 'cn'], + 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/2015/12/17/hello-world/'] + ] + ]; + // negative options go here + + } + + public function testMe() { + foreach ($this->data as $d) { + $result = Transifex_Live_Integration_Picker::generate_language_url_map( $d['raw_url'], $d['tokenized_url'], $d['language_map'] ); + + // eval(\Psy\sh()); + $this->assertEquals( $d['result'], $result ); + } + } + +} diff --git a/transifex-live-integration.php b/transifex-live-integration.php index b9d961b2..6bab206a 100755 --- a/transifex-live-integration.php +++ b/transifex-live-integration.php @@ -211,11 +211,11 @@ static function do_plugin( $is_admin, $version ) { $javascript = new Transifex_Live_Integration_Javascript( $settings, $rewrite ? true : false ); add_action( 'wp_head', [ $javascript, 'render' ], 1 ); - include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/transifex-live-integration-language-map.php'; - $language_map = Transifex_Live_Integration_Language_Map::create_language_maps($settings); - ($language_map) ? Plugin_Debug::logTrace( 'jsonp language map created' ) : Plugin_Debug::logTrace( 'jsonp language map skipped' ); - if ( $language_map ) { - add_action( 'wp_head', [ $language_map, 'render' ], 1 ); + include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/transifex-live-integration-picker.php'; + $picker = Transifex_Live_Integration_Picker::create_picker($settings); + ($picker) ? Plugin_Debug::logTrace( 'picker created' ) : Plugin_Debug::logTrace( 'picker skipped' ); + if ( $picker ) { + add_action( 'wp_head', [ $picker, 'render' ], 1 ); } } } From 604f19f6882dac672c6ea93fb609bc0bea208bdc Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 14 Mar 2016 13:23:23 -0700 Subject: [PATCH 07/18] Plugin UI Updates Fix for #56 Refactored js messaging to template --- ...fex-live-integration-settings-template.php | 11 +++++-- .../transifex-live-integration-defaults.php | 2 +- ...ransifex-live-integration-settings-page.js | 30 ++++++++++++++----- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/includes/admin/transifex-live-integration-settings-template.php b/includes/admin/transifex-live-integration-settings-template.php index 9fcf7031..de93011d 100644 --- a/includes/admin/transifex-live-integration-settings-template.php +++ b/includes/admin/transifex-live-integration-settings-template.php @@ -15,7 +15,12 @@

-

 

+

+ Validating your key! + Success! Advanced SEO settings enabled. + Oops! Please make sure you’ve entered a valid API key. + D’oh! No languages have been published from Transifex Live yet. +

@@ -48,8 +53,10 @@ - Your languages can't be loaded. Please re-check your API key. + Your languages can't be loaded. Please re-check your API key. + Refreshing languages will replace your current codes with those from Transifex Live. Continue?

+

Tweak your localized urls.

diff --git a/includes/transifex-live-integration-defaults.php b/includes/transifex-live-integration-defaults.php index 78f911c5..db217a86 100644 --- a/includes/transifex-live-integration-defaults.php +++ b/includes/transifex-live-integration-defaults.php @@ -66,7 +66,7 @@ static function settings() { 'raw_transifex_languages' => null, 'transifex_languages' => null, 'language_lookup' => null, - 'language_map' => null, + 'language_map' => '[]', 'languages_regex' => null, 'rewrite_option_all' => 0, 'enable_custom_urls' => 0, diff --git a/javascript/transifex-live-integration-settings-page.js b/javascript/transifex-live-integration-settings-page.js index 334e58cb..664e7da5 100755 --- a/javascript/transifex-live-integration-settings-page.js +++ b/javascript/transifex-live-integration-settings-page.js @@ -113,10 +113,15 @@ function addTransifexLanguages(obj) { } else { + var tlslm = JSON.parse(jQuery('#transifex_live_settings_language_map').val()); jQuery.each(transifex_language_fields['html'], function (i, o) { jQuery('#transifex_live_language_map_table').append(jQuery('#transifex_live_language_map_template').clone().show().addClass('cloned-language-map').each(function () { jQuery(this).find('span.tx-language').text(o.caption); + if (tlslm.length < 1 ) { jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(o.value); + } else { + jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(tlslm[0][o.value]); + } jQuery(this).machine({defaultState: {onEnter: function () { transifex_live_integration_map_update(); }, @@ -222,17 +227,20 @@ function updateTransifexSettingsFields(obj) { validating: { onEnter: function () { $.log.debug('transifex_live_settings_api_key:validating:onEnter'); - $('#transifex_live_settings_api_key_message').text('Checking key...'); + $('#transifex_live_settings_url_options_none').attr('disabled', true); $('#transifex_live_settings_url_options_subdirectory').attr('disabled', true); $('#transifex_live_settings_url_options_subdomain').attr('disabled', true); $('input#transifex_live_submit').trigger('disable'); + $('#transifex_live_settings_api_key_message_validating').toggleClass('hide-if-js', false); + $('#transifex_live_settings_api_key_message_valid').toggleClass('hide-if-js', true); + $('#transifex_live_settings_api_key_message_error').toggleClass('hide-if-js', true); + $('#transifex_live_settings_api_key_message_missing').toggleClass('hide-if-js', true); transifexLanguages(); transifex_settings_params = { url: "https://cdn.transifex.com/" + this.val() + "/latest/settings.jsonp", done: function (data) { if (data) { - console.log(data); updateTransifexSettingsFields(data); $('#transifex_live_settings_api_key').trigger('success'); } else { @@ -261,11 +269,12 @@ function updateTransifexSettingsFields(obj) { $('#transifex_live_settings_url_options_none').attr('disabled', false); $('#transifex_live_settings_url_options_subdirectory').attr('disabled', false); $('#transifex_live_settings_url_options_subdomain').attr('disabled', false); - if (jQuery('#transifex_live_settings_language_map').val() == '' || languages_override) { + if (jQuery('#transifex_live_settings_language_map').val() == '[]' || languages_override) { $('#transifex_live_languages').trigger('load'); languages_override = false; } - $('#transifex_live_settings_api_key_message').text('Success! Advanced SEO settings enabled.'); + $('#transifex_live_settings_api_key_message_validating').toggleClass('hide-if-js', true); + $('#transifex_live_settings_api_key_message_valid').toggleClass('hide-if-js', false); $('input#transifex_live_submit').trigger('enable'); }, events: {success: 'valid', change: 'validating', validating: 'validating'} @@ -274,7 +283,8 @@ function updateTransifexSettingsFields(obj) { onEnter: function () { $.log.debug('error:onEnter'); $('#transifex_live_settings_api_key_button').trigger('wait'); - $('#transifex_live_settings_api_key_message').text(' Oops! Please make sure you’ve entered a valid API key.'); + $('#transifex_live_settings_api_key_message_validating').toggleClass('hide-if-js', true); + $('#transifex_live_settings_api_key_message_error').toggleClass('hide-if-js', false); }, events: {change: 'validating', validating: 'validating'} }, @@ -282,7 +292,10 @@ function updateTransifexSettingsFields(obj) { onEnter: function () { $.log.debug('transifex_live_settings_api_key:blank:onEnter'); $('#transifex_live_settings_api_key_button').trigger('wait'); - $('#transifex_live_settings_api_key_message').text(''); + $('#transifex_live_settings_api_key_message_validating').toggleClass('hide-if-js', true); + $('#transifex_live_settings_api_key_message_valid').toggleClass('hide-if-js', true); + $('#transifex_live_settings_api_key_message_error').toggleClass('hide-if-js', true); + $('#transifex_live_settings_api_key_message_missing').toggleClass('hide-if-js', true); }, events: {change: 'validating', validating: 'validating'} }, @@ -290,7 +303,8 @@ function updateTransifexSettingsFields(obj) { onEnter: function () { $.log.debug('#transifex_live_settings_api_key:missing:onEnter'); $('#transifex_live_settings_api_key_button').trigger('wait'); - $('#transifex_live_settings_api_key_message').text('D’oh! No languages have been published from Transifex Live yet.'); + $('#transifex_live_settings_api_key_message_validating').toggleClass('hide-if-js', true); + $('#transifex_live_settings_api_key_message_missing').toggleClass('hide-if-js', false); }, events: {validating: 'validating'} } @@ -535,7 +549,7 @@ function updateTransifexSettingsFields(obj) { confirm: { onEnter: function () { $.log.debug('input#transifex_live_sync::confirm::onEnter'); - (confirm('Refreshing languages will replace your current codes with those from Transifex Live. Continue?')) ? this.trigger('refresh') : this.trigger('wait'); + (confirm($('#transifex_live_sync_message').text())) ? this.trigger('refresh') : this.trigger('wait'); }, events: {refresh: 'refresh', wait: 'wait'} }, From b1f88ae766720991d85443225e8a4ef3fefa6c13 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 14 Mar 2016 15:33:25 -0700 Subject: [PATCH 08/18] fixed settings jsonp and added notranslate to debug --- includes/plugin-debug.php | 2 +- javascript/transifex-live-integration-settings-page.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/plugin-debug.php b/includes/plugin-debug.php index e137f7a2..60729ff0 100644 --- a/includes/plugin-debug.php +++ b/includes/plugin-debug.php @@ -62,7 +62,7 @@ public static function logError( $severity, $message, $filename, $lineno ) { public function printLog() { if ( self::$debug_mode ) { - echo ('
' . "\n"); + echo ('
' . "\n"); echo ('

Plugin: Plugin Debug Mode Output

' . "\n"); array_walk( self::$calls, array( 'Plugin_Debug', 'printLogCallback' ) ); echo "
"; diff --git a/javascript/transifex-live-integration-settings-page.js b/javascript/transifex-live-integration-settings-page.js index 664e7da5..8f1ccef2 100755 --- a/javascript/transifex-live-integration-settings-page.js +++ b/javascript/transifex-live-integration-settings-page.js @@ -238,7 +238,7 @@ function updateTransifexSettingsFields(obj) { $('#transifex_live_settings_api_key_message_missing').toggleClass('hide-if-js', true); transifexLanguages(); transifex_settings_params = { - url: "https://cdn.transifex.com/" + this.val() + "/latest/settings.jsonp", + url: "https://cdn.transifex.com/" + this.val() + "/latest/settings.all.jsonp", done: function (data) { if (data) { updateTransifexSettingsFields(data); From d43f287d5136b80f0e77e473602c05961eea7e35 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 14 Mar 2016 15:57:47 -0700 Subject: [PATCH 09/18] set settings to production only --- javascript/transifex-live-integration-transifex-settings.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javascript/transifex-live-integration-transifex-settings.js b/javascript/transifex-live-integration-transifex-settings.js index 7feff064..71353be3 100755 --- a/javascript/transifex-live-integration-transifex-settings.js +++ b/javascript/transifex-live-integration-transifex-settings.js @@ -22,7 +22,7 @@ window.Transifex = window.Transifex || {}; self.validate = function (obj) { var ret = false; var expected_keys = ['picker', 'domain']; - var keys = Object.keys(obj); + var keys = Object.keys(obj['production']); var diff = []; $.grep(expected_keys, function (e) { if ($.inArray(e, keys) === -1) From 436fdf1a536de8e956302afe07b1d6a2fb7fc54a Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 14 Mar 2016 16:23:18 -0700 Subject: [PATCH 10/18] doing math --- readme.md | 8 ++++++-- readme.txt | 10 +++++++--- transifex-live-integration.php | 6 +++--- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/readme.md b/readme.md index 90d7f6f0..c3b12551 100755 --- a/readme.md +++ b/readme.md @@ -31,7 +31,7 @@ Translators can contribute new languages to this plugin or our other WordPress p == Minimum Requirements == * WordPress 3.5.2 or greater -* PHP version 5.5.0 or greater +* PHP version 5.4.0 or greater * MySQL version 5.0 or greater === Installation === @@ -96,4 +96,8 @@ Fixed support for PHP 5.4 Fixed hreflang tag output for subdirectories = 1.2.3 = -Improved admin UI \ No newline at end of file +Improved admin UI + += 1.2.4 = +Added picker support that respects locale +Additional admin API key validation \ No newline at end of file diff --git a/readme.txt b/readme.txt index 42049d5c..1e819c2c 100755 --- a/readme.txt +++ b/readme.txt @@ -3,8 +3,8 @@ Name: Transifex Live Translation Plugin Contributors: txmatthew, ThemeBoy, brooksx Tags: transifex, translate, translations, localize, localise, localization, localisation, multilingual, t9n, l10n, i18n, language, switcher, live, translation, translator Requires at least: 3.5.2 -Tested up to: 4.4.1 -Stable tag: 1.2.3 +Tested up to: 4.4.2 +Stable tag: 1.2.4 License: GPLv2 or later License URI: http://www.gnu.org/licenses/gpl-2.0.html @@ -37,7 +37,7 @@ Translators can contribute new languages to this plugin or our other WordPress p Minimum Requirements: * WordPress 3.5.2 or greater -* PHP version 5.5.0 or greater +* PHP version 5.4.0 or greater * MySQL version 5.0 or greater == Installation == @@ -105,3 +105,7 @@ Fixed hreflang tag output for subdirectories = 1.2.3 = Improved admin UI + += 1.2.4 = +Added picker support that respects locale +Additional admin API key validation diff --git a/transifex-live-integration.php b/transifex-live-integration.php index 6bab206a..6c87d9fa 100755 --- a/transifex-live-integration.php +++ b/transifex-live-integration.php @@ -5,13 +5,13 @@ * * @link http://docs.transifex.com/developer/integrations/wordpress * @package TransifexLiveIntegration - * @version 1.2.3 + * @version 1.2.4 * * @wordpress-plugin * Plugin Name: Transifex Live Translation Plugin * Plugin URI: http://docs.transifex.com/developer/integrations/wordpress * Description: Translate your WordPress website or blog without the usual complex setups. - * Version: 1.2.3 + * Version: 1.2.4 * License: GNU General Public License * License URI: http://www.gnu.org/licenses/gpl-2.0.txt * Text Domain: transifex-live-integration @@ -63,7 +63,7 @@ define( 'LANG_PARAM', 'lang' ); require_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/plugin-debug.php'; -$version = '1.2.3'; +$version = '1.2.4'; $debug = new Plugin_Debug(); /** From 8c49045875aae7e8906c7cbd6051d7e781ca5cd7 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 14 Mar 2016 17:55:24 -0700 Subject: [PATCH 11/18] fixed refresh --- .../transifex-live-integration-settings-page.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/javascript/transifex-live-integration-settings-page.js b/javascript/transifex-live-integration-settings-page.js index 8f1ccef2..ec03e353 100755 --- a/javascript/transifex-live-integration-settings-page.js +++ b/javascript/transifex-live-integration-settings-page.js @@ -88,7 +88,7 @@ function transifexLanguages() { } function addTransifexLanguages(obj) { - + if (typeof (obj) !== 'undefined' && obj !== null) { lm = jQuery.parseJSON(jQuery('#transifex_live_settings_language_map').val()); globalobj = obj; @@ -114,13 +114,17 @@ function addTransifexLanguages(obj) { } else { var tlslm = JSON.parse(jQuery('#transifex_live_settings_language_map').val()); + language_map = (tlslm.length<1)?language_map:[]; jQuery.each(transifex_language_fields['html'], function (i, o) { jQuery('#transifex_live_language_map_table').append(jQuery('#transifex_live_language_map_template').clone().show().addClass('cloned-language-map').each(function () { jQuery(this).find('span.tx-language').text(o.caption); - if (tlslm.length < 1 ) { + if (tlslm.length < 1 ) { jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(o.value); } else { jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(tlslm[0][o.value]); + var e = {}; + e[o.value] = tlslm[0][o.value]; + language_map.push(e); } jQuery(this).machine({defaultState: {onEnter: function () { transifex_live_integration_map_update(); @@ -131,6 +135,7 @@ function addTransifexLanguages(obj) { jQuery('#transifex_live_settings_source_language').val(source_language); jQuery('#transifex_live_settings_transifex_languages').val(JSON.stringify(transifex_languages)); jQuery('#transifex_live_settings_language_lookup').val(JSON.stringify(language_lookup)); + console.log(JSON.stringify(language_map)); jQuery('#transifex_live_settings_language_map').val(JSON.stringify(language_map)); } } @@ -549,7 +554,8 @@ function updateTransifexSettingsFields(obj) { confirm: { onEnter: function () { $.log.debug('input#transifex_live_sync::confirm::onEnter'); - (confirm($('#transifex_live_sync_message').text())) ? this.trigger('refresh') : this.trigger('wait'); + this.trigger('refresh'); + //this.trigger('wait'); }, events: {refresh: 'refresh', wait: 'wait'} }, From 8f926ccd4428206c5f990076fd1812e5837ded1c Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Mon, 14 Mar 2016 18:12:52 -0700 Subject: [PATCH 12/18] Adding production key --- includes/admin/transifex-live-integration-settings-page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/admin/transifex-live-integration-settings-page.php b/includes/admin/transifex-live-integration-settings-page.php index b4bb5fde..8409672b 100644 --- a/includes/admin/transifex-live-integration-settings-page.php +++ b/includes/admin/transifex-live-integration-settings-page.php @@ -134,7 +134,7 @@ static public function update_settings( $settings ) { Plugin_Debug::logTrace(); if ( isset($settings['transifex_live_transifex_settings']['settings'])) { - $p = json_decode( $settings['transifex_live_transifex_settings']['settings'], true )['picker']; + $p = json_decode( $settings['transifex_live_transifex_settings']['settings'], true )['production']['picker']; $settings['transifex_live_settings']['enable_picker'] = ($p !== 'no-picker')?true:false; } From 59cc23087e486ee73b9442a59d930ab6d0eaf9db Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Tue, 15 Mar 2016 10:49:39 -0700 Subject: [PATCH 13/18] Another update Refactor hreflang Javascript fixes --- .../transifex-live-integration-hreflang.php | 56 ++++++++----------- .../transifex-live-integration-picker.php | 39 ++++++------- tests/unit/GenerateLanguageUrlMapTest.php | 9 ++- 3 files changed, 49 insertions(+), 55 deletions(-) diff --git a/includes/transifex-live-integration-hreflang.php b/includes/transifex-live-integration-hreflang.php index 06631017..edbc8a4c 100644 --- a/includes/transifex-live-integration-hreflang.php +++ b/includes/transifex-live-integration-hreflang.php @@ -17,6 +17,7 @@ class Transifex_Live_Integration_Hreflang { private $settings; private $language_map; private $languages; + private $tokenized_url; /** * Public constructor, sets the settings @@ -27,6 +28,7 @@ public function __construct( $settings ) { $this->settings = $settings; $this->language_map = json_decode( $settings['language_map'], true )[0]; $this->languages = json_decode( $settings['transifex_languages'], true ); + $this->tokenized_url = $settings['tokenized_url']; } public function ok_to_add() { @@ -42,24 +44,28 @@ public function ok_to_add() { Plugin_Debug::logTrace( 'settings[url_options] set to none...skipping hreflang' ); return false; } + if ( !isset( $this->settings['tokenized_url'] ) ) { + Plugin_Debug::logTrace( 'settings[tokenized_url] not set...skipping hreflang' ); + return false; + } return true; } - private function generate_languages_hreflang( $raw_url, $languages, $lang, - $language_map ) { + private function generate_languages_hreflang( $raw_url, $languages, $language_map ) { Plugin_Debug::logTrace(); + include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/transifex-live-integration-picker.php'; + $url_map = Transifex_Live_Integration_Picker::generate_language_url_map( $raw_url, $this->tokenized_url, $language_map ); $ret = [ ]; - $tokenized_url = str_replace( $lang, "%lang%", $raw_url, $count ); - if ( $count !== 0 ) { + // $tokenized_url = str_replace( $lang, "%lang%", $raw_url, $count ); foreach ($languages as $language) { $arr = [ ]; - $hreflang_code = $language_map[$language]; - $language_url = str_replace( '%lang%', $hreflang_code, $tokenized_url ); - $arr['href'] = $language_url; - $arr['hreflang'] = $hreflang_code; + // $hreflang_code = $language_map[$language]; + // $language_url = str_replace( '%lang%', $hreflang_code, $tokenized_url ); + + $arr['href'] = $url_map[$language]; + $arr['hreflang'] = $language_map[$language]; array_push( $ret, $arr ); } - } return $ret; } @@ -69,36 +75,18 @@ private function generate_languages_hreflang( $raw_url, $languages, $lang, public function render_hreflang() { Plugin_Debug::logTrace(); global $wp; + $lang = get_query_var( 'lang' ); $raw_url = home_url( $wp->request ); - if ( '/' !== substr( $raw_url, -1 ) ) { - $raw_url = $raw_url . '/'; - } - $base_url = $raw_url; - if ( $this->settings['source_language'] === get_query_var( 'lang' ) ) { - $array_url = explode( "/", $raw_url ); - if ( $this->settings['url_options'] === '3' ) { - array_pop( $array_url ); - array_push( $array_url, get_query_var( 'lang' ) ); - array_push( $array_url, '' ); - } - if ( $this->settings['url_options'] === '2' ) { - $array_domain = explode( ".", $array_url[2] ); - $array_domain[0] = get_query_var( 'lang' ); - $array_url[2] = implode( '.', $array_domain ); - } - - $raw_url = implode( '/', $array_url ); - } else { - $base_url = str_replace( '/' . get_query_var( 'lang' ), "", $raw_url ); - } + $url_path = add_query_arg(array(), $wp->request); + $source_url_path = (substr( $url_path, 0, count($lang)-1 ) === $lang)?substr( $url_path, count($lang)-1, count($url_path)-1 ):$url_path; + $source = $this->settings['source_language']; + $source_url = site_url() + $source_url_path; $hreflang_out = ''; $hreflang_out .= <<\n +\n SOURCE; - $languages = $this->languages; - $lang = get_query_var( 'lang' ); - $hreflangs = $this->generate_languages_hreflang( $raw_url, $languages, $lang, $this->language_map ); + $hreflangs = $this->generate_languages_hreflang( $source_url_path, $this->languages, $this->language_map ); foreach ($hreflangs as $hreflang) { $href_attr = $hreflang['href']; $hreflang_attr = $hreflang['hreflang']; diff --git a/includes/transifex-live-integration-picker.php b/includes/transifex-live-integration-picker.php index 908b602e..df0f2b5f 100644 --- a/includes/transifex-live-integration-picker.php +++ b/includes/transifex-live-integration-picker.php @@ -5,15 +5,14 @@ class Transifex_Live_Integration_Picker { private $language_map; private $tokenized_url; private $enable_picker; + private $source_language; - public function __construct( $language_map, $tokenized_url, $enable_picker ) { + public function __construct( $language_map, $tokenized_url, $enable_picker, $source_language ) { Plugin_Debug::logTrace(); $this->language_map = json_decode( $language_map, true )[0]; $this->tokenized_url = $tokenized_url; $this->enable_picker = $enable_picker; - Plugin_Debug::logTrace($this->language_map); - Plugin_Debug::logTrace($this->tokenized_url); - Plugin_Debug::logTrace($this->enable_picker); + $this->source_language = $source_language; } static function create_picker ( $settings ) { @@ -30,16 +29,15 @@ static function create_picker ( $settings ) { Plugin_Debug::logTrace( 'settings[tokenized_url] not set and not truthy' ); return false; } - return new Transifex_Live_Integration_Picker( $settings['language_map'], $settings['tokenized_url'], $settings['enable_picker'] ); + return new Transifex_Live_Integration_Picker( $settings['language_map'], $settings['tokenized_url'], $settings['enable_picker'], $settings['source_language'] ); } static function generate_language_url_map( $raw_url, $tokenized_url, $language_map ) { Plugin_Debug::logTrace(); - $trimmed_url = ltrim($raw_url,"/"); $trimmed_tokenized_url = rtrim($tokenized_url,"/"); - $ret = [ ]; foreach ($language_map as $k => $v) { + $trimmed_url = ltrim(str_replace($v,'',$raw_url),"/"); $ret[$k] = str_replace('%lang%',$v,$trimmed_tokenized_url) . "/". $trimmed_url; } @@ -49,23 +47,26 @@ static function generate_language_url_map( $raw_url, $tokenized_url, $language_m function render() { Plugin_Debug::logTrace(); global $wp; - $raw_url = home_url( $wp->request ); + $lang = get_query_var( 'lang' ); + $home_url = home_url( $wp->request ); Plugin_Debug::logTrace(home_url( $wp->request )); - $raw_url = add_query_arg(array(), $wp->request); + $url_path = add_query_arg(array(), $wp->request); + $source_url_path = (substr( $url_path, 0, count($lang)-1 ) === $lang)?substr( $url_path, count($lang)-1, count($url_path)-1 ):$url_path; Plugin_Debug::logTrace(add_query_arg(array(), $wp->request)); - Plugin_Debug::logTrace(home_url(add_query_arg(array(),$wp->request))); - $url_map = json_encode($this->generate_language_url_map($raw_url, $this->tokenized_url, $this->language_map), JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ; - $language_map = $this->language_map; + Plugin_Debug::logTrace($source_url_path); + $url_map = $this->generate_language_url_map($url_path, $this->tokenized_url, $this->language_map); + $url_map[$this->source_language] = site_url()+$source_url_path; + $string_url_map = json_encode($url_map, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ; + $include = << Transifex.live.onBeforeTranslatePage(function(params) { - - var locale_urls = $url_map; - - params.noop = true; - window.location.href = locale_urls[params.lang_code]; - -}); + var locale_urls = $string_url_map; + if(Transifex.live.ready === true && Transifex.live.getSelectedLanguageCode() !== params.lang_code){ + params.noop = true; + window.location.href = locale_urls[params.lang_code]; + } + }); JSONP; echo $include; diff --git a/tests/unit/GenerateLanguageUrlMapTest.php b/tests/unit/GenerateLanguageUrlMapTest.php index aa114776..93081fe6 100644 --- a/tests/unit/GenerateLanguageUrlMapTest.php +++ b/tests/unit/GenerateLanguageUrlMapTest.php @@ -18,8 +18,13 @@ protected function setUp() { 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', 'language_map' => [ 'zh_CN' => 'cn'], 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/2015/12/17/hello-world/'] - ] - ]; + ], [ + 'raw_url' => 'cn/about', + 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', + 'language_map' => [ 'zh_CN' => 'cn'], + 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/about'] + ] + ]; // negative options go here } From 87c37c60f0d28613705e2a5d7256931f9687106c Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Tue, 15 Mar 2016 22:00:34 -0700 Subject: [PATCH 14/18] Post refactoring fixes --- includes/transifex-live-integration-hreflang.php | 6 +++--- includes/transifex-live-integration-picker.php | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/includes/transifex-live-integration-hreflang.php b/includes/transifex-live-integration-hreflang.php index edbc8a4c..5a639a41 100644 --- a/includes/transifex-live-integration-hreflang.php +++ b/includes/transifex-live-integration-hreflang.php @@ -78,10 +78,10 @@ public function render_hreflang() { $lang = get_query_var( 'lang' ); $raw_url = home_url( $wp->request ); $url_path = add_query_arg(array(), $wp->request); - $source_url_path = (substr( $url_path, 0, count($lang)-1 ) === $lang)?substr( $url_path, count($lang)-1, count($url_path)-1 ):$url_path; - + $source_url_path = (substr( $url_path, 0, strlen($lang) ) === $lang)?substr( $url_path, strlen($lang), strlen($url_path) ):$url_path; + Plugin_Debug::logTrace($source_url_path); $source = $this->settings['source_language']; - $source_url = site_url() + $source_url_path; + $source_url = site_url() . $source_url_path; $hreflang_out = ''; $hreflang_out .= <<\n diff --git a/includes/transifex-live-integration-picker.php b/includes/transifex-live-integration-picker.php index df0f2b5f..1661bf7a 100644 --- a/includes/transifex-live-integration-picker.php +++ b/includes/transifex-live-integration-picker.php @@ -35,9 +35,10 @@ static function create_picker ( $settings ) { static function generate_language_url_map( $raw_url, $tokenized_url, $language_map ) { Plugin_Debug::logTrace(); $trimmed_tokenized_url = rtrim($tokenized_url,"/"); + $trimmed_url = ltrim($raw_url,"/"); $ret = [ ]; foreach ($language_map as $k => $v) { - $trimmed_url = ltrim(str_replace($v,'',$raw_url),"/"); + $ret[$k] = str_replace('%lang%',$v,$trimmed_tokenized_url) . "/". $trimmed_url; } @@ -51,11 +52,11 @@ function render() { $home_url = home_url( $wp->request ); Plugin_Debug::logTrace(home_url( $wp->request )); $url_path = add_query_arg(array(), $wp->request); - $source_url_path = (substr( $url_path, 0, count($lang)-1 ) === $lang)?substr( $url_path, count($lang)-1, count($url_path)-1 ):$url_path; + $source_url_path = (substr( $url_path, 0, strlen($lang) ) === $lang)?substr( $url_path, strlen($lang), strlen($url_path) ):$url_path; Plugin_Debug::logTrace(add_query_arg(array(), $wp->request)); Plugin_Debug::logTrace($source_url_path); - $url_map = $this->generate_language_url_map($url_path, $this->tokenized_url, $this->language_map); - $url_map[$this->source_language] = site_url()+$source_url_path; + $url_map = $this->generate_language_url_map($source_url_path, $this->tokenized_url, $this->language_map); + $url_map[$this->source_language] = site_url() . $source_url_path; $string_url_map = json_encode($url_map, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ; $include = << Date: Tue, 15 Mar 2016 22:23:43 -0700 Subject: [PATCH 15/18] house cleanin --- ...ansifex-live-integration-settings-page.php | 10 ++--- ...fex-live-integration-settings-template.php | 4 +- includes/plugin-debug.php | 2 +- .../transifex-live-integration-hreflang.php | 25 +++++------ .../transifex-live-integration-picker.php | 43 +++++++++---------- ...ransifex-live-integration-settings-page.js | 24 +++++------ 6 files changed, 52 insertions(+), 56 deletions(-) diff --git a/includes/admin/transifex-live-integration-settings-page.php b/includes/admin/transifex-live-integration-settings-page.php index 8409672b..22d73c66 100644 --- a/includes/admin/transifex-live-integration-settings-page.php +++ b/includes/admin/transifex-live-integration-settings-page.php @@ -133,11 +133,11 @@ public function admin_init_hook() { static public function update_settings( $settings ) { Plugin_Debug::logTrace(); - if ( isset($settings['transifex_live_transifex_settings']['settings'])) { + if ( isset( $settings['transifex_live_transifex_settings']['settings'] ) ) { $p = json_decode( $settings['transifex_live_transifex_settings']['settings'], true )['production']['picker']; - $settings['transifex_live_settings']['enable_picker'] = ($p !== 'no-picker')?true:false; + $settings['transifex_live_settings']['enable_picker'] = ($p !== 'no-picker') ? true : false; } - + $transifex_languages = json_decode( stripslashes( $settings['transifex_live_settings']['transifex_languages'] ), true ); $tokenized_url = Transifex_Live_Integration_Settings_Util::generate_tokenized_url( site_url(), $settings['transifex_live_settings']['url_options'] ); $settings['transifex_live_settings']['tokenized_url'] = $tokenized_url; @@ -241,8 +241,8 @@ static public function sanitize_settings( $settings ) { $settings['transifex_live_settings']['languages_regex'] = ( isset( $settings['transifex_live_settings']['languages_regex'] )) ? sanitize_text_field( $settings['transifex_live_settings']['languages_regex'] ) : ''; $settings['transifex_live_settings']['transifex_languages'] = ( isset( $settings['transifex_live_settings']['transifex_languages'] )) ? sanitize_text_field( stripslashes( $settings['transifex_live_settings']['transifex_languages'] ) ) : ''; $settings['transifex_live_settings']['subdomain_pattern'] = ( isset( $settings['transifex_live_settings']['language_map'] )) ? sanitize_text_field( $settings['transifex_live_settings']['language_map'] ) : ''; - - + + $settings['transifex_live_transifex_settings']['settings'] = ( isset( $settings['transifex_live_transifex_settings']['settings'] )) ? sanitize_text_field( stripslashes( $settings['transifex_live_transifex_settings']['settings'] ) ) : ''; return $settings; } diff --git a/includes/admin/transifex-live-integration-settings-template.php b/includes/admin/transifex-live-integration-settings-template.php index de93011d..5528ae86 100644 --- a/includes/admin/transifex-live-integration-settings-template.php +++ b/includes/admin/transifex-live-integration-settings-template.php @@ -56,7 +56,7 @@ Your languages can't be loaded. Please re-check your API key. Refreshing languages will replace your current codes with those from Transifex Live. Continue?

- +

Tweak your localized urls.

@@ -88,7 +88,7 @@

- | + |

diff --git a/includes/plugin-debug.php b/includes/plugin-debug.php index 60729ff0..eb16b5a8 100644 --- a/includes/plugin-debug.php +++ b/includes/plugin-debug.php @@ -18,7 +18,7 @@ class Plugin_Debug { * Define the core functionality of the plugin. */ public function __construct() { - self::$debug_mode = true; + self::$debug_mode = false; $this->logTrace(); // Check to see if plugin is in debug mode diff --git a/includes/transifex-live-integration-hreflang.php b/includes/transifex-live-integration-hreflang.php index 5a639a41..5f51a13d 100644 --- a/includes/transifex-live-integration-hreflang.php +++ b/includes/transifex-live-integration-hreflang.php @@ -51,21 +51,19 @@ public function ok_to_add() { return true; } - private function generate_languages_hreflang( $raw_url, $languages, $language_map ) { + private function generate_languages_hreflang( $raw_url, $languages, + $language_map ) { Plugin_Debug::logTrace(); include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/transifex-live-integration-picker.php'; $url_map = Transifex_Live_Integration_Picker::generate_language_url_map( $raw_url, $this->tokenized_url, $language_map ); $ret = [ ]; - // $tokenized_url = str_replace( $lang, "%lang%", $raw_url, $count ); - foreach ($languages as $language) { - $arr = [ ]; - // $hreflang_code = $language_map[$language]; - // $language_url = str_replace( '%lang%', $hreflang_code, $tokenized_url ); - - $arr['href'] = $url_map[$language]; - $arr['hreflang'] = $language_map[$language]; - array_push( $ret, $arr ); - } + foreach ($languages as $language) { + $arr = [ ]; + + $arr['href'] = $url_map[$language]; + $arr['hreflang'] = $language_map[$language]; + array_push( $ret, $arr ); + } return $ret; } @@ -77,9 +75,8 @@ public function render_hreflang() { global $wp; $lang = get_query_var( 'lang' ); $raw_url = home_url( $wp->request ); - $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; - Plugin_Debug::logTrace($source_url_path); + $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; $source = $this->settings['source_language']; $source_url = site_url() . $source_url_path; $hreflang_out = ''; diff --git a/includes/transifex-live-integration-picker.php b/includes/transifex-live-integration-picker.php index 1661bf7a..13a56525 100644 --- a/includes/transifex-live-integration-picker.php +++ b/includes/transifex-live-integration-picker.php @@ -6,22 +6,23 @@ class Transifex_Live_Integration_Picker { private $tokenized_url; private $enable_picker; private $source_language; - - public function __construct( $language_map, $tokenized_url, $enable_picker, $source_language ) { + + public function __construct( $language_map, $tokenized_url, $enable_picker, + $source_language ) { Plugin_Debug::logTrace(); $this->language_map = json_decode( $language_map, true )[0]; $this->tokenized_url = $tokenized_url; $this->enable_picker = $enable_picker; $this->source_language = $source_language; } - - static function create_picker ( $settings ) { + + static function create_picker( $settings ) { Plugin_Debug::logTrace(); if ( !isset( $settings['enable_picker'] ) ) { Plugin_Debug::logTrace( 'settings[enable_picker] not set' ); return false; } - if ( !($settings['enable_picker']) ) { + if ( !($settings['enable_picker']) ) { Plugin_Debug::logTrace( 'settings[enable_picker] not truthy' ); return false; } @@ -31,17 +32,18 @@ static function create_picker ( $settings ) { } return new Transifex_Live_Integration_Picker( $settings['language_map'], $settings['tokenized_url'], $settings['enable_picker'], $settings['source_language'] ); } - - static function generate_language_url_map( $raw_url, $tokenized_url, $language_map ) { + + static function generate_language_url_map( $raw_url, $tokenized_url, + $language_map ) { Plugin_Debug::logTrace(); - $trimmed_tokenized_url = rtrim($tokenized_url,"/"); - $trimmed_url = ltrim($raw_url,"/"); + $trimmed_tokenized_url = rtrim( $tokenized_url, "/" ); + $trimmed_url = ltrim( $raw_url, "/" ); $ret = [ ]; - foreach ($language_map as $k => $v) { - - $ret[$k] = str_replace('%lang%',$v,$trimmed_tokenized_url) . "/". $trimmed_url; - } - + foreach ($language_map as $k => $v) { + + $ret[$k] = str_replace( '%lang%', $v, $trimmed_tokenized_url ) . "/" . $trimmed_url; + } + return $ret; } @@ -50,15 +52,12 @@ function render() { global $wp; $lang = get_query_var( 'lang' ); $home_url = home_url( $wp->request ); - Plugin_Debug::logTrace(home_url( $wp->request )); - $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; - Plugin_Debug::logTrace(add_query_arg(array(), $wp->request)); - Plugin_Debug::logTrace($source_url_path); - $url_map = $this->generate_language_url_map($source_url_path, $this->tokenized_url, $this->language_map); + $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; + $url_map = $this->generate_language_url_map( $source_url_path, $this->tokenized_url, $this->language_map ); $url_map[$this->source_language] = site_url() . $source_url_path; - $string_url_map = json_encode($url_map, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE) ; - + $string_url_map = json_encode( $url_map, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); + $include = << Transifex.live.onBeforeTranslatePage(function(params) { diff --git a/javascript/transifex-live-integration-settings-page.js b/javascript/transifex-live-integration-settings-page.js index ec03e353..1f3b8101 100755 --- a/javascript/transifex-live-integration-settings-page.js +++ b/javascript/transifex-live-integration-settings-page.js @@ -88,7 +88,7 @@ function transifexLanguages() { } function addTransifexLanguages(obj) { - + if (typeof (obj) !== 'undefined' && obj !== null) { lm = jQuery.parseJSON(jQuery('#transifex_live_settings_language_map').val()); globalobj = obj; @@ -114,18 +114,18 @@ function addTransifexLanguages(obj) { } else { var tlslm = JSON.parse(jQuery('#transifex_live_settings_language_map').val()); - language_map = (tlslm.length<1)?language_map:[]; + language_map = (tlslm.length < 1) ? language_map : []; jQuery.each(transifex_language_fields['html'], function (i, o) { jQuery('#transifex_live_language_map_table').append(jQuery('#transifex_live_language_map_template').clone().show().addClass('cloned-language-map').each(function () { jQuery(this).find('span.tx-language').text(o.caption); - if (tlslm.length < 1 ) { - jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(o.value); - } else { - jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(tlslm[0][o.value]); - var e = {}; - e[o.value] = tlslm[0][o.value]; - language_map.push(e); - } + if (tlslm.length < 1) { + jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(o.value); + } else { + jQuery(this).find('input.tx-code').attr('id', o.id).attr('name', o.name).val(tlslm[0][o.value]); + var e = {}; + e[o.value] = tlslm[0][o.value]; + language_map.push(e); + } jQuery(this).machine({defaultState: {onEnter: function () { transifex_live_integration_map_update(); }, @@ -232,7 +232,7 @@ function updateTransifexSettingsFields(obj) { validating: { onEnter: function () { $.log.debug('transifex_live_settings_api_key:validating:onEnter'); - + $('#transifex_live_settings_url_options_none').attr('disabled', true); $('#transifex_live_settings_url_options_subdirectory').attr('disabled', true); $('#transifex_live_settings_url_options_subdomain').attr('disabled', true); @@ -554,7 +554,7 @@ function updateTransifexSettingsFields(obj) { confirm: { onEnter: function () { $.log.debug('input#transifex_live_sync::confirm::onEnter'); - this.trigger('refresh'); + this.trigger('refresh'); //this.trigger('wait'); }, events: {refresh: 'refresh', wait: 'wait'} From c442978c52a8422c6b792afe0b47924dd0711b06 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Tue, 15 Mar 2016 22:28:11 -0700 Subject: [PATCH 16/18] fixed test --- tests/unit/GenerateLanguageUrlMapTest.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/tests/unit/GenerateLanguageUrlMapTest.php b/tests/unit/GenerateLanguageUrlMapTest.php index 93081fe6..d1bc3404 100644 --- a/tests/unit/GenerateLanguageUrlMapTest.php +++ b/tests/unit/GenerateLanguageUrlMapTest.php @@ -18,11 +18,6 @@ protected function setUp() { 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', 'language_map' => [ 'zh_CN' => 'cn'], 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/2015/12/17/hello-world/'] - ], [ - 'raw_url' => 'cn/about', - 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', - 'language_map' => [ 'zh_CN' => 'cn'], - 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/about'] ] ]; // negative options go here From 361d897f26a5dac88d3289acb08395a6f1a5a66d Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Wed, 16 Mar 2016 14:06:58 -0700 Subject: [PATCH 17/18] Fix for missing slashes --- includes/transifex-live-integration-picker.php | 8 ++++---- tests/unit/GenerateLanguageUrlMapTest.php | 9 +++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/includes/transifex-live-integration-picker.php b/includes/transifex-live-integration-picker.php index 13a56525..dcfd0d63 100644 --- a/includes/transifex-live-integration-picker.php +++ b/includes/transifex-live-integration-picker.php @@ -36,12 +36,12 @@ static function create_picker( $settings ) { static function generate_language_url_map( $raw_url, $tokenized_url, $language_map ) { Plugin_Debug::logTrace(); - $trimmed_tokenized_url = rtrim( $tokenized_url, "/" ); - $trimmed_url = ltrim( $raw_url, "/" ); + $trimmed_tokenized_url = rtrim( $tokenized_url, '/' ); + $trimmed_url = ltrim( $raw_url, '/' ); $ret = [ ]; foreach ($language_map as $k => $v) { - - $ret[$k] = str_replace( '%lang%', $v, $trimmed_tokenized_url ) . "/" . $trimmed_url; + $unslashed_url = str_replace( '%lang%', $v, $trimmed_tokenized_url ) . '/' . $trimmed_url; + $ret[$k] = rtrim($unslashed_url, '/') . '/'; } return $ret; diff --git a/tests/unit/GenerateLanguageUrlMapTest.php b/tests/unit/GenerateLanguageUrlMapTest.php index d1bc3404..0bad0b95 100644 --- a/tests/unit/GenerateLanguageUrlMapTest.php +++ b/tests/unit/GenerateLanguageUrlMapTest.php @@ -9,14 +9,19 @@ protected function setUp() { include_once './includes/transifex-live-integration-picker.php'; $this->data = [ [ - 'raw_url' => '/about', + 'raw_url' => '/about/', 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', 'language_map' => [ 'zh_CN' => 'cn'], - 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/about'] + 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/about/'] ], [ 'raw_url' => '2015/12/17/hello-world/', 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', 'language_map' => [ 'zh_CN' => 'cn'], + 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/2015/12/17/hello-world/'] + ],[ + 'raw_url' => '2015/12/17/hello-world', + 'tokenized_url' => 'http://192.168.99.100:32777/%lang%/', + 'language_map' => [ 'zh_CN' => 'cn'], 'result' => ['zh_CN' => 'http://192.168.99.100:32777/cn/2015/12/17/hello-world/'] ] ]; From 09639036cd385a6d8f1c4d018958ea5f83e38346 Mon Sep 17 00:00:00 2001 From: Matthew Jackowski Date: Wed, 16 Mar 2016 14:22:27 -0700 Subject: [PATCH 18/18] slash fixes for source urls --- includes/transifex-live-integration-hreflang.php | 3 ++- includes/transifex-live-integration-picker.php | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/includes/transifex-live-integration-hreflang.php b/includes/transifex-live-integration-hreflang.php index 5f51a13d..abee77c8 100644 --- a/includes/transifex-live-integration-hreflang.php +++ b/includes/transifex-live-integration-hreflang.php @@ -78,7 +78,8 @@ public function render_hreflang() { $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; $source = $this->settings['source_language']; - $source_url = site_url() . $source_url_path; + $unslashed_source_url = site_url() . $source_url_path; + $source_url = rtrim($unslashed_source_url, '/') . '/'; $hreflang_out = ''; $hreflang_out .= <<\n diff --git a/includes/transifex-live-integration-picker.php b/includes/transifex-live-integration-picker.php index dcfd0d63..1df0c6ed 100644 --- a/includes/transifex-live-integration-picker.php +++ b/includes/transifex-live-integration-picker.php @@ -55,7 +55,8 @@ function render() { $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; $url_map = $this->generate_language_url_map( $source_url_path, $this->tokenized_url, $this->language_map ); - $url_map[$this->source_language] = site_url() . $source_url_path; + $unslashed_source_url = site_url() . $source_url_path; + $url_map[$this->source_language] = rtrim($unslashed_source_url, '/') . '/'; $string_url_map = json_encode( $url_map, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ); $include = <<