post-types
5 months ago
tools
5 months ago
views
1 month ago
admin-email-opt-in-banner.php
1 month ago
admin-internal-post-type-list.php
5 months ago
admin-internal-post-type.php
5 months ago
admin-notices.php
5 months ago
admin-options-pages-preview.php
5 months ago
admin-tools.php
5 months ago
admin-upgrade.php
5 months ago
admin.php
1 month ago
index.php
2 years ago
admin-notices.php
152 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package ACF |
| 4 | * @author WP Engine |
| 5 | * |
| 6 | * © 2026 Advanced Custom Fields (ACF®). All rights reserved. |
| 7 | * "ACF" is a trademark of WP Engine. |
| 8 | * Licensed under the GNU General Public License v2 or later. |
| 9 | * https://www.gnu.org/licenses/gpl-2.0.html |
| 10 | */ |
| 11 | |
| 12 | // Exit if accessed directly. |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | // Register notices store. |
| 18 | acf_register_store( 'notices' ); |
| 19 | |
| 20 | /** |
| 21 | * ACF_Admin_Notice |
| 22 | * |
| 23 | * Class used to create an admin notice. |
| 24 | * |
| 25 | * @date 10/1/19 |
| 26 | * @since 5.7.10 |
| 27 | */ |
| 28 | if ( ! class_exists( 'ACF_Admin_Notice' ) ) : |
| 29 | |
| 30 | class ACF_Admin_Notice extends ACF_Data { |
| 31 | |
| 32 | /** @var array Storage for data. */ |
| 33 | var $data = array( |
| 34 | |
| 35 | /** @type string Text displayed in notice. */ |
| 36 | 'text' => '', |
| 37 | |
| 38 | /** @type string The type of notice (warning, error, success, info). */ |
| 39 | 'type' => 'info', |
| 40 | |
| 41 | /** @type bool If the notice can be dismissed. */ |
| 42 | 'dismissible' => true, |
| 43 | |
| 44 | /** @type bool If the dismissed state should be persisted to ACF user preferences. */ |
| 45 | 'persisted' => false, |
| 46 | ); |
| 47 | |
| 48 | /** |
| 49 | * render |
| 50 | * |
| 51 | * Renders the notice HTML. |
| 52 | * |
| 53 | * @date 27/12/18 |
| 54 | * @since 5.8.0 |
| 55 | * |
| 56 | * @param void |
| 57 | * @return void |
| 58 | */ |
| 59 | function render() { |
| 60 | $notice_text = $this->get( 'text' ); |
| 61 | $notice_type = $this->get( 'type' ); |
| 62 | $is_dismissible = $this->get( 'dismissible' ); |
| 63 | $is_persisted = $this->get( 'persisted' ); |
| 64 | |
| 65 | printf( |
| 66 | '<div class="acf-admin-notice notice notice-%s %s" data-persisted="%s" data-persist-id="%s">%s</div>', |
| 67 | esc_attr( $notice_type ), |
| 68 | $is_dismissible ? 'is-dismissible' : '', |
| 69 | $is_persisted ? 'true' : 'false', |
| 70 | esc_attr( md5( $notice_text ) ), |
| 71 | acf_esc_html( wpautop( acf_punctify( $notice_text ) ) ) |
| 72 | ); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | endif; // class_exists check |
| 77 | |
| 78 | /** |
| 79 | * acf_new_admin_notice |
| 80 | * |
| 81 | * Instantiates and returns a new model. |
| 82 | * |
| 83 | * @date 23/12/18 |
| 84 | * @since 5.8.0 |
| 85 | * |
| 86 | * @param array $data Optional data to set. |
| 87 | * @return ACF_Admin_Notice |
| 88 | */ |
| 89 | function acf_new_admin_notice( $data = false ) { |
| 90 | |
| 91 | // Create notice. |
| 92 | $instance = new ACF_Admin_Notice( $data ); |
| 93 | |
| 94 | // Register notice. |
| 95 | acf_get_store( 'notices' )->set( $instance->cid, $instance ); |
| 96 | |
| 97 | // Return notice. |
| 98 | return $instance; |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * acf_render_admin_notices |
| 103 | * |
| 104 | * Renders all admin notices HTML. |
| 105 | * |
| 106 | * @date 10/1/19 |
| 107 | * @since 5.7.10 |
| 108 | * |
| 109 | * @param void |
| 110 | * @return void |
| 111 | */ |
| 112 | function acf_render_admin_notices() { |
| 113 | |
| 114 | // Get notices. |
| 115 | $notices = acf_get_store( 'notices' )->get_data(); |
| 116 | |
| 117 | // Loop over notices and render. |
| 118 | if ( $notices ) { |
| 119 | foreach ( $notices as $notice ) { |
| 120 | $notice->render(); |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | // Render notices during admin action. |
| 126 | add_action( 'admin_notices', 'acf_render_admin_notices', 99 ); |
| 127 | |
| 128 | /** |
| 129 | * acf_add_admin_notice |
| 130 | * |
| 131 | * Creates and returns a new notice. |
| 132 | * |
| 133 | * @date 17/10/13 |
| 134 | * @since 5.0.0 |
| 135 | * |
| 136 | * @param string $text The admin notice text. |
| 137 | * @param string $class The type of notice (warning, error, success, info). |
| 138 | * @param boolean $dismissable Is this notification dismissible (default true) (since 5.11.0) |
| 139 | * @param boolean $persisted Store once a notice has been dismissed per user and prevent showing it again. (since 6.1.0) |
| 140 | * @return ACF_Admin_Notice |
| 141 | */ |
| 142 | function acf_add_admin_notice( $text = '', $type = 'info', $dismissible = true, $persisted = false ) { |
| 143 | return acf_new_admin_notice( |
| 144 | array( |
| 145 | 'text' => $text, |
| 146 | 'type' => $type, |
| 147 | 'dismissible' => $dismissible, |
| 148 | 'persisted' => $persisted, |
| 149 | ) |
| 150 | ); |
| 151 | } |
| 152 |