From f592a3a4fa4e9ddcb1b05c5851d1619241b038ff Mon Sep 17 00:00:00 2001 From: William Patton Date: Mon, 18 Mar 2024 12:53:03 +0000 Subject: [PATCH 1/4] Make a new class for the editor metabox --- admin/class-meta-boxes.php | 50 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 admin/class-meta-boxes.php diff --git a/admin/class-meta-boxes.php b/admin/class-meta-boxes.php new file mode 100644 index 00000000..5a54a6d0 --- /dev/null +++ b/admin/class-meta-boxes.php @@ -0,0 +1,50 @@ + Date: Mon, 18 Mar 2024 12:54:22 +0000 Subject: [PATCH 2/4] Add an `init_hooks` to metabox class and add 2 actions before and after the render --- admin/class-meta-boxes.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/admin/class-meta-boxes.php b/admin/class-meta-boxes.php index 5a54a6d0..3f724d91 100644 --- a/admin/class-meta-boxes.php +++ b/admin/class-meta-boxes.php @@ -14,6 +14,15 @@ */ class Meta_Boxes { + /** + * Initialize the hooks for the editor meta box. + * + * @return void + */ + public function init_hooks(): void { + add_action( 'add_meta_boxes', array( $this, 'register_meta_boxes' ) ); + } + /** * Register custom meta boxes for each post type. * @@ -45,6 +54,8 @@ public function register_meta_boxes(): void { * @return void */ public function render(): void { + do_action( 'edac_before_meta_box' ); include_once plugin_dir_path( __DIR__ ) . 'partials/custom-meta-box.php'; + do_action( 'edac_after_meta_box' ); } } From 0f6eff87043d29f3df5cdd37b6f08623b3286470 Mon Sep 17 00:00:00 2001 From: William Patton Date: Mon, 18 Mar 2024 12:55:05 +0000 Subject: [PATCH 3/4] Remove the old meta-boxes functions and flagthem deprecated --- accessibility-checker.php | 2 -- includes/deprecated/deprecated.php | 24 ++++++++++++++++++++ includes/meta-boxes.php | 36 ------------------------------ 3 files changed, 24 insertions(+), 38 deletions(-) delete mode 100644 includes/meta-boxes.php diff --git a/accessibility-checker.php b/accessibility-checker.php index f5506594..299d7ee4 100755 --- a/accessibility-checker.php +++ b/accessibility-checker.php @@ -123,7 +123,6 @@ require_once plugin_dir_path( __FILE__ ) . 'includes/activation.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/deactivation.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/helper-functions.php'; -require_once plugin_dir_path( __FILE__ ) . 'includes/meta-boxes.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/options-page.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/validate.php'; require_once plugin_dir_path( __FILE__ ) . 'includes/insert.php'; @@ -131,7 +130,6 @@ /** * Filters and Actions */ -add_action( 'add_meta_boxes', 'edac_register_meta_boxes' ); add_action( 'admin_menu', 'edac_add_options_page' ); add_action( 'admin_init', 'edac_register_setting' ); add_action( 'admin_head', 'edac_post_on_load' ); diff --git a/includes/deprecated/deprecated.php b/includes/deprecated/deprecated.php index cb8902e1..8bb6feee 100644 --- a/includes/deprecated/deprecated.php +++ b/includes/deprecated/deprecated.php @@ -73,3 +73,27 @@ function edac_delete_cpt_posts( $post_type ) { _deprecated_function( __FUNCTION__, '1.10.0', 'EDAC\Admin\Purge_Post_Data::delete_cpt_posts' ); Purge_Post_Data::delete_cpt_posts( $post_type ); } + +/** + * Register custom meta boxes + * + * @deprecated 1.10.0 + * + * @return void + */ +function edac_register_meta_boxes() { + _deprecated_function( __FUNCTION__, '1.10.0', 'EDAC\Admin\Meta_Boxes::register_meta_boxes' ); + ( new EDAC\Admin\Meta_Boxes() )->register_meta_boxes(); +} + +/** + * Render the custom meta box html + * + * @deprecated 1.10.0 + * + * @return void + */ +function edac_custom_meta_box_cb() { + _deprecated_function( __FUNCTION__, '1.10.0', 'EDAC\Admin\Meta_Boxes::render' ); + ( new EDAC\Admin\Meta_Boxes() )->render(); +} diff --git a/includes/meta-boxes.php b/includes/meta-boxes.php deleted file mode 100644 index 470023b9..00000000 --- a/includes/meta-boxes.php +++ /dev/null @@ -1,36 +0,0 @@ - Date: Mon, 18 Mar 2024 13:04:15 +0000 Subject: [PATCH 4/4] Use Dependency Injection to register the editor metabox inside the Admin class --- admin/class-admin.php | 22 ++++++-- includes/classes/class-plugin.php | 8 +-- tests/phpunit/Admin/MetaBoxesTest.php | 77 +++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 7 deletions(-) create mode 100644 tests/phpunit/Admin/MetaBoxesTest.php diff --git a/admin/class-admin.php b/admin/class-admin.php index 0fce92c2..c1b05bbb 100644 --- a/admin/class-admin.php +++ b/admin/class-admin.php @@ -16,9 +16,21 @@ class Admin { /** - * Class constructor. + * Meta boxes instance. + * + * @since 1.10.0 + * + * @var Meta_Boxes */ - public function __construct() { + private Meta_Boxes $meta_boxes; + + /** + * Class constructor for injecting dependencies. + * + * @param Meta_Boxes|null $meta_boxes Meta boxes instance. + */ + public function __construct( Meta_Boxes $meta_boxes = null ) { + $this->meta_boxes = $meta_boxes; } /** @@ -26,7 +38,7 @@ public function __construct() { * * @return void */ - public function init() { + public function init(): void { $update_database = new Update_Database(); $update_database->init_hooks(); @@ -44,6 +56,8 @@ public function init() { $site_health_info->init_hooks(); $this->init_ajax(); + + $this->meta_boxes->init_hooks(); } /** @@ -51,7 +65,7 @@ public function init() { * * @return void */ - private function init_ajax() { + private function init_ajax(): void { if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) { return; } diff --git a/includes/classes/class-plugin.php b/includes/classes/class-plugin.php index 9108e268..b2ea553d 100644 --- a/includes/classes/class-plugin.php +++ b/includes/classes/class-plugin.php @@ -8,6 +8,7 @@ namespace EDAC\Inc; use EDAC\Admin\Admin; +use EDAC\Admin\Meta_Boxes; /** * Main plugin functionality class. @@ -19,7 +20,8 @@ class Plugin { */ public function __construct() { if ( \is_admin() ) { - $admin = new Admin(); + $meta_boxes = new Meta_Boxes(); + $admin = new Admin( $meta_boxes ); $admin->init(); } else { $this->init(); @@ -36,9 +38,9 @@ public function __construct() { * @return void */ private function init() { - + add_action( 'wp_enqueue_scripts', array( 'EDAC\Inc\Enqueue_Frontend', 'enqueue' ) ); - + $accessibility_statement = new Accessibility_Statement(); $accessibility_statement->init_hooks(); diff --git a/tests/phpunit/Admin/MetaBoxesTest.php b/tests/phpunit/Admin/MetaBoxesTest.php new file mode 100644 index 00000000..fbab350a --- /dev/null +++ b/tests/phpunit/Admin/MetaBoxesTest.php @@ -0,0 +1,77 @@ +getMockBuilder( Meta_Boxes::class ) + ->onlyMethods( array( 'register_meta_boxes' ) ) + ->getMock(); + + $meta_boxes->expects( $this->once() ) + ->method( 'register_meta_boxes' ); + + $this->invoke_admin_init( $meta_boxes ); + do_action( 'add_meta_boxes' ); + } + + /** + * Test the init_hooks method. + * + * @return void + */ + public function test_init_hooks(): void { + $meta_boxes = new Meta_Boxes(); + $meta_boxes->init_hooks(); + + $this->assertEquals( + 10, + has_action( + 'add_meta_boxes', + array( + $meta_boxes, + 'register_meta_boxes', + ) + ) + ); + } + + /** + * Test the render method. + * + * @return void + */ + public function test_render(): void { + $meta_boxes = new Meta_Boxes(); + $meta_boxes->render(); + + $this->expectOutputRegex( '/^
/' ); + } + + /** + * Invoke the admin init method. + * + * @param Meta_Boxes $meta_boxes The metabox class. + * @return void + */ + private function invoke_admin_init( $meta_boxes ): void { + $admin = new Admin( $meta_boxes ); + $admin->init(); + } +}