Completed
Push — whitelist/sync ( 830219...fbc430 )
by
unknown
13:02 queued 02:23
created

on_upgrader_completion()   D

Complexity

Conditions 15
Paths 111

Size

Total Lines 81
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 15
eloc 29
nc 111
nop 2
dl 0
loc 81
rs 4.8068
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B Jetpack_Sync_Module_Plugins::check_plugin_edit() 0 25 5

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