diff --git a/classes/helpers/FrmAppHelper.php b/classes/helpers/FrmAppHelper.php index fa14a12c0f..9d0289001b 100644 --- a/classes/helpers/FrmAppHelper.php +++ b/classes/helpers/FrmAppHelper.php @@ -132,6 +132,7 @@ public static function admin_upgrade_link( $args, $page = '' ) { $page = 'https://formidableforms.com/lite-upgrade/'; } + $page = self::maybe_localize_site_url( $page ); $args = is_array( $args ) ? self::adjust_legacy_utm_args( $args ) : array( 'campaign' => $args ); $query_args = array( @@ -165,6 +166,73 @@ public static function admin_upgrade_link( $args, $page = '' ) { return self::make_affiliate_url( $link ); } + /** + * Point a formidableforms.com link at the translated page for the admin's language, when one exists. + * + * @since x.x + * + * @param string $url A formidableforms.com URL, with or without a query string and anchor. + * + * @return string + */ + private static function maybe_localize_site_url( $url ) { + if ( ! is_string( $url ) || ! preg_match( '#^(https?://(?:www\.)?formidableforms\.com/)([^?\#]*)(.*)$#', $url, $parts ) ) { + return $url; + } + + $locale = determine_locale(); + + /** + * Lists the formidableforms.com pages that are translated, so links in the plugin can point at them. + * + * Each key is a WordPress locale (pt_BR) or a language code (de) that covers every locale of that language. + * A locale key is used before its language key. + * Each value has the base URL of the translated site, which must be on formidableforms.com, and the English + * page paths that are translated there. The translated page must keep the English path after the base URL, + * for example https://formidableforms.com/de/pricing/. The home page is never changed. + * Example: array( 'de' => array( 'url' => 'https://formidableforms.com/de/', 'pages' => array( 'lite-upgrade', 'pricing' ) ) ) + * This runs once for every link the plugin builds, so callbacks should not read options or remote data. + * + * @since x.x + * + * @param array $sites Translated pages, keyed by locale or language code. + * @param string $locale The locale the links are made for. + */ + $sites = apply_filters( 'frm_localized_site_pages', array(), $locale ); + + if ( ! is_array( $sites ) ) { + return $url; + } + + $language = explode( '_', $locale )[0]; + $site = isset( $sites[ $locale ] ) && is_array( $sites[ $locale ] ) ? $sites[ $locale ] : ( $sites[ $language ] ?? null ); + + if ( ! is_array( $site ) || empty( $site['url'] ) || ! is_string( $site['url'] ) || empty( $site['pages'] ) || ! is_array( $site['pages'] ) ) { + return $url; + } + + $base = trim( $site['url'] ); + + if ( ! preg_match( '#^https://(?:www\.)?formidableforms\.com/#', $base ) ) { + return $url; + } + + $path = trim( $parts[2], '/' ); + $pages = array(); + + foreach ( $site['pages'] as $page ) { + if ( is_string( $page ) ) { + $pages[] = trim( $page, '/' ); + } + } + + if ( '' === $path || ! in_array( $path, $pages, true ) ) { + return $url; + } + + return trailingslashit( $base ) . ltrim( $parts[2], '/' ) . $parts[3]; + } + /** * If medium is "pro", add an additional utm_license param with their active license type. * @@ -244,6 +312,7 @@ private static function adjust_legacy_utm_args( $args ) { * @return string */ public static function maybe_add_missing_utm( $cta_link, $utm ) { + $cta_link = self::maybe_localize_site_url( $cta_link ); $utm = self::adjust_legacy_utm_args( $utm ); $query_args = array(); diff --git a/tests/phpunit/misc/test_FrmAppHelper.php b/tests/phpunit/misc/test_FrmAppHelper.php index e2812a338e..534179a3d2 100644 --- a/tests/phpunit/misc/test_FrmAppHelper.php +++ b/tests/phpunit/misc/test_FrmAppHelper.php @@ -887,4 +887,388 @@ public function test_resolve_error_focus_target_without_conflict() { $target ); } + + /** + * Filter callbacks added by the localized site link tests, so tearDown can remove them. + * + * @var array + */ + private $localized_site_filters = array(); + + public function tearDown(): void { + foreach ( $this->localized_site_filters as $filter ) { + remove_filter( $filter[0], $filter[1], $filter[2] ); + } + $this->localized_site_filters = array(); + + parent::tearDown(); + } + + /** + * @param string $hook + * @param callable $callback + * @param int $accepted_args + * + * @return void + */ + private function add_localized_site_filter( $hook, $callback, $accepted_args = 1 ) { + add_filter( $hook, $callback, 10, $accepted_args ); + $this->localized_site_filters[] = array( $hook, $callback, 10 ); + } + + /** + * @param string $locale + * + * @return void + */ + private function set_localized_site_locale( $locale ) { + $this->add_localized_site_filter( + 'pre_determine_locale', + function () use ( $locale ) { + return $locale; + } + ); + } + + /** + * @param array|string $sites + * + * @return void + */ + private function set_localized_site_pages( $sites ) { + $this->add_localized_site_filter( + 'frm_localized_site_pages', + function () use ( $sites ) { + return $sites; + }, + 2 + ); + } + + /** + * @return array + */ + private function get_german_site_map() { + return array( + 'de' => array( + 'url' => 'https://formidableforms.com/de/', + 'pages' => array( 'lite-upgrade', 'pricing/' ), + ), + ); + } + + /** + * @return array + */ + private function get_portuguese_site_map() { + return array( + 'pt' => array( + 'url' => 'https://formidableforms.com/pt/', + 'pages' => array( 'pricing' ), + ), + 'pt_BR' => array( + 'url' => 'https://formidableforms.com/pt-br/', + 'pages' => array( 'pricing' ), + ), + ); + } + + public function test_localized_site_links_unchanged_with_default_empty_map() { + $english_upgrade = FrmAppHelper::admin_upgrade_link( 'test', 'pricing/' ); + $english_utm = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/lite-upgrade/', array() ); + + $this->set_localized_site_locale( 'de_DE' ); + + $upgrade = FrmAppHelper::admin_upgrade_link( 'test', 'pricing/' ); + $utm = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/lite-upgrade/', array() ); + + $this->assertSame( $english_upgrade, $upgrade ); + $this->assertSame( $english_utm, $utm ); + $this->assertStringContainsString( 'https://formidableforms.com/pricing/', $upgrade ); + $this->assertStringNotContainsString( '/de/', $upgrade ); + $this->assertStringContainsString( 'https://formidableforms.com/lite-upgrade/', $utm ); + $this->assertStringNotContainsString( '/de/', $utm ); + } + + public function test_localized_site_filter_receives_empty_default_and_locale() { + $received = array(); + $this->add_localized_site_filter( + 'frm_localized_site_pages', + function ( $sites, $locale ) use ( &$received ) { + $received = array( $sites, $locale ); + return $sites; + }, + 2 + ); + $this->set_localized_site_locale( 'de_DE' ); + + FrmAppHelper::admin_upgrade_link( 'test' ); + + $this->assertSame( array( array(), 'de_DE' ), $received ); + } + + public function test_admin_upgrade_link_is_localized_when_page_is_listed() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::admin_upgrade_link( 'test' ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/lite-upgrade/?' ) ); + $this->assertStringContainsString( 'utm_source=plugin', $link ); + $this->assertStringContainsString( 'utm_campaign=test', $link ); + + $link = FrmAppHelper::admin_upgrade_link( 'test', 'pricing/' ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/pricing/?' ) ); + } + + public function test_admin_upgrade_link_keeps_anchor_when_localized() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::admin_upgrade_link( + array( + 'campaign' => 'x', + 'anchor' => 'faq', + ) + ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/lite-upgrade/?' ) ); + $this->assertStringContainsString( 'utm_source=plugin', $link ); + $this->assertStringContainsString( 'utm_campaign=x', $link ); + $this->assertSame( '#faq', substr( $link, -4 ) ); + } + + public function test_localized_site_links_leave_unlisted_pages_in_english() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::admin_upgrade_link( 'test', 'knowledgebase/foo/' ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/knowledgebase/foo/?' ) ); + $this->assertStringNotContainsString( '/de/', $link ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/knowledgebase/foo/', array() ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/knowledgebase/foo/?' ) ); + } + + public function test_localized_site_links_ignore_the_site_root() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/', array() ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/?' ) ); + } + + public function test_localized_site_links_use_language_for_regional_locales() { + $this->set_localized_site_locale( 'de_DE_formal' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::admin_upgrade_link( 'test' ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/lite-upgrade/?' ) ); + } + + public function test_localized_site_links_prefer_exact_locale_over_language() { + $this->set_localized_site_locale( 'pt_BR' ); + $this->set_localized_site_pages( $this->get_portuguese_site_map() ); + + $link = FrmAppHelper::admin_upgrade_link( 'test', 'pricing/' ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/pt-br/pricing/?' ) ); + } + + public function test_localized_site_links_fall_back_to_language_key() { + $this->set_localized_site_locale( 'pt_PT' ); + $this->set_localized_site_pages( $this->get_portuguese_site_map() ); + + $link = FrmAppHelper::admin_upgrade_link( 'test', 'pricing/' ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/pt/pricing/?' ) ); + } + + public function test_localized_site_links_stay_english_for_locale_without_a_site() { + $this->set_localized_site_locale( 'fr_FR' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::admin_upgrade_link( 'test' ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/lite-upgrade/?' ) ); + } + + public function test_maybe_add_missing_utm_only_localizes_formidableforms_com() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://sandbox.formidableforms.com/lite-upgrade/', array() ); + $this->assertSame( 0, strpos( $link, 'https://sandbox.formidableforms.com/lite-upgrade/?' ) ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://wordpress.org/lite-upgrade/', array() ); + $this->assertSame( 0, strpos( $link, 'https://wordpress.org/lite-upgrade/?' ) ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://www.formidableforms.com/pricing/', array() ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/pricing/?' ) ); + $this->assertStringContainsString( 'utm_source=plugin', $link ); + } + + public function test_localized_site_links_are_not_double_prefixed() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/de/pricing/', array() ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/pricing/?' ) ); + $this->assertStringNotContainsString( '/de/de/', $link ); + + $link = FrmAppHelper::admin_upgrade_link( 'test', 'de/pricing/' ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/pricing/?' ) ); + $this->assertStringNotContainsString( '/de/de/', $link ); + } + + public function test_localized_site_links_ignore_malformed_filter_values() { + $malformed = array( + 'string' => 'string', + 'site is not an array' => array( 'de' => 'x' ), + 'empty url' => array( 'de' => array( 'url' => '' ) ), + 'missing pages' => array( 'de' => array( 'url' => 'https://formidableforms.com/de/' ) ), + 'pages is not an array' => array( + 'de' => array( + 'url' => 'https://formidableforms.com/de/', + 'pages' => 'lite-upgrade', + ), + ), + 'url is not a string' => array( + 'de' => array( + 'url' => array( 'https://formidableforms.com/de/' ), + 'pages' => array( 'lite-upgrade' ), + ), + ), + 'page is not a string' => array( + 'de' => array( + 'url' => 'https://formidableforms.com/de/', + 'pages' => array( 123, null, array( 'lite-upgrade' ) ), + ), + ), + ); + + $this->set_localized_site_locale( 'de_DE' ); + + foreach ( $malformed as $label => $value ) { + $this->set_localized_site_pages( $value ); + + $link = FrmAppHelper::admin_upgrade_link( 'test' ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/lite-upgrade/?' ), $label ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/lite-upgrade/', array() ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/lite-upgrade/?' ), $label ); + + // Remove this value's filter so the next one is the only one applied. + $filter = array_pop( $this->localized_site_filters ); + remove_filter( $filter[0], $filter[1], $filter[2] ); + } + } + + public function test_localized_site_links_match_page_without_trailing_slash() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/pricing', array() ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/pricing?' ) ); + } + + public function test_localized_site_links_keep_query_string_and_anchor_in_maybe_add_missing_utm() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/pricing/?utm_source=abc#faq', array() ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/pricing/?' ) ); + $this->assertStringContainsString( 'utm_source=abc', $link ); + $this->assertStringContainsString( '#faq', $link ); + } + + public function test_localized_site_links_are_used_in_affiliate_url() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + $this->add_localized_site_filter( + 'frm_affiliate_id', + function () { + return 123; + } + ); + + $link = FrmAppHelper::admin_upgrade_link( 'test' ); + + $this->assertStringContainsString( 'u=123', $link ); + $this->assertStringContainsString( urlencode( 'formidableforms.com/de/lite-upgrade/' ), $link ); + } + + public function test_localized_site_links_ignore_urls_outside_formidableforms_com() { + $this->set_localized_site_locale( 'de_DE' ); + + $bad_urls = array( + 'javascript:alert(1)//', + 'https://evil.com/de/', + 'http://formidableforms.com/de/', + 'https://formidableforms.com.evil.com/de/', + ); + + foreach ( $bad_urls as $bad_url ) { + $this->set_localized_site_pages( + array( + 'de' => array( + 'url' => $bad_url, + 'pages' => array( 'lite-upgrade' ), + ), + ) + ); + + $link = FrmAppHelper::admin_upgrade_link( 'test' ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/lite-upgrade/?' ), $bad_url ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com/lite-upgrade/', array() ); + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/lite-upgrade/?' ), $bad_url ); + + // Remove this value's filter so the next one is the only one applied. + $filter = array_pop( $this->localized_site_filters ); + remove_filter( $filter[0], $filter[1], $filter[2] ); + } + } + + public function test_localized_site_links_trim_whitespace_around_site_url() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( + array( + 'de' => array( + 'url' => ' https://formidableforms.com/de/ ', + 'pages' => array( 'lite-upgrade' ), + ), + ) + ); + + $link = FrmAppHelper::admin_upgrade_link( 'test' ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/lite-upgrade/?' ) ); + } + + public function test_localized_site_links_fall_back_to_language_when_locale_entry_is_malformed() { + $sites = $this->get_german_site_map(); + $sites['de_DE'] = 'junk'; + + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $sites ); + + $link = FrmAppHelper::admin_upgrade_link( 'test' ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/lite-upgrade/?' ) ); + } + + public function test_localized_site_links_do_not_double_a_leading_slash_in_the_path() { + $this->set_localized_site_locale( 'de_DE' ); + $this->set_localized_site_pages( $this->get_german_site_map() ); + + $link = FrmAppHelper::maybe_add_missing_utm( 'https://formidableforms.com//pricing/', array() ); + + $this->assertSame( 0, strpos( $link, 'https://formidableforms.com/de/pricing/?' ) ); + $this->assertStringNotContainsString( '//pricing', $link ); + } }