class.jetpack-sync-module-plugins.php 7.7 KB

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