Completed
Push — pr/7794 ( cb7d3e...693e58 )
by George
09:28
created

Jetpack_Sync_Module_Callables   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 181
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 181
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 1
cbo 4

17 Methods

Rating   Name   Duplication   Size   Complexity  
A name() 0 3 1
A set_defaults() 0 7 2
A init_listeners() 0 22 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 12 1
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 should_send_callable() 0 12 3
C maybe_sync_callables() 0 40 8
A expand_callables() 0 13 3
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
26
		// For some options, we should always send the change right away!
27
		$always_send_updates_to_these_options = array(
28
			'jetpack_active_modules',
29
			'home',
30
			'siteurl',
31
			'jetpack_sync_error_idc'
32
		);
33
		foreach( $always_send_updates_to_these_options as $option ) {
34
			add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) );
35
		}
36
37
		// Provide a hook so that hosts can send changes to certain callables right away.
38
		// Especially useful when a host uses constants to change home and siteurl.
39
		add_action( 'jetpack_sync_unlock_sync_callable', array( $this, 'unlock_sync_callable' ) );
40
41
		// get_plugins and wp_version
42
		// gets fired when new code gets installed, updates etc.
43
		add_action( 'upgrader_process_complete', array( $this, 'unlock_sync_callable' ) );
44
	}
45
46
	public function init_full_sync_listeners( $callable ) {
47
		add_action( 'jetpack_full_sync_callables', $callable );
48
	}
49
50
	public function init_before_send() {
51
		add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) );
52
53
		// full sync
54
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) );
55
	}
56
57
	public function reset_data() {
58
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
59
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
60
61
		$url_callables = array( 'home_url', 'site_url', 'main_network_site_url' );
62
		foreach( $url_callables as $callable ) {
63
			delete_option( Jetpack_Sync_Functions::HTTPS_CHECK_OPTION_PREFIX . $callable );
64
		}
65
	}
66
67
	function set_callable_whitelist( $callables ) {
68
		$this->callable_whitelist = $callables;
69
	}
70
71
	function get_callable_whitelist() {
72
		return $this->callable_whitelist;
73
	}
74
75
	public function get_all_callables() {
76
		// get_all_callables should run as the master user always.
77
		$current_user_id = get_current_user_id();
78
		wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) );
79
		$callables = array_combine(
80
			array_keys( $this->get_callable_whitelist() ),
81
			array_map( array( $this, 'get_callable' ), array_values( $this->get_callable_whitelist() ) )
82
		);
83
		wp_set_current_user( $current_user_id );
84
85
		return $callables;
86
	}
87
88
	private function get_callable( $callable ) {
89
		return call_user_func( $callable );
90
	}
91
92
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
93
		/**
94
		 * Tells the client to sync all callables to the server
95
		 *
96
		 * @since 4.2.0
97
		 *
98
		 * @param boolean Whether to expand callables (should always be true)
99
		 */
100
		do_action( 'jetpack_full_sync_callables', true );
101
102
		// The number of actions enqueued, and next module state (true == done)
103
		return array( 1, true ); 
104
	}
105
106
	public function estimate_full_sync_actions( $config ) {
107
		return 1;
108
	}
109
110
	public function get_full_sync_actions() {
111
		return array( 'jetpack_full_sync_callables' );
112
	}
113
114
	public function unlock_sync_callable() {
115
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
116
	}
117
118
	public function should_send_callable( $callable_checksums, $name, $checksum ) {
119
		$idc_override_callables = array(
120
			'main_network_site',
121
			'home_url',
122
			'site_url',
123
		);
124
		if ( in_array( $name, $idc_override_callables ) && Jetpack_Options::get_option( 'migrate_for_idc' ) ) {
125
			return true;
126
		}
127
128
		return ! $this->still_valid_checksum( $callable_checksums, $name, $checksum );
129
	}
130
	
131
	public function maybe_sync_callables() {
132
		if ( ! is_admin() || Jetpack_Sync_Settings::is_doing_cron() ) {
133
			return;
134
		}
135
136
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
137
			return;
138
		}
139
140
		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...
141
142
		$callables = $this->get_all_callables();
143
144
		if ( empty( $callables ) ) {
145
			return;
146
		}
147
148
		$callable_checksums = (array) Jetpack_Options::get_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
149
150
		// only send the callables that have changed
151
		foreach ( $callables as $name => $value ) {
152
			$checksum = $this->get_check_sum( $value );
153
			// explicitly not using Identical comparison as get_option returns a string
154
			if ( ! is_null( $value ) && $this->should_send_callable( $callable_checksums, $name, $checksum ) ) {
155
				/**
156
				 * Tells the client to sync a callable (aka function) to the server
157
				 *
158
				 * @since 4.2.0
159
				 *
160
				 * @param string The name of the callable
161
				 * @param mixed The value of the callable
162
				 */
163
				do_action( 'jetpack_sync_callable', $name, $value );
164
				$callable_checksums[ $name ] = $checksum;
165
			} else {
166
				$callable_checksums[ $name ] = $checksum;
167
			}
168
		}
169
		Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
170
	}
171
172
	public function expand_callables( $args ) {
173
		if ( $args[0] ) {
174
			$callables = $this->get_all_callables();
175
			$callables_checksums = array();
176
			foreach ( $callables as $name => $value ) {
177
				$callables_checksums[ $name ] = $this->get_check_sum( $value );
178
			}
179
			Jetpack_Options::update_raw_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callables_checksums );
180
			return $callables;
181
		}
182
183
		return $args;
184
	}
185
}
186