Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use disciple_tools_load_plugins action hook to load plugin #38

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 42 additions & 52 deletions disciple-tools-plugin-starter-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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 ) ) {
Expand All @@ -93,15 +49,37 @@ public static function instance() {
return self::$_instance;
}

public static string $required_theme_version = '1.63';
public string $version;
public string $name;
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[$this->key] = [
'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' ], false );
$this->version = $plugin_data['Version'] ?? '';
$this->name = $plugin_data['name'] ?? '';
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
Expand Down Expand Up @@ -278,14 +256,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 ) ) { ?>
Expand Down
Loading