Completed
Push — add/sync_plugin_update_error ( 0328fe...66f20c )
by
unknown
21:18 queued 12:32
created

Jetpack_Sync_Module_Plugins::plugin_edit_ajax()   C

Complexity

Conditions 12
Paths 12

Size

Total Lines 57
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

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