Jetpack_Sync_Module_Plugins

The WordPress Core Jetpack Sync Module Plugins class.

Defined (1)

The class is defined in the following location(s).

/sync/class.jetpack-sync-module-plugins.php  
  1. class Jetpack_Sync_Module_Plugins extends Jetpack_Sync_Module { 
  2.  
  3. private $action_handler; 
  4. private $plugin_info = array(); 
  5.  
  6. public function name() { 
  7. return 'plugins'; 
  8.  
  9. public function init_listeners( $callable ) { 
  10. $this->action_handler = $callable; 
  11.  
  12. add_action( 'deleted_plugin', array( $this, 'deleted_plugin' ), 10, 2 ); 
  13. add_action( 'activated_plugin', $callable, 10, 2 ); 
  14. add_action( 'deactivated_plugin', $callable, 10, 2 ); 
  15. add_action( 'delete_plugin', array( $this, 'delete_plugin') ); 
  16. add_action( 'upgrader_process_complete', array( $this, 'check_upgrader'), 10, 2 ); 
  17. add_action( 'jetpack_installed_plugin', $callable, 10, 2 ); 
  18. add_action( 'admin_action_update', array( $this, 'check_plugin_edit') ); 
  19. add_action( 'jetpack_edited_plugin', $callable, 10, 2 ); 
  20.  
  21. public function init_before_send() { 
  22. add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) ); 
  23. add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) ); 
  24. //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 
  25.  
  26. public function check_upgrader( $upgrader, $details) { 
  27.  
  28. if ( ! isset( $details['type'] ) || 
  29. 'plugin' !== $details['type'] || 
  30. is_wp_error( $upgrader->skin->result ) || 
  31. ! method_exists( $upgrader, 'plugin_info' ) 
  32. ) { 
  33. return; 
  34.  
  35. if ( 'install' === $details['action'] ) { 
  36. $plugin_path = $upgrader->plugin_info(); 
  37. $plugins = get_plugins(); 
  38. $plugin_info = $plugins[ $plugin_path ]; 
  39.  
  40. /** 
  41. * Signals to the sync listener that a plugin was installed and a sync action 
  42. * reflecting the installation and the plugin info should be sent 
  43. * @since 4.9.0 
  44. * @param string $plugin_path Path of plugin installed 
  45. * @param mixed $plugin_info Array of info describing plugin installed 
  46. */ 
  47. do_action( 'jetpack_installed_plugin', $plugin_path, $plugin_info ); 
  48.  
  49. public function check_plugin_edit() { 
  50. $screen = get_current_screen(); 
  51. if ( 'plugin-editor' !== $screen->base || 
  52. ! isset( $_POST['newcontent'] ) || 
  53. ! isset( $_POST['plugin'] ) 
  54. ) { 
  55. return; 
  56.  
  57. $plugin = $_POST['plugin']; 
  58. $plugins = get_plugins(); 
  59. if ( ! isset( $plugins[ $plugin ] ) ) { 
  60. return; 
  61.  
  62. /** 
  63. * Helps Sync log that a plugin was edited 
  64. * @since 4.9.0 
  65. * @param string $plugin, Plugin slug 
  66. * @param mixed $plugins[ $plugin ], Array of plugin data 
  67. */ 
  68. do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] ); 
  69.  
  70. public function delete_plugin( $plugin_path ) { 
  71. $full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path; 
  72.  
  73. //Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk 
  74. if ( file_exists( $full_plugin_path ) ) { 
  75. $all_plugin_data = get_plugin_data( $full_plugin_path ); 
  76. $data = array( 
  77. 'name' => $all_plugin_data['Name'],  
  78. 'version' => $all_plugin_data['Version'],  
  79. ); 
  80. } else { 
  81. $data = array( 
  82. 'name' => $plugin_path,  
  83. 'version' => 'unknown',  
  84. ); 
  85.  
  86. $this->plugin_info[ $plugin_path ] = $data; 
  87.  
  88. public function deleted_plugin( $plugin_path, $is_deleted ) { 
  89. call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] ); 
  90. unset( $this->plugin_info[ $plugin_path ] ); 
  91.  
  92. public function expand_plugin_data( $args ) { 
  93. $plugin_path = $args[0]; 
  94. $plugin_data = array(); 
  95.  
  96. $all_plugins = get_plugins(); 
  97. if ( isset( $all_plugins[ $plugin_path ] ) ) { 
  98. $all_plugin_data = $all_plugins[ $plugin_path ]; 
  99. $plugin_data['name'] = $all_plugin_data['Name']; 
  100. $plugin_data['version'] = $all_plugin_data['Version']; 
  101.  
  102. return array( 
  103. $args[0],  
  104. $args[1],  
  105. $plugin_data,  
  106. );