Completed
Push — master-stable ( 2e27ce...519850 )
by Jeremy
12:36
created

estimate_full_sync_actions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
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;
0 ignored issues
show
Bug introduced by
The property default_constants_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...
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
		$constants = $this->get_all_constants();
74
		if ( empty( $constants ) ) {
75
			return;
76
		}
77
78
		set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time );
0 ignored issues
show
Bug introduced by
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...
79
		$constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() );
80
81
		foreach ( $constants as $name => $value ) {
82
			$checksum = $this->get_check_sum( $value );
83
			// explicitly not using Identical comparison as get_option returns a string
84
			if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && ! is_null( $value ) ) {
85
				/**
86
				 * Tells the client to sync a constant to the server
87
				 *
88
				 * @since 4.2.0
89
				 *
90
				 * @param string The name of the constant
91
				 * @param mixed The value of the constant
92
				 */
93
				do_action( 'jetpack_sync_constant', $name, $value );
94
				$constants_checksums[ $name ] = $checksum;
95
			} else {
96
				$constants_checksums[ $name ] = $checksum;
97
			}
98
		}
99
		update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
100
	}
101
102
	// public so that we don't have to store an option for each constant
103
	function get_all_constants() {
104
		return array_combine(
105
			$this->constants_whitelist,
106
			array_map( array( $this, 'get_constant' ), $this->constants_whitelist )
107
		);
108
	}
109
110
	private function get_constant( $constant ) {
111
		return ( defined( $constant ) ) ?
112
			constant( $constant )
113
			: null;
114
	}
115
116
	public function expand_constants( $args ) {
117
		if ( $args[0] ) {
118
			return $this->get_all_constants();
119
		}
120
121
		return $args;
122
	}
123
}
124