Completed
Push — branch-4.9-built ( f63861 )
by
unknown
737:56 queued 727:22
created

Jetpack_Sync_Module_Plugins::deleted_plugin()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
	}
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
27
	public function delete_plugin( $plugin_path ) {
28
		$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
29
30
		//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
31
		if ( file_exists( $full_plugin_path ) ) {
32
			$all_plugin_data = get_plugin_data( $full_plugin_path );
33
			$data = array(
34
				'name' => $all_plugin_data['Name'],
35
				'version' => $all_plugin_data['Version'],
36
			);
37
		} else {
38
			$data = array(
39
				'name' => $plugin_path,
40
				'version' => 'unknown',
41
			);
42
		}
43
44
		$this->plugin_info[ $plugin_path ] = $data;
45
	}
46
47
	public function deleted_plugin( $plugin_path, $is_deleted ) {
48
		call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] );
49
		unset( $this->plugin_info[ $plugin_path ] );
50
	}
51
52
	public function expand_plugin_data( $args ) {
53
		$plugin_path = $args[0];
54
		$plugin_data = array();
55
56
		$all_plugins = get_plugins();
57
		if ( isset( $all_plugins[ $plugin_path ] ) ) {
58
			$all_plugin_data = $all_plugins[ $plugin_path ];
59
			$plugin_data['name'] = $all_plugin_data['Name'];
60
			$plugin_data['version'] = $all_plugin_data['Version'];
61
		}
62
63
		return array(
64
			$args[0],
65
			$args[1],
66
			$plugin_data,
67
		);
68
	}
69
}
70