From d5a873cb6309a82afa448f7511a0284f0df30719 Mon Sep 17 00:00:00 2001 From: corsacca Date: Fri, 17 May 2024 09:41:57 +0100 Subject: [PATCH 1/2] use `disciple_tools_load_plugins` action hook to load plugin --- disciple-tools-plugin-starter-template.php | 95 ++++++++++------------ 1 file changed, 43 insertions(+), 52 deletions(-) diff --git a/disciple-tools-plugin-starter-template.php b/disciple-tools-plugin-starter-template.php index ad907bf..172d9a2 100755 --- a/disciple-tools-plugin-starter-template.php +++ b/disciple-tools-plugin-starter-template.php @@ -30,52 +30,9 @@ exit; // Exit if accessed directly } -/** - * Gets the instance of the `Disciple_Tools_Plugin_Starter_Template` class. - * - * @since 0.1 - * @access public - * @return object|bool - */ -function disciple_tools_plugin_starter_template() { - $disciple_tools_plugin_starter_template_required_dt_theme_version = '1.19'; - $wp_theme = wp_get_theme(); - $version = $wp_theme->version; - - /* - * Check if the Disciple.Tools theme is loaded and is the latest required version - */ - $is_theme_dt = class_exists( 'Disciple_Tools' ); - if ( $is_theme_dt && version_compare( $version, $disciple_tools_plugin_starter_template_required_dt_theme_version, '<' ) ) { - add_action( 'admin_notices', 'disciple_tools_plugin_starter_template_hook_admin_notice' ); - add_action( 'wp_ajax_dismissed_notice_handler', 'dt_hook_ajax_notice_handler' ); - return false; - } - if ( !$is_theme_dt ){ - return false; - } - /** - * Load useful function from the theme - */ - if ( !defined( 'DT_FUNCTIONS_READY' ) ){ - require_once get_template_directory() . '/dt-core/global-functions.php'; - } - - return Disciple_Tools_Plugin_Starter_Template::instance(); - -} -add_action( 'after_setup_theme', 'disciple_tools_plugin_starter_template', 20 ); - -//register the D.T Plugin -add_filter( 'dt_plugins', function ( $plugins ){ - $plugin_data = get_file_data( __FILE__, [ 'Version' => 'Version', 'Plugin Name' => 'Plugin Name' ], false ); - $plugins['disciple-tools-plugin-starter-template'] = [ - 'plugin_url' => trailingslashit( plugin_dir_url( __FILE__ ) ), - 'version' => $plugin_data['Version'] ?? null, - 'name' => $plugin_data['Plugin Name'] ?? null, - ]; - return $plugins; -}); +add_action( 'disciple_tools_load_plugins', function (){ + Disciple_Tools_Plugin_Starter_Template::instance(); +} ); /** * Singleton class for setting up the plugin. @@ -84,7 +41,6 @@ function disciple_tools_plugin_starter_template() { * @access public */ class Disciple_Tools_Plugin_Starter_Template { - private static $_instance = null; public static function instance() { if ( is_null( self::$_instance ) ) { @@ -93,15 +49,38 @@ public static function instance() { return self::$_instance; } + public static string $required_theme_version = '1.63'; + public string $version; + public string $name; + public string $key; + + + public function register_plugin( $plugins ){ + $plugins['disciple-tools-plugin-starter-template'] = [ + 'plugin_url' => trailingslashit( plugin_dir_url( __FILE__ ) ), + 'version' => $this->version, + 'name' => $this->name + ]; + return $plugins; + } + private function __construct() { + $plugin_data = get_file_data( __FILE__, [ 'Version' => 'Version', 'name' => 'Plugin Name', 'key' => 'Domain' ], false ); + $this->version = $plugin_data['Version'] ?? ''; + $this->name = $plugin_data['name'] ?? ''; + $this->key = $plugin_data['key'] ?? ''; + add_filter( 'dt_plugins', [ $this, 'register_plugin' ], 10, 1 ); + + if ( version_compare( self::$required_theme_version, disciple_tools()->version, '>' ) ) { + return false; + } + $is_rest = dt_is_rest(); /** * @todo Decide if you want to use the REST API example * To remove: delete this following line and remove the folder named /rest-api */ - if ( $is_rest && strpos( dt_get_url_path(), 'disciple-tools-plugin-starter-template' ) !== false ) { - require_once( 'rest-api/rest-api.php' ); // adds starter rest api class - } + require_once( 'rest-api/rest-api.php' ); // adds starter rest api class /** * @todo Decide if you want to create a new post type @@ -278,14 +257,26 @@ public function __call( $method = '', $args = array() ) { register_deactivation_hook( __FILE__, [ 'Disciple_Tools_Plugin_Starter_Template', 'deactivation' ] ); +/** + * Check if the Disciple.Tools theme is loaded and is the latest required version + */ +add_action( 'after_setup_theme', function(){ + $wp_theme = wp_get_theme(); + $version = $wp_theme->version; + $is_theme_dt = class_exists( 'Disciple_Tools' ); + if ( !$is_theme_dt || version_compare( $version, Disciple_Tools_Plugin_Starter_Template::$required_theme_version, '<' ) ) { + add_action( 'admin_notices', 'disciple_tools_plugin_starter_template_hook_admin_notice' ); + add_action( 'wp_ajax_dismissed_notice_handler', 'dt_hook_ajax_notice_handler' ); + } +}, 20 ); + if ( ! function_exists( 'disciple_tools_plugin_starter_template_hook_admin_notice' ) ) { function disciple_tools_plugin_starter_template_hook_admin_notice() { - global $disciple_tools_plugin_starter_template_required_dt_theme_version; $wp_theme = wp_get_theme(); $current_version = $wp_theme->version; $message = "'Disciple.Tools - Plugin Starter Template' plugin requires 'Disciple.Tools' theme to work. Please activate 'Disciple.Tools' theme or make sure it is latest version."; if ( $wp_theme->get_template() === 'disciple-tools-theme' ){ - $message .= ' ' . sprintf( esc_html( 'Current Disciple.Tools version: %1$s, required version: %2$s' ), esc_html( $current_version ), esc_html( $disciple_tools_plugin_starter_template_required_dt_theme_version ) ); + $message .= ' ' . sprintf( esc_html( 'Current Disciple.Tools version: %1$s, required version: %2$s' ), esc_html( $current_version ), esc_html( Disciple_Tools_Plugin_Starter_Template::$required_theme_version ) ); } // Check if it's been dismissed... if ( ! get_option( 'dismissed-disciple-tools-plugin-starter-template', false ) ) { ?> From 4bf16a2104d21e119fef9b452b334fcb8d480466 Mon Sep 17 00:00:00 2001 From: corsacca Date: Fri, 17 May 2024 09:49:51 +0100 Subject: [PATCH 2/2] this class key. --- disciple-tools-plugin-starter-template.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/disciple-tools-plugin-starter-template.php b/disciple-tools-plugin-starter-template.php index 172d9a2..3a2bf45 100755 --- a/disciple-tools-plugin-starter-template.php +++ b/disciple-tools-plugin-starter-template.php @@ -52,11 +52,11 @@ public static function instance() { public static string $required_theme_version = '1.63'; public string $version; public string $name; - public string $key; + public string $key = 'disciple-tools-plugin-starter-template'; // @todo change to your plugin key 'disciple-tools-plugin-starter-template public function register_plugin( $plugins ){ - $plugins['disciple-tools-plugin-starter-template'] = [ + $plugins[$this->key] = [ 'plugin_url' => trailingslashit( plugin_dir_url( __FILE__ ) ), 'version' => $this->version, 'name' => $this->name @@ -65,10 +65,9 @@ public function register_plugin( $plugins ){ } private function __construct() { - $plugin_data = get_file_data( __FILE__, [ 'Version' => 'Version', 'name' => 'Plugin Name', 'key' => 'Domain' ], false ); + $plugin_data = get_file_data( __FILE__, [ 'Version' => 'Version', 'name' => 'Plugin Name' ], false ); $this->version = $plugin_data['Version'] ?? ''; $this->name = $plugin_data['name'] ?? ''; - $this->key = $plugin_data['key'] ?? ''; add_filter( 'dt_plugins', [ $this, 'register_plugin' ], 10, 1 ); if ( version_compare( self::$required_theme_version, disciple_tools()->version, '>' ) ) {