PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.5
Jetpack – WP Security, Backup, Speed, & Growth v7.2.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / sync / class.jetpack-sync-module-plugins.php

class.jetpack-sync-module-plugins.php in Jetpack – WP Security, Backup, Speed, & Growth 7.2.5, at sync/class.jetpack-sync-module-plugins.php

290 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Jetpack_Sync_Module_Plugins extends Jetpack_Sync_Module {
4
5 private $action_handler;
6 private $plugin_info = array();
7 private $plugins = array();
8
9 public function name() {
10 return 'plugins';
11 }
12
13 public function init_listeners( $callable ) {
14 $this->action_handler = $callable;
15
16 add_action( 'deleted_plugin', array( $this, 'deleted_plugin' ), 10, 2 );
17 add_action( 'activated_plugin', $callable, 10, 2 );
18 add_action( 'deactivated_plugin', $callable, 10, 2 );
19 add_action( 'delete_plugin', array( $this, 'delete_plugin' ) );
20 add_filter( 'upgrader_pre_install', array( $this, 'populate_plugins' ), 10, 1 );
21 add_action( 'upgrader_process_complete', array( $this, 'on_upgrader_completion' ), 10, 2 );
22 add_action( 'jetpack_plugin_installed', $callable, 10, 1 );
23 add_action( 'jetpack_plugin_update_failed', $callable, 10, 4 );
24 add_action( 'jetpack_plugins_updated', $callable, 10, 2 );
25 add_action( 'admin_action_update', array( $this, 'check_plugin_edit' ) );
26 add_action( 'jetpack_edited_plugin', $callable, 10, 2 );
27 add_action( 'wp_ajax_edit-theme-plugin-file', array( $this, 'plugin_edit_ajax' ), 0 );
28 }
29
30 public function init_before_send() {
31 add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) );
32 add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) );
33 // Note that we don't simply 'expand_plugin_data' on the 'delete_plugin' action here because the plugin file is deleted when that action finishes
34 }
35 public function populate_plugins( $response ) {
36 $this->plugins = get_plugins();
37 return $response;
38 }
39 public function on_upgrader_completion( $upgrader, $details ) {
40 if ( ! isset( $details['type'] ) ) {
41 return;
42 }
43 if ( 'plugin' != $details['type'] ) {
44 return;
45 }
46
47 if ( ! isset( $details['action'] ) ) {
48 return;
49 }
50
51 $plugins = ( isset( $details['plugins'] ) ? $details['plugins'] : null );
52 if ( empty( $plugins ) ) {
53 $plugins = ( isset( $details['plugin'] ) ? array( $details['plugin'] ) : null );
54 }
55
56 // for plugin installer
57 if ( empty( $plugins ) && method_exists( $upgrader, 'plugin_info' ) ) {
58 $plugins = array( $upgrader->plugin_info() );
59 }
60
61 if ( empty( $plugins ) ) {
62 return; // We shouldn't be here
63 }
64
65 switch ( $details['action'] ) {
66 case 'update':
67 $state = array(
68 'is_autoupdate' => Jetpack_Constants::is_true( 'JETPACK_PLUGIN_AUTOUPDATE' ),
69 );
70 $errors = $this->get_errors( $upgrader->skin );
71 if ( $errors ) {
72 foreach ( $plugins as $slug ) {
73 /**
74 * Sync that a plugin update failed
75 *
76 * @since 5.8.0
77 *
78 * @module sync
79 *
80 * @param string $plugin , Plugin slug
81 * @param string Error code
82 * @param string Error message
83 */
84 do_action( 'jetpack_plugin_update_failed', $this->get_plugin_info( $slug ), $errors['code'], $errors['message'], $state );
85 }
86
87 return;
88 }
89 /**
90 * Sync that a plugin update
91 *
92 * @since 5.8.0
93 *
94 * @module sync
95 *
96 * @param array () $plugin, Plugin Data
97 */
98 do_action( 'jetpack_plugins_updated', array_map( array( $this, 'get_plugin_info' ), $plugins ), $state );
99 break;
100 case 'install':
101 }
102
103 if ( 'install' === $details['action'] ) {
104 /**
105 * Signals to the sync listener that a plugin was installed and a sync action
106 * reflecting the installation and the plugin info should be sent
107 *
108 * @since 5.8.0
109 *
110 * @module sync
111 *
112 * @param array () $plugin, Plugin Data
113 */
114 do_action( 'jetpack_plugin_installed', array_map( array( $this, 'get_plugin_info' ), $plugins ) );
115
116 return;
117 }
118 }
119
120 private function get_plugin_info( $slug ) {
121 $plugins = get_plugins(); // Get the most up to date info
122 if ( isset( $plugins[ $slug ] ) ) {
123 return array_merge( array( 'slug' => $slug ), $plugins[ $slug ] );
124 };
125 // Try grabbing the info from before the update
126 return isset( $this->plugins[ $slug ] ) ? array_merge( array( 'slug' => $slug ), $this->plugins[ $slug ] ) : array( 'slug' => $slug );
127 }
128
129 private function get_errors( $skin ) {
130 $errors = method_exists( $skin, 'get_errors' ) ? $skin->get_errors() : null;
131 if ( is_wp_error( $errors ) ) {
132 $error_code = $errors->get_error_code();
133 if ( ! empty( $error_code ) ) {
134 return array(
135 'code' => $error_code,
136 'message' => $errors->get_error_message(),
137 );
138 }
139 }
140
141 if ( isset( $skin->result ) ) {
142 $errors = $skin->result;
143 if ( is_wp_error( $errors ) ) {
144 return array(
145 'code' => $errors->get_error_code(),
146 'message' => $errors->get_error_message(),
147 );
148 }
149
150 if ( false == $skin->result ) {
151 return array(
152 'code' => 'unknown',
153 'message' => __( 'Unknown Plugin Update Failure', 'jetpack' ),
154 );
155 }
156 }
157 return false;
158 }
159
160 public function check_plugin_edit() {
161 $screen = get_current_screen();
162 if ( 'plugin-editor' !== $screen->base ||
163 ! isset( $_POST['newcontent'] ) ||
164 ! isset( $_POST['plugin'] )
165 ) {
166 return;
167 }
168
169 $plugin = $_POST['plugin'];
170 $plugins = get_plugins();
171 if ( ! isset( $plugins[ $plugin ] ) ) {
172 return;
173 }
174
175 /**
176 * Helps Sync log that a plugin was edited
177 *
178 * @since 4.9.0
179 *
180 * @param string $plugin, Plugin slug
181 * @param mixed $plugins[ $plugin ], Array of plugin data
182 */
183 do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] );
184 }
185
186 public function plugin_edit_ajax() {
187 // this validation is based on wp_edit_theme_plugin_file()
188 $args = wp_unslash( $_POST );
189 if ( empty( $args['file'] ) ) {
190 return;
191 }
192
193 $file = $args['file'];
194 if ( 0 !== validate_file( $file ) ) {
195 return;
196 }
197
198 if ( ! isset( $args['newcontent'] ) ) {
199 return;
200 }
201
202 if ( ! isset( $args['nonce'] ) ) {
203 return;
204 }
205
206 if ( empty( $args['plugin'] ) ) {
207 return;
208 }
209
210 $plugin = $args['plugin'];
211 if ( ! current_user_can( 'edit_plugins' ) ) {
212 return;
213 }
214
215 if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) {
216 return;
217 }
218 $plugins = get_plugins();
219 if ( ! array_key_exists( $plugin, $plugins ) ) {
220 return;
221 }
222
223 if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) {
224 return;
225 }
226
227 $real_file = WP_PLUGIN_DIR . '/' . $file;
228
229 if ( ! is_writeable( $real_file ) ) {
230 return;
231 }
232
233 $file_pointer = fopen( $real_file, 'w+' );
234 if ( false === $file_pointer ) {
235 return;
236 }
237 fclose( $file_pointer );
238 /**
239 * This action is documented already in this file
240 */
241 do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] );
242 }
243
244 public function delete_plugin( $plugin_path ) {
245 $full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
246
247 // Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
248 if ( file_exists( $full_plugin_path ) ) {
249 $all_plugin_data = get_plugin_data( $full_plugin_path );
250 $data = array(
251 'name' => $all_plugin_data['Name'],
252 'version' => $all_plugin_data['Version'],
253 );
254 } else {
255 $data = array(
256 'name' => $plugin_path,
257 'version' => 'unknown',
258 );
259 }
260
261 $this->plugin_info[ $plugin_path ] = $data;
262 }
263
264 public function deleted_plugin( $plugin_path, $is_deleted ) {
265 call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] );
266 unset( $this->plugin_info[ $plugin_path ] );
267 }
268
269 public function expand_plugin_data( $args ) {
270 $plugin_path = $args[0];
271 $plugin_data = array();
272
273 if ( ! function_exists( 'get_plugins' ) ) {
274 require_once ABSPATH . 'wp-admin/includes/plugin.php';
275 }
276 $all_plugins = get_plugins();
277 if ( isset( $all_plugins[ $plugin_path ] ) ) {
278 $all_plugin_data = $all_plugins[ $plugin_path ];
279 $plugin_data['name'] = $all_plugin_data['Name'];
280 $plugin_data['version'] = $all_plugin_data['Version'];
281 }
282
283 return array(
284 $args[0],
285 $args[1],
286 $plugin_data,
287 );
288 }
289 }
290