Completed
Push — master ( 3484cd...e53ab5 )
by Nabeel
08:24 queued 08:08
created

Jetpack_Sync_Module_Constants::set_defaults()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
	public function init_listeners( $callable ) {
14
		add_action( 'jetpack_sync_constant', $callable, 10, 2 );
15
	}
16
17
	public function init_full_sync_listeners( $callable ) {
18
		add_action( 'jetpack_full_sync_constants', $callable );
19
	}
20
21
	public function init_before_send() {
22
		add_action( 'jetpack_sync_before_send_queue_sync', array( $this, 'maybe_sync_constants' ) );
23
24
		// full sync
25
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) );
26
	}
27
28
	public function reset_data() {
29
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
30
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
31
	}
32
33
	function set_constants_whitelist( $constants ) {
34
		$this->constants_whitelist = $constants;
0 ignored issues
show
Bug introduced by
The property constants_whitelist does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
	}
36
37
	function get_constants_whitelist() {
38
		return Jetpack_Sync_Defaults::get_constants_whitelist();
39
	}
40
41
	function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
42
		/**
43
		 * Tells the client to sync all constants to the server
44
		 *
45
		 * @since 4.2.0
46
		 *
47
		 * @param boolean Whether to expand constants (should always be true)
48
		 */
49
		do_action( 'jetpack_full_sync_constants', true );
50
51
		// The number of actions enqueued, and next module state (true == done)
52
		return array( 1, true );
53
	}
54
55
	function estimate_full_sync_actions( $config ) {
56
		return 1;
57
	}
58
59
	function get_full_sync_actions() {
60
		return array( 'jetpack_full_sync_constants' );
61
	}
62
63 View Code Duplication
	function maybe_sync_constants() {
64
		if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
65
			return;
66
		}
67
68
		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...
69
70
		$constants = $this->get_all_constants();
71
		if ( empty( $constants ) ) {
72
			return;
73
		}
74
75
		$constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() );
76
77
		foreach ( $constants as $name => $value ) {
78
			$checksum = $this->get_check_sum( $value );
79
			// explicitly not using Identical comparison as get_option returns a string
80
			if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && ! is_null( $value ) ) {
81
				/**
82
				 * Tells the client to sync a constant to the server
83
				 *
84
				 * @since 4.2.0
85
				 *
86
				 * @param string The name of the constant
87
				 * @param mixed The value of the constant
88
				 */
89
				do_action( 'jetpack_sync_constant', $name, $value );
90
				$constants_checksums[ $name ] = $checksum;
91
			} else {
92
				$constants_checksums[ $name ] = $checksum;
93
			}
94
		}
95
		update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
96
	}
97
98
	// public so that we don't have to store an option for each constant
99
	function get_all_constants() {
100
		$constants_whitelist = $this->get_constants_whitelist();
101
		return array_combine(
102
			$constants_whitelist,
103
			array_map( array( $this, 'get_constant' ), $constants_whitelist )
104
		);
105
	}
106
107
	private function get_constant( $constant ) {
108
		return ( defined( $constant ) ) ?
109
			constant( $constant )
110
			: null;
111
	}
112
113
	public function expand_constants( $args ) {
114
		if ( $args[0] ) {
115
			return $this->get_all_constants();
116
		}
117
118
		return $args;
119
	}
120
}
121