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
admin-notices.php
147 lines
| 1 | <?php // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed |
| 2 | /** |
| 3 | * ACF Admin Notices |
| 4 | * |
| 5 | * Functions and classes to manage admin notices. |
| 6 | * |
| 7 | * @date 10/1/19 |
| 8 | * @since ACF 5.7.10 |
| 9 | */ |
| 10 | |
| 11 | // Exit if accessed directly. |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | // Register notices store. |
| 17 | acf_register_store( 'notices' ); |
| 18 | |
| 19 | /** |
| 20 | * ACF_Admin_Notice |
| 21 | * |
| 22 | * Class used to create an admin notice. |
| 23 | * |
| 24 | * @date 10/1/19 |
| 25 | * @since ACF 5.7.10 |
| 26 | */ |
| 27 | if ( ! class_exists( 'ACF_Admin_Notice' ) ) : |
| 28 | /** |
| 29 | * Class responsible for handling admin notices. |
| 30 | */ |
| 31 | class ACF_Admin_Notice extends ACF_Data { |
| 32 | |
| 33 | /** |
| 34 | * Storage for data. |
| 35 | * |
| 36 | * @var array |
| 37 | */ |
| 38 | public $data = array( |
| 39 | |
| 40 | /** Text displayed in notice. @type string */ |
| 41 | 'text' => '', |
| 42 | |
| 43 | /** The type of notice (warning, error, success, info). @type string */ |
| 44 | 'type' => 'info', |
| 45 | |
| 46 | /** If the notice can be dismissed. @type bool */ |
| 47 | 'dismissible' => true, |
| 48 | |
| 49 | /** If the dismissed state should be persisted to ACF user preferences. @type bool */ |
| 50 | 'persisted' => false, |
| 51 | ); |
| 52 | |
| 53 | /** |
| 54 | * Renders the notice HTML. |
| 55 | * |
| 56 | * @date 27/12/18 |
| 57 | * @since ACF 5.8.0 |
| 58 | * |
| 59 | * @return void |
| 60 | */ |
| 61 | public function render() { |
| 62 | $notice_text = $this->get( 'text' ); |
| 63 | $notice_type = $this->get( 'type' ); |
| 64 | $is_dismissible = $this->get( 'dismissible' ); |
| 65 | $is_persisted = $this->get( 'persisted' ); |
| 66 | |
| 67 | printf( |
| 68 | '<div class="acf-admin-notice notice notice-%s %s" data-persisted="%s" data-persist-id="%s">%s</div>', |
| 69 | esc_attr( $notice_type ), |
| 70 | $is_dismissible ? 'is-dismissible' : '', |
| 71 | $is_persisted ? 'true' : 'false', |
| 72 | esc_attr( md5( $notice_text ) ), |
| 73 | acf_esc_html( wpautop( acf_punctify( $notice_text ) ) ) |
| 74 | ); |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | endif; // class_exists check |
| 79 | |
| 80 | /** |
| 81 | * Instantiates and returns a new model. |
| 82 | * |
| 83 | * @date 23/12/18 |
| 84 | * @since ACF 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 | * Renders all admin notices HTML. |
| 103 | * |
| 104 | * @date 10/1/19 |
| 105 | * @since ACF 5.7.10 |
| 106 | * |
| 107 | * @return void |
| 108 | */ |
| 109 | function acf_render_admin_notices() { |
| 110 | |
| 111 | // Get notices. |
| 112 | $notices = acf_get_store( 'notices' )->get_data(); |
| 113 | |
| 114 | // Loop over notices and render. |
| 115 | if ( $notices ) { |
| 116 | foreach ( $notices as $notice ) { |
| 117 | $notice->render(); |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | // Render notices during admin action. |
| 123 | add_action( 'admin_notices', 'acf_render_admin_notices', 99 ); |
| 124 | |
| 125 | /** |
| 126 | * Creates and returns a new notice. |
| 127 | * |
| 128 | * @date 17/10/13 |
| 129 | * @since ACF 5.0.0 |
| 130 | * |
| 131 | * @param string $text The admin notice text. |
| 132 | * @param string $type The type of notice (warning, error, success, info). |
| 133 | * @param boolean $dismissible Is this notification dismissible (default true) (since 5.11.0). |
| 134 | * @param boolean $persisted Store once a notice has been dismissed per user and prevent showing it again. (since ACF 6.1.0). |
| 135 | * @return ACF_Admin_Notice |
| 136 | */ |
| 137 | function acf_add_admin_notice( $text = '', $type = 'info', $dismissible = true, $persisted = false ) { |
| 138 | return acf_new_admin_notice( |
| 139 | array( |
| 140 | 'text' => $text, |
| 141 | 'type' => $type, |
| 142 | 'dismissible' => $dismissible, |
| 143 | 'persisted' => $persisted, |
| 144 | ) |
| 145 | ); |
| 146 | } |
| 147 |