Completed
Push — add/plugin_modified ( e0bd66...9b9f5b )
by
unknown
09:04
created

Jetpack_Sync_Module_Plugins::check_plugin_edit2()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 26
Code Lines 12

Duplication

Lines 26
Ratio 100 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 3
nop 0
dl 26
loc 26
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
		add_action( 'wp_admin_update', array( $this, 'check_plugin_edit') );
22
		add_action( 'jetpack_edited_plugin', $callable, 10, 2 );
23
	}
24
25
	public function init_before_send() {
26
		add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) );
27
		add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) );
28
		//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
29
	}
30
31
	public function check_upgrader( $upgrader, $details) {
32
33 View Code Duplication
		if ( ! isset( $details['type'] ) ||
34
			'plugin' !== $details['type'] ||
35
			is_wp_error( $upgrader->skin->result ) ||
36
			! method_exists( $upgrader, 'plugin_info' )
37
		) {
38
			return;
39
		}
40
41
		if ( 'install' === $details['action'] ) {
42
			$plugin_path = $upgrader->plugin_info();
43
			$plugins = get_plugins();
44
			$plugin_info = $plugins[ $plugin_path ];
45
46
			/**
47
			 * Signals to the sync listener that a plugin was installed and a sync action
48
			 * reflecting the installation and the plugin info should be sent
49
			 *
50
			 * @since 4.9.0
51
			 *
52
			 * @param string $plugin_path Path of plugin installed
53
			 * @param mixed $plugin_info Array of info describing plugin installed
54
			 */
55
			do_action( 'jetpack_installed_plugin', $plugin_path, $plugin_info );
56
		}
57
	}
58
59
60 View Code Duplication
	public function check_plugin_edit() {
61
		$screen = get_current_screen();
62
		if ( 'plugin-editor' !== $screen->base ||
63
			! isset( $_POST['action'] ) ||
64
			'update' !== $_POST['action'] ||
65
			! isset( $_POST['plugin'] )
66
		) {
67
			return;
68
		}
69
70
		$plugin = $_POST['plugin'];
71
		$plugins = get_plugins();
72
		if ( ! isset( $plugins[ $plugin ] ) ) {
73
			return;
74
		}
75
76
		/**
77
		 * Helps Sync log that a plugin was edited
78
		 *
79
		 * @since 4.9.0
80
		 *
81
		 * @param string $plugin, Plugin slug
82
		 * @param mixed $plugins[ $plugin ], Array of plugin data
83
		 */
84
		do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] );
85
	}
86
87 View Code Duplication
	public function check_plugin_edit2() {
88
		$screen = get_current_screen();
89
		if ( 'plugin-editor' !== $screen->base ||
90
			! isset( $_GET['a'] ) ||
91
			! 'te' === $_GET['a'] ||
92
			! isset( $_GET['plugin'] )
93
		) {
94
			return;
95
		}
96
97
		$plugin = $_GET['plugin'];
98
		$plugins = get_plugins();
99
		if ( ! isset( $plugins[ $plugin ] ) ) {
100
			return;
101
		}
102
103
		/**
104
		 * Helps Sync log that a plugin was edited
105
		 *
106
		 * @since 4.9.0
107
		 *
108
		 * @param string $plugin, Plugin slug
109
		 * @param mixed $plugins[ $plugin ], Array of plugin data
110
		 */
111
		do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] );
112
	}
113
114
	public function delete_plugin( $plugin_path ) {
115
		$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
116
117
		//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
118
		if ( file_exists( $full_plugin_path ) ) {
119
			$all_plugin_data = get_plugin_data( $full_plugin_path );
120
			$data = array(
121
				'name' => $all_plugin_data['Name'],
122
				'version' => $all_plugin_data['Version'],
123
			);
124
		} else {
125
			$data = array(
126
				'name' => $plugin_path,
127
				'version' => 'unknown',
128
			);
129
		}
130
131
		$this->plugin_info[ $plugin_path ] = $data;
132
	}
133
134
	public function deleted_plugin( $plugin_path, $is_deleted ) {
135
		call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] );
136
		unset( $this->plugin_info[ $plugin_path ] );
137
	}
138
139
	public function expand_plugin_data( $args ) {
140
		$plugin_path = $args[0];
141
		$plugin_data = array();
142
143
		$all_plugins = get_plugins();
144
		if ( isset( $all_plugins[ $plugin_path ] ) ) {
145
			$all_plugin_data = $all_plugins[ $plugin_path ];
146
			$plugin_data['name'] = $all_plugin_data['Name'];
147
			$plugin_data['version'] = $all_plugin_data['Version'];
148
		}
149
150
		return array(
151
			$args[0],
152
			$args[1],
153
			$plugin_data,
154
		);
155
	}
156
}
157