Completed
Push — add/plugin_name ( f17452...000966 )
by
unknown
55:20 queued 43:33
created

Jetpack_Sync_Module_Plugins::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
	}
14
15
	public function init_before_send() {
16
		add_filter( 'jetpack_sync_before_send_deleted_plugin', array( $this, 'expand_plugin_data' ) );
17
		add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) );
18
		add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) );
19
	}
20
21
	public function expand_plugin_data( $args ) {
22
		$plugin_path = $args[0];
23
		$plugin_data = array();
24
25
		//Try to get plugin data from cache (if it isn't cached, get_plugins() tries to get it from disk)
26
		$all_plugins = get_plugins();
27
		if ( isset( $all_plugins[$plugin_path] ) ) {
28
			$all_plugin_data = $all_plugins[$plugin_path];
29
			$plugin_data['name'] = $all_plugin_data['Name'];
30
			$plugin_data['version'] = $all_plugin_data['Version'];
31
		} else {
32
			require_once ABSPATH . 'wp-admin/includes/plugin-install.php';
33
			$slug = Jetpack_Autoupdate::get_plugin_slug( $plugin_path );
34
			$all_plugin_data = plugins_api( 'plugin_information', array( 'slug' => $slug ) );
35
			if ( ! is_wp_error( $all_plugin_data ) ) {
36
				$plugin_data['name'] = $all_plugin_data['name'];
37
				$plugin_data['version'] = $all_plugin_data['version'];
38
			}
39
		}
40
		
41
		return array(
42
			$args[0],
43
			$args[1],
44
			$plugin_data,
45
		);
46
	}
47
}
48