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