Completed
Push — add/plugin_name ( 66850b...2a05d3 )
by
unknown
11:21
created

Jetpack_Sync_Module_Plugins::expand_plugin_data()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 4
nop 1
dl 0
loc 26
rs 8.8571
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( 'delete_plugin', $callable ); //This fires before the deletion, so plugin data is still available for the activity log
11
		add_action( 'deleted_plugin', $callable, 10, 2 ); //This fires after the deletion, and will inform us if the deletion failed
12
		add_action( 'activated_plugin', $callable, 10, 2 );
13
		add_action( 'deactivated_plugin', $callable, 10, 2 );
14
	}
15
16
	public function init_before_send() {
17
		add_filter( 'jetpack_sync_before_send_delete_plugin', array( $this, 'expand_plugin_data' ) );
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
	}
21
22
	public function expand_plugin_data( $args ) {
23
		$plugin_path = $args[0];
24
		$plugin_data = array();
25
26
		//Try to get plugin data from cache (if it isn't cached, get_plugins() tries to get it from disk)
27
		$all_plugins = get_plugins();
28
		if ( isset( $all_plugins[$plugin_path] ) ) {
29
			$all_plugin_data = $all_plugins[$plugin_path];
30
			$plugin_data['Name'] = $all_plugin_data['Name'];
31
			$plugin_data['Version'] = $all_plugin_data['Version'];
32
		}
33
34
		//If action is 'delete_plugin', it will have 1 argument, whereas it will have 2 if 'activated_plugin' or 'deactivated_plugin'. 
35
		if ( 'delete_plugin' === current_filter() ) {
36
			return array(
37
				$args[0],
38
				$plugin_data,
39
			);
40
		}
41
42
		return array(
43
			$args[0],
44
			$args[1],
45
			$plugin_data,
46
		);
47
	}
48
}
49