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