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

sync/class.jetpack-sync-module-constants.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-defaults.php';
4
5
class Jetpack_Sync_Module_Constants extends Jetpack_Sync_Module {
6
	const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum';
7
	const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await';
8
9
	public function name() {
10
		return 'constants';
11
	}
12
13
	private $constants_whitelist;
14
15
	public function set_defaults() {
16
		$this->constants_whitelist = Jetpack_Sync_Defaults::$default_constants_whitelist;
17
	}
18
19
	public function init_listeners( $callable ) {
20
		add_action( 'jetpack_sync_constant', $callable, 10, 2 );
21
	}
22
23
	public function init_full_sync_listeners( $callable ) {
24
		add_action( 'jetpack_full_sync_constants', $callable );
25
	}
26
27
	public function init_before_send() {
28
		add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_constants' ) );
29
30
		// full sync
31
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) );
32
	}
33
34
	public function reset_data() {
35
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
36
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
37
	}
38
39
	function set_constants_whitelist( $constants ) {
40
		$this->constants_whitelist = $constants;
41
	}
42
43
	function get_constants_whitelist() {
44
		return $this->constants_whitelist;
45
	}
46
47
	function enqueue_full_sync_actions( $config ) {
48
		/**
49
		 * Tells the client to sync all constants to the server
50
		 *
51
		 * @since 4.2.0
52
		 *
53
		 * @param boolean Whether to expand constants (should always be true)
54
		 */
55
		do_action( 'jetpack_full_sync_constants', true );
56
57
		return 1;
58
	}
59
60
	function estimate_full_sync_actions( $config ) {
61
		return 1;
62
	}
63
64
	function get_full_sync_actions() {
65
		return array( 'jetpack_full_sync_constants' );
66
	}
67
68 View Code Duplication
	function maybe_sync_constants() {
69
		if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
70
			return;
71
		}
72
73
		set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time );
0 ignored issues
show
The property default_sync_constants_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...
74
75
		$constants = $this->get_all_constants();
76
		if ( empty( $constants ) ) {
77
			return;
78
		}
79
80
		$constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() );
81
82
		foreach ( $constants as $name => $value ) {
83
			$checksum = $this->get_check_sum( $value );
84
			// explicitly not using Identical comparison as get_option returns a string
85
			if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && ! is_null( $value ) ) {
86
				/**
87
				 * Tells the client to sync a constant to the server
88
				 *
89
				 * @since 4.2.0
90
				 *
91
				 * @param string The name of the constant
92
				 * @param mixed The value of the constant
93
				 */
94
				do_action( 'jetpack_sync_constant', $name, $value );
95
				$constants_checksums[ $name ] = $checksum;
96
			} else {
97
				$constants_checksums[ $name ] = $checksum;
98
			}
99
		}
100
		update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
101
	}
102
103
	// public so that we don't have to store an option for each constant
104
	function get_all_constants() {
105
		return array_combine(
106
			$this->constants_whitelist,
107
			array_map( array( $this, 'get_constant' ), $this->constants_whitelist )
108
		);
109
	}
110
111
	private function get_constant( $constant ) {
112
		return ( defined( $constant ) ) ?
113
			constant( $constant )
114
			: null;
115
	}
116
117
	public function expand_constants( $args ) {
118
		if ( $args[0] ) {
119
			return $this->get_all_constants();
120
		}
121
122
		return $args;
123
	}
124
}
125