-
Notifications
You must be signed in to change notification settings - Fork 3
/
wp-plugin-dictator.php
165 lines (135 loc) · 4.05 KB
/
wp-plugin-dictator.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* Plugin Name: WP Plugin Dictator
* Plugin URI: https://github.com/dfmedia/wp-plugin-dictator
* Description: Control which plugins should/should not be active in a given environment
* Author: Ryan Kanner, Digital First Media
* Text Domain: wp-plugin-dictator
* Domain Path: /languages
* Version: 1.0.2
*
* @package WP_Plugin_Dictator
*/
// ensure the wp environment is loaded properly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'WPPluginDictator' ) ) {
class WPPluginDictator {
/**
* Stores the instance of the WPPluginDictator class
*
* @var Object $instance
* @access private
*/
private static $instance;
/**
* Retrieves the instance of the WPPluginDictator class
*
* @access public
* @return Object|WPPluginDictator
* @throws exception
*/
public static function instance() {
/**
* Make sure we are only instantiating the class once
*/
if ( ! isset( self::$instance ) && ! ( self::$instance instanceof WPPluginDictator ) ) {
self::$instance = new WPPluginDictator();
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->run();
}
/**
* Action that fires after we are done setting things up in the plugin. Extensions of
* this plugin should instantiate themselves on this hook to make sure the framework
* is available before they do anything.
*
* @param object $instance Instance of the current WPPluginDictator class
*/
do_action( 'wp_plugin_dictator_init', self::$instance );
return self::$instance;
}
/**
* Sets up the constants for the plugin to use
*
* @access private
* @return void
*/
private function setup_constants() {
// Plugin version.
if ( ! defined( 'WP_PLUGIN_DICTATOR_VERSION' ) ) {
define( 'WP_PLUGIN_DICTATOR_VERSION', '1.0.0' );
}
// Plugin Folder Path.
if ( ! defined( 'WP_PLUGIN_DICTATOR_PLUGIN_DIR' ) ) {
define( 'WP_PLUGIN_DICTATOR_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Folder URL.
if ( ! defined( 'WP_PLUGIN_DICTATOR_PLUGIN_URL' ) ) {
define( 'WP_PLUGIN_DICTATOR_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'WP_PLUGIN_DICTATOR_PLUGIN_FILE' ) ) {
define( 'WP_PLUGIN_DICTATOR_PLUGIN_FILE', __FILE__ );
}
}
/**
* Load the autoloaded files as well as the access functions
*
* @access private
* @return void
* @throws Exception
*/
private function includes() {
if ( file_exists( WP_PLUGIN_DICTATOR_PLUGIN_DIR . 'vendor/autoload.php' ) ) {
require_once( WP_PLUGIN_DICTATOR_PLUGIN_DIR . 'vendor/autoload.php' );
} else {
throw new Exception( __( 'Could not find autoloader file to include all files', 'wp-plugin-dictator' ) );
}
}
/**
* Instantiate the main classes we need for the plugin
*
* @access private
* @return void
*/
private function run() {
/**
* Instantiate classes here
*/
$dictator = new \WPPluginDictator\Dictate();
$dictator->run();
add_action( 'init', function() {
if ( is_admin() ) {
$admin = new \WPPluginDictator\Admin();
$admin->setup();
}
} );
if ( defined( 'WP_CLI' ) && true === WP_CLI ) {
// Instantiate class for CLI commands here
WP_CLI::add_command( 'plugin dictate', '\WPPluginDictator\CLI' );
}
}
}
}
/**
* Function to instantiate the WPPluginDictator class
*
* @return Object|WPPluginDictator Instance of the WPPluginDictator object
* @access public
* @throws Exception
*/
function wp_plugin_dictator_init() {
if ( did_action( 'plugins_loaded' ) ) {
throw new Exception( __( 'This plugin needs to be dropped in the wp-content/mu-plugins folder to work properly', 'wp-plugin-dictator' ) );
}
/**
* Returns an instance of the WPPluginDictator class
*/
return \WPPluginDictator::instance();
}
// Activate as early as possible, since this controls what plugins should/shouldn't be active, it needs to run before plugins are loaded.
if ( ! wp_installing() ) {
wp_plugin_dictator_init();
}