Completed
Push — add/plugin_name ( 0d020e...9707f3 )
by
unknown
08:36
created

Jetpack_Sync_Module_Plugins::expand_plugin_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class Jetpack_Sync_Module_Plugins extends Jetpack_Sync_Module {
4
5
	public function name() {
6
		return 'plugins';
7
	}
8
9
	public function init_listeners( $callable ) {
10
		add_action( 'deleted_plugin', $callable, 10, 2 );
11
		add_action( 'activated_plugin', $callable, 10, 2 );
12
		add_action( 'deactivated_plugin', $callable, 10, 2 );
13
		add_action( 'delete_plugin', array( $this, 'sync_deleted_plugin_info') );
14
		add_action( 'jetpack_delete_plugin', $callable, 10, 2);
15
	}
16
17
	public function init_before_send() {
18
		add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) );
19
		add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) );
20
		//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
21
	}
22
23
	public function sync_deleted_plugin_info( $plugin_path ) {
24
		$all_plugin_data = get_plugin_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path );
25
		$data = array (
26
			'name' => $all_plugin_data['Name'],
27
			'version' => $all_plugin_data['Version'],
28
		);
29
30
		/**
31
		 * Syncs information about a plugin whose deletion was attempted
32
		 *
33
		 * @since 4.8.3
34
		 *
35
		 * @param string $plugin_path Path of plugin whose deletion was attempted (deletion verified in deleted_plugin sync action)
36
		 * @param mixed $data Array of plugin information fields (name, version, etc.)
37
		 */
38
		do_action( 'jetpack_delete_plugin', $plugin_path, $data );
39
	}
40
41
	public function expand_plugin_data( $args ) {
42
		$plugin_path = $args[0];
43
		$plugin_data = array();
44
45
		//Try to get plugin data from cache (if it isn't cached, get_plugins() tries to get it from disk)
46
		$all_plugins = get_plugins();
47
		if ( isset( $all_plugins[$plugin_path] ) ) {
48
			$all_plugin_data = $all_plugins[$plugin_path];
49
			$plugin_data['name'] = $all_plugin_data['Name'];
50
			$plugin_data['version'] = $all_plugin_data['Version'];
51
		}
52
53
		return array(
54
			$args[0],
55
			$args[1],
56
			$plugin_data,
57
		);
58
	}
59
}
60