| 1 |
<?php |
| 2 |
|
| 3 |
class acf_wpml_compatibility { |
| 4 |
|
| 5 |
var $lang = ''; |
| 6 |
|
| 7 |
|
| 8 |
/* |
| 9 |
* Constructor |
| 10 |
* |
| 11 |
* This function will construct all the neccessary actions and filters |
| 12 |
* |
| 13 |
* @type function |
| 14 |
* @date 23/06/12 |
| 15 |
* @since 3.1.8 |
| 16 |
* |
| 17 |
* @param N/A |
| 18 |
* @return N/A |
| 19 |
*/ |
| 20 |
|
| 21 |
function __construct() { |
| 22 |
|
| 23 |
// global |
| 24 |
global $sitepress; |
| 25 |
|
| 26 |
|
| 27 |
// vars |
| 28 |
$this->lang = acf_maybe_get_POST('_acf_lang', ICL_LANGUAGE_CODE); |
| 29 |
|
| 30 |
|
| 31 |
// update settings |
| 32 |
acf_update_setting('default_language', $sitepress->get_default_language()); |
| 33 |
acf_update_setting('current_language', $this->lang); |
| 34 |
|
| 35 |
|
| 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); |
| 40 |
|
| 41 |
|
| 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 |
|
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
/* |
| 65 |
* is_translatable |
| 66 |
* |
| 67 |
* This fucntion will return true if the acf-field-group post type is translatable |
| 68 |
* |
| 69 |
* @type function |
| 70 |
* @date 10/04/2015 |
| 71 |
* @since 5.2.3 |
| 72 |
* |
| 73 |
* @param $post_id (int) |
| 74 |
* @return $post_id (int) |
| 75 |
*/ |
| 76 |
|
| 77 |
function is_translatable() { |
| 78 |
|
| 79 |
// global |
| 80 |
global $sitepress; |
| 81 |
|
| 82 |
|
| 83 |
// vars |
| 84 |
$post_types = $sitepress->get_setting('custom_posts_sync_option'); |
| 85 |
|
| 86 |
|
| 87 |
// bail early if no post types |
| 88 |
if( !acf_is_array($post_types) ) return false; |
| 89 |
|
| 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 |
|
| 96 |
// return true if acf-field-group is translatable |
| 97 |
if( !empty($post_types['acf-field-group']) ) { |
| 98 |
return true; |
| 99 |
} |
| 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 |
|
| 108 |
// return |
| 109 |
return false; |
| 110 |
|
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
/* |
| 115 |
* update_500 |
| 116 |
* |
| 117 |
* This function will update the WPML settings to allow 'acf-field-group' to be translatable |
| 118 |
* |
| 119 |
* @type function |
| 120 |
* @date 10/04/2015 |
| 121 |
* @since 5.2.3 |
| 122 |
* |
| 123 |
* @param $post_id (int) |
| 124 |
* @return $post_id (int) |
| 125 |
*/ |
| 126 |
|
| 127 |
function update_500() { |
| 128 |
|
| 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 |
| 171 |
global $wpdb; |
| 172 |
|
| 173 |
|
| 174 |
// get translation rows (old acf4 and new acf5) |
| 175 |
$old_row = $wpdb->get_row($wpdb->prepare( |
| 176 |
"SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d", |
| 177 |
'post_acf', $ofg->ID |
| 178 |
), ARRAY_A); |
| 179 |
|
| 180 |
$new_row = $wpdb->get_row($wpdb->prepare( |
| 181 |
"SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d", |
| 182 |
'post_acf-field-group', $field_group['ID'] |
| 183 |
), ARRAY_A); |
| 184 |
|
| 185 |
|
| 186 |
// bail ealry if no rows |
| 187 |
if( !$old_row || !$new_row ) { |
| 188 |
|
| 189 |
return; |
| 190 |
|
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
// create reference of old trid to new trid |
| 195 |
// trid is a simple int used to find associated objects |
| 196 |
if( empty($this->trid_ref) ) { |
| 197 |
|
| 198 |
$this->trid_ref = array(); |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
// update trid |
| 204 |
if( isset($this->trid_ref[ $old_row['trid'] ]) ) { |
| 205 |
|
| 206 |
// this field group is a translation of another, update it's trid to match the previously inserted group |
| 207 |
$new_row['trid'] = $this->trid_ref[ $old_row['trid'] ]; |
| 208 |
|
| 209 |
} else { |
| 210 |
|
| 211 |
// this field group is the first of it's translations, update the reference for future groups |
| 212 |
$this->trid_ref[ $old_row['trid'] ] = $new_row['trid']; |
| 213 |
|
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
// update icl_translations |
| 218 |
// Row is created by WPML, and much easier to tweak it here due to the very complicated and nonsensical WPML logic |
| 219 |
$table = "{$wpdb->prefix}icl_translations"; |
| 220 |
$data = array( 'trid' => $new_row['trid'], 'language_code' => $old_row['language_code'] ); |
| 221 |
$where = array( 'translation_id' => $new_row['translation_id'] ); |
| 222 |
$data_format = array( '%d', '%s' ); |
| 223 |
$where_format = array( '%d' ); |
| 224 |
|
| 225 |
|
| 226 |
// allow source_language_code to equal NULL |
| 227 |
if( $old_row['source_language_code'] ) { |
| 228 |
|
| 229 |
$data['source_language_code'] = $old_row['source_language_code']; |
| 230 |
$data_format[] = '%s'; |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
// update wpdb |
| 236 |
$result = $wpdb->update( $table, $data, $where, $data_format, $where_format ); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 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 |
/* |
| 264 |
* settings_save_json |
| 265 |
* |
| 266 |
* This function is hooked into the acf/update_field_group action and will save all field group data to a .json file |
| 267 |
* |
| 268 |
* @type function |
| 269 |
* @date 19/05/2014 |
| 270 |
* @since 5.0.0 |
| 271 |
* |
| 272 |
* @param $post_id (int) |
| 273 |
* @return $post_id (int) |
| 274 |
*/ |
| 275 |
|
| 276 |
function settings_save_json( $path ) { |
| 277 |
|
| 278 |
// bail early if dir does not exist |
| 279 |
if( !is_writable($path) ) { |
| 280 |
|
| 281 |
return $path; |
| 282 |
|
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
// remove trailing slash |
| 287 |
$path = untrailingslashit( $path ); |
| 288 |
|
| 289 |
|
| 290 |
// ammend |
| 291 |
$path = $path . '/' . $this->lang; |
| 292 |
|
| 293 |
|
| 294 |
// make dir if does not exist |
| 295 |
if( !file_exists($path) ) { |
| 296 |
|
| 297 |
mkdir($path, 0777, true); |
| 298 |
|
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
// return |
| 303 |
return $path; |
| 304 |
|
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
/* |
| 309 |
* settings_load_json |
| 310 |
* |
| 311 |
* description |
| 312 |
* |
| 313 |
* @type function |
| 314 |
* @date 19/05/2014 |
| 315 |
* @since 5.0.0 |
| 316 |
* |
| 317 |
* @param $post_id (int) |
| 318 |
* @return $post_id (int) |
| 319 |
*/ |
| 320 |
|
| 321 |
function settings_load_json( $paths ) { |
| 322 |
|
| 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 |
} |
| 336 |
|
| 337 |
|
| 338 |
// return |
| 339 |
return $paths; |
| 340 |
|
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
/* |
| 346 |
* icl_make_duplicate |
| 347 |
* |
| 348 |
* description |
| 349 |
* |
| 350 |
* @type function |
| 351 |
* @date 26/02/2014 |
| 352 |
* @since 5.0.0 |
| 353 |
* |
| 354 |
* @param $post_id (int) |
| 355 |
* @return $post_id (int) |
| 356 |
*/ |
| 357 |
|
| 358 |
function icl_make_duplicate( $master_post_id, $lang, $postarr, $id ) { |
| 359 |
|
| 360 |
// validate |
| 361 |
if( $postarr['post_type'] != 'acf-field-group' ) { |
| 362 |
|
| 363 |
return; |
| 364 |
|
| 365 |
} |
| 366 |
|
| 367 |
|
| 368 |
// duplicate field group |
| 369 |
acf_duplicate_field_group( $master_post_id, $id ); |
| 370 |
|
| 371 |
|
| 372 |
// always translate independately to avoid many many bugs! |
| 373 |
// - translation post gets a new key (post_name) when origional post is saved |
| 374 |
// - local json creates new files due to changed key |
| 375 |
global $iclTranslationManagement; |
| 376 |
|
| 377 |
$iclTranslationManagement->reset_duplicate_flag( $id ); |
| 378 |
|
| 379 |
} |
| 380 |
|
| 381 |
|
| 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 |
/* |
| 421 |
* verify_ajax |
| 422 |
* |
| 423 |
* This function will help avoid WPML conflicts when performing an ACF ajax request |
| 424 |
* |
| 425 |
* @type function |
| 426 |
* @date 7/08/2015 |
| 427 |
* @since 5.2.3 |
| 428 |
* |
| 429 |
* @param n/a |
| 430 |
* @return n/a |
| 431 |
*/ |
| 432 |
|
| 433 |
function verify_ajax() { |
| 434 |
|
| 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 |
| 448 |
// this will allow get_posts to work as expected (load posts from the correct language) |
| 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 |
|
| 457 |
} |
| 458 |
|
| 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 |
/* |
| 486 |
* get_translatable_documents |
| 487 |
* |
| 488 |
* This filter will remove 'acf-field' from the available post types for translation |
| 489 |
* |
| 490 |
* @type function |
| 491 |
* @date 17/8/17 |
| 492 |
* @since 5.6.0 |
| 493 |
* |
| 494 |
* @param $post_id (int) |
| 495 |
* @return $post_id (int) |
| 496 |
*/ |
| 497 |
|
| 498 |
function get_translatable_documents( $icl_post_types ) { |
| 499 |
|
| 500 |
// unset |
| 501 |
unset( $icl_post_types['acf-field'] ); |
| 502 |
|
| 503 |
|
| 504 |
// return |
| 505 |
return $icl_post_types; |
| 506 |
|
| 507 |
} |
| 508 |
|
| 509 |
} |
| 510 |
|
| 511 |
new acf_wpml_compatibility(); |
| 512 |
|
| 513 |
?> |