Blocks
1 week ago
Datastore
1 month ago
Meta
1 year ago
abilities
1 week ago
admin
1 week ago
ajax
1 month ago
api
1 week ago
fields
1 week ago
forms
1 week ago
legacy
1 year ago
locations
1 year ago
post-types
2 months ago
rest-api
1 week ago
walkers
1 year ago
acf-bidirectional-functions.php
1 year ago
acf-field-functions.php
2 months ago
acf-field-group-functions.php
7 months ago
acf-form-functions.php
1 year ago
acf-helper-functions.php
1 year ago
acf-hook-functions.php
1 year ago
acf-input-functions.php
7 months ago
acf-internal-post-type-functions.php
7 months ago
acf-meta-functions.php
2 weeks ago
acf-post-functions.php
1 year ago
acf-post-type-functions.php
1 year ago
acf-taxonomy-functions.php
1 year ago
acf-user-functions.php
1 week ago
acf-utility-functions.php
1 year ago
acf-value-functions.php
1 year ago
acf-wp-functions.php
1 year ago
assets.php
1 week ago
blocks-auto-inline-editing.php
2 months ago
blocks.php
2 weeks ago
class-acf-data.php
10 months ago
class-acf-internal-post-type.php
1 week ago
class-acf-options-page.php
1 year ago
class-acf-site-health.php
3 months ago
class-scf-json-schema-validator.php
6 months ago
class-scf-schema-builder.php
2 months ago
compatibility.php
1 year ago
datastore.php
1 month ago
deprecated.php
1 year ago
fields.php
10 months ago
index.php
1 year ago
l10n.php
1 year ago
local-fields.php
1 year ago
local-json.php
1 month ago
local-meta.php
1 year ago
locations.php
1 year ago
loop.php
10 months ago
media.php
1 year ago
rest-api.php
10 months ago
revisions.php
1 month ago
scf-ui-options-page-functions.php
1 year ago
third-party.php
7 months ago
upgrades.php
2 weeks ago
validation.php
10 months ago
wpml.php
1 year ago
wpml.php
324 lines
| 1 | <?php |
| 2 | /** |
| 3 | * ACF WPML Compatibility |
| 4 | * |
| 5 | * @package wordpress/secure-custom-fields |
| 6 | */ |
| 7 | |
| 8 | if ( ! defined( 'ABSPATH' ) ) { |
| 9 | exit; // Exit if accessed directly |
| 10 | } |
| 11 | |
| 12 | if ( ! class_exists( 'ACF_WPML_Compatibility' ) ) : |
| 13 | |
| 14 | /** |
| 15 | * ACF WPML Compatibility |
| 16 | * |
| 17 | * This class is used to add compatibility with WPML. |
| 18 | * |
| 19 | * @date 06/23/2012 |
| 20 | * @since ACF 3.1.8 |
| 21 | */ |
| 22 | class ACF_WPML_Compatibility { |
| 23 | |
| 24 | /** |
| 25 | * Translation ID reference mapping. |
| 26 | * |
| 27 | * @var array Storage for translation ID references |
| 28 | */ |
| 29 | public $trid_ref = array(); |
| 30 | |
| 31 | /** |
| 32 | * Sets up the class functionality. |
| 33 | * |
| 34 | * @date 23/06/12 |
| 35 | * @since ACF 3.1.8 |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | |
| 41 | // global |
| 42 | global $sitepress; |
| 43 | |
| 44 | // update settings |
| 45 | acf_update_setting( 'default_language', $sitepress->get_default_language() ); |
| 46 | acf_update_setting( 'current_language', $sitepress->get_current_language() ); |
| 47 | |
| 48 | // localize data |
| 49 | acf_localize_data( |
| 50 | array( |
| 51 | 'language' => $sitepress->get_current_language(), |
| 52 | ) |
| 53 | ); |
| 54 | |
| 55 | // switch lang during AJAX action |
| 56 | add_action( 'acf/verify_ajax', array( $this, 'verify_ajax' ) ); |
| 57 | |
| 58 | // prevent 'acf-field' from being translated |
| 59 | add_filter( 'get_translatable_documents', array( $this, 'get_translatable_documents' ) ); |
| 60 | |
| 61 | // check if 'acf-field-group' is translatable |
| 62 | if ( $this->is_translatable() ) { |
| 63 | |
| 64 | // actions |
| 65 | add_action( 'acf/upgrade_500_field_group', array( $this, 'upgrade_500_field_group' ), 10, 2 ); |
| 66 | add_action( 'icl_make_duplicate', array( $this, 'icl_make_duplicate' ), 10, 4 ); |
| 67 | |
| 68 | // filters |
| 69 | add_filter( 'acf/settings/save_json', array( $this, 'settings_save_json' ) ); |
| 70 | add_filter( 'acf/settings/load_json', array( $this, 'settings_load_json' ) ); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Returns true if the acf-field-group post type is translatable. |
| 76 | * Also adds compatibility with ACF4 settings |
| 77 | * |
| 78 | * @date 10/04/2015 |
| 79 | * @since ACF 5.2.3 |
| 80 | * |
| 81 | * @return boolean |
| 82 | */ |
| 83 | public function is_translatable() { |
| 84 | |
| 85 | // global |
| 86 | global $sitepress; |
| 87 | |
| 88 | // vars |
| 89 | $post_types = $sitepress->get_setting( 'custom_posts_sync_option' ); |
| 90 | |
| 91 | // return false if no post types |
| 92 | if ( ! acf_is_array( $post_types ) ) { |
| 93 | return false; |
| 94 | } |
| 95 | |
| 96 | // prevent 'acf-field' from being translated |
| 97 | if ( ! empty( $post_types['acf-field'] ) ) { |
| 98 | $post_types['acf-field'] = 0; |
| 99 | $sitepress->set_setting( 'custom_posts_sync_option', $post_types ); |
| 100 | } |
| 101 | |
| 102 | // when upgrading to version 5, review 'acf' setting |
| 103 | // update 'acf-field-group' if 'acf' is translatable, and 'acf-field-group' does not yet exist |
| 104 | if ( ! empty( $post_types['acf'] ) && ! isset( $post_types['acf-field-group'] ) ) { |
| 105 | $post_types['acf-field-group'] = 1; |
| 106 | $sitepress->set_setting( 'custom_posts_sync_option', $post_types ); |
| 107 | } |
| 108 | |
| 109 | // return true if acf-field-group is translatable |
| 110 | if ( ! empty( $post_types['acf-field-group'] ) ) { |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | // return |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Update the icl_translations table data when creating the field groups. |
| 120 | * |
| 121 | * @date 10/04/2015 |
| 122 | * @since ACF 5.2.3 |
| 123 | * |
| 124 | * @param array $field_group The new field group array. |
| 125 | * @param object $ofg The old field group WP_Post object. |
| 126 | * @return void |
| 127 | */ |
| 128 | public function upgrade_500_field_group( $field_group, $ofg ) { |
| 129 | |
| 130 | // global |
| 131 | global $wpdb; |
| 132 | |
| 133 | // get translation rows (old acf4 and new acf5) |
| 134 | $old_row = $wpdb->get_row( |
| 135 | $wpdb->prepare( |
| 136 | "SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d", |
| 137 | 'post_acf', |
| 138 | $ofg->ID |
| 139 | ), |
| 140 | ARRAY_A |
| 141 | ); |
| 142 | |
| 143 | $new_row = $wpdb->get_row( |
| 144 | $wpdb->prepare( |
| 145 | "SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d", |
| 146 | 'post_acf-field-group', |
| 147 | $field_group['ID'] |
| 148 | ), |
| 149 | ARRAY_A |
| 150 | ); |
| 151 | |
| 152 | // bail early if no rows |
| 153 | if ( ! $old_row || ! $new_row ) { |
| 154 | return; |
| 155 | } |
| 156 | |
| 157 | // create reference of old trid to new trid |
| 158 | // trid is a simple int used to find associated objects |
| 159 | if ( empty( $this->trid_ref ) ) { |
| 160 | $this->trid_ref = array(); |
| 161 | } |
| 162 | |
| 163 | // update trid |
| 164 | if ( isset( $this->trid_ref[ $old_row['trid'] ] ) ) { |
| 165 | |
| 166 | // this field group is a translation of another, update it's trid to match the previously inserted group |
| 167 | $new_row['trid'] = $this->trid_ref[ $old_row['trid'] ]; |
| 168 | } else { |
| 169 | |
| 170 | // this field group is the first of it's translations, update the reference for future groups |
| 171 | $this->trid_ref[ $old_row['trid'] ] = $new_row['trid']; |
| 172 | } |
| 173 | |
| 174 | // update icl_translations |
| 175 | // Row is created by WPML, and much easier to tweak it here due to the very complicated and nonsensical WPML logic |
| 176 | $table = "{$wpdb->prefix}icl_translations"; |
| 177 | $data = array( |
| 178 | 'trid' => $new_row['trid'], |
| 179 | 'language_code' => $old_row['language_code'], |
| 180 | ); |
| 181 | $where = array( 'translation_id' => $new_row['translation_id'] ); |
| 182 | $data_format = array( '%d', '%s' ); |
| 183 | $where_format = array( '%d' ); |
| 184 | |
| 185 | // allow source_language_code to equal NULL |
| 186 | if ( $old_row['source_language_code'] ) { |
| 187 | $data['source_language_code'] = $old_row['source_language_code']; |
| 188 | $data_format[] = '%s'; |
| 189 | } |
| 190 | |
| 191 | // update wpdb |
| 192 | $result = $wpdb->update( $table, $data, $where, $data_format, $where_format ); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Modifies the json path. |
| 197 | * |
| 198 | * @date 19/05/2014 |
| 199 | * @since ACF 5.0.0 |
| 200 | * |
| 201 | * @param string $path The json save path. |
| 202 | * @return string |
| 203 | */ |
| 204 | public function settings_save_json( $path ) { |
| 205 | |
| 206 | // bail early if dir does not exist |
| 207 | if ( ! wp_is_writable( $path ) ) { |
| 208 | return $path; |
| 209 | } |
| 210 | |
| 211 | // amend |
| 212 | $path = untrailingslashit( $path ) . '/' . acf_get_setting( 'current_language' ); |
| 213 | |
| 214 | // make dir if does not exist |
| 215 | if ( ! file_exists( $path ) ) { |
| 216 | mkdir( $path, 0777, true ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_mkdir -- Allow legacy mkdir call as this may fire outside admin. |
| 217 | } |
| 218 | |
| 219 | // return |
| 220 | return $path; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Modifies the json path. |
| 225 | * |
| 226 | * @date 19/05/2014 |
| 227 | * @since ACF 5.0.0 |
| 228 | * |
| 229 | * @param array $paths The json save path. |
| 230 | * @return string |
| 231 | */ |
| 232 | public function settings_load_json( $paths ) { |
| 233 | |
| 234 | // loop |
| 235 | if ( $paths && is_array( $paths ) ) { |
| 236 | foreach ( $paths as $i => $path ) { |
| 237 | $paths[ $i ] = untrailingslashit( $path ) . '/' . acf_get_setting( 'current_language' ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // return |
| 242 | return $paths; |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Duplicate the icl_translations row when duplicating a field group. |
| 247 | * |
| 248 | * @date 26/02/2014 |
| 249 | * @since ACF 5.0.0 |
| 250 | * |
| 251 | * @param int $master_post_id The original post ID. |
| 252 | * @param string $lang The language code. |
| 253 | * @param array $postarr The post data. |
| 254 | * @param int $id The new post ID. |
| 255 | * |
| 256 | * @return void |
| 257 | */ |
| 258 | public function icl_make_duplicate( $master_post_id, $lang, $postarr, $id ) { |
| 259 | |
| 260 | // bail early if not acf-field-group |
| 261 | if ( 'acf-field-group' !== $postarr['post_type'] ) { |
| 262 | return; |
| 263 | } |
| 264 | |
| 265 | // update the lang |
| 266 | acf_update_setting( 'current_language', $lang ); |
| 267 | |
| 268 | // duplicate field group specifying the $post_id |
| 269 | acf_duplicate_field_group( $master_post_id, $id ); |
| 270 | |
| 271 | // always translate independently to avoid many many bugs! |
| 272 | // - translation post gets a new key (post_name) when original post is saved |
| 273 | // - local json creates new files due to changed key |
| 274 | // phpcs:disable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase -- Variable name is from WPML. |
| 275 | global $iclTranslationManagement; |
| 276 | $iclTranslationManagement->reset_duplicate_flag( $id ); |
| 277 | // phpcs:enable WordPress.NamingConventions.ValidVariableName.VariableNotSnakeCase |
| 278 | } |
| 279 | |
| 280 | |
| 281 | /** |
| 282 | * Sets the correct language during AJAX requests. |
| 283 | * |
| 284 | * @type function |
| 285 | * @date 7/08/2015 |
| 286 | * @since ACF 5.2.3 |
| 287 | * |
| 288 | * @return void |
| 289 | */ |
| 290 | public function verify_ajax() { |
| 291 | |
| 292 | // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Verified elsewhere. |
| 293 | // set the language for this AJAX request |
| 294 | // this will allow get_posts to work as expected (load posts from the correct language) |
| 295 | if ( isset( $_REQUEST['lang'] ) ) { |
| 296 | global $sitepress; |
| 297 | $sitepress->switch_lang( sanitize_text_field( wp_unslash( $_REQUEST['lang'] ) ) ); |
| 298 | } |
| 299 | // phpcs:enable WordPress.Security.NonceVerification.Recommended |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * Removes 'acf-field' from the available post types for translation. |
| 304 | * |
| 305 | * @type function |
| 306 | * @date 17/8/17 |
| 307 | * @since ACF 5.6.0 |
| 308 | * |
| 309 | * @param array $icl_post_types The array of post types. |
| 310 | * @return array |
| 311 | */ |
| 312 | public function get_translatable_documents( $icl_post_types ) { |
| 313 | |
| 314 | // unset |
| 315 | unset( $icl_post_types['acf-field'] ); |
| 316 | |
| 317 | // return |
| 318 | return $icl_post_types; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | acf_new_instance( 'ACF_WPML_Compatibility' ); |
| 323 | endif; // class_exists check |
| 324 |