Completed
Push — add/plugin_install ( a622a6...54df06 )
by
unknown
11:39
created

Jetpack_Sync_Module_Plugins::delete_plugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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