Completed
Push — master-stable ( 26cf6e...6ead22 )
by
unknown
159:08 queued 149:11
created

sync/class.jetpack-sync-module-callables.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 );
18
		} else {
19
			$this->callable_whitelist = Jetpack_Sync_Defaults::$default_callable_whitelist;
20
		}
21
	}
22
23
	public function init_listeners( $callable ) {
24
		add_action( 'jetpack_sync_callable', $callable, 10, 2 );
25
26
		// always send change to active modules right away
27
		add_action( 'update_option_jetpack_active_modules', array( $this, 'unlock_sync_callable' ) );
28
29
		// get_plugins and wp_version
30
		// gets fired when new code gets installed, updates etc.
31
		add_action( 'upgrader_process_complete', array( $this, 'unlock_sync_callable' ) );
32
	}
33
34
	public function init_full_sync_listeners( $callable ) {
35
		add_action( 'jetpack_full_sync_callables', $callable );
36
	}
37
38
	public function init_before_send() {
39
		add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_callables' ) );
40
41
		// full sync
42
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) );
43
	}
44
45
	public function reset_data() {
46
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
47
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
48
	}
49
50
	function set_callable_whitelist( $callables ) {
51
		$this->callable_whitelist = $callables;
52
	}
53
54
	function get_callable_whitelist() {
55
		return $this->callable_whitelist;
56
	}
57
58
	public function get_all_callables() {
59
		// get_all_callables should run as the master user always.
60
		$current_user_id = get_current_user_id();
61
		wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) );
62
		$callables = array_combine(
63
			array_keys( $this->callable_whitelist ),
64
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
65
		);
66
		wp_set_current_user( $current_user_id );
67
68
		return $callables;
69
	}
70
71
	private function get_callable( $callable ) {
72
		return call_user_func( $callable );
73
	}
74
75
	public function enqueue_full_sync_actions( $config ) {
76
		/**
77
		 * Tells the client to sync all callables to the server
78
		 *
79
		 * @since 4.2.0
80
		 *
81
		 * @param boolean Whether to expand callables (should always be true)
82
		 */
83
		do_action( 'jetpack_full_sync_callables', true );
84
85
		return 1; // The number of actions enqueued
86
	}
87
88
	public function estimate_full_sync_actions( $config ) {
89
		return 1;
90
	}
91
92
	public function get_full_sync_actions() {
93
		return array( 'jetpack_full_sync_callables' );
94
	}
95
96
	public function unlock_sync_callable() {
97
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
98
	}
99
	
100 View Code Duplication
	public function maybe_sync_callables() {
101
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
102
			return;
103
		}
104
105
		set_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_callables_wait_time );
0 ignored issues
show
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...
106
107
		$callables = $this->get_all_callables();
108
109
		if ( empty( $callables ) ) {
110
			return;
111
		}
112
113
		$callable_checksums = (array) get_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
114
115
		// only send the callables that have changed
116
		foreach ( $callables as $name => $value ) {
117
			$checksum = $this->get_check_sum( $value );
118
			// explicitly not using Identical comparison as get_option returns a string
119
			if ( ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ) && ! is_null( $value ) ) {
120
				/**
121
				 * Tells the client to sync a callable (aka function) to the server
122
				 *
123
				 * @since 4.2.0
124
				 *
125
				 * @param string The name of the callable
126
				 * @param mixed The value of the callable
127
				 */
128
				do_action( 'jetpack_sync_callable', $name, $value );
129
				$callable_checksums[ $name ] = $checksum;
130
			} else {
131
				$callable_checksums[ $name ] = $checksum;
132
			}
133
		}
134
		update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
135
	}
136
137
	public function expand_callables( $args ) {
138
		if ( $args[0] ) {
139
			return $this->get_all_callables();
140
		}
141
142
		return $args;
143
	}
144
}
145