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 / pro / admin / post-types / admin-ui-options-page.php
secure-custom-fields / pro / admin / post-types Last commit date
admin-ui-options-page.php 1 year ago admin-ui-options-pages.php 1 year ago index.php 1 year ago
admin-ui-options-page.php
490 lines
1 <?php
2 /**
3 * ACF Admin Post Type Class
4 *
5 * @package ACF
6 * @subpackage Admin
7 */
8
9 if ( ! class_exists( 'ACF_Admin_UI_Options_Page' ) ) :
10
11 /**
12 * ACF Admin UI Options Page Class
13 *
14 * All the logic for editing an options page in the UI.
15 */
16 class ACF_Admin_UI_Options_Page extends ACF_Admin_Internal_Post_Type {
17
18 /**
19 * The slug for the internal post type.
20 *
21 * @since 6.1
22 * @var string
23 */
24 public $post_type = 'acf-ui-options-page';
25
26 /**
27 * The admin body class used for the post type.
28 *
29 * @since 6.1
30 * @var string
31 */
32 public $admin_body_class = 'acf-admin-single-options-page';
33
34 /**
35 * Constructs the class.
36 */
37 public function __construct() {
38 add_action( 'wp_ajax_acf/create_options_page', array( $this, 'ajax_create_options_page' ) );
39 add_action( 'acf/field_group/admin_enqueue_scripts', array( $this, 'add_js_parent_choices' ) );
40 parent::__construct();
41 }
42
43 /**
44 * Customizes the messages shown when editing a UI options page.
45 *
46 * @since 6.2
47 *
48 * @param array $messages Post type messages.
49 * @return array
50 */
51 public function post_updated_messages( $messages ) {
52 $messages['acf-ui-options-page'] = array(
53 0 => '', // Unused. Messages start at index 1.
54 1 => $this->options_page_created_message(), // Updated.
55 2 => $this->options_page_created_message(),
56 3 => __( 'Options page deleted.', 'acf' ),
57 4 => __( 'Options page updated.', 'acf' ),
58 5 => false, // Post type does not support revisions.
59 6 => $this->options_page_created_message( true ), // Created.
60 7 => __( 'Options page saved.', 'acf' ),
61 8 => __( 'Options page submitted.', 'acf' ),
62 9 => __( 'Options page scheduled for.', 'acf' ),
63 10 => __( 'Options page draft updated.', 'acf' ),
64 );
65
66 return $messages;
67 }
68
69 /**
70 * Renders the options page created message.
71 *
72 * @since 6.1
73 *
74 * @param boolean $created True if the options page was just created.
75 * @return string
76 */
77 public function options_page_created_message( $created = false ) {
78 global $post_id;
79
80 $title = get_the_title( $post_id );
81
82 /* translators: %s options page name */
83 $item_saved_text = sprintf( __( '%s options page updated', 'acf' ), $title );
84 /* translators: %s options page name */
85 $add_fields_text = sprintf( __( 'Add fields to %s', 'acf' ), $title );
86
87 if ( $created ) {
88 /* translators: %s options page name */
89 $item_saved_text = sprintf( __( '%s options page created', 'acf' ), $title );
90 }
91
92 $add_fields_link = wp_nonce_url(
93 admin_url( 'post-new.php?post_type=acf-field-group&use_options_page=' . $post_id ),
94 'add-fields-' . $post_id
95 );
96
97 ob_start();
98 ?>
99 <p class="acf-item-saved-text"><?php echo esc_html( $item_saved_text ); ?></p>
100 <div class="acf-item-saved-links">
101 <a href="<?php echo esc_url( $add_fields_link ); ?>"><?php echo esc_html( $add_fields_text ); ?></a>
102 <a class="acf-link-field-groups" href="#"><?php esc_html_e( 'Link existing field groups', 'acf' ); ?></a>
103 </div>
104 <?php
105 return ob_get_clean();
106 }
107
108 /**
109 * Allow other pages to get available option page parents.
110 *
111 * @since 6.2
112 */
113 public function add_js_parent_choices() {
114 acf_localize_data(
115 array(
116 'optionPageParentOptions' => $this->get_parent_page_choices(),
117 )
118 );
119 }
120
121 /**
122 * Enqueues any scripts necessary for internal post type.
123 *
124 * @since 6.2
125 */
126 public function admin_enqueue_scripts() {
127 wp_enqueue_style( 'acf-field-group' );
128
129 acf_localize_text(
130 array(
131 'Post' => __( 'Post', 'acf' ),
132 'Posts' => __( 'Posts', 'acf' ),
133 'Page' => __( 'Page', 'acf' ),
134 'Pages' => __( 'Pages', 'acf' ),
135 'Default' => __( 'Default', 'acf' ),
136 )
137 );
138
139 parent::admin_enqueue_scripts();
140
141 do_action( 'acf/ui_options_page/admin_enqueue_scripts' );
142 }
143
144 /**
145 * Sets up all functionality for the post type edit page to work.
146 *
147 * @since 3.1.8
148 */
149 public function admin_head() {
150 // global.
151 global $post, $acf_ui_options_page;
152
153 // set global var.
154 $acf_ui_options_page = acf_get_internal_post_type( $post->ID, $this->post_type );
155
156 // metaboxes.
157 add_meta_box( 'acf-basic-settings', __( 'Basic Settings', 'acf' ), array( $this, 'mb_basic_settings' ), 'acf-ui-options-page', 'normal', 'high' );
158 add_meta_box( 'acf-advanced-settings', __( 'Advanced Settings', 'acf' ), array( $this, 'mb_advanced_settings' ), 'acf-ui-options-page', 'normal', 'high' );
159
160 // actions.
161 add_action( 'post_submitbox_misc_actions', array( $this, 'post_submitbox_misc_actions' ), 10, 0 );
162 add_action( 'edit_form_after_title', array( $this, 'edit_form_after_title' ), 10, 0 );
163
164 // filters.
165 add_filter( 'screen_settings', array( $this, 'screen_settings' ), 10, 1 );
166 add_filter( 'get_user_option_screen_layout_acf-ui-options-page', array( $this, 'screen_layout' ), 10, 1 );
167 add_filter( 'get_user_option_metaboxhidden_acf-ui-options-page', array( $this, 'force_basic_settings' ), 10, 1 );
168 add_filter( 'get_user_option_closedpostboxes_acf-ui-options-page', array( $this, 'force_basic_settings' ), 10, 1 );
169 add_filter( 'get_user_option_closedpostboxes_acf-ui-options-page', array( $this, 'force_advanced_settings' ), 10, 1 );
170
171 // 3rd party hook.
172 do_action( 'acf/ui_options_page/admin_head' );
173 }
174
175 /**
176 * This action will allow ACF to render metaboxes after the title.
177 */
178 public function edit_form_after_title() {
179
180 // globals.
181 global $post;
182
183 // render post data.
184 acf_form_data(
185 array(
186 'screen' => 'ui_options_page',
187 'post_id' => $post->ID,
188 'delete_fields' => 0,
189 'validation' => 1,
190 )
191 );
192 }
193
194 /**
195 * This function will add extra HTML to the acf form data element
196 *
197 * @since 5.3.8
198 *
199 * @param array $args Arguments array to pass through to action.
200 * @return void
201 */
202 public function form_data( $args ) {
203 do_action( 'acf/ui_options_page/form_data', $args );
204 }
205
206 /**
207 * This function will append extra l10n strings to the acf JS object
208 *
209 * @since 5.3.8
210 *
211 * @param array $l10n The array of translated strings.
212 * @return array $l10n
213 */
214 public function admin_l10n( $l10n ) {
215 return apply_filters( 'acf/ui_options_page/admin_l10n', $l10n );
216 }
217
218 /**
219 * Admin footer third party hook support
220 *
221 * @since 5.3.2
222 */
223 public function admin_footer() {
224 do_action( 'acf/ui_options_page/admin_footer' );
225 }
226
227 /**
228 * Screen settings html output
229 *
230 * @since 3.6.0
231 *
232 * @param string $html Current screen settings HTML.
233 * @return string $html
234 */
235 public function screen_settings( $html ) {
236 return $html;
237 }
238
239 /**
240 * Sets the "Edit Post Type" screen to use a one-column layout.
241 *
242 * @param integer $columns Number of columns for layout.
243 * @return integer
244 */
245 public function screen_layout( $columns = 0 ) {
246 return 1;
247 }
248
249 /**
250 * Force basic settings to always be visible
251 *
252 * @param array $hidden_metaboxes The metaboxes hidden on this page.
253 * @return array
254 */
255 public function force_basic_settings( $hidden_metaboxes ) {
256 if ( ! is_array( $hidden_metaboxes ) ) {
257 return $hidden_metaboxes;
258 }
259 return array_diff( $hidden_metaboxes, array( 'acf-basic-settings' ) );
260 }
261
262 /**
263 * Force advanced settings to be visible
264 *
265 * @param array $hidden_metaboxes The metaboxes hidden on this page.
266 * @return array
267 */
268 public function force_advanced_settings( $hidden_metaboxes ) {
269 if ( ! is_array( $hidden_metaboxes ) ) {
270 return $hidden_metaboxes;
271 }
272 return array_diff( $hidden_metaboxes, array( 'acf-advanced-settings' ) );
273 }
274
275 /**
276 * This function will customize the publish metabox
277 *
278 * @since 5.2.9
279 */
280 public function post_submitbox_misc_actions() {
281 global $acf_ui_options_page;
282
283 $status_label = $acf_ui_options_page['active'] ? _x( 'Active', 'post status', 'acf' ) : _x( 'Inactive', 'post status', 'acf' );
284 ?>
285 <script type="text/javascript">
286 (function($) {
287 $('#post-status-display').html( '<?php echo esc_html( $status_label ); ?>' );
288 })(jQuery);
289 </script>
290 <?php
291 }
292
293 /**
294 * Saves post type data.
295 *
296 * @since 1.0.0
297 *
298 * @param integer $post_id The post ID.
299 * @param WP_Post $post The post object.
300 * @return integer $post_id
301 */
302 public function save_post( $post_id, $post ) {
303 if ( ! $this->verify_save_post( $post_id, $post ) ) {
304 return $post_id;
305 }
306
307 // Disable filters to ensure ACF loads raw data from DB.
308 acf_disable_filters();
309
310 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Validated in $this->verify_save_post() above.
311 // phpcs:disable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized when saved.
312 $_POST['acf_ui_options_page']['ID'] = $post_id;
313 $_POST['acf_ui_options_page']['title'] = isset( $_POST['acf_ui_options_page']['page_title'] ) ? $_POST['acf_ui_options_page']['page_title'] : '';
314
315 // Save the post type.
316 acf_update_internal_post_type( $_POST['acf_ui_options_page'], $this->post_type ); // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Validated in verify_save_post
317 // phpcs:enable WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
318 // phpcs:enable WordPress.Security.NonceVerification.Missing
319
320 return $post_id;
321 }
322
323 /**
324 * Renders HTML for the basic settings metabox.
325 *
326 * @since 6.2
327 */
328 public function mb_basic_settings() {
329 global $acf_ui_options_page, $acf_parent_page_options;
330
331 if ( ! acf_is_internal_post_type_key( $acf_ui_options_page['key'], 'acf-ui-options-page' ) ) {
332 $acf_ui_options_page['key'] = uniqid( 'ui_options_page_' );
333 }
334
335 $acf_parent_page_options = $this->get_parent_page_choices( (int) $acf_ui_options_page['ID'] );
336
337 acf_get_view( __DIR__ . '/../views/acf-ui-options-page/basic-settings.php' );
338 }
339
340
341 /**
342 * Renders the HTML for the advanced settings metabox.
343 *
344 * @since 6.2
345 */
346 public function mb_advanced_settings() {
347 acf_get_view( __DIR__ . '/../views/acf-ui-options-page/advanced-settings.php' );
348 }
349
350 /**
351 * Iterates through the registered options pages and finds eligible parent pages.
352 *
353 * @since 6.2
354 *
355 * @param integer $post_id The post ID of a current ACF UI options page used to prevent selection of itself as a child.
356 * @return array
357 */
358 public function get_parent_page_choices( int $post_id = 0 ) {
359 global $menu;
360 $acf_all_options_pages = acf_get_options_pages();
361 $acf_parent_page_choices = array( 'None' => array( 'none' => __( 'No Parent', 'acf' ) ) );
362 $self_slug = false;
363
364 if ( is_array( $acf_all_options_pages ) ) {
365 foreach ( $acf_all_options_pages as $options_page ) {
366 // Can't assign to child pages.
367 if ( ! empty( $options_page['parent_slug'] ) ) {
368 continue;
369 }
370
371 // Can't be a child of itself.
372 if ( isset( $options_page['ID'] ) && $post_id === $options_page['ID'] ) {
373 $self_slug = $options_page['menu_slug'];
374 continue;
375 }
376
377 $acf_parent_menu_slug = ! empty( $options_page['menu_slug'] ) ? $options_page['menu_slug'] : '';
378
379 // ACF overrides the `menu_slug` of parent pages with one child so they redirect to the child.
380 if ( ! empty( $options_page['_menu_slug'] ) ) {
381 $acf_parent_menu_slug = $options_page['_menu_slug'];
382 }
383
384 $acf_parent_page_choices['acfOptionsPages'][ $acf_parent_menu_slug ] = ! empty( $options_page['page_title'] ) ? $options_page['page_title'] : $options_page['menu_slug'];
385 }
386 }
387
388 foreach ( $menu as $item ) {
389 if ( ! empty( $item[0] ) ) {
390 $page_name = $item[0];
391 $markup = '/<[^>]+>.*<\/[^>]+>/';
392 $sanitized_name = preg_replace( $markup, '', $page_name );
393
394 // Prevent options pages being parents of themselves.
395 if ( ! empty( $item[2] ) && $item[2] === $self_slug ) {
396 continue;
397 }
398
399 // If the current item is not an ACF-created options page, add it to the "Others" list.
400 if ( empty( $acf_parent_page_choices['acfOptionsPages'][ $item[2] ] ) ) {
401 $acf_parent_page_choices['Others'][ $item[2] ] = acf_esc_html( $sanitized_name );
402 }
403 }
404 }
405 return $acf_parent_page_choices;
406 }
407
408 /**
409 * Creates a simple options page over AJAX.
410 *
411 * @since 6.2
412 * @return void
413 */
414 public function ajax_create_options_page() {
415 // Disable filters to ensure ACF loads raw data from DB.
416 acf_disable_filters();
417
418 // phpcs:disable WordPress.Security.NonceVerification.Missing
419 $args = acf_parse_args(
420 $_POST,
421 array(
422 'nonce' => '',
423 'post_id' => 0,
424 'acf_ui_options_page' => array(),
425 'field_group_title' => '',
426 'acf_parent_page_choices' => array(),
427 )
428 );
429 // phpcs:enable WordPress.Security.NonceVerification.Missing
430
431 // Verify nonce and user capability.
432 if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) || ! acf_current_user_can_admin() || ! $args['post_id'] ) {
433 die();
434 }
435
436 // Process form data.
437 if ( ! empty( $args['acf_ui_options_page'] ) ) {
438 // Prepare for save.
439 $options_page = acf_validate_internal_post_type( $args['acf_ui_options_page'], 'acf-ui-options-page' );
440 $options_page['key'] = uniqid( 'ui_options_page_' );
441 $options_page['title'] = ! empty( $args['acf_ui_options_page']['page_title'] ) ? $args['acf_ui_options_page']['page_title'] : '';
442 $existing_options_pages = acf_get_options_pages();
443
444 // Check for duplicates.
445 if ( ! empty( $existing_options_pages ) ) {
446 foreach ( $existing_options_pages as $existing_options_page ) {
447 if ( $existing_options_page['menu_slug'] === $options_page['menu_slug'] ) {
448 wp_send_json_error(
449 array(
450 'error' => __( 'The provided Menu Slug already exists.', 'acf' ),
451 )
452 );
453 }
454 }
455 }
456
457 // Save the options page.
458 acf_update_internal_post_type( $options_page, 'acf-ui-options-page' );
459
460 wp_send_json_success(
461 array(
462 'page_title' => esc_html( $options_page['page_title'] ),
463 'menu_slug' => esc_attr( $options_page['menu_slug'] ),
464 )
465 );
466 }
467
468 // Render the form.
469 ob_start();
470 acf_get_view(
471 __DIR__ . '/../views/acf-ui-options-page/create-options-page-modal.php',
472 array(
473 'field_group_title' => $args['field_group_title'],
474 'acf_parent_page_choices' => $args['acf_parent_page_choices'],
475 )
476 );
477 $content = ob_get_clean();
478
479 wp_send_json_success(
480 array(
481 'content' => $content,
482 'title' => esc_html__( 'Add New Options Page', 'acf' ),
483 )
484 );
485 }
486 }
487
488 new ACF_Admin_UI_Options_Page();
489 endif; // Class exists check.
490