Repository navigation
Add a filter to point formidableforms.com links at translated pages #3804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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<string, array{url: string, pages: string[]}> $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 ); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Malformed locale entries can block the language fallback. When a locale key, such as Define one behavior and state it in the filter docblock. The PR states that malformed filter values are ignored. Under that rule, a malformed locale entry should not suppress a valid language entry. If a malformed locale array should disable localization instead, the docblock must say so, and the tests should cover that case.
|
||
|
|
||
| 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 ) ) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject configured bases that contain a query or fragment. If the filter supplies Validate the base components and return the original URL when the base contains a query or fragment. Add regression cases for both inputs.
|
||
| 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(); | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A function with high cyclomatic complexity can be hard to understand and
maintain. Cyclomatic complexity is a software metric that measures the number of
independent paths through a function. A higher cyclomatic complexity indicates
that the function has more decision points and is more complex.