-
Notifications
You must be signed in to change notification settings - Fork 10
/
requirements.php
142 lines (125 loc) · 3.87 KB
/
requirements.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
/**
* This script is not used within Just Theme Framework itself.
*
* This script is meant to be used with your Just Theme Framework-dependent theme or plugin,
* so that your theme/plugin can verify whether the framework is installed.
*
* If framework is not installed, then the script will display a notice with a link to
* download. If it is installed but not activated, it will display the appropriate notice as well.
*
* To use this script, just copy it into your theme/plugin directory then add this in the main file of your project:
*
* require_once( 'requirements.php' );
*
* @version 1.0
*
* @package Just Theme Framework
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
// Required functions as it is only loaded in admin pages.
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( ! class_exists( 'Just_Theme_Framework_Checker' ) ) {
/**
* Class Just_Theme_Framework_Checker
*/
class Just_Theme_Framework_Checker {
/**
* Refers to a single instance of this class.
*
* @var Just_Theme_Framework_Checker|null
*/
private static $instance = null;
/**
* Required plugins settings.
*
* @var array
*/
private $required_plugins = array(
'wordpress-theme-framework/wordpress-theme-framework.php' => array(
'\JustCoded\WP\Framework\Autoload',
'WordPress Theme Framework',
'https://github.com/justcoded/wordpress-theme-framework',
),
);
/**
* Just_Theme_Framework_Checker constructor.
*
* Register admin notices with requirements.
*
* @throws Exception Show public fatal error if requirements are not met.
*/
private function __construct() {
global $pagenow;
if ( ! is_admin() && 'wp-login.php' !== $pagenow && ! $this->check_requirements() ) {
throw new Exception( 'Your theme requires WordPress Theme Framework plugin to be installed and activated.' );
}
add_action( 'admin_notices', array( $this, 'display_requirements_admin_notice' ) );
}
/**
* Returns the *Singleton* instance of this class.
*
* @return Just_Theme_Framework_Checker A single instance of this class.
*/
public static function single() {
if ( null === static::$instance ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Check final requirements.
*
* @return bool
*/
public function check_requirements() {
foreach ( $this->required_plugins as $plugin_file => $plugin_details ) {
if ( ! class_exists( $plugin_details[0] ) && ! is_plugin_active( $plugin_file ) ) {
return false;
}
}
return true;
}
/**
* Print admin notices in case of requirements are not met.
* Can print "missing plugin" or "not activated" warnings.
*/
public function display_requirements_admin_notice() {
if ( $this->check_requirements() ) {
return;
}
$warnings = array();
foreach ( $this->required_plugins as $plugin_file => $plugin_details ) {
$plugin_path = WP_PLUGIN_DIR . '/' . $plugin_file;
if ( ! is_file( $plugin_path ) ) {
$warnings[] = sprintf( '<strong>%s</strong> plugin should be installed. <a href="%s" target="_blank">Download plugin »</a>',
esc_html( $plugin_details[1] ),
esc_attr( $plugin_details[2] )
);
} elseif ( ! is_plugin_active( $plugin_file ) ) {
$warnings[] = sprintf( '<strong>%s</strong> plugin should be activated. <a href="%s">Manage Plugins »</a>',
esc_html( $plugin_details[1] ),
esc_attr( admin_url( 'plugins.php' ) )
);
}
}
$html = '<div class="error"><h3>Please fix the errors below to use current activated theme:</h3><p>'
. implode( '</p><p>', $warnings )
. '</p></div>';
echo wp_kses( $html, array(
'div' => [
'class' => true,
],
'h3' => true,
'p' => true,
'a' => [
'href' => true,
'target' => true,
],
) );
}
}
Just_Theme_Framework_Checker::single();
}