beta-features
7 months ago
post-types
2 months ago
tools
7 months ago
views
1 week ago
admin-commands.php
2 months ago
admin-internal-post-type-list.php
10 months ago
admin-internal-post-type.php
1 year ago
admin-notices.php
1 year ago
admin-tools.php
10 months ago
admin-upgrade.php
1 year ago
admin.php
10 months ago
beta-features.php
7 months ago
class-acf-admin-options-page.php
2 months ago
index.php
1 year ago
admin-tools.php
306 lines
| 1 | <?php // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; // Exit if accessed directly |
| 4 | } |
| 5 | |
| 6 | if ( ! class_exists( 'acf_admin_tools' ) ) : |
| 7 | /** |
| 8 | * Class AdminTools |
| 9 | * |
| 10 | * This class provides various administrative tools for managing secure custom fields. |
| 11 | */ |
| 12 | class acf_admin_tools { // phpcs:ignore |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * Contains an array of admin tool instance. |
| 17 | * |
| 18 | * @var array |
| 19 | */ |
| 20 | public $tools = array(); // @todo This should be private, but maintaining compatibility with the original code. |
| 21 | |
| 22 | |
| 23 | /** |
| 24 | * The active tool |
| 25 | * |
| 26 | * @var string |
| 27 | */ |
| 28 | public $active = ''; // @todo Check to see if this should be private, but maintaining compatibility with the original code for now. |
| 29 | |
| 30 | |
| 31 | /** |
| 32 | * This function will setup the class functionality |
| 33 | * |
| 34 | * @date 10/10/17 |
| 35 | * @since ACF 5.6.3 |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function __construct() { |
| 40 | |
| 41 | // actions |
| 42 | add_action( 'admin_menu', array( $this, 'admin_menu' ), 15 ); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * This function will store a tool class instance in the tools array. |
| 47 | * |
| 48 | * @date 10/10/17 |
| 49 | * @since ACF 5.6.3 |
| 50 | * |
| 51 | * @param string $class Class name. |
| 52 | * @return void |
| 53 | */ |
| 54 | public function register_tool( $class ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.classFound |
| 55 | |
| 56 | $instance = new $class(); |
| 57 | $this->tools[ $instance->name ] = $instance; |
| 58 | } |
| 59 | |
| 60 | |
| 61 | /** |
| 62 | * This function will return a tool class or null if not found. |
| 63 | * |
| 64 | * @date 10/10/17 |
| 65 | * @since ACF 5.6.3 |
| 66 | * |
| 67 | * @param string $name Name of tool. |
| 68 | * @return mixed (ACF_Admin_Tool|null) |
| 69 | */ |
| 70 | public function get_tool( $name ) { |
| 71 | |
| 72 | return isset( $this->tools[ $name ] ) ? $this->tools[ $name ] : null; |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * This function will return an array of all tool instances. |
| 78 | * |
| 79 | * @date 10/10/17 |
| 80 | * @since ACF 5.6.3 |
| 81 | * |
| 82 | * @return array |
| 83 | */ |
| 84 | public function get_tools() { |
| 85 | |
| 86 | return $this->tools; |
| 87 | } |
| 88 | |
| 89 | |
| 90 | /** |
| 91 | * This function will add the SCF menu item to the WP admin |
| 92 | * |
| 93 | * @type action (admin_menu) |
| 94 | * @date 28/09/13 |
| 95 | * @since ACF 5.0.0 |
| 96 | * |
| 97 | * @return void |
| 98 | */ |
| 99 | public function admin_menu() { |
| 100 | |
| 101 | // bail early if no show_admin |
| 102 | if ( ! acf_get_setting( 'show_admin' ) ) { |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | // add page |
| 107 | $page = add_submenu_page( 'edit.php?post_type=acf-field-group', __( 'Tools', 'secure-custom-fields' ), __( 'Tools', 'secure-custom-fields' ), acf_get_setting( 'capability' ), 'acf-tools', array( $this, 'html' ) ); |
| 108 | |
| 109 | // actions |
| 110 | add_action( 'load-' . $page, array( $this, 'load' ) ); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | /** |
| 115 | * Loads the admin tools page. |
| 116 | * |
| 117 | * @date 10/10/17 |
| 118 | * @since ACF 5.6.3 |
| 119 | * |
| 120 | * @return void |
| 121 | */ |
| 122 | public function load() { |
| 123 | |
| 124 | add_filter( 'admin_body_class', array( $this, 'admin_body_class' ) ); |
| 125 | |
| 126 | // disable filters (default to raw data) |
| 127 | acf_disable_filters(); |
| 128 | |
| 129 | // include tools |
| 130 | $this->include_tools(); |
| 131 | |
| 132 | // check submit |
| 133 | $this->check_submit(); |
| 134 | |
| 135 | // load acf scripts |
| 136 | acf_enqueue_scripts(); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Modifies the admin body class. |
| 141 | * |
| 142 | * @since ACF 6.0.0 |
| 143 | * |
| 144 | * @param string $classes Space-separated list of CSS classes. |
| 145 | * @return string |
| 146 | */ |
| 147 | public function admin_body_class( $classes ) { |
| 148 | $classes .= ' acf-admin-page'; |
| 149 | return $classes; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Includes various tool-related files. |
| 154 | * |
| 155 | * @date 10/10/17 |
| 156 | * @since ACF 5.6.3 |
| 157 | * |
| 158 | * @return void |
| 159 | */ |
| 160 | public function include_tools() { |
| 161 | |
| 162 | // include |
| 163 | acf_include( 'includes/admin/tools/class-acf-admin-tool.php' ); |
| 164 | acf_include( 'includes/admin/tools/class-acf-admin-tool-export.php' ); |
| 165 | acf_include( 'includes/admin/tools/class-acf-admin-tool-import.php' ); |
| 166 | |
| 167 | // action |
| 168 | do_action( 'acf/include_admin_tools' ); |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Verifies the nonces and submits the value if it passes. |
| 174 | * |
| 175 | * @date 10/10/17 |
| 176 | * @since ACF 5.6.3 |
| 177 | * |
| 178 | * @return void |
| 179 | */ |
| 180 | public function check_submit() { |
| 181 | |
| 182 | // loop |
| 183 | foreach ( $this->get_tools() as $tool ) { |
| 184 | |
| 185 | // load |
| 186 | $tool->load(); |
| 187 | |
| 188 | // submit |
| 189 | if ( acf_verify_nonce( $tool->name ) ) { |
| 190 | $tool->submit(); |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | |
| 196 | /** |
| 197 | * Admin Tools html |
| 198 | * |
| 199 | * @date 10/10/17 |
| 200 | * @since ACF 5.6.3 |
| 201 | * |
| 202 | * @return void |
| 203 | */ |
| 204 | public function html() { |
| 205 | |
| 206 | // vars |
| 207 | $screen = get_current_screen(); |
| 208 | $active = acf_maybe_get_GET( 'tool' ); |
| 209 | |
| 210 | // view |
| 211 | $view = array( |
| 212 | 'screen_id' => $screen->id, |
| 213 | 'active' => $active, |
| 214 | ); |
| 215 | |
| 216 | // register metaboxes |
| 217 | foreach ( $this->get_tools() as $tool ) { |
| 218 | |
| 219 | // check active |
| 220 | if ( $active && $active !== $tool->name ) { |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | // add metabox |
| 225 | 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 ) ); |
| 226 | } |
| 227 | |
| 228 | // view |
| 229 | acf_get_view( 'tools/tools', $view ); |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /** |
| 234 | * Output the metabox HTML for specific tools |
| 235 | * |
| 236 | * @since ACF 5.6.3 |
| 237 | * |
| 238 | * @param mixed $post The post this metabox is being displayed on, should be an empty string always for us on a tools page. |
| 239 | * @param array $metabox An array of the metabox attributes. |
| 240 | */ |
| 241 | public function metabox_html( $post, $metabox ) { |
| 242 | $tool = $this->get_tool( $metabox['args']['tool'] ); |
| 243 | $form_attrs = array( 'method' => 'post' ); |
| 244 | |
| 245 | if ( 'import' === $metabox['args']['tool'] ) { |
| 246 | $form_attrs['onsubmit'] = 'acf.disableForm(event)'; |
| 247 | } |
| 248 | |
| 249 | printf( '<form %s>', acf_esc_attrs( $form_attrs ) ); |
| 250 | $tool->html(); |
| 251 | acf_nonce_input( $tool->name ); |
| 252 | echo '</form>'; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // initialize |
| 257 | acf()->admin_tools = new acf_admin_tools(); |
| 258 | endif; // class_exists check |
| 259 | |
| 260 | |
| 261 | /** |
| 262 | * Alias of acf()->admin_tools->register_tool() |
| 263 | * |
| 264 | * @type function |
| 265 | * @date 31/5/17 |
| 266 | * @since ACF 5.6.0 |
| 267 | * |
| 268 | * @param ACF_Admin_Tool $class The tool class. |
| 269 | * @return void |
| 270 | */ |
| 271 | function acf_register_admin_tool( $class ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.classFound |
| 272 | acf()->admin_tools->register_tool( $class ); |
| 273 | } |
| 274 | |
| 275 | |
| 276 | /** |
| 277 | * |
| 278 | * This function will return the admin URL to the tools page |
| 279 | * |
| 280 | * @type function |
| 281 | * @date 31/5/17 |
| 282 | * @since ACF 5.6.0 |
| 283 | * |
| 284 | * @return string The URL to the tools page. |
| 285 | */ |
| 286 | function acf_get_admin_tools_url() { |
| 287 | |
| 288 | return admin_url( 'edit.php?post_type=acf-field-group&page=acf-tools' ); |
| 289 | } |
| 290 | |
| 291 | |
| 292 | /** |
| 293 | * This function will return the admin URL to the tools page |
| 294 | * |
| 295 | * @type function |
| 296 | * @date 31/5/17 |
| 297 | * @since ACF 5.6.0 |
| 298 | * |
| 299 | * @param string $tool The tool name. |
| 300 | * @return string The URL to a particular tool's page. |
| 301 | */ |
| 302 | function acf_get_admin_tool_url( $tool = '' ) { |
| 303 | |
| 304 | return acf_get_admin_tools_url() . '&tool=' . $tool; |
| 305 | } |
| 306 |