Completed
Push — add/sync_plugin_update_error ( bc3779...4fc209 )
by
unknown
08:47
created

Jetpack_Sync_Module_Plugins::delete_plugin()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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