diff --git a/classes/helpers.php b/classes/helpers.php index 5f7bcdf..95da1c1 100644 --- a/classes/helpers.php +++ b/classes/helpers.php @@ -95,8 +95,29 @@ public static function get_option_page_ids() { * @return bool */ public static function already_localized( $post_id ) { - preg_match( '/[a-z]{2}_[A-Z]{2}/', $post_id, $language ); + $locales_regex_fragment = self::locales_regex_fragment(); + if ( ! $locales_regex_fragment ) { + return false; + } + preg_match( '/_(' . $locales_regex_fragment . ')$/', $post_id, $language ); return ! empty( $language ); } + + /** + * @return string A regex fragment for all polylang configured locales + */ + public static function locales_regex_fragment(): string { + $locales = pll_languages_list( [ 'hide_empty' => false, 'fields' => 'locale' ] ); + if ( ! $locales ) { + return ''; + } + + return sprintf( + '(%s)', + implode( '|', array_map( function ( $lang ) { + return preg_quote( $lang, '/' ); + }, $locales ) ) + ); + } } diff --git a/classes/main.php b/classes/main.php index b2638fb..f5b992a 100644 --- a/classes/main.php +++ b/classes/main.php @@ -51,11 +51,15 @@ public function get_default_reference( $reference, $field_name, $post_id ) { return $reference; } + if ( ! $locales_regex_fragment = Helpers::locales_regex_fragment() ) { + return $reference; + } + /** * Dynamically get the options page ID * @see : https://regex101.com/r/58uhKg/2/ */ - $_post_id = preg_replace( '/(_[a-z]{2}_[A-Z]{2})/', '', $post_id ); + $_post_id = preg_replace( '/(_' . $locales_regex_fragment . ')$/', '', $post_id ); remove_filter( 'acf/load_reference', [ $this, 'get_default_reference' ] ); $reference = acf_get_reference( $field_name, $_post_id ); @@ -85,8 +89,8 @@ public function get_default_value( $value, $post_id, $field ) { /** * Activate or deactivate the default value (all languages) for the given post id * - * @param bool $show_default : whatever to show default for the given post id - * @param string $original_post_id : the original post id without lang attributes + * @param bool $show_default : whatever to show default for the given post id + * @param string $original_post_id : the original post id without lang attributes * * @since 1.0.4 */ @@ -144,8 +148,8 @@ function ( $value_c ) { /** * Manage to change the post_id with the current lang to save option against * - * @param string $future_post_id - * @param string $original_post_id + * @param string $future_post_id + * @param string $original_post_id * * @return string * @author Maxime CULEA