diff --git a/includes/lib/transifex-live-integration-rewrite.php b/includes/lib/transifex-live-integration-rewrite.php index 4086e4a2..e6cdede7 100644 --- a/includes/lib/transifex-live-integration-rewrite.php +++ b/includes/lib/transifex-live-integration-rewrite.php @@ -40,6 +40,7 @@ public function __construct( $settings, $rewrite_options ) { Plugin_Debug::logTrace(); include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/common/transifex-live-integration-validators.php'; include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/override/transifex-live-integration-generate-rewrite-rules.php'; + include_once TRANSIFEX_LIVE_INTEGRATION_DIRECTORY_BASE . '/includes/lib/transifex-live-integration-wp-services.php'; $this->rewrite_options = [ ]; $this->languages_regex = $settings['languages_regex']; $this->source_language = $settings['source_language']; @@ -97,6 +98,7 @@ public function __construct( $settings, $rewrite_options ) { $this->rewrite_pattern = $pattern; } } + $this->wp_services = new Transifex_Live_Integration_WP_Services(); } public function get_language_url( $atts ) { @@ -146,7 +148,7 @@ function wp_hook() { * @return string Returns modified link */ - static function reverse_hard_link( $lang, $link, $languages_map, $source_lang, + function reverse_hard_link( $lang, $link, $languages_map, $source_lang, $pattern ) { Plugin_Debug::logTrace(); if ( !(isset( $pattern )) ) { @@ -170,7 +172,7 @@ static function reverse_hard_link( $lang, $link, $languages_map, $source_lang, if ( count( $m ) > 1 ) { $link = str_replace( $m[1], $lang, $m[0] ); } else { - $site_host = parse_url(site_url())['host']; + $site_host = parse_url($this->wp_services->get_site_url())['host']; $parsed_url = parse_url($link); $link_host = isset($parsed_url['host']) ? $parsed_url['host'] : ''; // change only wordpress links - not links reffering to other domains diff --git a/includes/lib/transifex-live-integration-wp-services.php b/includes/lib/transifex-live-integration-wp-services.php new file mode 100644 index 00000000..c81523b6 --- /dev/null +++ b/includes/lib/transifex-live-integration-wp-services.php @@ -0,0 +1,18 @@ +data = [ [ 'page_url' => 'https://www.foo.bar/about/', diff --git a/tests/unit/ReverseHardLinkTest.php b/tests/unit/ReverseHardLinkTest.php index 9e8d0bdb..bfc67b6a 100644 --- a/tests/unit/ReverseHardLinkTest.php +++ b/tests/unit/ReverseHardLinkTest.php @@ -1,21 +1,31 @@ data = [[ //1 - 'lang' => 'zh_CN', - 'link' => 'http://www.mydomain.com/page-markup-and-formatting', - 'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ], - 'souce_lang' => 'en', - 'pattern' => '/http:\/\/www.mydomain.com\/(zh_CN|de_DE)\//', - 'result' => 'http://www.mydomain.com/zh_CN/page-markup-and-formatting' + 'host' => 'http://www.mydomain.com', + 'lang' => 'zh_CN', + 'link' => 'http://www.mydomain.com/page-markup-and-formatting', + 'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ], + 'souce_lang' => 'en', + 'pattern' => '/http:\/\/www.mydomain.com\/(zh_CN|de_DE)\//', + 'result' => 'http://www.mydomain.com/zh_CN/page-markup-and-formatting' ], [ //2 + 'host' => 'http://www.mydomain.com', 'lang' => 'zh_CN', 'link' => 'http://www.mydomain.com/page-markup-and-formatting', 'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ], @@ -24,6 +34,7 @@ protected function setUp() { 'result' => 'http://www.mydomain.com/page-markup-and-formatting' ], [ //3 + 'host' => 'http://www.mydomain.com', 'lang' => 'zh_HK', 'link' => 'http://www.mydomain.com/page-markup-and-formatting', 'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ], @@ -32,6 +43,7 @@ protected function setUp() { 'result' => 'http://www.mydomain.com/page-markup-and-formatting' ], [ //4 + 'host' => 'http://www.mydomain.com', 'lang' => 'cn', 'link' => 'http://www.mydomain.com/page-markup-and-formatting', 'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ], @@ -40,6 +52,7 @@ protected function setUp() { 'result' => 'http://www.mydomain.com/cn/page-markup-and-formatting' ], [ //5 + 'host' => 'http://www.mydomain.com', 'lang' => 'zh_CN', 'link' => 'http://www.mydomain.com/page-markup-and-formatting', 'languages_map' => [ ], @@ -48,6 +61,7 @@ protected function setUp() { 'result' => 'http://www.mydomain.com/page-markup-and-formatting' ], [ //6 + 'host' => 'http://www.mydomain.com', 'lang' => null, 'link' => 'http://www.mydomain.com/', 'languages_map' => null, @@ -56,6 +70,7 @@ protected function setUp() { 'result' => 'http://www.mydomain.com/' ], [ //7 + 'host' => 'http://www.mydomain.com', 'lang' => 'zh_CN', 'link' => 'http://www.mydomain.com/page-markup-and-formatting', 'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ], @@ -64,6 +79,7 @@ protected function setUp() { 'result' => 'http://www.mydomain.com/page-markup-and-formatting' ], [ //8 + 'host' => 'http://www.mydomain.com', 'lang' => 'zh_CN', 'link' => 'http://www.mydomain.com/page-markup-and-formatting', 'languages_map' => ["zh_CN" => "zh_CN", "de_DE" => "de_DE" ], @@ -72,6 +88,7 @@ protected function setUp() { 'result' => 'http://zh_CN.mydomain.com/page-markup-and-formatting' ], [ //9 + 'host' => 'http://www.mydomain.com', 'lang' => 'cn', 'link' => 'http://www.mydomain.com/page-markup-and-formatting', 'languages_map' => ["zh_CN" => "cn", "de_DE" => "de_DE" ], @@ -80,6 +97,7 @@ protected function setUp() { 'result' => 'http://cn.mydomain.com/page-markup-and-formatting' ], [ //10 plex case + 'host' => 'http://www.mydomain.com', 'lang' => 'de', 'link' => 'http://www.mydomain.com/page-markup-and-formatting-de', 'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ], @@ -88,25 +106,37 @@ protected function setUp() { 'result' => 'http://www.mydomain.com/de/page-markup-and-formatting-de' ], [ //11 + 'host' => 'http://www.mydomain.com', 'lang' => 'de', 'link' => 'http://www.mydomain.com/page-markup-and-formatting-de', 'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ], 'souce_lang' => 'en', 'pattern' => '/http:\/\/(cn|de|www).mydomain.com\/.*/', 'result' => 'http://de.mydomain.com/page-markup-and-formatting-de' + ], + [ //12 external link, leave intact + 'host' => 'http://www.mydomain.com', + 'lang' => 'de', + 'link' => 'http://www.another.com/page-markup-and-formatting-de', + 'languages_map' => ["zh_CN" => "cn", "de_DE" => "de" ], + 'souce_lang' => 'en', + 'pattern' => '/http:\/\/www.mydomain.com\/(cn|de)\//', + 'result' => 'http://www.another.com/page-markup-and-formatting-de' ] ]; } public function testMe() { $counter = 0; + $rewrite = new Transifex_Live_Integration_Rewrite( [], [] ); foreach ($this->data as $i) { $counter = $counter + 1; - $result = Transifex_Live_Integration_Rewrite::reverse_hard_link( + $rewrite->wp_services = \Codeception\Stub::make(Transifex_Live_Integration_WP_Services::class, ['get_site_url' => $i['host']]); + $result = $rewrite->reverse_hard_link( $i['lang'], $i['link'], $i['languages_map'], $i['souce_lang'], $i['pattern'] ); -// eval(\Psy\sh()); + // codecept_debug('result:' . $i['result']); $this->assertEquals( $i['result'], $result, 'Test Number:' . $counter ); } }