Completed
Push — update/do-not-maybe-send-calla... ( 7556a4...f3157d )
by
unknown
95:48 queued 87:03
created

Jetpack_Sync_Module_Callables::set_defaults()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 7
rs 9.4285
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
		// 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
		// full sync
38
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_callables', array( $this, 'expand_callables' ) );
39
	}
40
41
	public function reset_data() {
42
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
43
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
44
	}
45
46
	function set_callable_whitelist( $callables ) {
47
		$this->callable_whitelist = $callables;
48
	}
49
50
	function get_callable_whitelist() {
51
		return $this->callable_whitelist;
52
	}
53
54
	public function get_all_callables() {
55
		// get_all_callables should run as the master user always.
56
		$current_user_id = get_current_user_id();
57
		wp_set_current_user( Jetpack_Options::get_option( 'master_user' ) );
58
		$callables = array_combine(
59
			array_keys( $this->callable_whitelist ),
60
			array_map( array( $this, 'get_callable' ), array_values( $this->callable_whitelist ) )
61
		);
62
		wp_set_current_user( $current_user_id );
63
64
		return $callables;
65
	}
66
67
	private function get_callable( $callable ) {
68
		return call_user_func( $callable );
69
	}
70
71 View Code Duplication
	public function enqueue_full_sync_actions() {
72
		/**
73
		 * Tells the client to sync all callables to the server
74
		 *
75
		 * @since 4.2.0
76
		 *
77
		 * @param boolean Whether to expand callables (should always be true)
78
		 */
79
		do_action( 'jetpack_full_sync_callables', true );
80
		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...
81
		remove_action( 'jetpack_sync_before_send', array( $this, 'maybe_sync_callables' ) );
82
83
		return 1; // The number of actions enqueued
84
	}
85
86
	public function get_full_sync_actions() {
87
		return array( 'jetpack_full_sync_callables' );
88
	}
89
90
	public function force_sync_callables() {
91
		delete_option( self::CALLABLES_CHECKSUM_OPTION_NAME );
92
		delete_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME );
93
		$this->maybe_sync_callables();
94
	}
95
96 View Code Duplication
	public function maybe_sync_callables() {
97
		if ( get_transient( self::CALLABLES_AWAIT_TRANSIENT_NAME ) ) {
98
			return;
99
		}
100
101
		$callables = $this->get_all_callables();
102
103
		if ( empty( $callables ) ) {
104
			return;
105
		}
106
107
		$callable_checksums = (array) get_option( self::CALLABLES_CHECKSUM_OPTION_NAME, array() );
108
		if ( empty( $callable_checksums ) ) {
109
			$this->enqueue_full_sync_actions();
110
			return;
111
		}
112
		// only send the callables that have changed
113
		foreach ( $callables as $name => $value ) {
114
			$checksum = $this->get_check_sum( $value );
115
			// explicitly not using Identical comparison as get_option returns a string
116
			if ( ! $this->still_valid_checksum( $callable_checksums, $name, $checksum ) && ! is_null( $value ) ) {
117
				/**
118
				 * Tells the client to sync a callable (aka function) to the server
119
				 *
120
				 * @since 4.2.0
121
				 *
122
				 * @param string The name of the callable
123
				 * @param mixed The value of the callable
124
				 */
125
				do_action( 'jetpack_sync_callable', $name, $value );
126
				$callable_checksums[ $name ] = $checksum;
127
			} else {
128
				$callable_checksums[ $name ] = $checksum;
129
			}
130
		}
131
		update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
132
		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...
133
	}
134
135 View Code Duplication
	public function expand_callables( $args ) {
136
		if ( $args[0] ) {
137
			$callables = $this->get_all_callables();
138
			// Update the callable checksums on full sync.
139
			$callable_checksums = array();
140
			foreach ( $callables as $name => $value ) {
141
				$callable_checksums[ $name ] = $this->get_check_sum( $value );
142
			}
143
			update_option( self::CALLABLES_CHECKSUM_OPTION_NAME, $callable_checksums );
144
145
			return $callables;
146
		}
147
148
		return $args;
149
	}
150
}
151