admin
1 year ago
fields
1 year ago
locations
1 year ago
post-types
1 year ago
acf-pro.php
1 year ago
acf-ui-options-page-functions.php
1 year ago
blocks.php
1 year ago
index.php
1 year ago
options-page.php
1 year ago
options-page.php
545 lines
| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; // Exit if accessed directly |
| 5 | } |
| 6 | |
| 7 | if ( ! class_exists( 'acf_options_page' ) ) : |
| 8 | |
| 9 | class acf_options_page { |
| 10 | |
| 11 | /** @var array Contains an array of options page settings */ |
| 12 | var $pages = array(); |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Initialize filters, action, variables and includes |
| 17 | * |
| 18 | * @type function |
| 19 | * @date 23/06/12 |
| 20 | * @since 5.0.0 |
| 21 | * |
| 22 | * @param n/a |
| 23 | * @return n/a |
| 24 | */ |
| 25 | function __construct() { |
| 26 | |
| 27 | /* do nothing */ |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Validates an Options Page settings array. |
| 32 | * |
| 33 | * @date 28/2/17 |
| 34 | * @since 5.5.8 |
| 35 | * |
| 36 | * @param array|string $page The Options Page settings array or name. |
| 37 | * @return array |
| 38 | */ |
| 39 | function validate_page( $page ) { |
| 40 | |
| 41 | // Allow empty arg to generate the default Options Page. |
| 42 | if ( empty( $page ) ) { |
| 43 | $page_title = __( 'Options', 'acf' ); |
| 44 | $page = array( |
| 45 | 'page_title' => $page_title, |
| 46 | 'menu_title' => $page_title, |
| 47 | 'menu_slug' => 'acf-options', |
| 48 | ); |
| 49 | |
| 50 | // Allow string to define Options Page name. |
| 51 | } elseif ( is_string( $page ) ) { |
| 52 | $page_title = $page; |
| 53 | $page = array( |
| 54 | 'page_title' => $page_title, |
| 55 | 'menu_title' => $page_title, |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | // Apply defaults. |
| 60 | $page = wp_parse_args( |
| 61 | $page, |
| 62 | array( |
| 63 | 'page_title' => '', |
| 64 | 'menu_title' => '', |
| 65 | 'menu_slug' => '', |
| 66 | 'capability' => 'edit_posts', |
| 67 | 'parent_slug' => '', |
| 68 | 'position' => null, |
| 69 | 'icon_url' => false, |
| 70 | 'redirect' => true, |
| 71 | 'post_id' => 'options', |
| 72 | 'autoload' => false, |
| 73 | 'update_button' => __( 'Update', 'acf' ), |
| 74 | 'updated_message' => __( 'Options Updated', 'acf' ), |
| 75 | ) |
| 76 | ); |
| 77 | |
| 78 | // Allow compatibility for changed settings. |
| 79 | $migrate = array( |
| 80 | 'title' => 'page_title', |
| 81 | 'menu' => 'menu_title', |
| 82 | 'slug' => 'menu_slug', |
| 83 | 'parent' => 'parent_slug', |
| 84 | ); |
| 85 | foreach ( $migrate as $old => $new ) { |
| 86 | if ( ! empty( $page[ $old ] ) ) { |
| 87 | $page[ $new ] = $page[ $old ]; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // If no menu_title is set, use the page_title value. |
| 92 | if ( empty( $page['menu_title'] ) ) { |
| 93 | $page['menu_title'] = $page['page_title']; |
| 94 | } |
| 95 | |
| 96 | // If no menu_slug is set, generate one using the menu_title value. |
| 97 | if ( empty( $page['menu_slug'] ) ) { |
| 98 | $page['menu_slug'] = 'acf-options-' . sanitize_title( $page['menu_title'] ); |
| 99 | } |
| 100 | |
| 101 | // Standardize on position being either null or int. |
| 102 | $page['position'] = is_numeric( $page['position'] ) ? (int) $page['position'] : null; |
| 103 | |
| 104 | /** |
| 105 | * Filters the $page array after it has been validated. |
| 106 | * |
| 107 | * @since 5.5.8 |
| 108 | * @param array $page The Options Page settings array. |
| 109 | */ |
| 110 | return apply_filters( 'acf/validate_options_page', $page ); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * This function will store an options page settings |
| 116 | * |
| 117 | * @type function |
| 118 | * @date 9/6/17 |
| 119 | * @since 5.6.0 |
| 120 | * |
| 121 | * @param $page (array) |
| 122 | * @return n/a |
| 123 | */ |
| 124 | function add_page( $page ) { |
| 125 | |
| 126 | // validate |
| 127 | $page = $this->validate_page( $page ); |
| 128 | $slug = $page['menu_slug']; |
| 129 | |
| 130 | // bail early if already exists |
| 131 | if ( isset( $this->pages[ $slug ] ) ) { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | // append |
| 136 | $this->pages[ $slug ] = $page; |
| 137 | |
| 138 | // return |
| 139 | return $page; |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * description |
| 145 | * |
| 146 | * @type function |
| 147 | * @date 9/6/17 |
| 148 | * @since 5.6.0 |
| 149 | * |
| 150 | * @param $post_id (int) |
| 151 | * @return $post_id (int) |
| 152 | */ |
| 153 | function add_sub_page( $page ) { |
| 154 | |
| 155 | // validate |
| 156 | $page = $this->validate_page( $page ); |
| 157 | |
| 158 | // default parent |
| 159 | if ( ! $page['parent_slug'] ) { |
| 160 | $page['parent_slug'] = 'acf-options'; |
| 161 | } |
| 162 | |
| 163 | // create default parent if not yet exists |
| 164 | if ( $page['parent_slug'] == 'acf-options' && ! $this->get_page( 'acf-options' ) ) { |
| 165 | $this->add_page( '' ); |
| 166 | } |
| 167 | |
| 168 | // return |
| 169 | return $this->add_page( $page ); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * This function will update an options page settings |
| 175 | * |
| 176 | * @type function |
| 177 | * @date 9/6/17 |
| 178 | * @since 5.6.0 |
| 179 | * |
| 180 | * @param $slug (string) |
| 181 | * @param $data (array) |
| 182 | * @return (array) |
| 183 | */ |
| 184 | function update_page( $slug = '', $data = array() ) { |
| 185 | |
| 186 | // vars |
| 187 | $page = $this->get_page( $slug ); |
| 188 | |
| 189 | // bail early if no page |
| 190 | if ( ! $page ) { |
| 191 | return false; |
| 192 | } |
| 193 | |
| 194 | // loop |
| 195 | $page = array_merge( $page, $data ); |
| 196 | |
| 197 | // set |
| 198 | $this->pages[ $slug ] = $page; |
| 199 | |
| 200 | // return |
| 201 | return $page; |
| 202 | } |
| 203 | |
| 204 | |
| 205 | /** |
| 206 | * This function will return an options page settings |
| 207 | * |
| 208 | * @type function |
| 209 | * @date 6/07/2016 |
| 210 | * @since 5.4.0 |
| 211 | * |
| 212 | * @param $slug (string) |
| 213 | * @return (mixed) |
| 214 | */ |
| 215 | function get_page( $slug ) { |
| 216 | |
| 217 | return isset( $this->pages[ $slug ] ) ? $this->pages[ $slug ] : null; |
| 218 | } |
| 219 | |
| 220 | |
| 221 | /** |
| 222 | * This function will return all options page settings |
| 223 | * |
| 224 | * @type function |
| 225 | * @date 6/07/2016 |
| 226 | * @since 5.4.0 |
| 227 | * |
| 228 | * @param $slug (string) |
| 229 | * @return (mixed) |
| 230 | */ |
| 231 | function get_pages() { |
| 232 | |
| 233 | return $this->pages; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | |
| 238 | /* |
| 239 | * acf_options_page |
| 240 | * |
| 241 | * This function will return the options page instance |
| 242 | * |
| 243 | * @type function |
| 244 | * @date 9/6/17 |
| 245 | * @since 5.6.0 |
| 246 | * |
| 247 | * @param n/a |
| 248 | * @return (object) |
| 249 | */ |
| 250 | |
| 251 | function acf_options_page() { |
| 252 | |
| 253 | global $acf_options_page; |
| 254 | |
| 255 | if ( ! isset( $acf_options_page ) ) { |
| 256 | $acf_options_page = new acf_options_page(); |
| 257 | } |
| 258 | |
| 259 | return $acf_options_page; |
| 260 | } |
| 261 | |
| 262 | |
| 263 | // remove Options Page add-on conflict |
| 264 | unset( $GLOBALS['acf_options_page'] ); |
| 265 | |
| 266 | |
| 267 | // initialize |
| 268 | acf_options_page(); |
| 269 | endif; // class_exists check |
| 270 | |
| 271 | |
| 272 | /** |
| 273 | * alias of acf_options_page()->add_page() |
| 274 | * |
| 275 | * @type function |
| 276 | * @date 24/02/2014 |
| 277 | * @since 5.0.0 |
| 278 | * |
| 279 | * @param $page (mixed) |
| 280 | * @return (array) |
| 281 | */ |
| 282 | if ( ! function_exists( 'acf_add_options_page' ) ) : |
| 283 | |
| 284 | function acf_add_options_page( $page = '' ) { |
| 285 | return acf_options_page()->add_page( $page ); |
| 286 | } |
| 287 | |
| 288 | endif; |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * alias of acf_options_page()->add_sub_page() |
| 293 | * |
| 294 | * @type function |
| 295 | * @date 24/02/2014 |
| 296 | * @since 5.0.0 |
| 297 | * |
| 298 | * @param $page (mixed) |
| 299 | * @return (array) |
| 300 | */ |
| 301 | if ( ! function_exists( 'acf_add_options_sub_page' ) ) : |
| 302 | |
| 303 | function acf_add_options_sub_page( $page = '' ) { |
| 304 | |
| 305 | return acf_options_page()->add_sub_page( $page ); |
| 306 | } |
| 307 | |
| 308 | endif; |
| 309 | |
| 310 | |
| 311 | /** |
| 312 | * alias of acf_options_page()->update_page() |
| 313 | * |
| 314 | * @type function |
| 315 | * @date 24/02/2014 |
| 316 | * @since 5.0.0 |
| 317 | * |
| 318 | * @param $slug (string) |
| 319 | * @param $page (mixed) |
| 320 | * @return (array) |
| 321 | */ |
| 322 | if ( ! function_exists( 'acf_update_options_page' ) ) : |
| 323 | |
| 324 | function acf_update_options_page( $slug = '', $data = array() ) { |
| 325 | |
| 326 | return acf_options_page()->update_page( $slug, $data ); |
| 327 | } |
| 328 | |
| 329 | endif; |
| 330 | |
| 331 | |
| 332 | /** |
| 333 | * This function will return an options page settings |
| 334 | * |
| 335 | * @type function |
| 336 | * @date 24/02/2014 |
| 337 | * @since 5.0.0 |
| 338 | * |
| 339 | * @param $slug (string) |
| 340 | * @return (array) |
| 341 | */ |
| 342 | if ( ! function_exists( 'acf_get_options_page' ) ) : |
| 343 | |
| 344 | function acf_get_options_page( $slug ) { |
| 345 | |
| 346 | // vars |
| 347 | $page = acf_options_page()->get_page( $slug ); |
| 348 | |
| 349 | // bail early if no page |
| 350 | if ( ! $page ) { |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | // filter |
| 355 | $page = apply_filters( 'acf/get_options_page', $page, $slug ); |
| 356 | |
| 357 | // return |
| 358 | return $page; |
| 359 | } |
| 360 | |
| 361 | endif; |
| 362 | |
| 363 | |
| 364 | /** |
| 365 | * This function will return all options page settings |
| 366 | * |
| 367 | * @type function |
| 368 | * @date 24/02/2014 |
| 369 | * @since 5.0.0 |
| 370 | * |
| 371 | * @param n/a |
| 372 | * @return (array) |
| 373 | */ |
| 374 | if ( ! function_exists( 'acf_get_options_pages' ) ) : |
| 375 | |
| 376 | function acf_get_options_pages() { |
| 377 | |
| 378 | // global |
| 379 | global $_wp_last_utility_menu; |
| 380 | |
| 381 | // vars |
| 382 | $pages = acf_options_page()->get_pages(); |
| 383 | |
| 384 | // bail early if no pages |
| 385 | if ( empty( $pages ) ) { |
| 386 | return false; |
| 387 | } |
| 388 | |
| 389 | // apply filter to each page |
| 390 | foreach ( $pages as $slug => &$page ) { |
| 391 | $page = acf_get_options_page( $slug ); |
| 392 | } |
| 393 | |
| 394 | // calculate parent => child redirectes |
| 395 | foreach ( $pages as $slug => &$page ) { |
| 396 | |
| 397 | // bail early if is child |
| 398 | if ( $page['parent_slug'] ) { |
| 399 | continue; |
| 400 | } |
| 401 | |
| 402 | // add missing position |
| 403 | if ( ! $page['position'] ) { |
| 404 | ++$_wp_last_utility_menu; |
| 405 | $page['position'] = $_wp_last_utility_menu; |
| 406 | } |
| 407 | |
| 408 | // bail early if no redirect |
| 409 | if ( ! $page['redirect'] ) { |
| 410 | continue; |
| 411 | } |
| 412 | |
| 413 | // vars |
| 414 | $parent = $page['menu_slug']; |
| 415 | $child = ''; |
| 416 | |
| 417 | // update children |
| 418 | foreach ( $pages as &$sub_page ) { |
| 419 | |
| 420 | // bail early if not child of this parent |
| 421 | if ( $sub_page['parent_slug'] !== $parent ) { |
| 422 | continue; |
| 423 | } |
| 424 | |
| 425 | // set child (only once) |
| 426 | if ( ! $child ) { |
| 427 | $child = $sub_page['menu_slug']; |
| 428 | } |
| 429 | |
| 430 | // update parent_slug to the first child |
| 431 | $sub_page['parent_slug'] = $child; |
| 432 | } |
| 433 | |
| 434 | // finally update parent menu_slug |
| 435 | if ( $child ) { |
| 436 | $page['_menu_slug'] = $page['menu_slug']; |
| 437 | $page['menu_slug'] = $child; |
| 438 | } |
| 439 | } |
| 440 | |
| 441 | // filter |
| 442 | $pages = apply_filters( 'acf/get_options_pages', $pages ); |
| 443 | |
| 444 | // return |
| 445 | return $pages; |
| 446 | } |
| 447 | |
| 448 | endif; |
| 449 | |
| 450 | |
| 451 | /** |
| 452 | * This function is used to customize the options page admin menu title |
| 453 | * |
| 454 | * @type function |
| 455 | * @date 13/07/13 |
| 456 | * @since 4.0.0 |
| 457 | * |
| 458 | * @param $title (string) |
| 459 | * @return n/a |
| 460 | */ |
| 461 | if ( ! function_exists( 'acf_set_options_page_title' ) ) : |
| 462 | |
| 463 | function acf_set_options_page_title( $title = 'Options' ) { |
| 464 | |
| 465 | acf_update_options_page( |
| 466 | 'acf-options', |
| 467 | array( |
| 468 | 'page_title' => $title, |
| 469 | 'menu_title' => $title, |
| 470 | ) |
| 471 | ); |
| 472 | } |
| 473 | |
| 474 | endif; |
| 475 | |
| 476 | |
| 477 | /** |
| 478 | * This function is used to customize the options page admin menu name |
| 479 | * |
| 480 | * @type function |
| 481 | * @date 13/07/13 |
| 482 | * @since 4.0.0 |
| 483 | * |
| 484 | * @param $title (string) |
| 485 | * @return n/a |
| 486 | */ |
| 487 | if ( ! function_exists( 'acf_set_options_page_menu' ) ) : |
| 488 | |
| 489 | function acf_set_options_page_menu( $title = 'Options' ) { |
| 490 | |
| 491 | acf_update_options_page( |
| 492 | 'acf-options', |
| 493 | array( |
| 494 | 'menu_title' => $title, |
| 495 | ) |
| 496 | ); |
| 497 | } |
| 498 | |
| 499 | endif; |
| 500 | |
| 501 | |
| 502 | /** |
| 503 | * This function is used to customize the options page capability. Defaults to 'edit_posts' |
| 504 | * |
| 505 | * @type function |
| 506 | * @date 13/07/13 |
| 507 | * @since 4.0.0 |
| 508 | * |
| 509 | * @param $title (string) |
| 510 | * @return n/a |
| 511 | */ |
| 512 | if ( ! function_exists( 'acf_set_options_page_capability' ) ) : |
| 513 | |
| 514 | function acf_set_options_page_capability( $capability = 'edit_posts' ) { |
| 515 | |
| 516 | acf_update_options_page( |
| 517 | 'acf-options', |
| 518 | array( |
| 519 | 'capability' => $capability, |
| 520 | ) |
| 521 | ); |
| 522 | } |
| 523 | |
| 524 | endif; |
| 525 | |
| 526 | |
| 527 | /** |
| 528 | * This is an old function which is now referencing the new 'acf_add_options_sub_page' function |
| 529 | * |
| 530 | * @type function |
| 531 | * @since 3.0.0 |
| 532 | * @date 29/01/13 |
| 533 | * |
| 534 | * @param {string} $title |
| 535 | * @return N/A |
| 536 | */ |
| 537 | if ( ! function_exists( 'register_options_page' ) ) : |
| 538 | |
| 539 | function register_options_page( $page = '' ) { |
| 540 | |
| 541 | acf_add_options_sub_page( $page ); |
| 542 | } |
| 543 | |
| 544 | endif; |
| 545 |