PluginProbe
Advanced Custom Fields (ACF®) / 6.1.0
Advanced Custom Fields (ACF®) v6.1.0
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
advanced-custom-fields / includes / wpml.php
wpml.php
321 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 bool
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
176 $data['source_language_code'] = $old_row['source_language_code'];
177 $data_format[] = '%s';
178 }
179
180 // update wpdb
181 $result = $wpdb->update( $table, $data, $where, $data_format, $where_format );
182 }
183
184 /**
185 * settings_save_json
186 *
187 * Modifies the json path.
188 *
189 * @date 19/05/2014
190 * @since 5.0.0
191 *
192 * @param string $path The json save path.
193 * @return string
194 */
195 function settings_save_json( $path ) {
196
197 // bail early if dir does not exist
198 if ( ! is_writable( $path ) ) {
199 return $path;
200 }
201
202 // ammend
203 $path = untrailingslashit( $path ) . '/' . acf_get_setting( 'current_language' );
204
205 // make dir if does not exist
206 if ( ! file_exists( $path ) ) {
207 mkdir( $path, 0777, true );
208 }
209
210 // return
211 return $path;
212 }
213
214 /**
215 * settings_load_json
216 *
217 * Modifies the json path.
218 *
219 * @date 19/05/2014
220 * @since 5.0.0
221 *
222 * @param string $path The json save path.
223 * @return string
224 */
225 function settings_load_json( $paths ) {
226
227 // loop
228 if ( $paths ) {
229 foreach ( $paths as $i => $path ) {
230 $paths[ $i ] = untrailingslashit( $path ) . '/' . acf_get_setting( 'current_language' );
231 }
232 }
233
234 // return
235 return $paths;
236 }
237
238 /**
239 * icl_make_duplicate
240 *
241 * description
242 *
243 * @date 26/02/2014
244 * @since 5.0.0
245 *
246 * @param void
247 * @return void
248 */
249 function icl_make_duplicate( $master_post_id, $lang, $postarr, $id ) {
250
251 // bail early if not acf-field-group
252 if ( $postarr['post_type'] != 'acf-field-group' ) {
253 return;
254 }
255
256 // update the lang
257 acf_update_setting( 'current_language', $lang );
258
259 // duplicate field group specifying the $post_id
260 acf_duplicate_field_group( $master_post_id, $id );
261
262 // always translate independately to avoid many many bugs!
263 // - translation post gets a new key (post_name) when origional post is saved
264 // - local json creates new files due to changed key
265 global $iclTranslationManagement;
266 $iclTranslationManagement->reset_duplicate_flag( $id );
267 }
268
269
270 /**
271 * verify_ajax
272 *
273 * Sets the correct language during AJAX requests.
274 *
275 * @type function
276 * @date 7/08/2015
277 * @since 5.2.3
278 *
279 * @param void
280 * @return void
281 */
282 function verify_ajax() {
283
284 // phpcs:disable WordPress.Security.NonceVerification.Recommended -- Verified elsewhere.
285 // set the language for this AJAX request
286 // this will allow get_posts to work as expected (load posts from the correct language)
287 if ( isset( $_REQUEST['lang'] ) ) {
288 global $sitepress;
289 $sitepress->switch_lang( sanitize_text_field( $_REQUEST['lang'] ) );
290 }
291 // phpcs:enable WordPress.Security.NonceVerification.Recommended
292 }
293
294 /**
295 * get_translatable_documents
296 *
297 * Removes 'acf-field' from the available post types for translation.
298 *
299 * @type function
300 * @date 17/8/17
301 * @since 5.6.0
302 *
303 * @param array $icl_post_types The array of post types.
304 * @return array
305 */
306 function get_translatable_documents( $icl_post_types ) {
307
308 // unset
309 unset( $icl_post_types['acf-field'] );
310
311 // return
312 return $icl_post_types;
313 }
314 }
315
316 acf_new_instance( 'ACF_WPML_Compatibility' );
317
318 endif; // class_exists check
319
320
321