Completed
Push — add/sync_plugin_update_error ( f213bb...0328fe )
by
unknown
14:14 queued 05:38
created

Jetpack_Sync_Module_Plugins::check_plugin_edit()   B

Complexity

Conditions 5
Paths 3

Size

Total Lines 25
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

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