Completed
Push — update/react-create-class ( 7f3c72...120f37 )
by
unknown
257:16 queued 246:39
created

Jetpack_Sync_Module_Plugins::check_upgrader()   C

Complexity

Conditions 14
Paths 13

Size

Total Lines 63
Code Lines 25

Duplication

Lines 7
Ratio 11.11 %

Importance

Changes 0
Metric Value
cc 14
eloc 25
nc 13
nop 2
dl 7
loc 63
rs 6.0952
c 0
b 0
f 0

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