From 831f2a61f8798190aea58cc7df0622316f3753f5 Mon Sep 17 00:00:00 2001 From: albert Date: Thu, 29 Dec 2022 17:12:44 +0100 Subject: [PATCH 1/2] composer refact --- .gitignore | 4 + plugin-name/admin/class-plugin-name-admin.php | 103 --------- plugin-name/admin/index.php | 1 - plugin-name/composer.json | 46 ++++ .../includes/class-plugin-name-loader.php | 129 ----------- plugin-name/includes/class-plugin-name.php | 218 ------------------ plugin-name/includes/index.php | 1 - plugin-name/phpunit.xml | 26 +++ plugin-name/plugin-name.php | 55 +++-- .../public/class-plugin-name-public.php | 103 --------- plugin-name/public/index.php | 1 - plugin-name/src/admin/PluginNameAdmin.php | 101 ++++++++ .../{ => src}/admin/css/plugin-name-admin.css | 0 plugin-name/src/admin/index.php | 1 + .../{ => src}/admin/js/plugin-name-admin.js | 0 .../partials/plugin-name-admin-display.php | 0 .../includes/PluginNameActivator.php} | 26 ++- .../includes/PluginNameDeactivator.php} | 26 ++- plugin-name/src/includes/PluginNameEngine.php | 200 ++++++++++++++++ .../includes/PluginNameI18.php} | 37 ++- plugin-name/src/includes/PluginNameLoader.php | 130 +++++++++++ plugin-name/src/includes/index.php | 1 + .../{ => src}/languages/plugin-name.pot | 0 plugin-name/src/public/PluginNamePublic.php | 101 ++++++++ .../public/css/plugin-name-public.css | 0 plugin-name/src/public/index.php | 1 + .../{ => src}/public/js/plugin-name-public.js | 0 .../partials/plugin-name-public-display.php | 0 plugin-name/tests/unit/.gitkeep | 0 plugin-name/uninstall.php | 4 +- 30 files changed, 691 insertions(+), 624 deletions(-) delete mode 100644 plugin-name/admin/class-plugin-name-admin.php delete mode 100644 plugin-name/admin/index.php create mode 100644 plugin-name/composer.json delete mode 100644 plugin-name/includes/class-plugin-name-loader.php delete mode 100644 plugin-name/includes/class-plugin-name.php delete mode 100644 plugin-name/includes/index.php create mode 100644 plugin-name/phpunit.xml delete mode 100644 plugin-name/public/class-plugin-name-public.php delete mode 100644 plugin-name/public/index.php create mode 100644 plugin-name/src/admin/PluginNameAdmin.php rename plugin-name/{ => src}/admin/css/plugin-name-admin.css (100%) create mode 100644 plugin-name/src/admin/index.php rename plugin-name/{ => src}/admin/js/plugin-name-admin.js (100%) rename plugin-name/{ => src}/admin/partials/plugin-name-admin-display.php (100%) rename plugin-name/{includes/class-plugin-name-activator.php => src/includes/PluginNameActivator.php} (65%) rename plugin-name/{includes/class-plugin-name-deactivator.php => src/includes/PluginNameDeactivator.php} (65%) create mode 100644 plugin-name/src/includes/PluginNameEngine.php rename plugin-name/{includes/class-plugin-name-i18n.php => src/includes/PluginNameI18.php} (62%) create mode 100644 plugin-name/src/includes/PluginNameLoader.php create mode 100644 plugin-name/src/includes/index.php rename plugin-name/{ => src}/languages/plugin-name.pot (100%) create mode 100644 plugin-name/src/public/PluginNamePublic.php rename plugin-name/{ => src}/public/css/plugin-name-public.css (100%) create mode 100644 plugin-name/src/public/index.php rename plugin-name/{ => src}/public/js/plugin-name-public.js (100%) rename plugin-name/{ => src}/public/partials/plugin-name-public-display.php (100%) create mode 100644 plugin-name/tests/unit/.gitkeep diff --git a/.gitignore b/.gitignore index 3c5a4e28b..5f862b0b9 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,7 @@ dwsync.xml intermediate .idea cache + +# composer +vendor/ +composer.lock \ No newline at end of file diff --git a/plugin-name/admin/class-plugin-name-admin.php b/plugin-name/admin/class-plugin-name-admin.php deleted file mode 100644 index 3ef8d607a..000000000 --- a/plugin-name/admin/class-plugin-name-admin.php +++ /dev/null @@ -1,103 +0,0 @@ - - */ -class Plugin_Name_Admin { - - /** - * The ID of this plugin. - * - * @since 1.0.0 - * @access private - * @var string $plugin_name The ID of this plugin. - */ - private $plugin_name; - - /** - * The version of this plugin. - * - * @since 1.0.0 - * @access private - * @var string $version The current version of this plugin. - */ - private $version; - - /** - * Initialize the class and set its properties. - * - * @since 1.0.0 - * @param string $plugin_name The name of this plugin. - * @param string $version The version of this plugin. - */ - public function __construct( $plugin_name, $version ) { - - $this->plugin_name = $plugin_name; - $this->version = $version; - - } - - /** - * Register the stylesheets for the admin area. - * - * @since 1.0.0 - */ - public function enqueue_styles() { - - /** - * This function is provided for demonstration purposes only. - * - * An instance of this class should be passed to the run() function - * defined in Plugin_Name_Loader as all of the hooks are defined - * in that particular class. - * - * The Plugin_Name_Loader will then create the relationship - * between the defined hooks and the functions defined in this - * class. - */ - - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/plugin-name-admin.css', array(), $this->version, 'all' ); - - } - - /** - * Register the JavaScript for the admin area. - * - * @since 1.0.0 - */ - public function enqueue_scripts() { - - /** - * This function is provided for demonstration purposes only. - * - * An instance of this class should be passed to the run() function - * defined in Plugin_Name_Loader as all of the hooks are defined - * in that particular class. - * - * The Plugin_Name_Loader will then create the relationship - * between the defined hooks and the functions defined in this - * class. - */ - - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/plugin-name-admin.js', array( 'jquery' ), $this->version, false ); - - } - -} diff --git a/plugin-name/admin/index.php b/plugin-name/admin/index.php deleted file mode 100644 index e71af0ef2..000000000 --- a/plugin-name/admin/index.php +++ /dev/null @@ -1 +0,0 @@ - - */ -class Plugin_Name_Loader { - - /** - * The array of actions registered with WordPress. - * - * @since 1.0.0 - * @access protected - * @var array $actions The actions registered with WordPress to fire when the plugin loads. - */ - protected $actions; - - /** - * The array of filters registered with WordPress. - * - * @since 1.0.0 - * @access protected - * @var array $filters The filters registered with WordPress to fire when the plugin loads. - */ - protected $filters; - - /** - * Initialize the collections used to maintain the actions and filters. - * - * @since 1.0.0 - */ - public function __construct() { - - $this->actions = array(); - $this->filters = array(); - - } - - /** - * Add a new action to the collection to be registered with WordPress. - * - * @since 1.0.0 - * @param string $hook The name of the WordPress action that is being registered. - * @param object $component A reference to the instance of the object on which the action is defined. - * @param string $callback The name of the function definition on the $component. - * @param int $priority Optional. The priority at which the function should be fired. Default is 10. - * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. - */ - public function add_action( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { - $this->actions = $this->add( $this->actions, $hook, $component, $callback, $priority, $accepted_args ); - } - - /** - * Add a new filter to the collection to be registered with WordPress. - * - * @since 1.0.0 - * @param string $hook The name of the WordPress filter that is being registered. - * @param object $component A reference to the instance of the object on which the filter is defined. - * @param string $callback The name of the function definition on the $component. - * @param int $priority Optional. The priority at which the function should be fired. Default is 10. - * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 - */ - public function add_filter( $hook, $component, $callback, $priority = 10, $accepted_args = 1 ) { - $this->filters = $this->add( $this->filters, $hook, $component, $callback, $priority, $accepted_args ); - } - - /** - * A utility function that is used to register the actions and hooks into a single - * collection. - * - * @since 1.0.0 - * @access private - * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). - * @param string $hook The name of the WordPress filter that is being registered. - * @param object $component A reference to the instance of the object on which the filter is defined. - * @param string $callback The name of the function definition on the $component. - * @param int $priority The priority at which the function should be fired. - * @param int $accepted_args The number of arguments that should be passed to the $callback. - * @return array The collection of actions and filters registered with WordPress. - */ - private function add( $hooks, $hook, $component, $callback, $priority, $accepted_args ) { - - $hooks[] = array( - 'hook' => $hook, - 'component' => $component, - 'callback' => $callback, - 'priority' => $priority, - 'accepted_args' => $accepted_args - ); - - return $hooks; - - } - - /** - * Register the filters and actions with WordPress. - * - * @since 1.0.0 - */ - public function run() { - - foreach ( $this->filters as $hook ) { - add_filter( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); - } - - foreach ( $this->actions as $hook ) { - add_action( $hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args'] ); - } - - } - -} diff --git a/plugin-name/includes/class-plugin-name.php b/plugin-name/includes/class-plugin-name.php deleted file mode 100644 index 9906f3304..000000000 --- a/plugin-name/includes/class-plugin-name.php +++ /dev/null @@ -1,218 +0,0 @@ - - */ -class Plugin_Name { - - /** - * The loader that's responsible for maintaining and registering all hooks that power - * the plugin. - * - * @since 1.0.0 - * @access protected - * @var Plugin_Name_Loader $loader Maintains and registers all hooks for the plugin. - */ - protected $loader; - - /** - * The unique identifier of this plugin. - * - * @since 1.0.0 - * @access protected - * @var string $plugin_name The string used to uniquely identify this plugin. - */ - protected $plugin_name; - - /** - * The current version of the plugin. - * - * @since 1.0.0 - * @access protected - * @var string $version The current version of the plugin. - */ - protected $version; - - /** - * Define the core functionality of the plugin. - * - * Set the plugin name and the plugin version that can be used throughout the plugin. - * Load the dependencies, define the locale, and set the hooks for the admin area and - * the public-facing side of the site. - * - * @since 1.0.0 - */ - public function __construct() { - if ( defined( 'PLUGIN_NAME_VERSION' ) ) { - $this->version = PLUGIN_NAME_VERSION; - } else { - $this->version = '1.0.0'; - } - $this->plugin_name = 'plugin-name'; - - $this->load_dependencies(); - $this->set_locale(); - $this->define_admin_hooks(); - $this->define_public_hooks(); - - } - - /** - * Load the required dependencies for this plugin. - * - * Include the following files that make up the plugin: - * - * - Plugin_Name_Loader. Orchestrates the hooks of the plugin. - * - Plugin_Name_i18n. Defines internationalization functionality. - * - Plugin_Name_Admin. Defines all hooks for the admin area. - * - Plugin_Name_Public. Defines all hooks for the public side of the site. - * - * Create an instance of the loader which will be used to register the hooks - * with WordPress. - * - * @since 1.0.0 - * @access private - */ - private function load_dependencies() { - - /** - * The class responsible for orchestrating the actions and filters of the - * core plugin. - */ - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-plugin-name-loader.php'; - - /** - * The class responsible for defining internationalization functionality - * of the plugin. - */ - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'includes/class-plugin-name-i18n.php'; - - /** - * The class responsible for defining all actions that occur in the admin area. - */ - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-plugin-name-admin.php'; - - /** - * The class responsible for defining all actions that occur in the public-facing - * side of the site. - */ - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'public/class-plugin-name-public.php'; - - $this->loader = new Plugin_Name_Loader(); - - } - - /** - * Define the locale for this plugin for internationalization. - * - * Uses the Plugin_Name_i18n class in order to set the domain and to register the hook - * with WordPress. - * - * @since 1.0.0 - * @access private - */ - private function set_locale() { - - $plugin_i18n = new Plugin_Name_i18n(); - - $this->loader->add_action( 'plugins_loaded', $plugin_i18n, 'load_plugin_textdomain' ); - - } - - /** - * Register all of the hooks related to the admin area functionality - * of the plugin. - * - * @since 1.0.0 - * @access private - */ - private function define_admin_hooks() { - - $plugin_admin = new Plugin_Name_Admin( $this->get_plugin_name(), $this->get_version() ); - - $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_styles' ); - $this->loader->add_action( 'admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts' ); - - } - - /** - * Register all of the hooks related to the public-facing functionality - * of the plugin. - * - * @since 1.0.0 - * @access private - */ - private function define_public_hooks() { - - $plugin_public = new Plugin_Name_Public( $this->get_plugin_name(), $this->get_version() ); - - $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_styles' ); - $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); - - } - - /** - * Run the loader to execute all of the hooks with WordPress. - * - * @since 1.0.0 - */ - public function run() { - $this->loader->run(); - } - - /** - * The name of the plugin used to uniquely identify it within the context of - * WordPress and to define internationalization functionality. - * - * @since 1.0.0 - * @return string The name of the plugin. - */ - public function get_plugin_name() { - return $this->plugin_name; - } - - /** - * The reference to the class that orchestrates the hooks with the plugin. - * - * @since 1.0.0 - * @return Plugin_Name_Loader Orchestrates the hooks of the plugin. - */ - public function get_loader() { - return $this->loader; - } - - /** - * Retrieve the version number of the plugin. - * - * @since 1.0.0 - * @return string The version number of the plugin. - */ - public function get_version() { - return $this->version; - } - -} diff --git a/plugin-name/includes/index.php b/plugin-name/includes/index.php deleted file mode 100644 index e71af0ef2..000000000 --- a/plugin-name/includes/index.php +++ /dev/null @@ -1 +0,0 @@ - + + + + tests/unit + + + + + src + + + \ No newline at end of file diff --git a/plugin-name/plugin-name.php b/plugin-name/plugin-name.php index fbd8e6808..14ac1716d 100644 --- a/plugin-name/plugin-name.php +++ b/plugin-name/plugin-name.php @@ -25,44 +25,56 @@ * Domain Path: /languages */ +namespace PluginName; + +// check whether the WordPress environment loaded or not +if (!defined('ABSPATH')) { + exit('WordPress is not loaded.'); +} + // If this file is called directly, abort. -if ( ! defined( 'WPINC' ) ) { - die; +if (! defined('WPINC')) { + exit('No WordPress includes.'); } +// checking Composer autoloader +if (!is_readable(__DIR__ . '/vendor/autoload.php')) { + exit('No readable composer autoloader dependency.'); +} + +require_once __DIR__ . '/vendor/autoload.php'; + /** * Currently plugin version. * Start at version 1.0.0 and use SemVer - https://semver.org * Rename this for your plugin and update it as you release new versions. */ -define( 'PLUGIN_NAME_VERSION', '1.0.0' ); +define('PLUGIN_NAME_VERSION', '1.0.0'); + +use PluginName\Includes\PluginNameActivator; +use PluginName\Includes\PluginNameDeactivator; +use PluginName\Includes\PluginNameEngine; /** * The code that runs during plugin activation. * This action is documented in includes/class-plugin-name-activator.php */ -function activate_plugin_name() { - require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-activator.php'; - Plugin_Name_Activator::activate(); +function activate_plugin_name() +{ + PluginNameActivator::activate(); } /** * The code that runs during plugin deactivation. * This action is documented in includes/class-plugin-name-deactivator.php */ -function deactivate_plugin_name() { - require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-deactivator.php'; - Plugin_Name_Deactivator::deactivate(); +function deactivate_plugin_name() +{ + PluginNameDeactivator::deactivate(); } -register_activation_hook( __FILE__, 'activate_plugin_name' ); -register_deactivation_hook( __FILE__, 'deactivate_plugin_name' ); - -/** - * The core plugin class that is used to define internationalization, - * admin-specific hooks, and public-facing site hooks. - */ -require plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name.php'; +register_activation_hook(__FILE__, 'PluginName\activate_plugin_name'); +register_deactivation_hook(__FILE__, 'PluginName\deactivate_plugin_name'); /** * Begins execution of the plugin. @@ -73,10 +85,9 @@ function deactivate_plugin_name() { * * @since 1.0.0 */ -function run_plugin_name() { - - $plugin = new Plugin_Name(); - $plugin->run(); - +function run_plugin_name() +{ + $plugin = new PluginNameEngine; + $plugin->run(); } run_plugin_name(); diff --git a/plugin-name/public/class-plugin-name-public.php b/plugin-name/public/class-plugin-name-public.php deleted file mode 100644 index 8336a3ad1..000000000 --- a/plugin-name/public/class-plugin-name-public.php +++ /dev/null @@ -1,103 +0,0 @@ - - */ -class Plugin_Name_Public { - - /** - * The ID of this plugin. - * - * @since 1.0.0 - * @access private - * @var string $plugin_name The ID of this plugin. - */ - private $plugin_name; - - /** - * The version of this plugin. - * - * @since 1.0.0 - * @access private - * @var string $version The current version of this plugin. - */ - private $version; - - /** - * Initialize the class and set its properties. - * - * @since 1.0.0 - * @param string $plugin_name The name of the plugin. - * @param string $version The version of this plugin. - */ - public function __construct( $plugin_name, $version ) { - - $this->plugin_name = $plugin_name; - $this->version = $version; - - } - - /** - * Register the stylesheets for the public-facing side of the site. - * - * @since 1.0.0 - */ - public function enqueue_styles() { - - /** - * This function is provided for demonstration purposes only. - * - * An instance of this class should be passed to the run() function - * defined in Plugin_Name_Loader as all of the hooks are defined - * in that particular class. - * - * The Plugin_Name_Loader will then create the relationship - * between the defined hooks and the functions defined in this - * class. - */ - - wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/plugin-name-public.css', array(), $this->version, 'all' ); - - } - - /** - * Register the JavaScript for the public-facing side of the site. - * - * @since 1.0.0 - */ - public function enqueue_scripts() { - - /** - * This function is provided for demonstration purposes only. - * - * An instance of this class should be passed to the run() function - * defined in Plugin_Name_Loader as all of the hooks are defined - * in that particular class. - * - * The Plugin_Name_Loader will then create the relationship - * between the defined hooks and the functions defined in this - * class. - */ - - wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/plugin-name-public.js', array( 'jquery' ), $this->version, false ); - - } - -} diff --git a/plugin-name/public/index.php b/plugin-name/public/index.php deleted file mode 100644 index e71af0ef2..000000000 --- a/plugin-name/public/index.php +++ /dev/null @@ -1 +0,0 @@ - + */ +class PluginNameAdmin +{ + /** + * The ID of this plugin. + * + * @since 1.0.0 + * @access private + * @var string $plugin_name The ID of this plugin. + */ + private $plugin_name; + + /** + * The version of this plugin. + * + * @since 1.0.0 + * @access private + * @var string $version The current version of this plugin. + */ + private $version; + + /** + * Initialize the class and set its properties. + * + * @since 1.0.0 + * @param string $plugin_name The name of this plugin. + * @param string $version The version of this plugin. + */ + public function __construct($plugin_name, $version) + { + $this->plugin_name = $plugin_name; + $this->version = $version; + } + + /** + * Register the stylesheets for the admin area. + * + * @since 1.0.0 + */ + public function enqueueStyles() + { + /** + * This function is provided for demonstration purposes only. + * + * An instance of this class should be passed to the run() function + * defined in Plugin_Name_Loader as all of the hooks are defined + * in that particular class. + * + * The Plugin_Name_Loader will then create the relationship + * between the defined hooks and the functions defined in this + * class. + */ + + wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/plugin-name-admin.css', array(), $this->version, 'all'); + } + + /** + * Register the JavaScript for the admin area. + * + * @since 1.0.0 + */ + public function enqueueScripts() + { + /** + * This function is provided for demonstration purposes only. + * + * An instance of this class should be passed to the run() function + * defined in Plugin_Name_Loader as all of the hooks are defined + * in that particular class. + * + * The Plugin_Name_Loader will then create the relationship + * between the defined hooks and the functions defined in this + * class. + */ + + wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/plugin-name-admin.js', array( 'jquery' ), $this->version, false); + } +} diff --git a/plugin-name/admin/css/plugin-name-admin.css b/plugin-name/src/admin/css/plugin-name-admin.css similarity index 100% rename from plugin-name/admin/css/plugin-name-admin.css rename to plugin-name/src/admin/css/plugin-name-admin.css diff --git a/plugin-name/src/admin/index.php b/plugin-name/src/admin/index.php new file mode 100644 index 000000000..8142269b1 --- /dev/null +++ b/plugin-name/src/admin/index.php @@ -0,0 +1 @@ + */ -class Plugin_Name_Activator { - - /** - * Short Description. (use period) - * - * Long Description. - * - * @since 1.0.0 - */ - public static function activate() { - - } +class PluginNameActivator +{ + /** + * Short Description. (use period) + * + * Long Description. + * + * @since 1.0.0 + */ + public static function activate() + { + } } diff --git a/plugin-name/includes/class-plugin-name-deactivator.php b/plugin-name/src/includes/PluginNameDeactivator.php similarity index 65% rename from plugin-name/includes/class-plugin-name-deactivator.php rename to plugin-name/src/includes/PluginNameDeactivator.php index 548795cff..464bad96f 100644 --- a/plugin-name/includes/class-plugin-name-deactivator.php +++ b/plugin-name/src/includes/PluginNameDeactivator.php @@ -10,6 +10,8 @@ * @subpackage Plugin_Name/includes */ +namespace PluginName\Includes; + /** * Fired during plugin deactivation. * @@ -20,17 +22,17 @@ * @subpackage Plugin_Name/includes * @author Your Name */ -class Plugin_Name_Deactivator { - - /** - * Short Description. (use period) - * - * Long Description. - * - * @since 1.0.0 - */ - public static function deactivate() { - - } +class PluginNameDeactivator +{ + /** + * Short Description. (use period) + * + * Long Description. + * + * @since 1.0.0 + */ + public static function deactivate() + { + } } diff --git a/plugin-name/src/includes/PluginNameEngine.php b/plugin-name/src/includes/PluginNameEngine.php new file mode 100644 index 000000000..3594ac778 --- /dev/null +++ b/plugin-name/src/includes/PluginNameEngine.php @@ -0,0 +1,200 @@ + + */ +class PluginNameEngine +{ + /** + * The loader that's responsible for maintaining and registering all hooks that power + * the plugin. + * + * @since 1.0.0 + * @access protected + * @var PluginNameLoader $loader Maintains and registers all hooks for the plugin. + */ + protected $loader; + + /** + * The unique identifier of this plugin. + * + * @since 1.0.0 + * @access protected + * @var string $plugin_name The string used to uniquely identify this plugin. + */ + protected $plugin_name; + + /** + * The current version of the plugin. + * + * @since 1.0.0 + * @access protected + * @var string $version The current version of the plugin. + */ + protected $version; + + /** + * Define the core functionality of the plugin. + * + * Set the plugin name and the plugin version that can be used throughout the plugin. + * Load the dependencies, define the locale, and set the hooks for the admin area and + * the public-facing side of the site. + * + * @since 1.0.0 + */ + public function __construct() + { + if (defined('PLUGIN_NAME_VERSION')) { + $this->version = PLUGIN_NAME_VERSION; + } else { + $this->version = '1.0.0'; + } + $this->plugin_name = 'plugin-name'; + + $this->loadDependencies(); + $this->setLocale(); + $this->defineAdminHooks(); + $this->definePublicHooks(); + } + + /** + * Load the required dependencies for this plugin. + * + * Include the following files that make up the plugin: + * + * - Plugin_Name_Loader. Orchestrates the hooks of the plugin. + * - Plugin_Name_i18n. Defines internationalization functionality. + * - Plugin_Name_Admin. Defines all hooks for the admin area. + * - Plugin_Name_Public. Defines all hooks for the public side of the site. + * + * Create an instance of the loader which will be used to register the hooks + * with WordPress. + * + * @since 1.0.0 + * @access private + */ + private function loadDependencies() + { + $this->loader = new PluginNameLoader(); + } + + /** + * Define the locale for this plugin for internationalization. + * + * Uses the Plugin_Name_i18n class in order to set the domain and to register the hook + * with WordPress. + * + * @since 1.0.0 + * @access private + */ + private function setLocale() + { + $plugin_i18n = new PluginNameI18(); + + $this->loader->addAction('plugins_loaded', $plugin_i18n, 'loadPluginTextdomain'); + } + + /** + * Register all of the hooks related to the admin area functionality + * of the plugin. + * + * @since 1.0.0 + * @access private + */ + private function defineAdminHooks() + { + + $plugin_admin = new PluginNameAdmin($this->getPluginName(), $this->getVersion()); + + $this->loader->addAction('admin_enqueue_scripts', $plugin_admin, 'enqueueStyles'); + $this->loader->addAction('admin_enqueue_scripts', $plugin_admin, 'enqueueScripts'); + } + + /** + * Register all of the hooks related to the public-facing functionality + * of the plugin. + * + * @since 1.0.0 + * @access private + */ + private function definePublicHooks() + { + $plugin_public = new PluginNamePublic($this->getPluginName(), $this->getVersion()); + + $this->loader->addAction('wp_enqueue_scripts', $plugin_public, 'enqueueStyles'); + $this->loader->addAction('wp_enqueue_scripts', $plugin_public, 'enqueueScripts'); + } + + /** + * Run the loader to execute all of the hooks with WordPress. + * + * @since 1.0.0 + */ + public function run() + { + $this->loader->run(); + } + + /** + * The name of the plugin used to uniquely identify it within the context of + * WordPress and to define internationalization functionality. + * + * @since 1.0.0 + * @return string The name of the plugin. + */ + public function getPluginName() + { + return $this->plugin_name; + } + + /** + * The reference to the class that orchestrates the hooks with the plugin. + * + * @since 1.0.0 + * @return PluginNameLoader Orchestrates the hooks of the plugin. + */ + public function getLoader() + { + return $this->loader; + } + + /** + * Retrieve the version number of the plugin. + * + * @since 1.0.0 + * @return string The version number of the plugin. + */ + public function getVersion() + { + return $this->version; + } +} diff --git a/plugin-name/includes/class-plugin-name-i18n.php b/plugin-name/src/includes/PluginNameI18.php similarity index 62% rename from plugin-name/includes/class-plugin-name-i18n.php rename to plugin-name/src/includes/PluginNameI18.php index 214e049b8..a916e4de1 100644 --- a/plugin-name/includes/class-plugin-name-i18n.php +++ b/plugin-name/src/includes/PluginNameI18.php @@ -13,6 +13,8 @@ * @subpackage Plugin_Name/includes */ +namespace PluginName\Includes; + /** * Define the internationalization functionality. * @@ -24,24 +26,19 @@ * @subpackage Plugin_Name/includes * @author Your Name */ -class Plugin_Name_i18n { - - - /** - * Load the plugin text domain for translation. - * - * @since 1.0.0 - */ - public function load_plugin_textdomain() { - - load_plugin_textdomain( - 'plugin-name', - false, - dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' - ); - - } - - - +class PluginNameI18 +{ + /** + * Load the plugin text domain for translation. + * + * @since 1.0.0 + */ + public function loadPluginTextdomain() + { + load_plugin_textdomain( + 'plugin-name', + false, + dirname(dirname(plugin_basename(__FILE__))) . '/languages/' + ); + } } diff --git a/plugin-name/src/includes/PluginNameLoader.php b/plugin-name/src/includes/PluginNameLoader.php new file mode 100644 index 000000000..a01a24b60 --- /dev/null +++ b/plugin-name/src/includes/PluginNameLoader.php @@ -0,0 +1,130 @@ + + */ +class PluginNameLoader +{ + /** + * The array of actions registered with WordPress. + * + * @since 1.0.0 + * @access protected + * @var array $actions The actions registered with WordPress to fire when the plugin loads. + */ + protected $actions; + + /** + * The array of filters registered with WordPress. + * + * @since 1.0.0 + * @access protected + * @var array $filters The filters registered with WordPress to fire when the plugin loads. + */ + protected $filters; + + /** + * Initialize the collections used to maintain the actions and filters. + * + * @since 1.0.0 + */ + public function __construct() + { + $this->actions = array(); + $this->filters = array(); + } + + /** + * Add a new action to the collection to be registered with WordPress. + * + * @since 1.0.0 + * @param string $hook The name of the WordPress action that is being registered. + * @param object $component A reference to the instance of the object on which the action is defined. + * @param string $callback The name of the function definition on the $component. + * @param int $priority Optional. The priority at which the function should be fired. Default is 10. + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1. + */ + public function addAction($hook, $component, $callback, $priority = 10, $accepted_args = 1) + { + $this->actions = $this->add($this->actions, $hook, $component, $callback, $priority, $accepted_args); + } + + /** + * Add a new filter to the collection to be registered with WordPress. + * + * @since 1.0.0 + * @param string $hook The name of the WordPress filter that is being registered. + * @param object $component A reference to the instance of the object on which the filter is defined. + * @param string $callback The name of the function definition on the $component. + * @param int $priority Optional. The priority at which the function should be fired. Default is 10. + * @param int $accepted_args Optional. The number of arguments that should be passed to the $callback. Default is 1 + */ + public function addFilter($hook, $component, $callback, $priority = 10, $accepted_args = 1) + { + $this->filters = $this->add($this->filters, $hook, $component, $callback, $priority, $accepted_args); + } + + /** + * A utility function that is used to register the actions and hooks into a single + * collection. + * + * @since 1.0.0 + * @access private + * @param array $hooks The collection of hooks that is being registered (that is, actions or filters). + * @param string $hook The name of the WordPress filter that is being registered. + * @param object $component A reference to the instance of the object on which the filter is defined. + * @param string $callback The name of the function definition on the $component. + * @param int $priority The priority at which the function should be fired. + * @param int $accepted_args The number of arguments that should be passed to the $callback. + * @return array The collection of actions and filters registered with WordPress. + */ + private function add($hooks, $hook, $component, $callback, $priority, $accepted_args) + { + + $hooks[] = array( + 'hook' => $hook, + 'component' => $component, + 'callback' => $callback, + 'priority' => $priority, + 'accepted_args' => $accepted_args + ); + + return $hooks; + } + + /** + * Register the filters and actions with WordPress. + * + * @since 1.0.0 + */ + public function run() + { + foreach ($this->filters as $hook) { + add_filter($hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args']); + } + + foreach ($this->actions as $hook) { + add_action($hook['hook'], array( $hook['component'], $hook['callback'] ), $hook['priority'], $hook['accepted_args']); + } + } +} diff --git a/plugin-name/src/includes/index.php b/plugin-name/src/includes/index.php new file mode 100644 index 000000000..8142269b1 --- /dev/null +++ b/plugin-name/src/includes/index.php @@ -0,0 +1 @@ + + */ +class PluginNamePublic +{ + /** + * The ID of this plugin. + * + * @since 1.0.0 + * @access private + * @var string $plugin_name The ID of this plugin. + */ + private $plugin_name; + + /** + * The version of this plugin. + * + * @since 1.0.0 + * @access private + * @var string $version The current version of this plugin. + */ + private $version; + + /** + * Initialize the class and set its properties. + * + * @since 1.0.0 + * @param string $plugin_name The name of the plugin. + * @param string $version The version of this plugin. + */ + public function __construct($plugin_name, $version) + { + $this->plugin_name = $plugin_name; + $this->version = $version; + } + + /** + * Register the stylesheets for the public-facing side of the site. + * + * @since 1.0.0 + */ + public function enqueueStyles() + { + /** + * This function is provided for demonstration purposes only. + * + * An instance of this class should be passed to the run() function + * defined in Plugin_Name_Loader as all of the hooks are defined + * in that particular class. + * + * The Plugin_Name_Loader will then create the relationship + * between the defined hooks and the functions defined in this + * class. + */ + + wp_enqueue_style($this->plugin_name, plugin_dir_url(__FILE__) . 'css/plugin-name-public.css', array(), $this->version, 'all'); + } + + /** + * Register the JavaScript for the public-facing side of the site. + * + * @since 1.0.0 + */ + public function enqueueScripts() + { + /** + * This function is provided for demonstration purposes only. + * + * An instance of this class should be passed to the run() function + * defined in Plugin_Name_Loader as all of the hooks are defined + * in that particular class. + * + * The Plugin_Name_Loader will then create the relationship + * between the defined hooks and the functions defined in this + * class. + */ + + wp_enqueue_script($this->plugin_name, plugin_dir_url(__FILE__) . 'js/plugin-name-public.js', array( 'jquery' ), $this->version, false); + } +} diff --git a/plugin-name/public/css/plugin-name-public.css b/plugin-name/src/public/css/plugin-name-public.css similarity index 100% rename from plugin-name/public/css/plugin-name-public.css rename to plugin-name/src/public/css/plugin-name-public.css diff --git a/plugin-name/src/public/index.php b/plugin-name/src/public/index.php new file mode 100644 index 000000000..8142269b1 --- /dev/null +++ b/plugin-name/src/public/index.php @@ -0,0 +1 @@ + Date: Thu, 29 Dec 2022 17:12:46 +0100 Subject: [PATCH 2/2] Create Makefile --- plugin-name/Makefile | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 plugin-name/Makefile diff --git a/plugin-name/Makefile b/plugin-name/Makefile new file mode 100644 index 000000000..04ccf7e87 --- /dev/null +++ b/plugin-name/Makefile @@ -0,0 +1,26 @@ +#!/bin/sh + +.DEFAULT_GOAL := help +.PHONY: $(filter-out vendor node_modules, $(MAKECMDGOALS)) + +install: + composer install + composer phpunit + +install-prod: + composer install-prod + +test: + @composer test + +build: + @composer build + +help: + @echo Silence is golden + +vendor: composer.json $(wildcard composer.lock) ## Install PHP application + @composer install + +node_modules: $(wildcard package.json) $(wildcard package.lock) ## Install Node modules + @npm install