Completed
Push — master-stable ( c165f1...74e410 )
by
unknown
08:22
created

maybe_sync_callables()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 40
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 18
nc 6
nop 0
dl 0
loc 40
rs 5.3846
c 0
b 0
f 0
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::$default_callable_whitelist, Jetpack_Sync_Defaults::$default_multisite_callable_whitelist );
0 ignored issues
show
Bug introduced by
The property default_callable_whitelist 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...
Bug introduced by
The property default_multisite_callable_whitelist 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...
18
		} else {
19
			$this->callable_whitelist = Jetpack_Sync_Defaults::$default_callable_whitelist;
0 ignored issues
show
Bug introduced by
The property default_callable_whitelist 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...
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( 'jetpack_active_modules', 'home', 'siteurl' );
28
		foreach( $always_send_updates_to_these_options as $option ) {
29
			add_action( "update_option_{$option}", array( $this, 'unlock_sync_callable' ) );
30
		}
31
32
		// get_plugins and wp_version
33
		// gets fired when new code gets installed, updates etc.
34
		add_action( 'upgrader_process_complete', array( $this, 'unlock_sync_callable' ) );
35
	}
36
37
	public function init_full_sync_listeners( $callable ) {
38
		add_action( 'jetpack_full_sync_callables', $callable );
39
	}
40
41
	public function init_before_send() {
42
		add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) );
43
44
		// full sync
45
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) );
46
	}
47
48
	public function reset_data() {
49
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
50
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
51
	}
52
53
	function set_callable_whitelist( $callables ) {
54
		$this->callable_whitelist = $callables;
55
	}
56
57
	function get_callable_whitelist() {
58
		return $this->callable_whitelist;
59
	}
60
61
	public function get_all_callables() {
62
		// get_all_callables should run as the master user always.
63
		$current_user_id = get_current_user_id();
64
		wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) );
65
		$callables = array_combine(
66
			array_keys( $this->callable_whitelist ),
67
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
68
		);
69
		wp_set_current_user( $current_user_id );
70
71
		return $callables;
72
	}
73
74
	private function get_callable( $callable ) {
75
		return call_user_func( $callable );
76
	}
77
78
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
79
		/**
80
		 * Tells the client to sync all callables to the server
81
		 *
82
		 * @since 4.2.0
83
		 *
84
		 * @param boolean Whether to expand callables (should always be true)
85
		 */
86
		do_action( 'jetpack_full_sync_callables', true );
87
88
		// The number of actions enqueued, and next module state (true == done)
89
		return array( 1, true ); 
90
	}
91
92
	public function estimate_full_sync_actions( $config ) {
93
		return 1;
94
	}
95
96
	public function get_full_sync_actions() {
97
		return array( 'jetpack_full_sync_callables' );
98
	}
99
100
	public function unlock_sync_callable() {
101
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
102
	}
103
	
104
	public function maybe_sync_callables() {
105
		if ( ! is_admin() || Jetpack_Sync_Settings::is_doing_cron() ) {
106
			return;
107
		}
108
109
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
110
			return;
111
		}
112
113
		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...
114
115
		$callables = $this->get_all_callables();
116
117
		if ( empty( $callables ) ) {
118
			return;
119
		}
120
121
		$callable_checksums = (array) get_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
122
123
		// only send the callables that have changed
124
		foreach ( $callables as $name => $value ) {
125
			$checksum = $this->get_check_sum( $value );
126
			// explicitly not using Identical comparison as get_option returns a string
127
			if ( ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ) && ! is_null( $value ) ) {
128
				/**
129
				 * Tells the client to sync a callable (aka function) to the server
130
				 *
131
				 * @since 4.2.0
132
				 *
133
				 * @param string The name of the callable
134
				 * @param mixed The value of the callable
135
				 */
136
				do_action( 'jetpack_sync_callable', $name, $value );
137
				$callable_checksums[ $name ] = $checksum;
138
			} else {
139
				$callable_checksums[ $name ] = $checksum;
140
			}
141
		}
142
		update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
143
	}
144
145
	public function expand_callables( $args ) {
146
		if ( $args[0] ) {
147
			return $this->get_all_callables();
148
		}
149
150
		return $args;
151
	}
152
}
153