1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Base class for working with plugins. |
5
|
|
|
*/ |
6
|
|
|
abstract class Jetpack_JSON_API_Plugins_Endpoint extends Jetpack_JSON_API_Endpoint { |
7
|
|
|
|
8
|
|
|
protected $plugins = array(); |
9
|
|
|
|
10
|
|
|
protected $network_wide = false; |
11
|
|
|
|
12
|
|
|
protected $bulk = true; |
13
|
|
|
protected $log; |
14
|
|
|
|
15
|
|
|
static $_response_format = array( |
|
|
|
|
16
|
|
|
'id' => '(safehtml) The plugin\'s ID', |
17
|
|
|
'slug' => '(safehtml) The plugin\'s .org slug', |
18
|
|
|
'active' => '(boolean) The plugin status.', |
19
|
|
|
'update' => '(object) The plugin update info.', |
20
|
|
|
'name' => '(safehtml) The name of the plugin.', |
21
|
|
|
'plugin_url' => '(url) Link to the plugin\'s web site.', |
22
|
|
|
'version' => '(safehtml) The plugin version number.', |
23
|
|
|
'description' => '(safehtml) Description of what the plugin does and/or notes from the author', |
24
|
|
|
'author' => '(safehtml) The author\'s name', |
25
|
|
|
'author_url' => '(url) The authors web site address', |
26
|
|
|
'network' => '(boolean) Whether the plugin can only be activated network wide.', |
27
|
|
|
'autoupdate' => '(boolean) Whether the plugin is automatically updated', |
28
|
|
|
'autoupdate_translation' => '(boolean) Whether the plugin is automatically updating translations', |
29
|
|
|
'next_autoupdate' => '(string) Y-m-d H:i:s for next scheduled update event', |
30
|
|
|
'log' => '(array:safehtml) An array of update log strings.', |
31
|
|
|
'uninstallable' => '(boolean) Whether the plugin is unistallable.', |
32
|
|
|
'action_links' => '(array) An array of action links that the plugin uses.', |
33
|
|
|
); |
34
|
|
|
|
35
|
|
|
protected function result() { |
36
|
|
|
|
37
|
|
|
$plugins = $this->get_plugins(); |
38
|
|
|
|
39
|
|
|
if ( ! $this->bulk && ! empty( $plugins ) ) { |
40
|
|
|
return array_pop( $plugins ); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return array( 'plugins' => $plugins ); |
44
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
protected function validate_input( $plugin ) { |
48
|
|
|
|
49
|
|
|
if ( is_wp_error( $error = parent::validate_input( $plugin ) ) ) { |
50
|
|
|
return $error; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ( is_wp_error( $error = $this->validate_network_wide() ) ) { |
54
|
|
|
return $error; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$args = $this->input(); |
58
|
|
|
// find out what plugin, or plugins we are dealing with |
59
|
|
|
// validate the requested plugins |
60
|
|
|
if ( ! isset( $plugin ) || empty( $plugin ) ) { |
61
|
|
|
if ( ! $args['plugins'] || empty( $args['plugins'] ) ) { |
62
|
|
|
return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin.', 'jetpack' ), 400 ); |
63
|
|
|
} |
64
|
|
|
if ( is_array( $args['plugins'] ) ) { |
65
|
|
|
$this->plugins = $args['plugins']; |
66
|
|
|
} else { |
67
|
|
|
$this->plugins[] = $args['plugins']; |
68
|
|
|
} |
69
|
|
|
} else { |
70
|
|
|
$this->bulk = false; |
71
|
|
|
$this->plugins[] = urldecode( $plugin ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
if ( is_wp_error( $error = $this->validate_plugins() ) ) { |
75
|
|
|
return $error; |
76
|
|
|
}; |
77
|
|
|
|
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Walks through submitted plugins to make sure they are valid |
83
|
|
|
* @return bool|WP_Error |
84
|
|
|
*/ |
85
|
|
|
protected function validate_plugins() { |
86
|
|
View Code Duplication |
if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) { |
87
|
|
|
return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' )); |
88
|
|
|
} |
89
|
|
|
foreach( $this->plugins as $index => $plugin ) { |
90
|
|
|
if ( ! preg_match( "/\.php$/", $plugin ) ) { |
91
|
|
|
$plugin = $plugin . '.php'; |
92
|
|
|
$this->plugins[ $index ] = $plugin; |
93
|
|
|
} |
94
|
|
|
$valid = $this->validate_plugin( urldecode( $plugin ) ) ; |
95
|
|
|
if ( is_wp_error( $valid ) ) { |
96
|
|
|
return $valid; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function format_plugin( $plugin_file, $plugin_data ) { |
104
|
|
|
$plugin = array(); |
105
|
|
|
$plugin['id'] = preg_replace("/(.+)\.php$/", "$1", $plugin_file ); |
106
|
|
|
$plugin['slug'] = Jetpack_Autoupdate::get_plugin_slug( $plugin_file ); |
107
|
|
|
$plugin['active'] = Jetpack::is_plugin_active( $plugin_file ); |
108
|
|
|
$plugin['name'] = $plugin_data['Name']; |
109
|
|
|
$plugin['plugin_url'] = $plugin_data['PluginURI']; |
110
|
|
|
$plugin['version'] = $plugin_data['Version']; |
111
|
|
|
$plugin['description'] = $plugin_data['Description']; |
112
|
|
|
$plugin['author'] = $plugin_data['Author']; |
113
|
|
|
$plugin['author_url'] = $plugin_data['AuthorURI']; |
114
|
|
|
$plugin['network'] = $plugin_data['Network']; |
115
|
|
|
$plugin['update'] = $this->get_plugin_updates( $plugin_file ); |
116
|
|
|
$plugin['next_autoupdate'] = date( 'Y-m-d H:i:s', wp_next_scheduled( 'wp_maybe_auto_update' ) ); |
117
|
|
|
$plugin['action_links'] = $this->get_plugin_action_links( $plugin_file ); |
118
|
|
|
|
119
|
|
|
$autoupdate = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) ); |
120
|
|
|
$plugin['autoupdate'] = $autoupdate; |
121
|
|
|
|
122
|
|
|
$autoupdate_translation = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins_translations', array() ) ); |
123
|
|
|
$plugin['autoupdate_translation'] = $autoupdate || $autoupdate_translation || Jetpack_Options::get_option( 'autoupdate_translations', false ); |
124
|
|
|
|
125
|
|
|
$plugin['uninstallable'] = is_uninstallable_plugin( $plugin_file ); |
|
|
|
|
126
|
|
|
|
127
|
|
|
if ( ! empty ( $this->log[ $plugin_file ] ) ) { |
128
|
|
|
$plugin['log'] = $this->log[ $plugin_file ]; |
129
|
|
|
} |
130
|
|
|
return $plugin; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
protected function get_plugins() { |
134
|
|
|
// Do the admin_init action in order to capture plugin action links. |
135
|
|
|
// See get_plugin_action_links() |
136
|
|
|
/** This action is documented in wp-admin/admin.php */ |
137
|
|
|
do_action( 'admin_init' ); |
138
|
|
|
$plugins = array(); |
139
|
|
|
/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */ |
140
|
|
|
$installed_plugins = apply_filters( 'all_plugins', get_plugins() ); |
141
|
|
|
foreach( $this->plugins as $plugin ) { |
142
|
|
|
if ( ! isset( $installed_plugins[ $plugin ] ) ) |
143
|
|
|
continue; |
144
|
|
|
$plugins[] = $this->format_plugin( $plugin, $installed_plugins[ $plugin ] ); |
145
|
|
|
} |
146
|
|
|
$args = $this->query_args(); |
147
|
|
|
|
148
|
|
|
if ( isset( $args['offset'] ) ) { |
149
|
|
|
$plugins = array_slice( $plugins, (int) $args['offset'] ); |
150
|
|
|
} |
151
|
|
|
if ( isset( $args['limit'] ) ) { |
152
|
|
|
$plugins = array_slice( $plugins, 0, (int) $args['limit'] ); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return $plugins; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
protected function validate_network_wide() { |
159
|
|
|
$args = $this->input(); |
160
|
|
|
|
161
|
|
|
if ( isset( $args['network_wide'] ) && $args['network_wide'] ) { |
162
|
|
|
$this->network_wide = true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
if ( $this->network_wide && ! current_user_can( 'manage_network_plugins' ) ) { |
166
|
|
|
return new WP_Error( 'unauthorized', __( 'This user is not authorized to manage plugins network wide.', 'jetpack' ), 403 ); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return true; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
|
173
|
|
|
protected function validate_plugin( $plugin ) { |
174
|
|
|
if ( ! isset( $plugin) || empty( $plugin ) ) { |
175
|
|
|
return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin to activate.', 'jetpack' ), 400 ); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
if ( is_wp_error( $error = validate_plugin( $plugin ) ) ) { |
179
|
|
|
return new WP_Error( 'unknown_plugin', $error->get_error_messages() , 404 ); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
return true; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
protected function get_plugin_updates( $plugin_file ) { |
186
|
|
|
$plugin_updates = get_plugin_updates(); |
187
|
|
|
if ( isset( $plugin_updates[ $plugin_file ] ) ){ |
188
|
|
|
return $plugin_updates[ $plugin_file ]->update; |
189
|
|
|
} |
190
|
|
|
return null; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
protected function get_plugin_action_links( $plugin_file ) { |
194
|
|
|
require_once JETPACK__PLUGIN_DIR . 'sync/class.jetpack-sync-functions.php'; |
195
|
|
|
return Jetpack_Sync_Functions::get_plugins_action_links( $plugin_file ); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.