| 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 |
|
| 104 |
if ( 'install' === $details['action'] ) { |
| 105 |
/** |
| 106 |
* Signals to the sync listener that a plugin was installed and a sync action |
| 107 |
* reflecting the installation and the plugin info should be sent |
| 108 |
* |
| 109 |
* @since 5.8.0 |
| 110 |
* |
| 111 |
* @module sync |
| 112 |
* |
| 113 |
* @param array () $plugin, Plugin Data |
| 114 |
*/ |
| 115 |
do_action( 'jetpack_plugin_installed', array_map( array( $this, 'get_plugin_info' ), $plugins ) ); |
| 116 |
|
| 117 |
return; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
private function get_plugin_info( $slug ) { |
| 122 |
$plugins = get_plugins(); // Get the most up to date info |
| 123 |
if ( isset( $plugins[ $slug ] ) ) { |
| 124 |
return array_merge( array( 'slug' => $slug ), $plugins[ $slug ] ); |
| 125 |
}; |
| 126 |
// Try grabbing the info from before the update |
| 127 |
return isset( $this->plugins[ $slug ] ) ? array_merge( array( 'slug' => $slug ), $this->plugins[ $slug ] ): array( 'slug' => $slug ); |
| 128 |
} |
| 129 |
|
| 130 |
private function get_errors( $skin ) { |
| 131 |
$errors = method_exists( $skin, 'get_errors' ) ? $skin->get_errors() : null; |
| 132 |
if ( is_wp_error( $errors ) ) { |
| 133 |
$error_code = $errors->get_error_code(); |
| 134 |
if ( ! empty( $error_code ) ) { |
| 135 |
return array( 'code' => $error_code, 'message' => $errors->get_error_message() ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
if ( isset( $skin->result ) ) { |
| 140 |
$errors = $skin->result; |
| 141 |
if ( is_wp_error( $errors ) ) { |
| 142 |
return array( 'code' => $errors->get_error_code(), 'message' => $errors->get_error_message() ); |
| 143 |
} |
| 144 |
|
| 145 |
if ( false == $skin->result ) { |
| 146 |
return array( 'code' => 'unknown', 'message' => __( 'Unknown Plugin Update Failure', 'jetpack' ) ); |
| 147 |
} |
| 148 |
} |
| 149 |
return false; |
| 150 |
} |
| 151 |
|
| 152 |
public function check_plugin_edit() { |
| 153 |
$screen = get_current_screen(); |
| 154 |
if ( 'plugin-editor' !== $screen->base || |
| 155 |
! isset( $_POST['newcontent'] ) || |
| 156 |
! isset( $_POST['plugin'] ) |
| 157 |
) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
$plugin = $_POST['plugin']; |
| 162 |
$plugins = get_plugins(); |
| 163 |
if ( ! isset( $plugins[ $plugin ] ) ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Helps Sync log that a plugin was edited |
| 169 |
* |
| 170 |
* @since 4.9.0 |
| 171 |
* |
| 172 |
* @param string $plugin, Plugin slug |
| 173 |
* @param mixed $plugins[ $plugin ], Array of plugin data |
| 174 |
*/ |
| 175 |
do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] ); |
| 176 |
} |
| 177 |
|
| 178 |
public function plugin_edit_ajax() { |
| 179 |
// this validation is based on wp_edit_theme_plugin_file() |
| 180 |
$args = wp_unslash( $_POST ); |
| 181 |
if ( empty( $args['file'] ) ) { |
| 182 |
return; |
| 183 |
} |
| 184 |
|
| 185 |
$file = $args['file']; |
| 186 |
if ( 0 !== validate_file( $file ) ) { |
| 187 |
return; |
| 188 |
} |
| 189 |
|
| 190 |
if ( ! isset( $args['newcontent'] ) ) { |
| 191 |
return; |
| 192 |
} |
| 193 |
|
| 194 |
if ( ! isset( $args['nonce'] ) ) { |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
if ( empty( $args['plugin'] ) ) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
$plugin = $args['plugin']; |
| 203 |
if ( ! current_user_can( 'edit_plugins' ) ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) { |
| 208 |
return; |
| 209 |
} |
| 210 |
$plugins = get_plugins(); |
| 211 |
if ( ! array_key_exists( $plugin, $plugins ) ) { |
| 212 |
return; |
| 213 |
} |
| 214 |
|
| 215 |
if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) { |
| 216 |
return; |
| 217 |
} |
| 218 |
|
| 219 |
$real_file = WP_PLUGIN_DIR . '/' . $file; |
| 220 |
|
| 221 |
if ( ! is_writeable( $real_file ) ) { |
| 222 |
return; |
| 223 |
} |
| 224 |
|
| 225 |
$file_pointer = fopen( $real_file, 'w+' ); |
| 226 |
if ( false === $file_pointer ) { |
| 227 |
return; |
| 228 |
} |
| 229 |
fclose( $file_pointer ); |
| 230 |
/** |
| 231 |
* This action is documented already in this file |
| 232 |
*/ |
| 233 |
do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] ); |
| 234 |
} |
| 235 |
|
| 236 |
public function delete_plugin( $plugin_path ) { |
| 237 |
$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path; |
| 238 |
|
| 239 |
//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk |
| 240 |
if ( file_exists( $full_plugin_path ) ) { |
| 241 |
$all_plugin_data = get_plugin_data( $full_plugin_path ); |
| 242 |
$data = array( |
| 243 |
'name' => $all_plugin_data['Name'], |
| 244 |
'version' => $all_plugin_data['Version'], |
| 245 |
); |
| 246 |
} else { |
| 247 |
$data = array( |
| 248 |
'name' => $plugin_path, |
| 249 |
'version' => 'unknown', |
| 250 |
); |
| 251 |
} |
| 252 |
|
| 253 |
$this->plugin_info[ $plugin_path ] = $data; |
| 254 |
} |
| 255 |
|
| 256 |
public function deleted_plugin( $plugin_path, $is_deleted ) { |
| 257 |
call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] ); |
| 258 |
unset( $this->plugin_info[ $plugin_path ] ); |
| 259 |
} |
| 260 |
|
| 261 |
public function expand_plugin_data( $args ) { |
| 262 |
$plugin_path = $args[0]; |
| 263 |
$plugin_data = array(); |
| 264 |
|
| 265 |
if ( ! function_exists( 'get_plugins' ) ) { |
| 266 |
require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 267 |
} |
| 268 |
$all_plugins = get_plugins(); |
| 269 |
if ( isset( $all_plugins[ $plugin_path ] ) ) { |
| 270 |
$all_plugin_data = $all_plugins[ $plugin_path ]; |
| 271 |
$plugin_data['name'] = $all_plugin_data['Name']; |
| 272 |
$plugin_data['version'] = $all_plugin_data['Version']; |
| 273 |
} |
| 274 |
|
| 275 |
return array( |
| 276 |
$args[0], |
| 277 |
$args[1], |
| 278 |
$plugin_data, |
| 279 |
); |
| 280 |
} |
| 281 |
} |
| 282 |
|