Completed
Push — remove/local-site-data-rest-ap... ( f316a5...4e2164 )
by
unknown
16:57 queued 08:25
created

Jetpack_Sync_Module_Callables   B

Complexity

Total Complexity 44

Size/Duplication

Total Lines 236
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 236
rs 8.3396
c 0
b 0
f 0
wmc 44
lcom 1
cbo 5

19 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A set_defaults() 0 7 2
A get_callable() 0 3 1
A enqueue_full_sync_actions() 0 13 1
A estimate_full_sync_actions() 0 3 1
A get_full_sync_actions() 0 3 1
A unlock_sync_callable() 0 3 1
A unlock_plugin_action_link_and_callables() 0 4 1
B init_listeners() 0 24 2
A init_full_sync_listeners() 0 3 1
A init_before_send() 0 6 1
A reset_data() 0 9 2
A set_callable_whitelist() 0 3 1
A get_callable_whitelist() 0 3 1
A get_all_callables() 0 11 1
C set_plugin_action_links() 0 48 12
A should_send_callable() 0 12 3
C maybe_sync_callables() 0 40 8
A expand_callables() 0 13 3

How to fix   Complexity   

Complex Class

Complex classes like Jetpack_Sync_Module_Callables often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Jetpack_Sync_Module_Callables, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
require_once dirname( __FILE__ ) . '/class.jetpack-sync-functions.php';
4
5
class Jetpack_Sync_Module_Callables extends Jetpack_Sync_Module {
6
	const CALLABLES_CHECKSUM_OPTION_NAME = 'jetpack_callables_sync_checksum';
7
	const CALLABLES_AWAIT_TRANSIENT_NAME = 'jetpack_sync_callables_await';
8
9
	private $callable_whitelist;
10
11
	public function name() {
12
		return 'functions';
13
	}
14
15
	public function set_defaults() {
16
		if ( is_multisite() ) {
17
			$this->callable_whitelist = array_merge( Jetpack_Sync_Defaults::get_callable_whitelist(), Jetpack_Sync_Defaults::get_multisite_callable_whitelist() );
18
		} else {
19
			$this->callable_whitelist = Jetpack_Sync_Defaults::get_callable_whitelist();
20
		}
21
	}
22
23
	public function init_listeners( $callable ) {
24
		add_action( 'jetpack_sync_callable', $callable, 10, 2 );
25
		add_action( 'admin_init', array( $this, 'set_plugin_action_links' ), 9999 ); // Should happen very late
26
27
		// For some options, we should always send the change right away!
28
		$always_send_updates_to_these_options = array(
29
			'jetpack_active_modules',
30
			'home',
31
			'siteurl',
32
			'jetpack_sync_error_idc'
33
		);
34
		foreach( $always_send_updates_to_these_options as $option ) {
35
			add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) );
36
		}
37
38
		// Provide a hook so that hosts can send changes to certain callables right away.
39
		// Especially useful when a host uses constants to change home and siteurl.
40
		add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) );
41
42
		// get_plugins and wp_version
43
		// gets fired when new code gets installed, updates etc.
44
		add_action( 'upgrader_process_complete', array( $this, 'unlock_plugin_action_link_and_callables' ) );
45
		add_action( 'update_option_active_plugins', array( $this, 'unlock_plugin_action_link_and_callables' ) );
46
	}
47
48
	public function init_full_sync_listeners( $callable ) {
49
		add_action( 'jetpack_full_sync_callables', $callable );
50
	}
51
52
	public function init_before_send() {
53
		add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) );
54
55
		// full sync
56
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) );
57
	}
58
59
	public function reset_data() {
60
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
61
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
62
63
		$url_callables = array( 'home_url', 'site_url', 'main_network_site_url' );
64
		foreach( $url_callables as $callable ) {
65
			delete_option( Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable );
66
		}
67
	}
68
69
	function set_callable_whitelist( $callables ) {
70
		$this->callable_whitelist = $callables;
71
	}
72
73
	function get_callable_whitelist() {
74
		return $this->callable_whitelist;
75
	}
76
77
	public function get_all_callables() {
78
		// get_all_callables should run as the master user always.
79
		$current_user_id = get_current_user_id();
80
		wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) );
81
		$callables = array_combine(
82
			array_keys( $this->get_callable_whitelist() ),
83
			array_map( array( $this, 'get_callable' ), array_values( $this->get_callable_whitelist() ) )
84
		);
85
		wp_set_current_user( $current_user_id );
86
		return $callables;
87
	}
88
89
	private function get_callable( $callable ) {
90
		return call_user_func( $callable );
91
	}
92
93
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
94
		/**
95
		 * Tells the client to sync all callables to the server
96
		 *
97
		 * @since 4.2.0
98
		 *
99
		 * @param boolean Whether to expand callables (should always be true)
100
		 */
101
		do_action( 'jetpack_full_sync_callables', true );
102
103
		// The number of actions enqueued, and next module state (true == done)
104
		return array( 1, true ); 
105
	}
106
107
	public function estimate_full_sync_actions( $config ) {
108
		return 1;
109
	}
110
111
	public function get_full_sync_actions() {
112
		return array( 'jetpack_full_sync_callables' );
113
	}
114
115
	public function unlock_sync_callable() {
116
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
117
	}
118
119
	public function unlock_plugin_action_link_and_callables() {
120
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
121
		delete_transient( 'jetpack_plugin_api_action_links_refresh' );
122
	}
123
124
	public function set_plugin_action_links() {
125
		if ( ! class_exists( 'DOMDocument' ) ) {
126
			return;
127
		}
128
129
		// Is the transient lock in place?
130
		$plugins_lock = get_transient( 'jetpack_plugin_api_action_links_refresh', false );
131
		if ( ! empty( $plugins_lock ) ) {
132
			return;
133
		}
134
		$plugins = array_keys( Jetpack_Sync_Functions::get_plugins() );
135
		foreach ( $plugins as $plugin_file ) {
136
			/** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */
137
			$action_links = apply_filters( 'plugin_action_links', array(), $plugin_file, null, 'all' );
138
			/** This filter is documented in src/wp-admin/includes/class-wp-plugins-list-table.php */
139
			$action_links = apply_filters( "plugin_action_links_{$plugin_file}", $action_links, $plugin_file, null, 'all' );
140
			$formatted_action_links = null;
141
			if ( ! empty( $action_links ) && count( $action_links ) > 0 ) {
142
				$dom_doc = new DOMDocument;
143
				foreach ( $action_links as $action_link ) {
144
					$dom_doc->loadHTML( mb_convert_encoding( $action_link, 'HTML-ENTITIES', 'UTF-8' ) );
145
					$link_elements = $dom_doc->getElementsByTagName( 'a' );
146
					if ( $link_elements->length == 0 ) {
147
						continue;
148
					}
149
150
					$link_element = $link_elements->item( 0 );
151
					if ( $link_element->hasAttribute( 'href' ) && $link_element->nodeValue ) {
152
						$link_url = trim( $link_element->getAttribute( 'href' ) );
153
154
						// Add the full admin path to the url if the plugin did not provide it
155
						$link_url_scheme = wp_parse_url( $link_url, PHP_URL_SCHEME );
156
						if ( empty( $link_url_scheme ) ) {
157
							$link_url = admin_url( $link_url );
158
						}
159
160
						$formatted_action_links[ $link_element->nodeValue ] = $link_url;
161
					}
162
				}
163
			}
164
			if ( $formatted_action_links ) {
165
				$plugins_action_links[ $plugin_file ] = $formatted_action_links;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$plugins_action_links was never initialized. Although not strictly required by PHP, it is generally a good practice to add $plugins_action_links = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
166
			}
167
		}
168
		// Cache things for a long time
169
		set_transient( 'jetpack_plugin_api_action_links_refresh', time(), DAY_IN_SECONDS );
170
		update_option( 'jetpack_plugin_api_action_links', $plugins_action_links );
0 ignored issues
show
Bug introduced by
The variable $plugins_action_links does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
171
	}
172
173
	public function should_send_callable( $callable_checksums, $name, $checksum ) {
174
		$idc_override_callables = array(
175
			'main_network_site',
176
			'home_url',
177
			'site_url',
178
		);
179
		if ( in_array( $name, $idc_override_callables ) && Jetpack_Options::get_option( 'migrate_for_idc' ) ) {
180
			return true;
181
		}
182
183
		return ! $this->still_valid_checksum( $callable_checksums, $name, $checksum );
184
	}
185
	
186
	public function maybe_sync_callables() {
187
		if ( ! is_admin() || Jetpack_Sync_Settings::is_doing_cron() ) {
188
			return;
189
		}
190
191
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
192
			return;
193
		}
194
195
		set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time );
0 ignored issues
show
Bug introduced by
The property default_sync_callables_wait_time cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
196
197
		$callables = $this->get_all_callables();
198
199
		if ( empty( $callables ) ) {
200
			return;
201
		}
202
203
		$callable_checksums = (array) Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
204
205
		// only send the callables that have changed
206
		foreach ( $callables as $name => $value ) {
207
			$checksum = $this->get_check_sum( $value );
208
			// explicitly not using Identical comparison as get_option returns a string
209
			if ( ! is_null( $value ) && $this->should_send_callable( $callable_checksums, $name, $checksum ) ) {
210
				/**
211
				 * Tells the client to sync a callable (aka function) to the server
212
				 *
213
				 * @since 4.2.0
214
				 *
215
				 * @param string The name of the callable
216
				 * @param mixed The value of the callable
217
				 */
218
				do_action( 'jetpack_sync_callable', $name, $value );
219
				$callable_checksums[ $name ] = $checksum;
220
			} else {
221
				$callable_checksums[ $name ] = $checksum;
222
			}
223
		}
224
		Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
225
	}
226
227
	public function expand_callables( $args ) {
228
		if ( $args[0] ) {
229
			$callables = $this->get_all_callables();
230
			$callables_checksums = array();
231
			foreach ( $callables as $name => $value ) {
232
				$callables_checksums[ $name ] = $this->get_check_sum( $value );
233
			}
234
			Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callables_checksums );
235
			return $callables;
236
		}
237
238
		return $args;
239
	}
240
}
241