Completed
Push — fix/videopress-options-alignme... ( 894d9a...41f58b )
by
unknown
08:22
created

Jetpack_JSON_API_Plugins_Endpoint::format_plugin()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 28
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

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