Skip to content

Commit

Permalink
Updated plugin requirements and refactored codebase
Browse files Browse the repository at this point in the history
- [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
smileBeda committed May 29, 2024
1 parent 18afb63 commit ea59b43
Show file tree
Hide file tree
Showing 15 changed files with 311 additions and 142 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ jobs:
cp -R src/* $REPO_NAME/
sed -i 's|/src/src/|/src/|g' $REPO_NAME/vendor/composer/autoload_classmap.php
sed -i 's|/src/src/|/src/|g' $REPO_NAME/vendor/composer/autoload_static.php
sed -i 's|/../../..|/../..|g' $REPO_NAME/vendor/composer/autoload_static.php
zip -r $REPO_NAME.zip $REPO_NAME/
- name: Get the version
Expand Down
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
# Changelog

## 2024-05-29
- [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

## ## 2024-05-26
- [Fixed] Properly initialise WP_Filesystem
- [Fixed] Use HTML file extension for resouurce files
Expand Down
19 changes: 6 additions & 13 deletions src/plugin-name.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
* Description: This is a short description of what the plugin does. It's displayed in the WordPress admin area.
* Version: 1.0.0
* Author: Ideally, your WordPress forum username.
* Requires at least: X.X
* Requires PHP: X.X
* Requires at least: 4.0
* Requires PHP: 8.0.0
* Tested up to: X.X
* Author URI: https://site.tld/author-name-uri/
* License: GPL-3.0-or-later
Expand All @@ -56,13 +56,6 @@
namespace Company\Plugins\PluginName;

use Company\Plugins\PluginName\Core\{Config, Requirements, Lifecycle};
use function add_action;
use function deactivate_plugins;
use function current_user_can;
use function plugin_basename;
use function register_activation_hook;
use function register_deactivation_hook;
use function register_uninstall_hook;

/**
* Exit the code if this file is accessed directly
Expand Down Expand Up @@ -98,7 +91,7 @@
Config::init(
array(
'version' => '1.0.0',
'requires_php' => '7.4.0',
'requires_php' => '8.0.0',
'requires_os' => array(
'wp' => '6.0.0',
'cp' => '1.0.0',
Expand Down Expand Up @@ -134,8 +127,8 @@

if ( ! $requirements->met() ) {

require_once \ABSPATH . 'wp-includes/capabilities.php';
require_once \ABSPATH . 'wp-includes/pluggable.php';
require_once ABSPATH . 'wp-includes/capabilities.php';
require_once ABSPATH . 'wp-includes/pluggable.php';
if ( current_user_can( 'deactivate_plugins' ) ) {

unset( $_GET['activate'] );
Expand All @@ -147,7 +140,7 @@
)
);

require_once \ABSPATH . 'wp-admin/includes/plugin.php';
require_once ABSPATH . 'wp-admin/includes/plugin.php';
deactivate_plugins( array( Config::get( 'base_name' ) ), true );

}
Expand Down
2 changes: 1 addition & 1 deletion src/readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Contributors: user.a,user.b
Donate link: https://domain.tld/donate
Tags: tag-1,tag-2
Requires at least: X.X
Requires at least: 4.0
Tested up to: X.X
Stable tag: 1.0.0
License: GPL-3.0-or-later
Expand Down
2 changes: 1 addition & 1 deletion src/src/admin/class-admin-notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
namespace Company\Plugins\PluginName;

use Company\Plugins\PluginName\Registerables\CPT\Item_CPT;
use Company\Plugins\PluginName\Registerables\MetaBoxes\Image_Upload_Metabox;
use Company\Plugins\PluginName\Registerables\MetaBoxes\Example_Metabox;
use Company\Plugins\PluginName\Registerables\Taxonomies\Collection_Taxonomy;
use Company\Plugins\PluginName\Registerables\Shortcodes\Example_Shortcode;

Expand All @@ -41,6 +41,6 @@
* Initialise all services
*/
( new Item_CPT() )->register();
( new Image_Upload_Metabox() )->register();
( new Example_Metabox() )->register();
( new Collection_Taxonomy() )->register();
( new Example_Shortcode() )->register();
1 change: 0 additions & 1 deletion src/src/core/class-lifecycle.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
namespace Company\Plugins\PluginName\Core;

use Company\Plugins\PluginName\Core\Config;
use function current_user_can;

/**
* Exit the code if this file is accessed directly
Expand Down
7 changes: 0 additions & 7 deletions src/src/core/class-requirements.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,6 @@
*/
namespace Company\Plugins\PluginName\Core;

use function get_bloginfo;
use function get_option;
use function get_site_option;
use function get_template;
use function is_multisite;
use function is_plugin_active;

/**
* Exit the code if this file is accessed directly
*/
Expand Down
4 changes: 2 additions & 2 deletions src/src/registerables/cpt/class-item-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ final class Item_CPT extends Base_CPT {
* @return void
*/
protected function set_key(): void {
$this->key = 'item';
$this->key = Config::get( 'slug' ) . '-item';
}

/**
Expand All @@ -78,7 +78,7 @@ protected function set_key(): void {
protected function set_specific_args(): array {
return array(
'menu_position' => 1,
'taxonomies' => array( 'collection' ),
'taxonomies' => array( Config::get( 'slug' ) . '-collection' ),
'menu_icon' => 'data:image/svg+xml;base64,' . ( new WP_Filesystem_Utility() )
->get_base64_encoded_contents( Config::get( 'plugin_dir' ) . 'public/icons/cpt-icon.svg' ),
);
Expand Down
147 changes: 137 additions & 10 deletions src/src/registerables/metaboxes/class-base-metabox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
*
Expand All @@ -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.
Expand All @@ -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
*/
Expand All @@ -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() );
}
}
}
}
Loading

0 comments on commit ea59b43

Please sign in to comment.