| 1 |
<?php |
| 2 |
|
| 3 |
if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 |
|
| 5 |
if( ! class_exists('ACF_WPML_Compatibility') ) : |
| 6 |
|
| 7 |
class ACF_WPML_Compatibility { |
| 8 |
|
| 9 |
/** |
| 10 |
* __construct |
| 11 |
* |
| 12 |
* Sets up the class functionality. |
| 13 |
* |
| 14 |
* @date 23/06/12 |
| 15 |
* @since 3.1.8 |
| 16 |
* |
| 17 |
* @param void |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
function __construct() { |
| 21 |
|
| 22 |
// global |
| 23 |
global $sitepress; |
| 24 |
|
| 25 |
// update settings |
| 26 |
acf_update_setting('default_language', $sitepress->get_default_language()); |
| 27 |
acf_update_setting('current_language', $sitepress->get_current_language()); |
| 28 |
|
| 29 |
// localize data |
| 30 |
acf_localize_data(array( |
| 31 |
'language' => $sitepress->get_current_language() |
| 32 |
)); |
| 33 |
|
| 34 |
// switch lang during AJAX action |
| 35 |
add_action('acf/verify_ajax', array($this, 'verify_ajax')); |
| 36 |
|
| 37 |
// prevent 'acf-field' from being translated |
| 38 |
add_filter('get_translatable_documents', array($this, 'get_translatable_documents')); |
| 39 |
|
| 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 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* is_translatable |
| 55 |
* |
| 56 |
* Returns true if the acf-field-group post type is translatable. |
| 57 |
* Also adds compatibility with ACF4 settings |
| 58 |
* |
| 59 |
* @date 10/04/2015 |
| 60 |
* @since 5.2.3 |
| 61 |
* |
| 62 |
* @param void |
| 63 |
* @return bool |
| 64 |
*/ |
| 65 |
function is_translatable() { |
| 66 |
|
| 67 |
// global |
| 68 |
global $sitepress; |
| 69 |
|
| 70 |
// vars |
| 71 |
$post_types = $sitepress->get_setting('custom_posts_sync_option'); |
| 72 |
|
| 73 |
// return false if no post types |
| 74 |
if( !acf_is_array($post_types) ) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 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 |
} |
| 83 |
|
| 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 |
|
| 91 |
// return true if acf-field-group is translatable |
| 92 |
if( !empty($post_types['acf-field-group']) ) { |
| 93 |
return true; |
| 94 |
} |
| 95 |
|
| 96 |
// return |
| 97 |
return false; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* upgrade_500_field_group |
| 102 |
* |
| 103 |
* Update the icl_translations table data when creating the field groups. |
| 104 |
* |
| 105 |
* @date 10/04/2015 |
| 106 |
* @since 5.2.3 |
| 107 |
* |
| 108 |
* @param array $field_group The new field group array. |
| 109 |
* @param object $ofg The old field group WP_Post object. |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
function upgrade_500_field_group($field_group, $ofg) { |
| 113 |
|
| 114 |
// global |
| 115 |
global $wpdb; |
| 116 |
|
| 117 |
// get translation rows (old acf4 and new acf5) |
| 118 |
$old_row = $wpdb->get_row($wpdb->prepare( |
| 119 |
"SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d", |
| 120 |
'post_acf', $ofg->ID |
| 121 |
), ARRAY_A); |
| 122 |
|
| 123 |
$new_row = $wpdb->get_row($wpdb->prepare( |
| 124 |
"SELECT * FROM {$wpdb->prefix}icl_translations WHERE element_type=%s AND element_id=%d", |
| 125 |
'post_acf-field-group', $field_group['ID'] |
| 126 |
), ARRAY_A); |
| 127 |
|
| 128 |
// bail ealry if no rows |
| 129 |
if( !$old_row || !$new_row ) { |
| 130 |
return; |
| 131 |
} |
| 132 |
|
| 133 |
// create reference of old trid to new trid |
| 134 |
// trid is a simple int used to find associated objects |
| 135 |
if( empty($this->trid_ref) ) { |
| 136 |
$this->trid_ref = array(); |
| 137 |
} |
| 138 |
|
| 139 |
// update trid |
| 140 |
if( isset($this->trid_ref[ $old_row['trid'] ]) ) { |
| 141 |
|
| 142 |
// this field group is a translation of another, update it's trid to match the previously inserted group |
| 143 |
$new_row['trid'] = $this->trid_ref[ $old_row['trid'] ]; |
| 144 |
} else { |
| 145 |
|
| 146 |
// this field group is the first of it's translations, update the reference for future groups |
| 147 |
$this->trid_ref[ $old_row['trid'] ] = $new_row['trid']; |
| 148 |
} |
| 149 |
|
| 150 |
// update icl_translations |
| 151 |
// Row is created by WPML, and much easier to tweak it here due to the very complicated and nonsensical WPML logic |
| 152 |
$table = "{$wpdb->prefix}icl_translations"; |
| 153 |
$data = array( 'trid' => $new_row['trid'], 'language_code' => $old_row['language_code'] ); |
| 154 |
$where = array( 'translation_id' => $new_row['translation_id'] ); |
| 155 |
$data_format = array( '%d', '%s' ); |
| 156 |
$where_format = array( '%d' ); |
| 157 |
|
| 158 |
// allow source_language_code to equal NULL |
| 159 |
if( $old_row['source_language_code'] ) { |
| 160 |
|
| 161 |
$data['source_language_code'] = $old_row['source_language_code']; |
| 162 |
$data_format[] = '%s'; |
| 163 |
} |
| 164 |
|
| 165 |
// update wpdb |
| 166 |
$result = $wpdb->update( $table, $data, $where, $data_format, $where_format ); |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* settings_save_json |
| 171 |
* |
| 172 |
* Modifies the json path. |
| 173 |
* |
| 174 |
* @date 19/05/2014 |
| 175 |
* @since 5.0.0 |
| 176 |
* |
| 177 |
* @param string $path The json save path. |
| 178 |
* @return string |
| 179 |
*/ |
| 180 |
function settings_save_json( $path ) { |
| 181 |
|
| 182 |
// bail early if dir does not exist |
| 183 |
if( !is_writable($path) ) { |
| 184 |
return $path; |
| 185 |
} |
| 186 |
|
| 187 |
// ammend |
| 188 |
$path = untrailingslashit($path) . '/' . acf_get_setting('current_language'); |
| 189 |
|
| 190 |
// make dir if does not exist |
| 191 |
if( !file_exists($path) ) { |
| 192 |
mkdir($path, 0777, true); |
| 193 |
} |
| 194 |
|
| 195 |
// return |
| 196 |
return $path; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* settings_load_json |
| 201 |
* |
| 202 |
* Modifies the json path. |
| 203 |
* |
| 204 |
* @date 19/05/2014 |
| 205 |
* @since 5.0.0 |
| 206 |
* |
| 207 |
* @param string $path The json save path. |
| 208 |
* @return string |
| 209 |
*/ |
| 210 |
function settings_load_json( $paths ) { |
| 211 |
|
| 212 |
// loop |
| 213 |
if( $paths ) { |
| 214 |
foreach( $paths as $i => $path ) { |
| 215 |
$paths[ $i ] = untrailingslashit($path) . '/' . acf_get_setting('current_language'); |
| 216 |
}} |
| 217 |
|
| 218 |
// return |
| 219 |
return $paths; |
| 220 |
} |
| 221 |
|
| 222 |
/** |
| 223 |
* icl_make_duplicate |
| 224 |
* |
| 225 |
* description |
| 226 |
* |
| 227 |
* @date 26/02/2014 |
| 228 |
* @since 5.0.0 |
| 229 |
* |
| 230 |
* @param void |
| 231 |
* @return void |
| 232 |
*/ |
| 233 |
function icl_make_duplicate( $master_post_id, $lang, $postarr, $id ) { |
| 234 |
|
| 235 |
// bail early if not acf-field-group |
| 236 |
if( $postarr['post_type'] != 'acf-field-group' ) { |
| 237 |
return; |
| 238 |
} |
| 239 |
|
| 240 |
// update the lang |
| 241 |
acf_update_setting('current_language', $lang); |
| 242 |
|
| 243 |
// duplicate field group specifying the $post_id |
| 244 |
acf_duplicate_field_group( $master_post_id, $id ); |
| 245 |
|
| 246 |
// always translate independately to avoid many many bugs! |
| 247 |
// - translation post gets a new key (post_name) when origional post is saved |
| 248 |
// - local json creates new files due to changed key |
| 249 |
global $iclTranslationManagement; |
| 250 |
$iclTranslationManagement->reset_duplicate_flag( $id ); |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* verify_ajax |
| 256 |
* |
| 257 |
* Sets the correct language during AJAX requests. |
| 258 |
* |
| 259 |
* @type function |
| 260 |
* @date 7/08/2015 |
| 261 |
* @since 5.2.3 |
| 262 |
* |
| 263 |
* @param void |
| 264 |
* @return void |
| 265 |
*/ |
| 266 |
function verify_ajax() { |
| 267 |
|
| 268 |
// set the language for this AJAX request |
| 269 |
// 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 |
} |
| 274 |
} |
| 275 |
|
| 276 |
/** |
| 277 |
* get_translatable_documents |
| 278 |
* |
| 279 |
* Removes 'acf-field' from the available post types for translation. |
| 280 |
* |
| 281 |
* @type function |
| 282 |
* @date 17/8/17 |
| 283 |
* @since 5.6.0 |
| 284 |
* |
| 285 |
* @param array $icl_post_types The array of post types. |
| 286 |
* @return array |
| 287 |
*/ |
| 288 |
function get_translatable_documents( $icl_post_types ) { |
| 289 |
|
| 290 |
// unset |
| 291 |
unset( $icl_post_types['acf-field'] ); |
| 292 |
|
| 293 |
// return |
| 294 |
return $icl_post_types; |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
acf_new_instance('ACF_WPML_Compatibility'); |
| 299 |
|
| 300 |
endif; // class_exists check |
| 301 |
|
| 302 |
?> |