PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 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 / admin / post-types / admin-field-group.php
secure-custom-fields / includes / admin / post-types Last commit date
admin-field-group.php 1 year ago admin-field-groups.php 1 year ago admin-post-type.php 1 year ago admin-post-types.php 1 year ago admin-taxonomies.php 1 year ago admin-taxonomy.php 1 year ago index.php 1 year ago
admin-field-group.php
594 lines
1 <?php
2 /**
3 * ACF Admin Field Group Class
4 *
5 * @class acf_admin_field_group
6 *
7 * @package ACF
8 * @subpackage Admin
9 */
10
11 if ( ! class_exists( 'acf_admin_field_group' ) ) :
12
13 /**
14 * ACF Admin Field Group Class
15 *
16 * All the logic for editing a field group
17 */
18 class acf_admin_field_group extends ACF_Admin_Internal_Post_Type {
19
20 /**
21 * The slug for the internal post type.
22 *
23 * @since 6.1
24 * @var string
25 */
26 public $post_type = 'acf-field-group';
27
28 /**
29 * The admin body class used for the post type.
30 *
31 * @since 6.1
32 * @var string
33 */
34 public $admin_body_class = 'acf-admin-single-field-group';
35
36 /**
37 * Constructs the class.
38 *
39 * @since 5.0.0
40 */
41 public function __construct() {
42 parent::__construct();
43
44 add_action( 'wp_ajax_acf/field_group/render_field_settings', array( $this, 'ajax_render_field_settings' ) );
45 add_action( 'wp_ajax_acf/field_group/render_location_rule', array( $this, 'ajax_render_location_rule' ) );
46 add_action( 'wp_ajax_acf/field_group/move_field', array( $this, 'ajax_move_field' ) );
47 }
48
49 /**
50 * Customizes the messages shown when editing a field group.
51 *
52 * @since 5.0.0
53 *
54 * @param array $messages Post type messages.
55 * @return array
56 */
57 public function post_updated_messages( $messages ) {
58 $messages['acf-field-group'] = array(
59 0 => '', // Unused. Messages start at index 1.
60 1 => __( 'Field group updated.', 'acf' ),
61 2 => __( 'Field group updated.', 'acf' ),
62 3 => __( 'Field group deleted.', 'acf' ),
63 4 => __( 'Field group updated.', 'acf' ),
64 5 => false, // field group does not support revisions.
65 6 => __( 'Field group published.', 'acf' ),
66 7 => __( 'Field group saved.', 'acf' ),
67 8 => __( 'Field group submitted.', 'acf' ),
68 9 => __( 'Field group scheduled for.', 'acf' ),
69 10 => __( 'Field group draft updated.', 'acf' ),
70 );
71
72 return $messages;
73 }
74
75 /**
76 * Enqueues any scripts necessary for internal post type.
77 *
78 * @since 5.0.0
79 */
80 public function admin_enqueue_scripts() {
81 parent::admin_enqueue_scripts();
82
83 acf_localize_text(
84 array(
85 'The string "field_" may not be used at the start of a field name' => esc_html__( 'The string "field_" may not be used at the start of a field name', 'acf' ),
86 'This field cannot be moved until its changes have been saved' => esc_html__( 'This field cannot be moved until its changes have been saved', 'acf' ),
87 'Field group title is required' => esc_html__( 'Field group title is required', 'acf' ),
88 'Move field group to trash?' => esc_html__( 'Move field group to trash?', 'acf' ),
89 'No toggle fields available' => esc_html__( 'No toggle fields available', 'acf' ),
90 'Move Custom Field' => esc_html__( 'Move Custom Field', 'acf' ),
91 'Close modal' => esc_html__( 'Close modal', 'acf' ),
92 'Field moved to other group' => esc_html__( 'Field moved to other group', 'acf' ),
93 'Field groups linked successfully.' => esc_html__( 'Field groups linked successfully.', 'acf' ),
94 'Checked' => esc_html__( 'Checked', 'acf' ),
95 '(no label)' => esc_html__( '(no label)', 'acf' ),
96 '(this field)' => esc_html__( '(this field)', 'acf' ),
97 'copy' => esc_html__( 'copy', 'acf' ),
98 'or' => esc_html__( 'or', 'acf' ),
99 'Show this field group if' => esc_html__( 'Show this field group if', 'acf' ),
100 'Null' => esc_html__( 'Null', 'acf' ),
101 'PRO Only' => esc_html__( 'PRO Only', 'acf' ),
102
103 // Conditions.
104 'Has any value' => esc_html__( 'Has any value', 'acf' ),
105 'Has no value' => esc_html__( 'Has no value', 'acf' ),
106 'Value is equal to' => esc_html__( 'Value is equal to', 'acf' ),
107 'Value is not equal to' => esc_html__( 'Value is not equal to', 'acf' ),
108 'Value matches pattern' => esc_html__( 'Value matches pattern', 'acf' ),
109 'Value contains' => esc_html__( 'Value contains', 'acf' ),
110 'Value is greater than' => esc_html__( 'Value is greater than', 'acf' ),
111 'Value is less than' => esc_html__( 'Value is less than', 'acf' ),
112 'Selection is greater than' => esc_html__( 'Selection is greater than', 'acf' ),
113 'Selection is less than' => esc_html__( 'Selection is less than', 'acf' ),
114 'Relationship is equal to' => esc_html__( 'Relationship is equal to', 'acf' ),
115 'Relationship is not equal to' => esc_html__( 'Relationship is not equal to', 'acf' ),
116 'Relationships contain' => esc_html__( 'Relationships contain', 'acf' ),
117 'Relationships do not contain' => esc_html__( 'Relationships do not contain', 'acf' ),
118 'Post is equal to' => esc_html__( 'Post is equal to', 'acf' ),
119 'Post is not equal to' => esc_html__( 'Post is not equal to', 'acf' ),
120 'Posts contain' => esc_html__( 'Posts contain', 'acf' ),
121 'Posts do not contain' => esc_html__( 'Posts do not contain', 'acf' ),
122 'Has any post selected' => esc_html__( 'Has any post selected', 'acf' ),
123 'Has no post selected' => esc_html__( 'Has no post selected', 'acf' ),
124 'Has any relationship selected' => esc_html__( 'Has any relationship selected', 'acf' ),
125 'Has no relationship selected' => esc_html__( 'Has no relationship selected', 'acf' ),
126 'Page is equal to' => esc_html__( 'Page is equal to', 'acf' ),
127 'Page is not equal to' => esc_html__( 'Page is not equal to', 'acf' ),
128 'Pages contain' => esc_html__( 'Pages contain', 'acf' ),
129 'Pages do not contain' => esc_html__( 'Pages do not contain', 'acf' ),
130 'Has any page selected' => esc_html__( 'Has any page selected', 'acf' ),
131 'Has no page selected' => esc_html__( 'Has no page selected', 'acf' ),
132 'User is equal to' => esc_html__( 'User is equal to', 'acf' ),
133 'User is not equal to' => esc_html__( 'User is not equal to', 'acf' ),
134 'Users contain' => esc_html__( 'Users contain', 'acf' ),
135 'Users do not contain' => esc_html__( 'Users do not contain', 'acf' ),
136 'Has any user selected' => esc_html__( 'Has any user selected', 'acf' ),
137 'Has no user selected' => esc_html__( 'Has no user selected', 'acf' ),
138 'Term is equal to' => esc_html__( 'Term is equal to', 'acf' ),
139 'Term is not equal to' => esc_html__( 'Term is not equal to', 'acf' ),
140 'Terms contain' => esc_html__( 'Terms contain', 'acf' ),
141 'Terms do not contain' => esc_html__( 'Terms do not contain', 'acf' ),
142 'Has any term selected' => esc_html__( 'Has any term selected', 'acf' ),
143 'Has no term selected' => esc_html__( 'Has no term selected', 'acf' ),
144
145 // Custom Select2 templates.
146 'Type to search...' => esc_html__( 'Type to search...', 'acf' ),
147 'This Field' => esc_html__( 'This Field', 'acf' ),
148 )
149 );
150
151 acf_localize_data(
152 array(
153 'fieldTypes' => acf_get_field_types_info(),
154 'fieldCategoriesL10n' => acf_get_field_categories_i18n(),
155 'PROUpgradeURL' => 'https://www.advancedcustomfields.com/pro/',
156 'PROFieldTypes' => acf_get_pro_field_types(),
157 'PROLocationTypes' => array(
158 'block' => esc_html__( 'Block', 'acf' ),
159 'options_page' => esc_html__( 'Options Page', 'acf' ),
160 ),
161 )
162 );
163
164 do_action( 'acf/field_group/admin_enqueue_scripts' );
165 }
166
167 /**
168 * Set up functionality for the field group edit page.
169 *
170 * @since 3.1.8
171 */
172 public function admin_head() {
173 global $post, $field_group;
174
175 // Set global var.
176 $field_group = acf_get_field_group( $post->ID );
177
178 // metaboxes.
179 add_meta_box( 'acf-field-group-fields', __( 'Fields', 'acf' ), array( $this, 'mb_fields' ), 'acf-field-group', 'normal', 'high' );
180 add_meta_box( 'acf-field-group-options', __( 'Settings', 'acf' ), array( $this, 'mb_options' ), 'acf-field-group', 'normal', 'high' );
181
182 // actions.
183 add_action( 'post_submitbox_misc_actions', array( $this, 'post_submitbox_misc_actions' ), 10, 0 );
184 add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ), 10, 0 );
185
186 // filters.
187 add_filter( 'screen_settings', array( $this, 'screen_settings' ), 10, 1 );
188 add_filter( 'get_user_option_screen_layout_acf-field-group', array( $this, 'screen_layout' ), 10, 1 );
189
190 // 3rd party hook.
191 do_action( 'acf/field_group/admin_head' );
192 }
193
194 /**
195 * This action will allow ACF to render metaboxes after the title.
196 */
197 public function edit_form_after_title() {
198 global $post;
199
200 // Render post data.
201 acf_form_data(
202 array(
203 'screen' => 'field_group',
204 'post_id' => $post->ID,
205 'delete_fields' => 0,
206 'validation' => 0,
207 )
208 );
209 }
210
211 /**
212 * This function will add extra HTML to the acf form data element
213 *
214 * @since 5.3.8
215 *
216 * @param array $args Arguments array to pass through to action.
217 * @return void
218 */
219 public function form_data( $args ) {
220 do_action( 'acf/field_group/form_data', $args );
221 }
222
223 /**
224 * This function will append extra l10n strings to the acf JS object
225 *
226 * @since 5.3.8
227 *
228 * @param array $l10n The array of translated strings.
229 * @return array $l10n
230 */
231 public function admin_l10n( $l10n ) {
232 return apply_filters( 'acf/field_group/admin_l10n', $l10n );
233 }
234
235 /**
236 * Admin footer third party hook support
237 *
238 * @since 5.3.2
239 */
240 public function admin_footer() {
241 do_action( 'acf/field_group/admin_footer' );
242 }
243
244 /**
245 * Screen settings html output
246 *
247 * @since 3.6.0
248 *
249 * @param string $html Current screen settings HTML.
250 * @return string $html
251 */
252 public function screen_settings( $html ) {
253 $show_field_keys = acf_get_user_setting( 'show_field_keys' ) ? 'checked="checked"' : '';
254 $show_field_settings_tabs = acf_get_user_setting( 'show_field_settings_tabs', true ) ? 'checked="checked"' : '';
255 $hide_field_settings_tabs = apply_filters( 'acf/field_group/disable_field_settings_tabs', false );
256
257 $html .= '<div id="acf-append-show-on-screen" class="acf-hidden">';
258 $html .= '<label for="acf-field-key-hide"><input id="acf-field-key-hide" type="checkbox" value="1" name="show_field_keys" ' . $show_field_keys . ' /> ' . __( 'Field Keys', 'acf' ) . '</label>';
259
260 if ( ! $hide_field_settings_tabs ) {
261 $html .= '<label for="acf-field-settings-tabs"><input id="acf-field-settings-tabs" type="checkbox" value="1" name="show_field_settings_tabs" ' . $show_field_settings_tabs . ' />' . __( 'Field Settings Tabs', 'acf' ) . '</label>';
262 }
263
264 $html .= '</div>';
265
266 return $html;
267 }
268
269 /**
270 * Sets the "Edit Field Group" screen to use a one-column layout.
271 *
272 * @param integer $columns Number of columns for layout.
273 * @return integer
274 */
275 public function screen_layout( $columns = 0 ) {
276 return 1;
277 }
278
279 /**
280 * This function will customize the publish metabox
281 *
282 * @since 5.2.9
283 */
284 public function post_submitbox_misc_actions() {
285 global $field_group;
286 $status_label = $field_group['active'] ? _x( 'Active', 'post status', 'acf' ) : _x( 'Inactive', 'post status', 'acf' );
287
288 ?>
289 <script type="text/javascript">
290 (function($) {
291 $('#post-status-display').html( '<?php echo esc_html( $status_label ); ?>' );
292 })(jQuery);
293 </script>
294 <?php
295 }
296
297 /**
298 * Saves field group data.
299 *
300 * @since 1.0.0
301 *
302 * @param integer $post_id The post ID.
303 * @param WP_Post $post The post object.
304 * @return integer $post_id
305 */
306 public function save_post( $post_id, $post ) {
307 if ( ! $this->verify_save_post( $post_id, $post ) ) {
308 return $post_id;
309 }
310
311 // disable filters to ensure ACF loads raw data from DB.
312 acf_disable_filters();
313
314 // save fields.
315 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Validated by WordPress.
316 if ( ! empty( $_POST['acf_fields'] ) ) {
317
318 // loop.
319 foreach ( $_POST['acf_fields'] as $field ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized when saved.
320
321 if ( ! isset( $field['key'] ) ) {
322 continue;
323 }
324
325 // vars.
326 $specific = false;
327 $save = acf_extract_var( $field, 'save' );
328
329 // only saved field if has changed.
330 if ( $save == 'meta' ) {
331 $specific = array(
332 'menu_order',
333 'post_parent',
334 );
335 }
336
337 // set parent.
338 if ( ! $field['parent'] ) {
339 $field['parent'] = $post_id;
340 }
341
342 // save field.
343 acf_update_field( $field, $specific );
344 }
345 }
346
347 // delete fields.
348 if ( acf_maybe_get_POST( '_acf_delete_fields', false ) ) { // phpcs:ignore -- Sanitized below, unslash not needed
349
350 // clean.
351 $ids = explode( '|', $_POST['_acf_delete_fields'] ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- Sanitized below, unslash not required.
352 $ids = array_map( 'intval', $ids );
353
354 // loop.
355 foreach ( $ids as $id ) {
356
357 // bai early if no id.
358 if ( ! $id ) {
359 continue;
360 }
361
362 // delete.
363 acf_delete_field( $id );
364 }
365 }
366
367 $_POST['acf_field_group']['ID'] = $post_id;
368 // phpcs:disable WordPress.Security.ValidatedSanitizedInput
369 $_POST['acf_field_group']['title'] = isset( $_POST['post_title'] ) ? $_POST['post_title'] : ''; // Post title is stored unsafe like WordPress, escaped on output.
370
371 // save field group.
372 acf_update_field_group( $_POST['acf_field_group'] );
373 // phpcs:enable WordPress.Security.ValidatedSanitizedInput
374 // phpcs:enable WordPress.Security.NonceVerification.Missing
375
376 return $post_id;
377 }
378
379 /**
380 * This function will render the HTML for the metabox 'acf-field-group-fields'
381 *
382 * @since 5.0.0
383 */
384 public function mb_fields() {
385 global $field_group;
386
387 $view = array(
388 'fields' => acf_get_fields( $field_group ),
389 'parent' => 0,
390 );
391
392 acf_get_view( $this->post_type . '/fields', $view );
393 }
394
395 /**
396 * This function will render the HTML for the metabox 'acf-field-group-options'
397 *
398 * @since 5.0.0
399 */
400 public function mb_options() {
401 global $field_group;
402
403 // Field group key (leave in for compatibility).
404 if ( ! acf_is_field_group_key( $field_group['key'] ) ) {
405 $field_group['key'] = uniqid( 'group_' );
406 }
407
408 acf_get_view( $this->post_type . '/options' );
409 }
410
411 /**
412 * This function can be accessed via an AJAX action and will return the result from the render_location_value function
413 *
414 * @since 5.0.0
415 */
416 public function ajax_render_location_rule() {
417 // validate.
418 if ( ! acf_verify_ajax() ) {
419 die();
420 }
421
422 // verify user capability.
423 if ( ! acf_current_user_can_admin() ) {
424 die();
425 }
426
427 if ( empty( $_POST['rule'] ) ) {
428 die();
429 }
430
431 // validate rule.
432 $rule = acf_validate_location_rule( acf_sanitize_request_args( $_POST['rule'] ) ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- values not saved.
433
434 acf_get_view(
435 'acf-field-group/location-rule',
436 array(
437 'rule' => $rule,
438 )
439 );
440
441 die();
442 }
443
444 /**
445 * This function will return HTML containing the field's settings based on it's new type
446 *
447 * @since 5.0.0
448 */
449 public function ajax_render_field_settings() {
450 // Verify the current request.
451 if ( ! acf_verify_ajax() || ! acf_current_user_can_admin() ) {
452 wp_send_json_error();
453 }
454
455 // Make sure we have a field.
456 $field = acf_maybe_get_POST( 'field' );
457 if ( ! $field ) {
458 wp_send_json_error();
459 }
460
461 $field['prefix'] = acf_maybe_get_POST( 'prefix' );
462 $field = acf_get_valid_field( $field );
463 $tabs = acf_get_combined_field_type_settings_tabs();
464 $tab_keys = array_keys( $tabs );
465 $sections = array();
466
467 foreach ( $tab_keys as $tab ) {
468 ob_start();
469
470 if ( 'general' === $tab ) {
471 // Back-compat for fields not using tab-specific hooks.
472 do_action( "acf/render_field_settings/type={$field['type']}", $field );
473 }
474
475 do_action( "acf/field_group/render_field_settings_tab/{$tab}/type={$field['type']}", $field );
476 do_action( "acf/render_field_{$tab}_settings/type={$field['type']}", $field );
477
478 $sections[ $tab ] = ob_get_clean();
479 }
480
481 wp_send_json_success( $sections );
482 }
483
484 /**
485 * Moves fields between field groups via AJAX.
486 *
487 * @since 5.0.0
488 *
489 * @return void
490 */
491 public function ajax_move_field() {
492 // Disable filters to ensure ACF loads raw data from DB.
493 acf_disable_filters();
494
495 // phpcs:disable WordPress.Security.NonceVerification.Missing
496 $args = acf_parse_args(
497 $_POST,
498 array(
499 'nonce' => '',
500 'post_id' => 0,
501 'field_id' => 0,
502 'field_group_id' => 0,
503 )
504 );
505 // phpcs:enable WordPress.Security.NonceVerification.Missing
506
507 // Verify nonce.
508 if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) ) {
509 die();
510 }
511
512 // Verify user capability.
513 if ( ! acf_current_user_can_admin() ) {
514 die();
515 }
516
517 // Move the field if the user has confirmed.
518 if ( $args['field_id'] && $args['field_group_id'] ) {
519 $field = acf_get_field( $args['field_id'] );
520 $old_field_group = acf_get_field_group( $args['post_id'] );
521 $new_field_group = acf_get_field_group( $args['field_group_id'] );
522
523 // Update the field parent and remove conditional logic.
524 $field['parent'] = $new_field_group['ID'];
525 $field['conditional_logic'] = 0;
526
527 // Update the field in the database.
528 acf_update_field( $field );
529
530 // Fire `acf/update_field_group` action hook so JSON can sync if necessary.
531 do_action( 'acf/update_field_group', $old_field_group );
532 do_action( 'acf/update_field_group', $new_field_group );
533
534 // Output HTML.
535 $link = '<a href="' . admin_url( 'post.php?post=' . $new_field_group['ID'] . '&action=edit' ) . '" target="_blank">' . esc_html( $new_field_group['title'] ) . '</a>';
536
537 echo '' .
538 '<p><strong>' . esc_html__( 'Move Complete.', 'acf' ) . '</strong></p>' .
539 '<p>' . sprintf(
540 /* translators: Confirmation message once a field has been moved to a different field group. */
541 acf_punctify( __( 'The %1$s field can now be found in the %2$s field group', 'acf' ) ),
542 esc_html( $field['label'] ),
543 $link //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
544 ) . '</p>' .
545 '<a href="#" class="button button-primary acf-close-popup">' . esc_html__( 'Close Modal', 'acf' ) . '</a>';
546 die();
547 }
548
549 // Get all field groups.
550 $field_groups = acf_get_field_groups();
551 $choices = array();
552
553 if ( ! empty( $field_groups ) ) {
554 foreach ( $field_groups as $field_group ) {
555 // Bail early if no ID.
556 if ( ! $field_group['ID'] ) {
557 continue;
558 }
559
560 // Bail early if is current.
561 if ( $field_group['ID'] == $args['post_id'] ) {
562 continue;
563 }
564
565 $choices[ $field_group['ID'] ] = $field_group['title'];
566 }
567 }
568
569 // Render options.
570 $field = acf_get_valid_field(
571 array(
572 'type' => 'select',
573 'name' => 'acf_field_group',
574 'choices' => $choices,
575 'aria-label' => __( 'Please select the destination for this field', 'acf' ),
576 )
577 );
578
579 echo '<p>' . esc_html__( 'Please select the destination for this field', 'acf' ) . '</p>';
580 echo '<form id="acf-move-field-form">';
581 acf_render_field_wrap( $field );
582 echo '<button type="submit" class="acf-btn">' . esc_html__( 'Move Field', 'acf' ) . '</button>';
583 echo '</form>';
584
585 die();
586 }
587 }
588
589 // initialize.
590 new acf_admin_field_group();
591 endif; // Class exists check.
592
593 ?>
594