Completed
Push — core/custom-css-4.7 ( 7f4836...adaf63 )
by
unknown
09:28
created

get_plugin_slug()   C

Complexity

Conditions 8
Paths 27

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 13
nc 27
nop 1
dl 0
loc 23
rs 6.1403
c 0
b 0
f 0
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(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $_response_format.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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
		'next_autoupdate' => '(string) Y-m-d H:i:s for next scheduled update event',
29
		'log'             => '(array:safehtml) An array of update log strings.',
30
		'uninstallable'   => '(boolean) Whether the plugin is unistallable.',
31
	);
32
33
	protected function result() {
34
35
		$plugins = $this->get_plugins();
36
37
		if ( ! $this->bulk && ! empty( $plugins ) ) {
38
			return array_pop( $plugins );
39
		}
40
41
		return array( 'plugins' => $plugins );
42
43
	}
44
45
	protected function validate_input( $plugin ) {
46
47
		if ( is_wp_error( $error = parent::validate_input( $plugin ) ) ) {
48
			return $error;
49
		}
50
51
		if ( is_wp_error( $error = $this->validate_network_wide() ) ) {
52
			return $error;
53
		}
54
55
		$args = $this->input();
56
		// find out what plugin, or plugins we are dealing with
57
		// validate the requested plugins
58
		if ( ! isset( $plugin ) || empty( $plugin ) ) {
59
			if ( ! $args['plugins'] || empty( $args['plugins'] ) ) {
60
				return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin.', 'jetpack' ), 400 );
61
			}
62
			if ( is_array( $args['plugins'] ) ) {
63
				$this->plugins = $args['plugins'];
64
			} else {
65
				$this->plugins[] = $args['plugins'];
66
			}
67
		} else {
68
			$this->bulk = false;
69
			$this->plugins[] = urldecode( $plugin );
70
		}
71
72
		if ( is_wp_error( $error = $this->validate_plugins() ) ) {
73
			return $error;
74
		};
75
76
		return true;
77
	}
78
79
	/**
80
	 * Walks through submitted plugins to make sure they are valid
81
	 * @return bool|WP_Error
82
	 */
83
	protected function validate_plugins() {
84 View Code Duplication
		if ( empty( $this->plugins ) || ! is_array( $this->plugins ) ) {
85
			return new WP_Error( 'missing_plugins', __( 'No plugins found.', 'jetpack' ));
86
		}
87
		foreach( $this->plugins as $index => $plugin ) {
88
			if ( ! preg_match( "/\.php$/", $plugin ) ) {
89
				$plugin =  $plugin . '.php';
90
				$this->plugins[ $index ] = $plugin;
91
			}
92
			if ( is_wp_error( $error = $this->validate_plugin( $plugin ) ) ) {
93
				return $error;
94
			}
95
		}
96
		return true;
97
	}
98
99
	protected function format_plugin( $plugin_file, $plugin_data ) {
100
		$plugin = array();
101
		$plugin['id']              = preg_replace("/(.+)\.php$/", "$1", $plugin_file );
102
		$plugin['slug']            = $this->get_plugin_slug( $plugin_file );
103
		$plugin['active']          = Jetpack::is_plugin_active( $plugin_file );
104
		$plugin['name']            = $plugin_data['Name'];
105
		$plugin['plugin_url']      = $plugin_data['PluginURI'];
106
		$plugin['version']         = $plugin_data['Version'];
107
		$plugin['description']     = $plugin_data['Description'];
108
		$plugin['author']          = $plugin_data['Author'];
109
		$plugin['author_url']      = $plugin_data['AuthorURI'];
110
		$plugin['network']         = $plugin_data['Network'];
111
		$plugin['update']          = $this->get_plugin_updates( $plugin_file );
112
		$plugin['next_autoupdate'] = date( 'Y-m-d H:i:s', wp_next_scheduled( 'wp_maybe_auto_update' ) );
113
		$plugin['autoupdate']      = in_array( $plugin_file, Jetpack_Options::get_option( 'autoupdate_plugins', array() ) );
114
		$plugin['uninstallable']   = is_uninstallable_plugin( $plugin_file );
115
		if ( ! empty ( $this->log[ $plugin_file ] ) ) {
116
			$plugin['log'] = $this->log[ $plugin_file ];
117
		}
118
		return $plugin;
119
	}
120
121
	protected function get_plugins() {
122
		$plugins = array();
123
		/** This filter is documented in wp-admin/includes/class-wp-plugins-list-table.php */
124
		$installed_plugins = apply_filters( 'all_plugins', get_plugins() );
125
		foreach( $this->plugins as $plugin ) {
126
			if ( ! isset( $installed_plugins[ $plugin ] ) )
127
				continue;
128
			$plugins[] = $this->format_plugin( $plugin, $installed_plugins[ $plugin ] );
129
		}
130
		$args = $this->query_args();
131
132
		if ( isset( $args['offset'] ) ) {
133
			$plugins = array_slice( $plugins, (int) $args['offset'] );
134
		}
135
		if ( isset( $args['limit'] ) ) {
136
			$plugins = array_slice( $plugins, 0, (int) $args['limit'] );
137
		}
138
139
		return $plugins;
140
	}
141
142
	protected function validate_network_wide() {
143
		$args = $this->input();
144
145
		if ( isset( $args['network_wide'] ) && $args['network_wide'] ) {
146
			$this->network_wide = true;
147
		}
148
149
		if ( $this->network_wide && ! current_user_can( 'manage_network_plugins' ) ) {
150
			return new WP_Error( 'unauthorized', __( 'This user is not authorized to manage plugins network wide.', 'jetpack' ), 403 );
151
		}
152
153
		return true;
154
	}
155
156
157
	protected function validate_plugin( $plugin ) {
158
		if ( ! isset( $plugin) || empty( $plugin ) ) {
159
			return new WP_Error( 'missing_plugin', __( 'You are required to specify a plugin to activate.', 'jetpack' ), 400 );
160
		}
161
162
		if ( is_wp_error( $error = validate_plugin( urldecode( $plugin ) ) ) ) {
163
			return new WP_Error( 'unknown_plugin', $error->get_error_messages() , 404 );
164
		}
165
166
		return true;
167
	}
168
169
	protected function get_plugin_updates( $plugin_file ) {
170
		$plugin_updates = get_plugin_updates();
171
		if ( isset( $plugin_updates[ $plugin_file ] ) ){
172
			return $plugin_updates[ $plugin_file ]->update;
173
		}
174
		return null;
175
	}
176
177
	protected function get_plugin_slug( $plugin_file ) {
178
		$update_plugins   = get_site_transient( 'update_plugins' );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 3 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
179
		if ( isset( $update_plugins->no_update ) ) {
180
			if ( isset( $update_plugins->no_update[ $plugin_file ] ) ) {
181
				$slug = $update_plugins->no_update[ $plugin_file ]->slug;
182
			}
183
		}
184
185
		if ( empty( $slug ) && isset( $update_plugins->response ) ) {
186
			if ( isset( $update_plugins->response[ $plugin_file ] ) ) {
187
				$slug = $update_plugins->response[ $plugin_file ]->slug;
188
			}
189
		}
190
191
		// Try to infer from the plugin file if not cached
192
		if ( empty( $slug) ) {
193
			$slug = dirname( $plugin_file );
194
			if ( '.' === $slug ) {
195
				$slug = preg_replace("/(.+)\.php$/", "$1", $plugin_file );
196
			}
197
		}
198
		return $slug;
199
	}
200
}
201