admin-tools.php
| 1 | <?php |
| 2 | |
| 3 | if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 4 | |
| 5 | if( ! class_exists('acf_admin_tools') ) : |
| 6 | |
| 7 | class acf_admin_tools { |
| 8 | |
| 9 | |
| 10 | /** @var array Contains an array of admin tool instances */ |
| 11 | var $tools = array(); |
| 12 | |
| 13 | |
| 14 | /** @var string The active tool */ |
| 15 | var $active = ''; |
| 16 | |
| 17 | |
| 18 | /** |
| 19 | * __construct |
| 20 | * |
| 21 | * This function will setup the class functionality |
| 22 | * |
| 23 | * @date 10/10/17 |
| 24 | * @since 5.6.3 |
| 25 | * |
| 26 | * @param n/a |
| 27 | * @return n/a |
| 28 | */ |
| 29 | |
| 30 | function __construct() { |
| 31 | |
| 32 | // actions |
| 33 | add_action('admin_menu', array($this, 'admin_menu')); |
| 34 | |
| 35 | } |
| 36 | |
| 37 | |
| 38 | /** |
| 39 | * register_tool |
| 40 | * |
| 41 | * This function will store a tool tool class |
| 42 | * |
| 43 | * @date 10/10/17 |
| 44 | * @since 5.6.3 |
| 45 | * |
| 46 | * @param string $class |
| 47 | * @return n/a |
| 48 | */ |
| 49 | |
| 50 | function register_tool( $class ) { |
| 51 | |
| 52 | $instance = new $class(); |
| 53 | $this->tools[ $instance->name ] = $instance; |
| 54 | |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /** |
| 59 | * get_tool |
| 60 | * |
| 61 | * This function will return a tool tool class |
| 62 | * |
| 63 | * @date 10/10/17 |
| 64 | * @since 5.6.3 |
| 65 | * |
| 66 | * @param string $name |
| 67 | * @return n/a |
| 68 | */ |
| 69 | |
| 70 | function get_tool( $name ) { |
| 71 | |
| 72 | return isset( $this->tools[$name] ) ? $this->tools[$name] : null; |
| 73 | |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /** |
| 78 | * get_tools |
| 79 | * |
| 80 | * This function will return an array of all tools |
| 81 | * |
| 82 | * @date 10/10/17 |
| 83 | * @since 5.6.3 |
| 84 | * |
| 85 | * @param n/a |
| 86 | * @return array |
| 87 | */ |
| 88 | |
| 89 | function get_tools() { |
| 90 | |
| 91 | return $this->tools; |
| 92 | |
| 93 | } |
| 94 | |
| 95 | |
| 96 | /* |
| 97 | * admin_menu |
| 98 | * |
| 99 | * This function will add the ACF menu item to the WP admin |
| 100 | * |
| 101 | * @type action (admin_menu) |
| 102 | * @date 28/09/13 |
| 103 | * @since 5.0.0 |
| 104 | * |
| 105 | * @param n/a |
| 106 | * @return n/a |
| 107 | */ |
| 108 | |
| 109 | function admin_menu() { |
| 110 | |
| 111 | // bail early if no show_admin |
| 112 | if( !acf_get_setting('show_admin') ) return; |
| 113 | |
| 114 | |
| 115 | // add page |
| 116 | $page = add_submenu_page('edit.php?post_type=acf-field-group', __('Tools','acf'), __('Tools','acf'), acf_get_setting('capability'), 'acf-tools', array($this, 'html')); |
| 117 | |
| 118 | |
| 119 | // actions |
| 120 | add_action('load-' . $page, array($this, 'load')); |
| 121 | |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * load |
| 127 | * |
| 128 | * description |
| 129 | * |
| 130 | * @date 10/10/17 |
| 131 | * @since 5.6.3 |
| 132 | * |
| 133 | * @param n/a |
| 134 | * @return n/a |
| 135 | */ |
| 136 | |
| 137 | function load() { |
| 138 | |
| 139 | // disable filters (default to raw data) |
| 140 | acf_disable_filters(); |
| 141 | |
| 142 | |
| 143 | // include tools |
| 144 | $this->include_tools(); |
| 145 | |
| 146 | |
| 147 | // check submit |
| 148 | $this->check_submit(); |
| 149 | |
| 150 | |
| 151 | // load acf scripts |
| 152 | acf_enqueue_scripts(); |
| 153 | |
| 154 | } |
| 155 | |
| 156 | |
| 157 | /** |
| 158 | * include_tools |
| 159 | * |
| 160 | * description |
| 161 | * |
| 162 | * @date 10/10/17 |
| 163 | * @since 5.6.3 |
| 164 | * |
| 165 | * @param n/a |
| 166 | * @return n/a |
| 167 | */ |
| 168 | |
| 169 | function include_tools() { |
| 170 | |
| 171 | // include |
| 172 | acf_include('includes/admin/tools/class-acf-admin-tool.php'); |
| 173 | acf_include('includes/admin/tools/class-acf-admin-tool-export.php'); |
| 174 | acf_include('includes/admin/tools/class-acf-admin-tool-import.php'); |
| 175 | |
| 176 | |
| 177 | // action |
| 178 | do_action('acf/include_admin_tools'); |
| 179 | |
| 180 | } |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * check_submit |
| 185 | * |
| 186 | * description |
| 187 | * |
| 188 | * @date 10/10/17 |
| 189 | * @since 5.6.3 |
| 190 | * |
| 191 | * @param n/a |
| 192 | * @return n/a |
| 193 | */ |
| 194 | |
| 195 | function check_submit() { |
| 196 | |
| 197 | // loop |
| 198 | foreach( $this->get_tools() as $tool ) { |
| 199 | |
| 200 | // load |
| 201 | $tool->load(); |
| 202 | |
| 203 | |
| 204 | // submit |
| 205 | if( acf_verify_nonce($tool->name) ) { |
| 206 | $tool->submit(); |
| 207 | } |
| 208 | |
| 209 | } |
| 210 | |
| 211 | } |
| 212 | |
| 213 | |
| 214 | /** |
| 215 | * html |
| 216 | * |
| 217 | * description |
| 218 | * |
| 219 | * @date 10/10/17 |
| 220 | * @since 5.6.3 |
| 221 | * |
| 222 | * @param n/a |
| 223 | * @return n/a |
| 224 | */ |
| 225 | |
| 226 | function html() { |
| 227 | |
| 228 | // vars |
| 229 | $screen = get_current_screen(); |
| 230 | $active = acf_maybe_get_GET('tool'); |
| 231 | |
| 232 | |
| 233 | // view |
| 234 | $view = array( |
| 235 | 'screen_id' => $screen->id, |
| 236 | 'active' => $active |
| 237 | ); |
| 238 | |
| 239 | |
| 240 | // register metaboxes |
| 241 | foreach( $this->get_tools() as $tool ) { |
| 242 | |
| 243 | // check active |
| 244 | if( $active && $active !== $tool->name ) continue; |
| 245 | |
| 246 | |
| 247 | // add metabox |
| 248 | add_meta_box( 'acf-admin-tool-' . $tool->name, $tool->title, array($this, 'metabox_html'), $screen->id, 'normal', 'default', array('tool' => $tool->name) ); |
| 249 | |
| 250 | } |
| 251 | |
| 252 | |
| 253 | // view |
| 254 | acf_get_view( 'html-admin-tools', $view ); |
| 255 | |
| 256 | } |
| 257 | |
| 258 | |
| 259 | /** |
| 260 | * meta_box_html |
| 261 | * |
| 262 | * description |
| 263 | * |
| 264 | * @date 10/10/17 |
| 265 | * @since 5.6.3 |
| 266 | * |
| 267 | * @param n/a |
| 268 | * @return n/a |
| 269 | */ |
| 270 | |
| 271 | function metabox_html( $post, $metabox ) { |
| 272 | |
| 273 | // vars |
| 274 | $tool = $this->get_tool($metabox['args']['tool']); |
| 275 | |
| 276 | |
| 277 | ?> |
| 278 | <form method="post"> |
| 279 | <?php $tool->html(); ?> |
| 280 | <?php acf_nonce_input( $tool->name ); ?> |
| 281 | </form> |
| 282 | <?php |
| 283 | |
| 284 | } |
| 285 | |
| 286 | } |
| 287 | |
| 288 | // initialize |
| 289 | acf()->admin_tools = new acf_admin_tools(); |
| 290 | |
| 291 | endif; // class_exists check |
| 292 | |
| 293 | |
| 294 | /* |
| 295 | * acf_register_admin_tool |
| 296 | * |
| 297 | * alias of acf()->admin_tools->register_tool() |
| 298 | * |
| 299 | * @type function |
| 300 | * @date 31/5/17 |
| 301 | * @since 5.6.0 |
| 302 | * |
| 303 | * @param n/a |
| 304 | * @return n/a |
| 305 | */ |
| 306 | |
| 307 | function acf_register_admin_tool( $class ) { |
| 308 | |
| 309 | return acf()->admin_tools->register_tool( $class ); |
| 310 | |
| 311 | } |
| 312 | |
| 313 | |
| 314 | /* |
| 315 | * acf_get_admin_tools_url |
| 316 | * |
| 317 | * This function will return the admin URL to the tools page |
| 318 | * |
| 319 | * @type function |
| 320 | * @date 31/5/17 |
| 321 | * @since 5.6.0 |
| 322 | * |
| 323 | * @param n/a |
| 324 | * @return n/a |
| 325 | */ |
| 326 | |
| 327 | function acf_get_admin_tools_url() { |
| 328 | |
| 329 | return admin_url('edit.php?post_type=acf-field-group&page=acf-tools'); |
| 330 | |
| 331 | } |
| 332 | |
| 333 | |
| 334 | /* |
| 335 | * acf_get_admin_tool_url |
| 336 | * |
| 337 | * This function will return the admin URL to the tools page |
| 338 | * |
| 339 | * @type function |
| 340 | * @date 31/5/17 |
| 341 | * @since 5.6.0 |
| 342 | * |
| 343 | * @param n/a |
| 344 | * @return n/a |
| 345 | */ |
| 346 | |
| 347 | function acf_get_admin_tool_url( $tool = '' ) { |
| 348 | |
| 349 | return acf_get_admin_tools_url() . '&tool='.$tool; |
| 350 | |
| 351 | } |
| 352 | |
| 353 | |
| 354 | ?> |