Completed
Push — add/plugin_install ( 13506f )
by
unknown
12:20
created

Jetpack_Sync_Module_Plugins   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 76
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 76
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 1

7 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 10 1
A init_before_send() 0 5 1
A install_plugin() 0 6 2
A delete_plugin() 0 19 2
A deleted_plugin() 0 4 1
A expand_plugin_data() 0 17 2
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, 'install_plugin') );
20
		add_action( 'jetpack_installed_plugin', $callable );
21
	}
22
23
	public function init_before_send() {
24
		add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) );
25
		add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) );
26
		//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
27
	}
28
29
	public function install_plugin( $upgrader ) {
30
		if ( 'Plugin_Upgrader' === get_class( $upgrader ) ) {
31
			error_log(print_r($upgrader->plugin_info(), true) );
32
		}
33
34
	}
35
36
	public function delete_plugin( $plugin_path ) {
37
		$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
38
39
		//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
40
		if ( file_exists( $full_plugin_path ) ) {
41
			$all_plugin_data = get_plugin_data( $full_plugin_path );
42
			$data = array(
43
				'name' => $all_plugin_data['Name'],
44
				'version' => $all_plugin_data['Version'],
45
			);
46
		} else {
47
			$data = array(
48
				'name' => $plugin_path,
49
				'version' => 'unknown',
50
			);
51
		}
52
53
		$this->plugin_info[ $plugin_path ] = $data;
54
	}
55
56
	public function deleted_plugin( $plugin_path, $is_deleted ) {
57
		call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] );
58
		unset( $this->plugin_info[ $plugin_path ] );
59
	}
60
61
	public function expand_plugin_data( $args ) {
62
		$plugin_path = $args[0];
63
		$plugin_data = array();
64
65
		$all_plugins = get_plugins();
66
		if ( isset( $all_plugins[ $plugin_path ] ) ) {
67
			$all_plugin_data = $all_plugins[ $plugin_path ];
68
			$plugin_data['name'] = $all_plugin_data['Name'];
69
			$plugin_data['version'] = $all_plugin_data['Version'];
70
		}
71
72
		return array(
73
			$args[0],
74
			$args[1],
75
			$plugin_data,
76
		);
77
	}
78
}
79