Completed
Push — branch-4.2 ( 0eaf06...5c7a26 )
by Jeremy
25:35 queued 16:13
created

maybe_sync_callables()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 35
Code Lines 16

Duplication

Lines 35
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 16
c 1
b 0
f 0
nc 5
nop 0
dl 35
loc 35
rs 8.439
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 "callables";
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
		// full sync
27
		add_action( 'jetpack_full_sync_callables', $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, 'force_sync_callables' ) );
32
	}
33
34
	public function init_before_send() {
35
		add_action( 'jetpack_sync_before_send', array( $this, 'maybe_sync_callables' ) );
36
	}
37
38
	public function reset_data() {
39
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
40
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
41
	}
42
43
	function set_callable_whitelist( $callables ) {
44
		$this->callable_whitelist = $callables;
45
	}
46
47
	function get_callable_whitelist() {
48
		return $this->callable_whitelist;
49
	}
50
51
	public function get_all_callables() {
52
		// get_all_callables should run as the master user always.
53
		$current_user_id = get_current_user_id();
54
		wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) );
55
		$callables = array_combine(
56
			array_keys( $this->callable_whitelist ),
57
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
58
		);
59
		wp_set_current_user( $current_user_id );
60
		return $callables;
61
	}
62
63
	private function get_callable( $callable ) {
64
		return call_user_func( $callable );
65
	}
66
67
	public function full_sync() {
68
		/**
69
		 * Tells the client to sync all callables to the server
70
		 *
71
		 * @since 4.2
72
		 *
73
		 * @param boolean Whether to expand callables (should always be true)
74
		 */
75
		do_action( 'jetpack_full_sync_callables', true );
76
		return 1; // The number of actions enqueued
77
	}
78
79
	public function force_sync_callables() {
80
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
81
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
82
		$this->maybe_sync_callables();
83
	}
84
85 View Code Duplication
	public function maybe_sync_callables() {
86
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
87
			return;
88
		}
89
		$callables = $this->get_all_callables();
90
		
91
		if ( empty( $callables ) ) {
92
			return;
93
		}
94
95
		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...
96
97
		$callable_checksums = (array) get_option( self::CALLABLES_CHECKSUM_OPTION_NAME , array() );
98
99
		// only send the callables that have changed
100
		foreach ( $callables as $name => $value ) {
101
			$checksum = $this->get_check_sum( $value );
102
			// explicitly not using Identical comparison as get_option returns a string
103
			if ( ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ) && ! is_null( $value ) ) {
104
				/**
105
				 * Tells the client to sync a callable (aka function) to the server
106
				 *
107
				 * @since 4.2.0
108
				 *
109
				 * @param string The name of the callable
110
				 * @param mixed The value of the callable
111
				 */
112
				do_action( 'jetpack_sync_callable', $name, $value );
113
				$callable_checksums[ $name ] = $checksum;
114
			} else {
115
				$callable_checksums[ $name ] = $checksum;
116
			}
117
		}
118
		update_option( self::CALLABLES_CHECKSUM_OPTION_NAME , $callable_checksums );
119
	}
120
}