PluginProbe
Advanced Custom Fields (ACF®) / 5.6.6
Advanced Custom Fields (ACF®) v5.6.6
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
← All changes | includes/wpml.php +308 -97 5.9.85.6.6 View file →
@@ -1,120 +1,177 @@
1 -<?php
1 +<?php
2 2
3 -if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4 -
5 -if( ! class_exists('ACF_WPML_Compatibility') ) :
6 -
7 -class ACF_WPML_Compatibility {
3 +class acf_wpml_compatibility {
8 4
9 - /**
10 - * __construct
5 + var $lang = '';
6 +
7 +
8 + /*
9 + * Constructor
11 10 *
12 - * Sets up the class functionality.
11 + * This function will construct all the neccessary actions and filters
13 12 *
13 + * @type function
14 14 * @date 23/06/12
15 15 * @since 3.1.8
16 16 *
17 - * @param void
18 - * @return void
17 + * @param N/A
18 + * @return N/A
19 19 */
20 +
20 21 function __construct() {
21 22
22 23 // global
23 24 global $sitepress;
24 25
26 +
27 + // vars
28 + $this->lang = acf_maybe_get_POST('_acf_lang', ICL_LANGUAGE_CODE);
29 +
30 +
25 31 // update settings
26 32 acf_update_setting('default_language', $sitepress->get_default_language());
27 - acf_update_setting('current_language', $sitepress->get_current_language());
33 + acf_update_setting('current_language', $this->lang);
28 34
29 - // localize data
30 - acf_localize_data(array(
31 - 'language' => $sitepress->get_current_language()
32 - ));
33 35
34 - // switch lang during AJAX action
35 - add_action('acf/verify_ajax', array($this, 'verify_ajax'));
36 + // actions
37 + add_action('acf/verify_ajax', array($this, 'verify_ajax'));
38 + add_action('acf/input/admin_footer', array($this, 'admin_footer'));
39 + add_action('acf/input/form_data', array($this, 'acf_input_form_data'), 10, 1);
36 40
37 - // prevent 'acf-field' from being translated
38 - add_filter('get_translatable_documents', array($this, 'get_translatable_documents'));
39 41
40 - // check if 'acf-field-group' is translatable
41 - if( $this->is_translatable() ) {
42 -
43 - // actions
44 - add_action('acf/upgrade_500_field_group', array($this, 'upgrade_500_field_group'), 10, 2);
45 - add_action('icl_make_duplicate', array($this, 'icl_make_duplicate'), 10, 4);
46 -
47 - // filters
48 - add_filter('acf/settings/save_json', array($this, 'settings_save_json'));
49 - add_filter('acf/settings/load_json', array($this, 'settings_load_json'));
50 - }
42 + // always prevent 'acf-field' from being translated
43 + add_filter('get_translatable_documents', array($this, 'get_translatable_documents'));
44 +
45 +
46 + // bail early if not transaltable
47 + if( !$this->is_translatable() ) return;
48 +
49 +
50 + // actions
51 + add_action('acf/update_500', array($this, 'update_500'), 10);
52 + add_action('acf/update_500_field_group', array($this, 'update_500_field_group'), 10, 2);
53 + add_action('acf/update_field_group', array($this, 'update_field_group'), 2, 1);
54 + add_action('icl_make_duplicate', array($this, 'icl_make_duplicate'), 10, 4);
55 +
56 +
57 + // filters
58 + add_filter('acf/settings/save_json', array($this, 'settings_save_json'));
59 + add_filter('acf/settings/load_json', array($this, 'settings_load_json'));
60 +
51 61 }
52 62
53 - /**
63 +
64 + /*
54 65 * is_translatable
55 66 *
56 - * Returns true if the acf-field-group post type is translatable.
57 - * Also adds compatibility with ACF4 settings
67 + * This fucntion will return true if the acf-field-group post type is translatable
58 68 *
69 + * @type function
59 70 * @date 10/04/2015
60 71 * @since 5.2.3
61 72 *
62 - * @param void
63 - * @return bool
73 + * @param $post_id (int)
74 + * @return $post_id (int)
64 75 */
76 +
65 77 function is_translatable() {
66 78
67 79 // global
68 80 global $sitepress;
69 81
82 +
70 83 // vars
71 84 $post_types = $sitepress->get_setting('custom_posts_sync_option');
72 85
73 - // return false if no post types
74 - if( !acf_is_array($post_types) ) {
75 - return false;
76 - }
77 86
78 - // prevent 'acf-field' from being translated
79 - if( !empty($post_types['acf-field']) ) {
80 - $post_types['acf-field'] = 0;
81 - $sitepress->set_setting('custom_posts_sync_option', $post_types);
82 - }
87 + // bail early if no post types
88 + if( !acf_is_array($post_types) ) return false;
83 89
84 - // when upgrading to version 5, review 'acf' setting
85 - // update 'acf-field-group' if 'acf' is translatable, and 'acf-field-group' does not yet exist
86 - if( !empty($post_types['acf']) && !isset($post_types['acf-field-group']) ) {
87 - $post_types['acf-field-group'] = 1;
88 - $sitepress->set_setting('custom_posts_sync_option', $post_types);
89 - }
90 90
91 + // always prevent 'acf-field' from being translated
92 + $post_types['acf-field'] = 0;
93 + $sitepress->set_setting('custom_posts_sync_option', $post_types);
94 +
95 +
91 96 // return true if acf-field-group is translatable
92 97 if( !empty($post_types['acf-field-group']) ) {
93 98 return true;
94 99 }
95 100
101 +
102 + // return true if acf is translatable, and acf-field-group does not yet exist
103 + if( !empty($post_types['acf']) && !isset($post_types['acf-field-group']) ) {
104 + return true;
105 + }
106 +
107 +
96 108 // return
97 109 return false;
110 +
98 111 }
99 112
100 - /**
101 - * upgrade_500_field_group
113 +
114 + /*
115 + * update_500
102 116 *
103 - * Update the icl_translations table data when creating the field groups.
117 + * This function will update the WPML settings to allow 'acf-field-group' to be translatable
104 118 *
119 + * @type function
105 120 * @date 10/04/2015
106 121 * @since 5.2.3
107 122 *
108 - * @param array $field_group The new field group array.
109 - * @param object $ofg The old field group WP_Post object.
110 - * @return void
123 + * @param $post_id (int)
124 + * @return $post_id (int)
111 125 */
112 - function upgrade_500_field_group($field_group, $ofg) {
126 +
127 + function update_500() {
113 128
114 129 // global
130 + global $sitepress;
131 +
132 +
133 + // vars
134 + $post_types = $sitepress->get_setting('custom_posts_sync_option');
135 +
136 +
137 + // bail early if no post types
138 + if( !acf_is_array($post_types) ) return false;
139 +
140 +
141 + // post type has changed from 'acf' to 'acf-field-group'
142 + if( !empty($post_types['acf']) ) {
143 +
144 + $post_types['acf-field-group'] = 1;
145 +
146 + }
147 +
148 +
149 + // update
150 + $sitepress->set_setting('custom_posts_sync_option', $post_types);
151 +
152 + }
153 +
154 +
155 + /*
156 + * update_500_field_group
157 + *
158 + * This function will update the icl_translations table data when creating the fiedl groups
159 + *
160 + * @type function
161 + * @date 10/04/2015
162 + * @since 5.2.3
163 + *
164 + * @param $field_group (array)
165 + * @return n/a
166 + */
167 +
168 + function update_500_field_group($field_group, $ofg) {
169 +
170 + // global
115 171 global $wpdb;
116 172
173 +
117 174 // get translation rows (old acf4 and new acf5)
118 175 $old_row = $wpdb->get_row($wpdb->prepare(
119 176 "SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d",
120 177 'post_acf', $ofg->ID
@@ -124,30 +181,40 @@
124 181 "SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d",
125 182 'post_acf-field-group', $field_group['ID']
126 183 ), ARRAY_A);
127 184
185 +
128 186 // bail ealry if no rows
129 187 if( !$old_row || !$new_row ) {
188 +
130 189 return;
190 +
131 191 }
132 192
193 +
133 194 // create reference of old trid to new trid
134 195 // trid is a simple int used to find associated objects
135 196 if( empty($this->trid_ref) ) {
197 +
136 198 $this->trid_ref = array();
199 +
137 200 }
138 201
202 +
139 203 // update trid
140 204 if( isset($this->trid_ref[ $old_row['trid'] ]) ) {
141 205
142 206 // this field group is a translation of another, update it's trid to match the previously inserted group
143 207 $new_row['trid'] = $this->trid_ref[ $old_row['trid'] ];
208 +
144 209 } else {
145 210
146 211 // this field group is the first of it's translations, update the reference for future groups
147 212 $this->trid_ref[ $old_row['trid'] ] = $new_row['trid'];
213 +
148 214 }
149 215
216 +
150 217 // update icl_translations
151 218 // Row is created by WPML, and much easier to tweak it here due to the very complicated and nonsensical WPML logic
152 219 $table = "{$wpdb->prefix}icl_translations";
153 220 $data = array( 'trid' => $new_row['trid'], 'language_code' => $old_row['language_code'] );
@@ -154,149 +221,293 @@
154 221 $where = array( 'translation_id' => $new_row['translation_id'] );
155 222 $data_format = array( '%d', '%s' );
156 223 $where_format = array( '%d' );
157 224
225 +
158 226 // allow source_language_code to equal NULL
159 227 if( $old_row['source_language_code'] ) {
160 228
161 229 $data['source_language_code'] = $old_row['source_language_code'];
162 230 $data_format[] = '%s';
231 +
163 232 }
164 233
234 +
165 235 // update wpdb
166 236 $result = $wpdb->update( $table, $data, $where, $data_format, $where_format );
237 +
167 238 }
168 239
169 - /**
240 +
241 + /*
242 + * update_field_group
243 + *
244 + * This function will update the lang when saving a field group
245 + *
246 + * @type function
247 + * @date 10/03/2014
248 + * @since 5.0.0
249 + *
250 + * @param $field_group (array)
251 + * @return n/a
252 + */
253 +
254 + function update_field_group( $field_group ) {
255 +
256 + global $sitepress;
257 +
258 + $this->lang = $sitepress->get_language_for_element($field_group['ID'], 'post_acf-field-group');
259 +
260 + }
261 +
262 +
263 + /*
170 264 * settings_save_json
171 265 *
172 - * Modifies the json path.
266 + * This function is hooked into the acf/update_field_group action and will save all field group data to a .json file
173 267 *
268 + * @type function
174 269 * @date 19/05/2014
175 270 * @since 5.0.0
176 271 *
177 - * @param string $path The json save path.
178 - * @return string
272 + * @param $post_id (int)
273 + * @return $post_id (int)
179 274 */
275 +
180 276 function settings_save_json( $path ) {
181 -
277 +
182 278 // bail early if dir does not exist
183 279 if( !is_writable($path) ) {
280 +
184 281 return $path;
282 +
185 283 }
186 284
285 +
286 + // remove trailing slash
287 + $path = untrailingslashit( $path );
288 +
289 +
187 290 // ammend
188 - $path = untrailingslashit($path) . '/' . acf_get_setting('current_language');
291 + $path = $path . '/' . $this->lang;
189 292
293 +
190 294 // make dir if does not exist
191 295 if( !file_exists($path) ) {
296 +
192 297 mkdir($path, 0777, true);
298 +
193 299 }
194 300
301 +
195 302 // return
196 303 return $path;
304 +
197 305 }
198 306
199 - /**
307 +
308 + /*
200 309 * settings_load_json
201 310 *
202 - * Modifies the json path.
311 + * description
203 312 *
313 + * @type function
204 314 * @date 19/05/2014
205 315 * @since 5.0.0
206 316 *
207 - * @param string $path The json save path.
208 - * @return string
209 - */
317 + * @param $post_id (int)
318 + * @return $post_id (int)
319 + */
320 +
210 321 function settings_load_json( $paths ) {
211 322
212 - // loop
213 - if( $paths ) {
214 - foreach( $paths as $i => $path ) {
215 - $paths[ $i ] = untrailingslashit($path) . '/' . acf_get_setting('current_language');
216 - }}
323 + if( !empty($paths) ) {
324 +
325 + foreach( $paths as $i => $path ) {
326 +
327 + // remove trailing slash
328 + $path = untrailingslashit( $path );
329 +
330 +
331 + // ammend
332 + $paths[ $i ] = $path . '/' . $this->lang;
333 +
334 + }
335 + }
217 336
337 +
218 338 // return
219 339 return $paths;
340 +
220 341 }
221 342
222 - /**
343 +
344 +
345 + /*
223 346 * icl_make_duplicate
224 347 *
225 348 * description
226 349 *
350 + * @type function
227 351 * @date 26/02/2014
228 352 * @since 5.0.0
229 353 *
230 - * @param void
231 - * @return void
354 + * @param $post_id (int)
355 + * @return $post_id (int)
232 356 */
357 +
233 358 function icl_make_duplicate( $master_post_id, $lang, $postarr, $id ) {
234 359
235 - // bail early if not acf-field-group
360 + // validate
236 361 if( $postarr['post_type'] != 'acf-field-group' ) {
362 +
237 363 return;
364 +
238 365 }
239 366
240 - // update the lang
241 - acf_update_setting('current_language', $lang);
242 367
243 - // duplicate field group specifying the $post_id
368 + // duplicate field group
244 369 acf_duplicate_field_group( $master_post_id, $id );
245 370
371 +
246 372 // always translate independately to avoid many many bugs!
247 373 // - translation post gets a new key (post_name) when origional post is saved
248 374 // - local json creates new files due to changed key
249 375 global $iclTranslationManagement;
376 +
250 377 $iclTranslationManagement->reset_duplicate_flag( $id );
378 +
251 379 }
252 380
253 381
254 - /**
382 + /*
383 + * admin_footer
384 + *
385 + * description
386 + *
387 + * @type function
388 + * @date 27/02/2014
389 + * @since 5.0.0
390 + *
391 + * @param $post_id (int)
392 + * @return $post_id (int)
393 + */
394 +
395 + function admin_footer() {
396 +
397 + ?>
398 + <script type="text/javascript">
399 + (function($) {
400 +
401 + // add filter
402 + acf.add_filter('prepare_for_ajax', function( args ){
403 +
404 + // append
405 + args.lang = '<?php echo $this->lang; ?>';
406 +
407 +
408 + // return
409 + return args;
410 +
411 + });
412 +
413 + })(jQuery);
414 + </script>
415 + <?php
416 +
417 + }
418 +
419 +
420 + /*
255 421 * verify_ajax
256 422 *
257 - * Sets the correct language during AJAX requests.
423 + * This function will help avoid WPML conflicts when performing an ACF ajax request
258 424 *
259 425 * @type function
260 426 * @date 7/08/2015
261 427 * @since 5.2.3
262 428 *
263 - * @param void
264 - * @return void
429 + * @param n/a
430 + * @return n/a
265 431 */
432 +
266 433 function verify_ajax() {
267 434
268 - // set the language for this AJAX request
435 + // globals
436 + global $sitepress;
437 +
438 +
439 + // vars
440 + $lang = acf_maybe_get($_POST, 'lang');
441 +
442 +
443 + // bail early if no lang
444 + if( !$lang ) return;
445 +
446 +
447 + // switch lang
269 448 // this will allow get_posts to work as expected (load posts from the correct language)
270 - if( isset($_REQUEST['lang']) ) {
271 - global $sitepress;
272 - $sitepress->switch_lang( $_REQUEST['lang'] );
273 - }
449 + $sitepress->switch_lang( $_REQUEST['lang'] );
450 +
451 +
452 + // remove post_id
453 + // this will prevent WPML from setting the current language based on the current post being edited
454 + // in theory, WPML is correct, however, when adding a new post, the post's lang is not found and will default to 'en'
455 + unset( $_REQUEST['post_id'] );
456 +
274 457 }
275 458
276 - /**
459 +
460 + /*
461 + * acf_input_form_data
462 + *
463 + * description
464 + *
465 + * @type function
466 + * @date 16/12/16
467 + * @since 5.5.0
468 + *
469 + * @param $post_id (int)
470 + * @return $post_id (int)
471 + */
472 +
473 + function acf_input_form_data( $data ) {
474 +
475 + // bail early if not options
476 + if( $data['nonce'] !== 'options' ) return;
477 +
478 +
479 + // add hidden input
480 + acf_hidden_input(array('id' => '_acf_lang', 'name' => '_acf_lang', 'value' => $this->lang));
481 +
482 + }
483 +
484 +
485 + /*
277 486 * get_translatable_documents
278 487 *
279 - * Removes 'acf-field' from the available post types for translation.
488 + * This filter will remove 'acf-field' from the available post types for translation
280 489 *
281 490 * @type function
282 491 * @date 17/8/17
283 492 * @since 5.6.0
284 493 *
285 - * @param array $icl_post_types The array of post types.
286 - * @return array
494 + * @param $post_id (int)
495 + * @return $post_id (int)
287 496 */
497 +
288 498 function get_translatable_documents( $icl_post_types ) {
289 499
290 500 // unset
291 501 unset( $icl_post_types['acf-field'] );
292 502
503 +
293 504 // return
294 505 return $icl_post_types;
506 +
295 507 }
508 +
296 509 }
297 510
298 -acf_new_instance('ACF_WPML_Compatibility');
299 -
300 -endif; // class_exists check
511 +new acf_wpml_compatibility();
301 512
302 513 ?>