Completed
Push — add/plugin_modified ( 9cbe2c...5c758d )
by
unknown
31:14 queued 22:18
created

Jetpack_Sync_Module_Plugins   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 126
Duplicated Lines 5.56 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 7
loc 126
rs 10
c 0
b 0
f 0
wmc 20
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 26 6
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['action'] ) ||
63
			'update' !== $_POST['action'] ||
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 delete_plugin( $plugin_path ) {
87
		$full_plugin_path = WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin_path;
88
89
		//Checking for file existence because some sync plugin module tests simulate plugin installation and deletion without putting file on disk
90
		if ( file_exists( $full_plugin_path ) ) {
91
			$all_plugin_data = get_plugin_data( $full_plugin_path );
92
			$data = array(
93
				'name' => $all_plugin_data['Name'],
94
				'version' => $all_plugin_data['Version'],
95
			);
96
		} else {
97
			$data = array(
98
				'name' => $plugin_path,
99
				'version' => 'unknown',
100
			);
101
		}
102
103
		$this->plugin_info[ $plugin_path ] = $data;
104
	}
105
106
	public function deleted_plugin( $plugin_path, $is_deleted ) {
107
		call_user_func( $this->action_handler, $plugin_path, $is_deleted, $this->plugin_info[ $plugin_path ] );
108
		unset( $this->plugin_info[ $plugin_path ] );
109
	}
110
111
	public function expand_plugin_data( $args ) {
112
		$plugin_path = $args[0];
113
		$plugin_data = array();
114
115
		$all_plugins = get_plugins();
116
		if ( isset( $all_plugins[ $plugin_path ] ) ) {
117
			$all_plugin_data = $all_plugins[ $plugin_path ];
118
			$plugin_data['name'] = $all_plugin_data['Name'];
119
			$plugin_data['version'] = $all_plugin_data['Version'];
120
		}
121
122
		return array(
123
			$args[0],
124
			$args[1],
125
			$plugin_data,
126
		);
127
	}
128
}
129