Skip to content

Commit

Permalink
Merge pull request #537 from equalizedigital/william/467/make-meta-bo…
Browse files Browse the repository at this point in the history
…xes-functions-into-a-class

Convert editor meta boxes function into a class
  • Loading branch information
pattonwebz authored Mar 18, 2024
2 parents 8b3485e + 481da0b commit 9265064
Show file tree
Hide file tree
Showing 7 changed files with 185 additions and 45 deletions.
2 changes: 0 additions & 2 deletions accessibility-checker.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,11 @@
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';
/**
* 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' );
Expand Down
22 changes: 18 additions & 4 deletions admin/class-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,29 @@
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;
}

/**
* Initialize.
*
* @return void
*/
public function init() {
public function init(): void {

$update_database = new Update_Database();
$update_database->init_hooks();
Expand All @@ -44,14 +56,16 @@ public function init() {
$site_health_info->init_hooks();

$this->init_ajax();

$this->meta_boxes->init_hooks();
}

/**
* Initialize ajax.
*
* @return void
*/
private function init_ajax() {
private function init_ajax(): void {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) {
return;
}
Expand Down
61 changes: 61 additions & 0 deletions admin/class-meta-boxes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
/**
* Class for handling the editor meta box.
*
* @package Accessibility_Checker
*/

namespace EDAC\Admin;

/**
* Class Meta_Box
*
* Handles the editor meta box registration and rendering.
*/
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.
*
* @since 1.10.0
*
* @return void
*/
public function register_meta_boxes(): void {
$post_types = get_option( 'edac_post_types' );
if ( $post_types ) {
foreach ( $post_types as $post_type ) {
add_meta_box(
'edac-meta-box',
__( 'Accessibility Checker', 'accessibility-checker' ),
array( $this, 'render' ),
$post_type,
'normal',
'high'
);
}
}
}

/**
* Render the custom meta box html.
*
* @since 1.10.0
*
* @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' );
}
}
8 changes: 5 additions & 3 deletions includes/classes/class-plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace EDAC\Inc;

use EDAC\Admin\Admin;
use EDAC\Admin\Meta_Boxes;

/**
* Main plugin functionality class.
Expand All @@ -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();
Expand All @@ -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();

Expand Down
24 changes: 24 additions & 0 deletions includes/deprecated/deprecated.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ function edac_delete_cpt_posts( $post_type ) {
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();
}

/**
* Insert rule date into database
*
Expand Down
36 changes: 0 additions & 36 deletions includes/meta-boxes.php

This file was deleted.

77 changes: 77 additions & 0 deletions tests/phpunit/Admin/MetaBoxesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/**
* Test class for the editor metabox.
*
* @package Accessibility_Checker
*/

use EDAC\Admin\Admin;
use EDAC\Admin\Meta_Boxes;

/**
* Tests for the editor meta box class.
*/
class MetaBoxesTest extends WP_UnitTestCase {

/**
* Test the hooks are added only in admin.
*
* @return void
*/
public function test_meta_boxes_are_registered_in_admin(): void {

$meta_boxes = $this->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( '/^<div id="edac-tabs">/' );
}

/**
* 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();
}
}

0 comments on commit 9265064

Please sign in to comment.