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