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