Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 22 additions & 1 deletion classes/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) )
);
}
}
14 changes: 9 additions & 5 deletions classes/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
Expand Down