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