Completed
Push — branch-4.9 ( 85ea1f...e36aed )
by
unknown
24:00 queued 15:34
created

Jetpack_Sync_Module_Plugins   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 125
Duplicated Lines 5.6 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 7
loc 125
rs 10
c 0
b 0
f 0
wmc 19
lcom 1
cbo 1

8 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A init_listeners() 0 12 1
A init_before_send() 0 5 1
B check_upgrader() 7 27 6
B check_plugin_edit() 0 25 5
A delete_plugin() 0 19 2
A deleted_plugin() 0 4 1
A expand_plugin_data() 0 17 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
	}
24
25
	public function init_before_send() {
26
		add_filter( 'jetpack_sync_before_send_activated_plugin', array( $this, 'expand_plugin_data' ) );
27
		add_filter( 'jetpack_sync_before_send_deactivated_plugin', array( $this, 'expand_plugin_data' ) );
28
		//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
29
	}
30
31
	public function check_upgrader( $upgrader, $details) {
32
33 View Code Duplication
		if ( ! isset( $details['type'] ) ||
34
			'plugin' !== $details['type'] ||
35
			is_wp_error( $upgrader->skin->result ) ||
36
			! method_exists( $upgrader, 'plugin_info' )
37
		) {
38
			return;
39
		}
40
41
		if ( 'install' === $details['action'] ) {
42
			$plugin_path = $upgrader->plugin_info();
43
			$plugins = get_plugins();
44
			$plugin_info = $plugins[ $plugin_path ];
45
46
			/**
47
			 * Signals to the sync listener that a plugin was installed and a sync action
48
			 * reflecting the installation and the plugin info should be sent
49
			 *
50
			 * @since 4.9.0
51
			 *
52
			 * @param string $plugin_path Path of plugin installed
53
			 * @param mixed $plugin_info Array of info describing plugin installed
54
			 */
55
			do_action( 'jetpack_installed_plugin', $plugin_path, $plugin_info );
56
		}
57
	}
58
59
	public function check_plugin_edit() {
60
		$screen = get_current_screen();
61
		if ( 'plugin-editor' !== $screen->base ||
62
			! isset( $_POST['newcontent'] ) ||
63
			! isset( $_POST['plugin'] )
64
		) {
65
			return;
66
		}
67
68
		$plugin = $_POST['plugin'];
69
		$plugins = get_plugins();
70
		if ( ! isset( $plugins[ $plugin ] ) ) {
71
			return;
72
		}
73
74
		/**
75
		 * Helps Sync log that a plugin was edited
76
		 *
77
		 * @since 4.9.0
78
		 *
79
		 * @param string $plugin, Plugin slug
80
		 * @param mixed $plugins[ $plugin ], Array of plugin data
81
		 */
82
		do_action( 'jetpack_edited_plugin', $plugin, $plugins[ $plugin ] );
83
	}
84
85
	public function delete_plugin( $plugin_path ) {
86
		$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
87
88
		//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
89
		if ( file_exists( $full_plugin_path ) ) {
90
			$all_plugin_data = get_plugin_data( $full_plugin_path );
91
			$data = array(
92
				'name' => $all_plugin_data['Name'],
93
				'version' => $all_plugin_data['Version'],
94
			);
95
		} else {
96
			$data = array(
97
				'name' => $plugin_path,
98
				'version' => 'unknown',
99
			);
100
		}
101
102
		$this->plugin_info[ $plugin_path ] = $data;
103
	}
104
105
	public function deleted_plugin( $plugin_path, $is_deleted ) {
106
		call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] );
107
		unset( $this->plugin_info[ $plugin_path ] );
108
	}
109
110
	public function expand_plugin_data( $args ) {
111
		$plugin_path = $args[0];
112
		$plugin_data = array();
113
114
		$all_plugins = get_plugins();
115
		if ( isset( $all_plugins[ $plugin_path ] ) ) {
116
			$all_plugin_data = $all_plugins[ $plugin_path ];
117
			$plugin_data['name'] = $all_plugin_data['Name'];
118
			$plugin_data['version'] = $all_plugin_data['Version'];
119
		}
120
121
		return array(
122
			$args[0],
123
			$args[1],
124
			$plugin_data,
125
		);
126
	}
127
}
128