beta-features
7 months ago
post-types
2 months ago
tools
7 months ago
views
1 week ago
admin-commands.php
2 months ago
admin-internal-post-type-list.php
10 months ago
admin-internal-post-type.php
1 year ago
admin-notices.php
1 year ago
admin-tools.php
10 months ago
admin-upgrade.php
1 year ago
admin.php
10 months ago
beta-features.php
7 months ago
class-acf-admin-options-page.php
2 months ago
index.php
1 year ago
class-acf-admin-options-page.php
321 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Admin Options Page Class |
| 4 | * |
| 5 | * Handles the admin interface for options pages. |
| 6 | * |
| 7 | * @package wordpress/secure-custom-fields |
| 8 | */ |
| 9 | |
| 10 | // phpcs:disable PEAR.NamingConventions.ValidClassName |
| 11 | if ( ! defined( 'ABSPATH' ) ) { |
| 12 | exit; // Exit if accessed directly |
| 13 | } |
| 14 | |
| 15 | if ( ! class_exists( 'acf_admin_options_page' ) ) : |
| 16 | |
| 17 | /** |
| 18 | * Class for managing options pages in the WordPress admin. |
| 19 | */ |
| 20 | class acf_admin_options_page { |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * Current options page data. |
| 25 | * |
| 26 | * @var array Contains the current options page |
| 27 | */ |
| 28 | public $page; |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * Initialize filters, action, variables and includes |
| 33 | * |
| 34 | * @since ACF 5.0.0 |
| 35 | */ |
| 36 | public function __construct() { |
| 37 | // add menu items |
| 38 | add_action( 'admin_menu', array( $this, 'admin_menu' ), 99, 0 ); |
| 39 | } |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * Adds menu items for registered options pages. |
| 44 | * |
| 45 | * @since ACF 5.0.0 |
| 46 | */ |
| 47 | public function admin_menu() { |
| 48 | |
| 49 | // vars |
| 50 | $pages = acf_get_options_pages(); |
| 51 | |
| 52 | // bail early if no pages |
| 53 | if ( empty( $pages ) ) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | // loop |
| 58 | foreach ( $pages as $page ) { |
| 59 | |
| 60 | // vars |
| 61 | $slug = ''; |
| 62 | // parent |
| 63 | if ( empty( $page['parent_slug'] ) ) { |
| 64 | $slug = add_menu_page( $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array( $this, 'html' ), $page['icon_url'], $page['position'] ); |
| 65 | // child |
| 66 | } else { |
| 67 | $slug = add_submenu_page( $page['parent_slug'], $page['page_title'], $page['menu_title'], $page['capability'], $page['menu_slug'], array( $this, 'html' ), $page['position'] ); |
| 68 | } |
| 69 | |
| 70 | // actions |
| 71 | add_action( "load-{$slug}", array( $this, 'admin_load' ) ); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Handles the load action for the options page. |
| 78 | * |
| 79 | * Enqueues scripts, validates saves, and sets up the options page. |
| 80 | * |
| 81 | * @since ACF 3.6.0 |
| 82 | */ |
| 83 | public function admin_load() { |
| 84 | |
| 85 | // globals |
| 86 | global $plugin_page; |
| 87 | |
| 88 | // vars |
| 89 | $this->page = acf_get_options_page( $plugin_page ); |
| 90 | |
| 91 | // get post_id (allow lang modification) |
| 92 | $this->page['post_id'] = acf_get_valid_post_id( $this->page['post_id'] ); |
| 93 | |
| 94 | // verify and remove nonce |
| 95 | if ( acf_verify_nonce( 'options' ) ) { |
| 96 | |
| 97 | // save data |
| 98 | if ( acf_validate_save_post( true ) ) { |
| 99 | |
| 100 | // set autoload |
| 101 | acf_update_setting( 'autoload', $this->page['autoload'] ); |
| 102 | |
| 103 | // save |
| 104 | acf_save_post( $this->page['post_id'] ); |
| 105 | |
| 106 | /** |
| 107 | * Fires after publishing a save on an options page. |
| 108 | * |
| 109 | * @since ACF 6.1.7 |
| 110 | * |
| 111 | * @param string|int $post_id The current id. |
| 112 | * @param string $menu_slug The current options page menu slug. |
| 113 | */ |
| 114 | do_action( 'acf/options_page/save', $this->page['post_id'], $this->page['menu_slug'] ); |
| 115 | |
| 116 | // redirect |
| 117 | wp_safe_redirect( add_query_arg( array( 'message' => '1' ) ) ); |
| 118 | exit; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // load acf scripts |
| 123 | acf_enqueue_scripts(); |
| 124 | |
| 125 | // Localize options page slug for repeater pagination capability checks. |
| 126 | acf_localize_data( array( 'options_page_slug' => $this->page['menu_slug'] ) ); |
| 127 | |
| 128 | // actions |
| 129 | add_action( 'acf/input/admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); |
| 130 | add_action( 'acf/input/admin_head', array( $this, 'admin_head' ) ); |
| 131 | |
| 132 | // add columns support |
| 133 | add_screen_option( |
| 134 | 'layout_columns', |
| 135 | array( |
| 136 | 'max' => 2, |
| 137 | 'default' => 2, |
| 138 | ) |
| 139 | ); |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * This function will enqueue the 'post.js' script which adds support for 'Screen Options' column toggle |
| 145 | * |
| 146 | * @since ACF 5.3.2 |
| 147 | */ |
| 148 | public function admin_enqueue_scripts() { |
| 149 | |
| 150 | wp_enqueue_script( 'post' ); |
| 151 | } |
| 152 | |
| 153 | |
| 154 | /** |
| 155 | * This action will find and add field groups to the current edit page |
| 156 | * |
| 157 | * @type action (admin_head) |
| 158 | * @since ACF 3.1.8 |
| 159 | */ |
| 160 | public function admin_head() { |
| 161 | |
| 162 | // get field groups |
| 163 | $field_groups = acf_get_field_groups( |
| 164 | array( |
| 165 | 'options_page' => $this->page['menu_slug'], |
| 166 | ) |
| 167 | ); |
| 168 | |
| 169 | // notices |
| 170 | if ( ! empty( $_GET['message'] ) && '1' === $_GET['message'] ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Used to display a notice. |
| 171 | acf_add_admin_notice( $this->page['updated_message'], 'success' ); |
| 172 | } |
| 173 | |
| 174 | // add submit div |
| 175 | add_meta_box( 'submitdiv', __( 'Publish', 'secure-custom-fields' ), array( $this, 'postbox_submitdiv' ), 'acf_options_page', 'side', 'high' ); |
| 176 | |
| 177 | if ( empty( $field_groups ) ) { |
| 178 | /* translators: %s: URL to create a new field group */ |
| 179 | acf_add_admin_notice( sprintf( __( 'No Custom Field Groups found for this options page. <a href="%s">Create a Custom Field Group</a>', 'secure-custom-fields' ), admin_url( 'post-new.php?post_type=acf-field-group' ) ), 'warning' ); |
| 180 | } else { |
| 181 | foreach ( $field_groups as $i => $field_group ) { |
| 182 | |
| 183 | // vars |
| 184 | $id = "acf-{$field_group['key']}"; |
| 185 | $context = $field_group['position']; |
| 186 | $priority = 'high'; |
| 187 | $args = array( 'field_group' => $field_group ); |
| 188 | |
| 189 | // tweaks to vars |
| 190 | if ( 'acf_after_title' === $context ) { |
| 191 | $context = 'normal'; |
| 192 | } elseif ( 'side' === $context ) { |
| 193 | $priority = 'core'; |
| 194 | } |
| 195 | |
| 196 | // filter for 3rd party customization |
| 197 | $priority = apply_filters( 'acf/input/meta_box_priority', $priority, $field_group ); |
| 198 | |
| 199 | // add meta box |
| 200 | add_meta_box( |
| 201 | $id, |
| 202 | acf_esc_html( acf_get_field_group_title( $field_group ) ), |
| 203 | array( $this, 'postbox_acf' ), |
| 204 | 'acf_options_page', |
| 205 | $context, |
| 206 | $priority, |
| 207 | $args |
| 208 | ); |
| 209 | } |
| 210 | // foreach |
| 211 | } |
| 212 | // if |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * This function will render the submitdiv metabox |
| 218 | * |
| 219 | * @since ACF 5.3.2 |
| 220 | */ |
| 221 | public function postbox_submitdiv() { |
| 222 | |
| 223 | /** |
| 224 | * Fires before the major-publishing-actions div. |
| 225 | * |
| 226 | * @date 24/9/18 |
| 227 | * @since ACF 5.7.7 |
| 228 | * |
| 229 | * @param array $page The current options page. |
| 230 | */ |
| 231 | do_action( 'acf/options_page/submitbox_before_major_actions', $this->page ); |
| 232 | ?> |
| 233 | <div id="major-publishing-actions"> |
| 234 | |
| 235 | <div id="publishing-action"> |
| 236 | <span class="spinner"></span> |
| 237 | <input type="submit" accesskey="p" value="<?php echo esc_attr( $this->page['update_button'] ); ?>" class="button button-primary button-large" id="publish" name="publish"> |
| 238 | </div> |
| 239 | |
| 240 | <?php |
| 241 | /** |
| 242 | * Fires before the major-publishing-actions div. |
| 243 | * |
| 244 | * @date 24/9/18 |
| 245 | * @since ACF 5.7.7 |
| 246 | * |
| 247 | * @param array $page The current options page. |
| 248 | */ |
| 249 | do_action( 'acf/options_page/submitbox_major_actions', $this->page ); |
| 250 | ?> |
| 251 | <div class="clear"></div> |
| 252 | |
| 253 | </div> |
| 254 | <?php |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /** |
| 259 | * Renders a postbox on an ACF options page. |
| 260 | * |
| 261 | * @since ACF 5.0.0 |
| 262 | * |
| 263 | * @param object $post The post object. |
| 264 | * @param array $args The metabox arguments. |
| 265 | */ |
| 266 | public function postbox_acf( $post, $args ) { |
| 267 | $id = $args['id']; |
| 268 | $field_group = $args['args']['field_group']; |
| 269 | |
| 270 | // vars |
| 271 | $o = array( |
| 272 | 'id' => $id, |
| 273 | 'key' => $field_group['key'], |
| 274 | 'style' => $field_group['style'], |
| 275 | 'label' => $field_group['label_placement'], |
| 276 | 'editLink' => '', |
| 277 | 'editTitle' => __( 'Edit field group', 'secure-custom-fields' ), |
| 278 | 'visibility' => true, |
| 279 | ); |
| 280 | |
| 281 | // edit_url |
| 282 | if ( $field_group['ID'] && acf_current_user_can_admin() ) { |
| 283 | $o['editLink'] = admin_url( 'post.php?post=' . $field_group['ID'] . '&action=edit' ); |
| 284 | } |
| 285 | |
| 286 | // load fields |
| 287 | $fields = acf_get_fields( $field_group ); |
| 288 | |
| 289 | // render |
| 290 | acf_render_fields( $fields, $this->page['post_id'], 'div', $field_group['instruction_placement'] ); |
| 291 | |
| 292 | ?> |
| 293 | <script type="text/javascript"> |
| 294 | if (typeof acf !== 'undefined') { |
| 295 | |
| 296 | acf.newPostbox(<?php echo wp_json_encode( $o ); ?>); |
| 297 | |
| 298 | } |
| 299 | </script> |
| 300 | <?php |
| 301 | } |
| 302 | |
| 303 | |
| 304 | /** |
| 305 | * Renders the options page HTML content. |
| 306 | * |
| 307 | * @since ACF 2.0.4 |
| 308 | */ |
| 309 | public function html() { |
| 310 | |
| 311 | // load view |
| 312 | acf_get_view( __DIR__ . '/views/html-options-page.php', $this->page ); |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | |
| 317 | // initialize |
| 318 | new acf_admin_options_page(); |
| 319 | endif; |
| 320 | |
| 321 | ?> |