-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathwp-plugin.php
132 lines (102 loc) · 4.6 KB
/
wp-plugin.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
<?php
// just making sure the constant is defined
if (!defined('WP_CONTENT_DIR'))
define('WP_CONTENT_DIR', ABSPATH . 'wp-content');
if (!class_exists('Environment')) {
class Environment {
const WordPress = 1; // regular wordpress
const WordPressMU = 2; // wordpress mu
const WordPressMS = 3; // wordpress multi-site
}
}
if (!class_exists('WPPlugin')) {
abstract class WPPlugin {
protected $environment; // what environment are we in
protected $options_name; // the name of the options associated with this plugin
protected $options;
function WPPlugin($options_name) {
$args = func_get_args();
call_user_func_array(array(&$this, "__construct"), $args);
}
function __construct($options_name) {
$this->environment = WPPlugin::determine_environment();
$this->options_name = $options_name;
$this->options = WPPlugin::retrieve_options($this->options_name);
}
// sub-classes determine what actions and filters to hook
abstract protected function register_actions();
abstract protected function register_filters();
// environment checking
static function determine_environment() {
global $wpmu_version;
if (function_exists('is_multisite'))
if (is_multisite())
return Environment::WordPressMS;
if (!empty($wpmu_version))
return Environment::WordPressMU;
return Environment::WordPress;
}
// path finding
static function plugins_directory() {
if (WPPlugin::determine_environment() == Environment::WordPressMU)
return WP_CONTENT_DIR . '/mu-plugins';
else
return WP_CONTENT_DIR . '/plugins';
}
static function plugins_url() {
if (WPPlugin::determine_environment() == Environment::WordPressMU)
return site_url() . '/wp-content/mu-plugins';
else
return site_url() . '/wp-content/plugins';
}
static function path_to_plugin_directory() {
$current_directory = basename(dirname(__FILE__));
return WPPlugin::plugins_directory() . "/${current_directory}";
}
static function url_to_plugin_directory() {
$current_directory = basename(dirname(__FILE__));
return WPPlugin::plugins_url() . "/${current_directory}";
}
static function path_to_plugin($file_path) {
$file_name = basename($file_path); // /etc/blah/file.txt => file.txt
if (WPPlugin::determine_environment() == Environment::WordPressMU)
return WPPlugin::plugins_directory() . "/${file_name}";
else
return WPPlugin::path_to_plugin_directory() . "/${file_name}";
}
// options
abstract protected function register_default_options();
// option retrieval
static function retrieve_options($options_name) {
if (WPPlugin::determine_environment() == Environment::WordPressMU)
return get_site_option($options_name);
else
return get_option($options_name);
}
static function remove_options($options_name) {
if (WPPlugin::determine_environment() == Environment::WordPressMU)
return delete_site_option($options_name);
else
return delete_option($options_name);
}
static function add_options($options_name, $options) {
if (WPPlugin::determine_environment() == Environment::WordPressMU)
return add_site_option($options_name, $options);
else
return add_option($options_name, $options);
}
protected function is_multi_blog() {
return $this->environment != Environment::WordPress;
}
// calls the appropriate 'authority' checking function depending on the environment
protected function is_authority() {
if ($this->environment == Environment::WordPress)
return is_admin();
if ($this->environment == Environment::WordPressMU)
return is_site_admin();
if ($this->environment == Environment::WordPressMS)
return is_super_admin();
}
}
}
?>