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