Completed
Push — add/plugin_install ( 3a9d43...6595e5 )
by
unknown
28:09 queued 19:40
created

Jetpack_Sync_Module_Plugins::check_upgrader()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 27
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 11
nc 3
nop 2
dl 0
loc 27
rs 8.439
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
		add_action( 'upgrader_process_complete', array( $this, 'check_upgrader'), 10, 2 );
20
		add_action( 'jetpack_installed_plugin', $callable, 10, 2 );
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 check_upgrader( $upgrader, $details) {
30
31
		if ( ! isset( $details['type'] ) ||
32
			'plugin' !== $details['type'] ||
33
			is_wp_error( $upgrader->skin->result ) ||
34
			! method_exists( $upgrader, 'plugin_info' )
35
		) {
36
			return;
37
		}
38
39
		if ( 'install' === $details['action'] ) {
40
			$plugin_path = $upgrader->plugin_info();
41
			$plugins = get_plugins();
42
			$plugin_info = $plugins[ $plugin_path ];
43
44
			/**
45
			 * Signals to the sync listener that a plugin was installed and a sync action
46
			 * reflecting the installation and the plugin info should be sent
47
			 *
48
			 * @since 4.9.0
49
			 *
50
			 * @param string $plugin_path Path of plugin installed
51
			 * @param mixed $plugin_info Array of info describing plugin installed
52
			 */
53
			do_action( 'jetpack_installed_plugin', $plugin_path, $plugin_info );
54
		}
55
	}
56
57
	public function delete_plugin( $plugin_path ) {
58
		$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
59
60
		//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
61
		if ( file_exists( $full_plugin_path ) ) {
62
			$all_plugin_data = get_plugin_data( $full_plugin_path );
63
			$data = array(
64
				'name' => $all_plugin_data['Name'],
65
				'version' => $all_plugin_data['Version'],
66
			);
67
		} else {
68
			$data = array(
69
				'name' => $plugin_path,
70
				'version' => 'unknown',
71
			);
72
		}
73
74
		$this->plugin_info[ $plugin_path ] = $data;
75
	}
76
77
	public function deleted_plugin( $plugin_path, $is_deleted ) {
78
		call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] );
79
		unset( $this->plugin_info[ $plugin_path ] );
80
	}
81
82
	public function expand_plugin_data( $args ) {
83
		$plugin_path = $args[0];
84
		$plugin_data = array();
85
86
		$all_plugins = get_plugins();
87
		if ( isset( $all_plugins[ $plugin_path ] ) ) {
88
			$all_plugin_data = $all_plugins[ $plugin_path ];
89
			$plugin_data['name'] = $all_plugin_data['Name'];
90
			$plugin_data['version'] = $all_plugin_data['Version'];
91
		}
92
93
		return array(
94
			$args[0],
95
			$args[1],
96
			$plugin_data,
97
		);
98
	}
99
}
100