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