Completed
Push — add/plugin_name ( 4766b7...0053d7 )
by
unknown
14:35
created

sync_deleted_plugin_info()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 12
nc 2
nop 1
dl 0
loc 27
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( 'deleted_plugin', $callable, 10, 2 );
11
		add_action( 'activated_plugin', $callable, 10, 2 );
12
		add_action( 'deactivated_plugin', $callable, 10, 2 );
13
		add_action( 'delete_plugin', array( $this, 'sync_deleted_plugin_info') );
14
		add_action( 'jetpack_delete_plugin', $callable, 10, 2);
15
	}
16
17
	public function init_before_send() {
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
		//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
21
	}
22
23
	public function sync_deleted_plugin_info( $plugin_path ) {
24
		$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
25
26
		//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
27
		if ( file_exists( $full_plugin_path ) ) {
28
			$all_plugin_data = get_plugin_data( $full_plugin_path );
29
			$data = array(
30
				'name' => $all_plugin_data['Name'],
31
				'version' => $all_plugin_data['Version'],
32
			);
33
		} else {
34
			$data = array(
35
				'name' => $plugin_path,
36
				'version' => 'unknown',
37
			);
38
		}
39
40
		/**
41
		 * Syncs information about a plugin whose deletion was attempted
42
		 *
43
		 * @since 4.8.3
44
		 *
45
		 * @param string $plugin_path Path of plugin whose deletion was attempted (deletion verified in deleted_plugin sync action)
46
		 * @param mixed $data Array of plugin information fields (name, version, etc.)
47
		 */
48
		do_action( 'jetpack_delete_plugin', $plugin_path, $data );
49
	}
50
51
	public function expand_plugin_data( $args ) {
52
		$plugin_path = $args[0];
53
		$plugin_data = array();
54
55
		//Try to get plugin data from cache (if it isn't cached, get_plugins() tries to get it from disk)
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