-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated plugin requirements and refactored codebase
- [Added]: New paths in the release workflow file - [Added]: Additional properties and methods to Base_Metabox class for better flexibility - [Changed]: Updated minimum PHP version requirement to 8.0.0 - [Changed]: Updated minimum WordPress version requirement to 4.0 - [Changed]: Refactored how metaboxes are registered and saved, improving code clarity - [Changed]: Renamed keys for shortcodes, taxonomies, and custom post types using a configurable slug - [Changed]: Replaced specific metabox class with a more generic example - [Fixed]: Incorrect author information in several files - [Removed]: Unused function imports across multiple files
- Loading branch information
Showing
15 changed files
with
311 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -64,7 +64,7 @@ class Admin_Notifications { | |
* reviewers: phpcs:ignore is used deterministically because GitHub WPCS Workflow fails otherwise. | ||
* | ||
* @since 1.0.0 Introduced on 2023-08-02 15:14 | ||
* @author Beda Schmid <[email protected]> | ||
* @author Your Name <[email protected]> | ||
* @return void | ||
*/ | ||
public static function print_requirement_notice(): void { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
namespace Company\Plugins\PluginName\Registerables\Metaboxes; | ||
|
||
use Company\Plugins\PluginName\Registerables\Base_WP_Registerable; | ||
use Company\Plugins\PluginName\Core\Config; | ||
|
||
/** | ||
* Exit the code if this file is accessed directly | ||
|
@@ -60,6 +61,32 @@ abstract class Base_Metabox extends Base_WP_Registerable { | |
*/ | ||
protected array $post_types = array(); | ||
|
||
/** | ||
* Context to add metabox | ||
* | ||
* Possible values are 'normal', 'side', and 'advanced' | ||
* | ||
* @see https://developer.wordpress.org/reference/functions/add_meta_box/ | ||
* @since 1.0.0 Introduced on 2023-08-02 14:14 | ||
* @author Your Name <[email protected]> | ||
* @access private | ||
* @var array $context Associativ array of the context to add the metabox to, keyed by post type, value the context. | ||
*/ | ||
protected array $context = array(); | ||
|
||
/** | ||
* Priority to add metabox | ||
* | ||
* Possible values are 'high', 'core', 'default', or 'low' | ||
* | ||
* @see https://developer.wordpress.org/reference/functions/add_meta_box/ | ||
* @since 1.0.0 Introduced on 2023-08-02 14:14 | ||
* @author Your Name <[email protected]> | ||
* @access private | ||
* @var array $priority Associativ array of the context to add the metabox to, keyed by post type, value the context. | ||
*/ | ||
protected array $priority = array(); | ||
|
||
/** | ||
* User Roles to assign metabox | ||
* | ||
|
@@ -80,25 +107,61 @@ abstract class Base_Metabox extends Base_WP_Registerable { | |
*/ | ||
protected array $taxonomies = array(); | ||
|
||
/** | ||
* Abstract method to initialise properties. | ||
* | ||
* Provides an abstract method to initialise meta box properties. | ||
* | ||
* @since 1.0.0 Introduced 2024-05-28 17:37 | ||
* @author Your Name <[email protected]> | ||
* @return void | ||
*/ | ||
abstract protected function initialize_properties(): void; | ||
|
||
/** | ||
* Abstract method to sanitize the POSTed data. | ||
* | ||
* Provides an abstract method to sanitize the posted data before saving it. | ||
* | ||
* @since 1.0.0 Introduced 2024-05-28 17:37 | ||
* @author Your Name <[email protected]> | ||
* @param int|string|array $data The POSTed data. | ||
* @return int|array|string | ||
*/ | ||
abstract protected function sanitize_meta_box_data( int|string|array $data ): int|array|string; | ||
|
||
/** | ||
* Abstract method to set callback args. | ||
* | ||
* Provides an abstract method to set the add_meta_box() second argument data array. | ||
* | ||
* @since 1.0.0 Introduced 2024-05-28 17:37 | ||
* @author Your Name <[email protected]> | ||
* @return array | ||
*/ | ||
abstract protected function set_args(): array; | ||
|
||
/** | ||
* Abstract method to render metabox | ||
* | ||
* Provides an abstract to render the metabox | ||
* | ||
* @since 1.0.0 Introduced on 2023-10-08 17:09 | ||
* @see https://developer.wordpress.org/reference/functions/add_meta_box/#parameters | ||
* @author Beda Schmid <[email protected]> | ||
* @author Your Name <[email protected]> | ||
* @param object $obj The Current object to which the metabox is added. | ||
* @param array $args The Arguments passed to the callback. | ||
* @return array | ||
*/ | ||
abstract public function render(): void; | ||
abstract public function render( object $obj = new \stdClass(), array $args = array() ): void; | ||
|
||
/** | ||
* Validate the key | ||
* | ||
* Validate $key for get_key(). | ||
* | ||
* @since 1.0.0 Introduced on 2023-10-08 16:30 | ||
* @author Beda Schmid <[email protected]> | ||
* @author Your Name <[email protected]> | ||
* @throws \LengthException If the key length validation fails. | ||
* @throws \UnexpectedValueException If the key sanitization fails. | ||
* @return string The validated string. | ||
|
@@ -119,11 +182,23 @@ protected function validate_key(): string { | |
* | ||
* @since 1.0.0 Introduced on 2023-10-08 17:09 | ||
* @see https://developer.wordpress.org/reference/functions/add_meta_box/ | ||
* @author Beda Schmid <[email protected]> | ||
* @author Your Name <[email protected]> | ||
* @return void | ||
*/ | ||
public function register(): void { | ||
$this->initialize_properties(); | ||
$this->register_metaboxes(); | ||
$this->register_save_hooks(); | ||
} | ||
|
||
/** | ||
* Register the metaboxes with WordPress | ||
* | ||
* @since 1.0.0 Introduced 2024-05-29 14:54 | ||
* @author Your Name <[email protected]> | ||
* @return void | ||
*/ | ||
protected function register_metaboxes(): void { | ||
/** | ||
* Register for Post types if any | ||
*/ | ||
|
@@ -149,26 +224,78 @@ public function register(): void { | |
} | ||
|
||
/** | ||
* Callback for Post Types | ||
* Register the saving hooks with WordPress | ||
* | ||
* @since 1.0.0 Introduced 2024-05-29 14:55 | ||
* @author Your Name <[email protected]> | ||
* @return void | ||
*/ | ||
protected function register_save_hooks(): void { | ||
/** | ||
* Register for Post types if any | ||
*/ | ||
if ( ! empty( $this->post_types ) ) { | ||
add_action( 'save_post', array( $this, 'save_custom_post_meta_box_data' ) ); | ||
} | ||
} | ||
|
||
/** | ||
* Callback for Post Types metabox add action | ||
* | ||
* True metaboxes are only available on Post Types. | ||
* | ||
* @since 1.0.0 Introduced on 2023-10-08 17:09 | ||
* @see https://developer.wordpress.org/reference/functions/add_meta_box/#parameters | ||
* @author Beda Schmid <[email protected]> | ||
* @author Your Name <[email protected]> | ||
* @return void | ||
*/ | ||
public function register_metaboxes_for_post_types(): void { | ||
foreach ( $this->post_types as $post_type ) { | ||
add_meta_box( | ||
$this->get_key() . '_' . $post_type, | ||
ucwords( str_replace( '_', ' ', $this->get_key() ) ), | ||
ucwords( str_replace( '-', ' ', str_replace( Config::get( 'slug' ), '', $this->get_key() ) ) ), | ||
array( $this, 'render' ), | ||
$post_type, | ||
'side', | ||
'high', | ||
null | ||
array_key_exists( $post_type, $this->context ) ? $this->context[ $post_type ] : 'advanced', | ||
array_key_exists( $post_type, $this->priority ) ? $this->context[ $post_type ] : 'default', | ||
$this->set_args() | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* Callback for Post Types saving action | ||
* | ||
* Saves the post metabox value to the database. | ||
* | ||
* @since 1.0.0 Introduced 2024-05-28 17:14 | ||
* @author Your Name <[email protected]> | ||
* @param int $post_id The Post ID being saved. | ||
* @return void | ||
*/ | ||
public function save_custom_post_meta_box_data( int $post_id = 0 ): void { | ||
|
||
foreach ( $this->post_types as $post_type ) { | ||
|
||
if ( ! isset( $_POST['_wpnonce'] ) | ||
|| ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'update-post_' . $post_id ) | ||
|| ( defined( 'DOING_AUTOSAVE' ) | ||
&& DOING_AUTOSAVE | ||
) | ||
|| ! isset( $_POST['post_type'] ) | ||
|| $post_type !== $_POST['post_type'] | ||
|| ! current_user_can( 'edit_post', $post_id ) | ||
) { | ||
return; | ||
} | ||
|
||
if ( isset( $_POST[ $this->get_key() ] ) ) { | ||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | ||
$sanitized_data = $this->sanitize_meta_box_data( wp_unslash( $_POST[ $this->get_key() ] ) ); | ||
update_post_meta( $post_id, $this->get_key(), $sanitized_data ); | ||
} else { | ||
delete_post_meta( $post_id, $this->get_key() ); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.