Completed
Push — add/sync_plugin_update_error ( 66f20c...e98905 )
by
unknown
40:15 queued 31:10
created

Jetpack_Sync_Module_Plugins::check_upgrader()   C

Complexity

Conditions 11
Paths 7

Size

Total Lines 45
Code Lines 21

Duplication

Lines 7
Ratio 15.56 %

Importance

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