From 29ef24cc9a7b641b9164323e132fcf53aedb6a91 Mon Sep 17 00:00:00 2001 From: Beda Schmid Date: Sat, 19 Feb 2022 19:41:22 +0700 Subject: [PATCH 1/2] 2022-02-19 ### Changed - Removed uninstall hook with class again in favour of uninstall.php file. Peformance issues with uninstall hook never resolved in Core are the reason. - Updated Header tags to be more dynamic for Generate Plugin features, does not affect functionality. - Updated prefixes of global functions for better replacement with Generate Plugin Features. Does not affect functionality. ### Fixed - Issue where activation and deactivation could have thrown notices due to undefined indexes. --- CHANGELOG.md | 8 + plugin-name/README.txt | 4 +- .../includes/class-plugin-name-activator.php | 6 +- .../class-plugin-name-deactivator.php | 6 +- .../includes/class-plugin-name-uninstall.php | 143 ------------------ plugin-name/includes/class-plugin-name.php | 2 +- plugin-name/plugin-name.php | 31 ++-- .../public/class-plugin-name-public.php | 2 +- plugin-name/uninstall.php | 61 ++++++++ 9 files changed, 91 insertions(+), 172 deletions(-) delete mode 100644 plugin-name/includes/class-plugin-name-uninstall.php create mode 100644 plugin-name/uninstall.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f04882..9452f0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 2022-02-19 +### Changed +- Removed uninstall hook with class again in favour of uninstall.php file. Peformance issues with uninstall hook never resolved in Core are the reason. +- Updated Header tags to be more dynamic for Generate Plugin features, does not affect functionality. +- Updated prefixes of global functions for better replacement with Generate Plugin Features. Does not affect functionality. +### Fixed +- Issue where activation and deactivation could have thrown notices due to undefined indexes. + ## 2021-09-15 ### Changed - Removed uninstall.php file and added uninstall hook with class instead. Allows for dynamic plugin name retrieval during unistall diff --git a/plugin-name/README.txt b/plugin-name/README.txt index c796c36..b1f6e12 100644 --- a/plugin-name/README.txt +++ b/plugin-name/README.txt @@ -2,8 +2,8 @@ Contributors: Your Name or Your Company Name Donate link: https://donate.tld/ Tags: comments, spam -Requires at least: 4.9 -Tested up to: 5.8 +Requires at least: X.X +Tested up to: X.X Stable tag: 1.0.0 License: GPLv2 or later License URI: https://www.gnu.org/licenses/gpl-2.0.html diff --git a/plugin-name/includes/class-plugin-name-activator.php b/plugin-name/includes/class-plugin-name-activator.php index 108cb5f..249d229 100644 --- a/plugin-name/includes/class-plugin-name-activator.php +++ b/plugin-name/includes/class-plugin-name-activator.php @@ -124,13 +124,15 @@ private static function get_request() { */ private static function validate_request( $plugin ) { - if ( $plugin === self::$request['plugin'] + if ( isset( self::$request['plugin'] ) + && $plugin === self::$request['plugin'] && 'activate' === self::$request['action'] ) { return true; - } elseif ( 'activate-selected' === self::$request['action'] + } elseif ( isset( self::$request['plugins'] ) + && 'activate-selected' === self::$request['action'] && in_array( $plugin, self::$request['plugins'] ) ) { return true; diff --git a/plugin-name/includes/class-plugin-name-deactivator.php b/plugin-name/includes/class-plugin-name-deactivator.php index 0c4a2a6..223b76a 100644 --- a/plugin-name/includes/class-plugin-name-deactivator.php +++ b/plugin-name/includes/class-plugin-name-deactivator.php @@ -124,13 +124,15 @@ private static function get_request() { */ private static function validate_request( $plugin ) { - if ( $plugin === self::$request['plugin'] + if ( isset( self::$request['plugin'] ) + && $plugin === self::$request['plugin'] && 'deactivate' === self::$request['action'] ) { return true; - } elseif ( 'deactivate-selected' === self::$request['action'] + } elseif ( isset( self::$request['plugins'] ) + && 'deactivate-selected' === self::$request['action'] && in_array( $plugin, self::$request['plugins'] ) ) { return true; diff --git a/plugin-name/includes/class-plugin-name-uninstall.php b/plugin-name/includes/class-plugin-name-uninstall.php deleted file mode 100644 index dba96a5..0000000 --- a/plugin-name/includes/class-plugin-name-uninstall.php +++ /dev/null @@ -1,143 +0,0 @@ - - */ -class Plugin_Name_Uninstall { - - /** - * The $_REQUEST during plugin uninstall. - * - * @since 1.0.0 - * @access private - * @var array $request The $_REQUEST array during plugin uninstall. - */ - private static $request = array(); - - /** - * The $_REQUEST['plugin'] during plugin uninstall. - * - * @since 1.0.0 - * @access private - * @var string $plugin The $_REQUEST['plugin'] value during plugin uninstall. - */ - private static $plugin = PLUGIN_NAME_BASE_NAME; - - /** - * Activate the plugin. - * - * Checks if the plugin was (safely) activated. - * Place to add any custom action during plugin uninstall. - * - * @since 1.0.0 - */ - public static function uninstall() { - - if ( false === self::get_request() - || false === self::validate_request( self::$plugin ) - || false === self::check_caps() - ) { - if ( isset( $_REQUEST['plugin'] ) ) { - if ( ! check_ajax_referer( 'updates', '_ajax_nonce' ) ) { - exit; - } - } - } - - /** - * The plugin is now safely activated. - * Perform your uninstall actions here. - */ - - } - - /** - * Get the request. - * - * Gets the $_REQUEST array and checks if necessary keys are set. - * Populates self::request with necessary and sanitized values. - * - * @since 1.0.0 - * @return bool|array false or self::$request array. - */ - private static function get_request() { - - if ( ! empty( $_REQUEST ) - && isset( $_REQUEST['action'] ) - ) { - if ( isset( $_REQUEST['plugin'] ) ) { - if ( false !== check_ajax_referer( 'updates', '_ajax_nonce' ) ) { - - self::$request['plugin'] = sanitize_text_field( wp_unslash( $_REQUEST['plugin'] ) ); - self::$request['action'] = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); - - return self::$request; - - } - } - } else { - - return false; - } - - } - - /** - * Validate the Request data. - * - * Validates the $_REQUESTed data is matching this plugin and action. - * - * @since 1.0.0 - * @param string $plugin The Plugin folder/name.php. - * @return bool false if either plugin or action does not match, else true. - */ - private static function validate_request( $plugin ) { - - if ( $plugin === self::$request['plugin'] - && 'delete-plugin' === self::$request['action'] - ) { - - return true; - - } - - return false; - - } - - /** - * Check Capabilities. - * - * We want no one else but users with activate_plugins or above to be able to uninstall this plugin. - * - * @since 1.0.0 - * @return bool false if no caps, else true. - */ - private static function check_caps() { - - if ( current_user_can( 'activate_plugins' ) ) { - return true; - } - - return false; - - } - -} diff --git a/plugin-name/includes/class-plugin-name.php b/plugin-name/includes/class-plugin-name.php index 14ed5ab..69d32ac 100644 --- a/plugin-name/includes/class-plugin-name.php +++ b/plugin-name/includes/class-plugin-name.php @@ -189,7 +189,7 @@ private function define_public_hooks() { $this->loader->add_action( 'wp_enqueue_scripts', $plugin_public, 'enqueue_scripts' ); // Shortcode name must be the same as in shortcode_atts() third parameter. - $this->loader->add_shortcode( $this->get_plugin_prefix() . 'shortcode', $plugin_public, 'plugin_name_shortcode_func' ); + $this->loader->add_shortcode( $this->get_plugin_prefix() . 'shortcode', $plugin_public, 'pfx_shortcode_func' ); } diff --git a/plugin-name/plugin-name.php b/plugin-name/plugin-name.php index 59bb7b9..1ddcd21 100644 --- a/plugin-name/plugin-name.php +++ b/plugin-name/plugin-name.php @@ -12,13 +12,14 @@ * @package Plugin_Name * * @wordpress-plugin - * Plugin Name: Plugin Name + * Plugin Name: My Plugin Name * Plugin URI: https://plugin.com/plugin-name-uri/ * Description: This is a short description of what the plugin does. It's displayed in the WordPress admin area. * Version: 1.0.0 * Author: Your Name or Your Company Name - * Requires at least: 4.9 - * Tested up to: 5.8 + * Requires at least: X.X + * Requires PHP: X.X + * Tested up to: X.X * Author URI: https://example.com/ * License: GPL-2.0+ * License URI: https://www.gnu.org/licenses/gpl-2.0.txt @@ -49,7 +50,7 @@ * This action is documented in includes/class-plugin-name-activator.php * Full security checks are performed inside the class. */ -function plugin_name_activate() { +function pfx_activate() { require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-activator.php'; Plugin_Name_Activator::activate(); } @@ -60,25 +61,13 @@ function plugin_name_activate() { * This action is documented in includes/class-plugin-name-deactivator.php * Full security checks are performed inside the class. */ -function plugin_name_deactivate() { +function pfx_deactivate() { require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-deactivator.php'; Plugin_Name_Deactivator::deactivate(); } -/** - * The code that runs during plugin uninstall. - * - * This action is documented in includes/class-plugin-name-uninstall.php - * Full security checks are performed inside the class. - */ -function plugin_name_uninstall() { - require_once plugin_dir_path( __FILE__ ) . 'includes/class-plugin-name-uninstall.php'; - Plugin_Name_Uninstall::uninstall(); -} - -register_activation_hook( __FILE__, 'plugin_name_activate' ); -register_deactivation_hook( __FILE__, 'plugin_name_deactivate' ); -register_uninstall_hook( __FILE__, 'plugin_name_uninstall' ); +register_activation_hook( __FILE__, 'pfx_activate' ); +register_deactivation_hook( __FILE__, 'pfx_deactivate' ); /** * The core plugin class that is used to define internationalization, @@ -98,10 +87,10 @@ function plugin_name_uninstall() { * * @since 1.0.0 */ -function plugin_name_run() { +function pfx_run() { $plugin = new Plugin_Name(); $plugin->run(); } -plugin_name_run(); +pfx_run(); diff --git a/plugin-name/public/class-plugin-name-public.php b/plugin-name/public/class-plugin-name-public.php index 9b98b34..843b46e 100644 --- a/plugin-name/public/class-plugin-name-public.php +++ b/plugin-name/public/class-plugin-name-public.php @@ -100,7 +100,7 @@ public function enqueue_scripts() { * @param mixed $content ShortCode enclosed content. * @param string $tag The Shortcode tag. */ - public function plugin_name_shortcode_func( $atts, $content = null, $tag ) { + public function pfx_shortcode_func( $atts, $content = null, $tag ) { /** * Combine user attributes with known attributes. diff --git a/plugin-name/uninstall.php b/plugin-name/uninstall.php new file mode 100644 index 0000000..cf62993 --- /dev/null +++ b/plugin-name/uninstall.php @@ -0,0 +1,61 @@ + Date: Sun, 20 Feb 2022 11:18:30 +0700 Subject: [PATCH 2/2] 2022-02-20 - Add pfx_ in readme --- plugin-name/README.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin-name/README.txt b/plugin-name/README.txt index b1f6e12..67366f2 100644 --- a/plugin-name/README.txt +++ b/plugin-name/README.txt @@ -45,7 +45,7 @@ e.g. 1. Upload `plugin-name.php` to the `/wp-content/plugins/` directory 1. Activate the plugin through the 'Plugins' menu in WordPress -1. Place `` in your templates +1. Place `` in your templates == Frequently Asked Questions ==