Completed
Push — add/sync_plugin_update_error ( f891d4...bc3779 )
by
unknown
13:39
created

Jetpack_Sync_Module_Plugins::check_upgrader()   C

Complexity

Conditions 11
Paths 4

Size

Total Lines 40
Code Lines 20

Duplication

Lines 7
Ratio 17.5 %

Importance

Changes 0
Metric Value
cc 11
eloc 20
nc 4
nop 2
dl 7
loc 40
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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( 'jetpack_plugin_update_failed', $callable, 10, 3 );
22
		add_action( 'admin_action_update', array( $this, 'check_plugin_edit') );
23
		add_action( 'jetpack_edited_plugin', $callable, 10, 2 );
24
		add_action( 'wp_ajax_edit-theme-plugin-file', array( $this, 'plugin_edit_ajax' ), 0 );
25
	}
26
27
	public function init_before_send() {
28
		add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) );
29
		add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) );
30
		//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
31
	}
32
33
	public function check_upgrader( $upgrader, $details) {
34
		if (
35
			( ( isset( $details['type'] ) &&
36
			    'plugin' == $details['type'] ) ||
37
			  ( isset ( $upgrader['block'] ) &&
38
			    1 == $upgrader['block'] ) )
39
			&&
40
			is_wp_error( $upgrader->skin->result )
41
		) {
42
			do_action( 'jetpack_plugin_update_failed', $details, $upgrader->skin->result->errors, $upgrader->skin->result->error_data );
43
44
			return;
45
		}
46
47
48 View Code Duplication
		if ( ! isset( $details['type'] ) ||
49
			'plugin' !== $details['type'] ||
50
			is_wp_error( $upgrader->skin->result ) ||
51
			! method_exists( $upgrader, 'plugin_info' )
52
		) {
53
			return;
54
		}
55
56
		if ( 'install' === $details['action'] ) {
57
			$plugin_path = $upgrader->plugin_info();
58
			$plugins = get_plugins();
59
			$plugin_info = $plugins[ $plugin_path ];
60
61
			/**
62
			 * Signals to the sync listener that a plugin was installed and a sync action
63
			 * reflecting the installation and the plugin info should be sent
64
			 *
65
			 * @since 4.9.0
66
			 *
67
			 * @param string $plugin_path Path of plugin installed
68
			 * @param mixed $plugin_info Array of info describing plugin installed
69
			 */
70
			do_action( 'jetpack_installed_plugin', $plugin_path, $plugin_info );
71
		}
72
	}
73
74
	public function check_plugin_edit() {
75
		$screen = get_current_screen();
76
		if ( 'plugin-editor' !== $screen->base ||
77
			! isset( $_POST['newcontent'] ) ||
78
			! isset( $_POST['plugin'] )
79
		) {
80
			return;
81
		}
82
83
		$plugin = $_POST['plugin'];
84
		$plugins = get_plugins();
85
		if ( ! isset( $plugins[ $plugin ] ) ) {
86
			return;
87
		}
88
89
		/**
90
		 * Helps Sync log that a plugin was edited
91
		 *
92
		 * @since 4.9.0
93
		 *
94
		 * @param string $plugin, Plugin slug
95
		 * @param mixed $plugins[ $plugin ], Array of plugin data
96
		 */
97
		do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] );
98
	}
99
100
	public function plugin_edit_ajax() {
101
		// this validation is based on wp_edit_theme_plugin_file()
102
		$args = wp_unslash( $_POST );
103
		if ( empty( $args['file'] ) ) {
104
			return;
105
		}
106
107
		$file = $args['file'];
108
		if ( 0 !== validate_file( $file ) ) {
109
			return;
110
		}
111
112
		if ( ! isset( $args['newcontent'] ) ) {
113
			return;
114
		}
115
116
		if ( ! isset( $args['nonce'] ) ) {
117
			return;
118
		}
119
120
		if ( empty( $args['plugin'] ) ) {
121
			return;
122
		}
123
124
		$plugin = $args['plugin'];
125
		if ( ! current_user_can( 'edit_plugins' ) ) {
126
			return;
127
		}
128
129
		if ( ! wp_verify_nonce( $args['nonce'], 'edit-plugin_' . $file ) ) {
130
			return;
131
		}
132
		$plugins = get_plugins();
133
		if ( ! array_key_exists( $plugin, $plugins ) ) {
134
			return;
135
		}
136
137
		if ( 0 !== validate_file( $file, get_plugin_files( $plugin ) ) ) {
138
			return;
139
		}
140
141
		$real_file = WP_PLUGIN_DIR . '/' . $file;
142
143
		if ( ! is_writeable( $real_file ) ) {
144
			return;
145
		}
146
147
		$file_pointer = fopen( $real_file, 'w+' );
148
		if ( false === $file_pointer ) {
149
			return;
150
		}
151
152
		/**
153
		 * This action is documented already in this file
154
		 */
155
		do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] );
156
	}
157
158
	public function delete_plugin( $plugin_path ) {
159
		$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
160
161
		//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
162
		if ( file_exists( $full_plugin_path ) ) {
163
			$all_plugin_data = get_plugin_data( $full_plugin_path );
164
			$data = array(
165
				'name' => $all_plugin_data['Name'],
166
				'version' => $all_plugin_data['Version'],
167
			);
168
		} else {
169
			$data = array(
170
				'name' => $plugin_path,
171
				'version' => 'unknown',
172
			);
173
		}
174
175
		$this->plugin_info[ $plugin_path ] = $data;
176
	}
177
178
	public function deleted_plugin( $plugin_path, $is_deleted ) {
179
		call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] );
180
		unset( $this->plugin_info[ $plugin_path ] );
181
	}
182
183
	public function expand_plugin_data( $args ) {
184
		$plugin_path = $args[0];
185
		$plugin_data = array();
186
187
		if ( ! function_exists( 'get_plugins' ) ) {
188
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
189
		}
190
		$all_plugins = get_plugins();
191
		if ( isset( $all_plugins[ $plugin_path ] ) ) {
192
			$all_plugin_data = $all_plugins[ $plugin_path ];
193
			$plugin_data['name'] = $all_plugin_data['Name'];
194
			$plugin_data['version'] = $all_plugin_data['Version'];
195
		}
196
197
		return array(
198
			$args[0],
199
			$args[1],
200
			$plugin_data,
201
		);
202
	}
203
}
204