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