admin.php
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 | |
| 5 | if( ! class_exists('acf_admin') ) : |
| 6 | |
| 7 | class acf_admin { |
| 8 | |
| 9 | // vars |
| 10 | var $notices = array(); |
| 11 | |
| 12 | |
| 13 | /* |
| 14 | * __construct |
| 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 | |
| 26 | function __construct() { |
| 27 | |
| 28 | // actions |
| 29 | add_action('admin_menu', array($this, 'admin_menu')); |
| 30 | add_action('admin_enqueue_scripts', array($this, 'admin_enqueue_scripts'), 0); |
| 31 | add_action('admin_notices', array($this, 'admin_notices')); |
| 32 | |
| 33 | } |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | * add_notice |
| 38 | * |
| 39 | * This function will add the notice data to a setting in the acf object for the admin_notices action to use |
| 40 | * |
| 41 | * @type function |
| 42 | * @date 17/10/13 |
| 43 | * @since 5.0.0 |
| 44 | * |
| 45 | * @param $text (string) |
| 46 | * @param $class (string) |
| 47 | * @param wrap (string) |
| 48 | * @return n/a |
| 49 | */ |
| 50 | |
| 51 | function add_notice( $text = '', $class = '', $wrap = 'p' ) { |
| 52 | |
| 53 | // append |
| 54 | $this->notices[] = array( |
| 55 | 'text' => $text, |
| 56 | 'class' => 'updated ' . $class, |
| 57 | 'wrap' => $wrap |
| 58 | ); |
| 59 | |
| 60 | } |
| 61 | |
| 62 | |
| 63 | /* |
| 64 | * get_notices |
| 65 | * |
| 66 | * This function will return an array of admin notices |
| 67 | * |
| 68 | * @type function |
| 69 | * @date 17/10/13 |
| 70 | * @since 5.0.0 |
| 71 | * |
| 72 | * @param n/a |
| 73 | * @return (array) |
| 74 | */ |
| 75 | |
| 76 | function get_notices() { |
| 77 | |
| 78 | // bail early if no notices |
| 79 | if( empty($this->notices) ) return false; |
| 80 | |
| 81 | |
| 82 | // return |
| 83 | return $this->notices; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | |
| 88 | /* |
| 89 | * admin_menu |
| 90 | * |
| 91 | * This function will add the ACF menu item to the WP admin |
| 92 | * |
| 93 | * @type action (admin_menu) |
| 94 | * @date 28/09/13 |
| 95 | * @since 5.0.0 |
| 96 | * |
| 97 | * @param n/a |
| 98 | * @return n/a |
| 99 | */ |
| 100 | |
| 101 | function admin_menu() { |
| 102 | |
| 103 | // bail early if no show_admin |
| 104 | if( !acf_get_setting('show_admin') ) return; |
| 105 | |
| 106 | |
| 107 | // vars |
| 108 | $slug = 'edit.php?post_type=acf-field-group'; |
| 109 | $cap = acf_get_setting('capability'); |
| 110 | |
| 111 | |
| 112 | // add parent |
| 113 | add_menu_page(__("Custom Fields",'acf'), __("Custom Fields",'acf'), $cap, $slug, false, 'dashicons-welcome-widgets-menus', '80.025'); |
| 114 | |
| 115 | |
| 116 | // add children |
| 117 | add_submenu_page($slug, __('Field Groups','acf'), __('Field Groups','acf'), $cap, $slug ); |
| 118 | add_submenu_page($slug, __('Add New','acf'), __('Add New','acf'), $cap, 'post-new.php?post_type=acf-field-group' ); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | |
| 123 | /* |
| 124 | * admin_enqueue_scripts |
| 125 | * |
| 126 | * This function will add the already registered css |
| 127 | * |
| 128 | * @type function |
| 129 | * @date 28/09/13 |
| 130 | * @since 5.0.0 |
| 131 | * |
| 132 | * @param n/a |
| 133 | * @return n/a |
| 134 | */ |
| 135 | |
| 136 | function admin_enqueue_scripts() { |
| 137 | |
| 138 | wp_enqueue_style( 'acf-global' ); |
| 139 | |
| 140 | } |
| 141 | |
| 142 | |
| 143 | /* |
| 144 | * admin_notices |
| 145 | * |
| 146 | * This function will render any admin notices |
| 147 | * |
| 148 | * @type function |
| 149 | * @date 17/10/13 |
| 150 | * @since 5.0.0 |
| 151 | * |
| 152 | * @param n/a |
| 153 | * @return n/a |
| 154 | */ |
| 155 | |
| 156 | function admin_notices() { |
| 157 | |
| 158 | // vars |
| 159 | $notices = $this->get_notices(); |
| 160 | |
| 161 | |
| 162 | // bail early if no notices |
| 163 | if( !$notices ) return; |
| 164 | |
| 165 | |
| 166 | // loop |
| 167 | foreach( $notices as $notice ) { |
| 168 | |
| 169 | $open = ''; |
| 170 | $close = ''; |
| 171 | |
| 172 | if( $notice['wrap'] ) { |
| 173 | |
| 174 | $open = "<{$notice['wrap']}>"; |
| 175 | $close = "</{$notice['wrap']}>"; |
| 176 | |
| 177 | } |
| 178 | |
| 179 | ?> |
| 180 | <div class="acf-admin-notice notice is-dismissible <?php echo esc_attr($notice['class']); ?>"><?php echo $open . $notice['text'] . $close; ?></div> |
| 181 | <?php |
| 182 | |
| 183 | } |
| 184 | |
| 185 | } |
| 186 | |
| 187 | } |
| 188 | |
| 189 | // initialize |
| 190 | acf()->admin = new acf_admin(); |
| 191 | |
| 192 | endif; // class_exists check |
| 193 | |
| 194 | |
| 195 | /* |
| 196 | * acf_add_admin_notice |
| 197 | * |
| 198 | * This function will add the notice data to a setting in the acf object for the admin_notices action to use |
| 199 | * |
| 200 | * @type function |
| 201 | * @date 17/10/13 |
| 202 | * @since 5.0.0 |
| 203 | * |
| 204 | * @param $text (string) |
| 205 | * @param $class (string) |
| 206 | * @return (int) message ID (array position) |
| 207 | */ |
| 208 | |
| 209 | function acf_add_admin_notice( $text, $class = '', $wrap = 'p' ) { |
| 210 | |
| 211 | return acf()->admin->add_notice($text, $class, $wrap); |
| 212 | |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /* |
| 217 | * acf_get_admin_notices |
| 218 | * |
| 219 | * This function will return an array containing any admin notices |
| 220 | * |
| 221 | * @type function |
| 222 | * @date 17/10/13 |
| 223 | * @since 5.0.0 |
| 224 | * |
| 225 | * @param n/a |
| 226 | * @return (array) |
| 227 | */ |
| 228 | |
| 229 | function acf_get_admin_notices() { |
| 230 | |
| 231 | return acf()->admin->get_notices(); |
| 232 | |
| 233 | } |
| 234 | |
| 235 | ?> |