admin-notices.php
| 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') ) exit; |
| 13 | |
| 14 | // Register notices store. |
| 15 | acf_register_store( 'notices' ); |
| 16 | |
| 17 | /** |
| 18 | * ACF_Admin_Notice |
| 19 | * |
| 20 | * Class used to create an admin notice. |
| 21 | * |
| 22 | * @date 10/1/19 |
| 23 | * @since 5.7.10 |
| 24 | */ |
| 25 | if( ! class_exists('ACF_Admin_Notice') ) : |
| 26 | |
| 27 | class ACF_Admin_Notice extends ACF_Data { |
| 28 | |
| 29 | /** @var array Storage for data. */ |
| 30 | var $data = array( |
| 31 | |
| 32 | /** @type string Text displayed in notice. */ |
| 33 | 'text' => '', |
| 34 | |
| 35 | /** @type string Optional HTML alternative to text. |
| 36 | 'html' => '', */ |
| 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 | |
| 45 | /** |
| 46 | * render |
| 47 | * |
| 48 | * Renders the notice HTML. |
| 49 | * |
| 50 | * @date 27/12/18 |
| 51 | * @since 5.8.0 |
| 52 | * |
| 53 | * @param void |
| 54 | * @return void |
| 55 | */ |
| 56 | function render() { |
| 57 | |
| 58 | // Ensure text contains punctuation. |
| 59 | // todo: Remove this after updating translations. |
| 60 | $text = $this->get('text'); |
| 61 | if( substr($text, -1) !== '.' && substr($text, -1) !== '>' ) { |
| 62 | $text .= '.'; |
| 63 | } |
| 64 | |
| 65 | // Print HTML. |
| 66 | printf('<div class="acf-admin-notice notice notice-%s %s">%s</div>', |
| 67 | |
| 68 | // Type class. |
| 69 | $this->get('type'), |
| 70 | |
| 71 | // Dismissible class. |
| 72 | $this->get('dismissible') ? 'is-dismissible' : '', |
| 73 | |
| 74 | // InnerHTML |
| 75 | $this->has('html') ? $this->get('html') : wpautop($text) |
| 76 | ); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | endif; // class_exists check |
| 81 | |
| 82 | /** |
| 83 | * acf_new_admin_notice |
| 84 | * |
| 85 | * Instantiates and returns a new model. |
| 86 | * |
| 87 | * @date 23/12/18 |
| 88 | * @since 5.8.0 |
| 89 | * |
| 90 | * @param array $data Optional data to set. |
| 91 | * @return ACF_Admin_Notice |
| 92 | */ |
| 93 | function acf_new_admin_notice( $data = false ) { |
| 94 | |
| 95 | // Create notice. |
| 96 | $instance = new ACF_Admin_Notice( $data ); |
| 97 | |
| 98 | // Register notice. |
| 99 | acf_get_store( 'notices' )->set( $instance->cid, $instance ); |
| 100 | |
| 101 | // Return notice. |
| 102 | return $instance; |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * acf_render_admin_notices |
| 107 | * |
| 108 | * Renders all admin notices HTML. |
| 109 | * |
| 110 | * @date 10/1/19 |
| 111 | * @since 5.7.10 |
| 112 | * |
| 113 | * @param void |
| 114 | * @return void |
| 115 | */ |
| 116 | function acf_render_admin_notices() { |
| 117 | |
| 118 | // Get notices. |
| 119 | $notices = acf_get_store( 'notices' )->get_data(); |
| 120 | |
| 121 | // Loop over notices and render. |
| 122 | if( $notices ) { |
| 123 | foreach( $notices as $notice ) { |
| 124 | $notice->render(); |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | |
| 129 | // Render notices during admin action. |
| 130 | add_action('admin_notices', 'acf_render_admin_notices', 99); |
| 131 | |
| 132 | /** |
| 133 | * acf_add_admin_notice |
| 134 | * |
| 135 | * Creates and returns a new notice. |
| 136 | * |
| 137 | * @date 17/10/13 |
| 138 | * @since 5.0.0 |
| 139 | * |
| 140 | * @param string $text The admin notice text. |
| 141 | * @param string $class The type of notice (warning, error, success, info). |
| 142 | * @return ACF_Admin_Notice |
| 143 | */ |
| 144 | function acf_add_admin_notice( $text = '', $type = 'info' ) { |
| 145 | return acf_new_admin_notice( array( 'text' => $text, 'type' => $type ) ); |
| 146 | } |