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

maybe_sync_constants()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 33
Code Lines 16

Duplication

Lines 33
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 33
loc 33
rs 8.439
1
<?php
2
3
class Jetpack_Sync_Module_Constants extends Jetpack_Sync_Module {
4
	const CONSTANTS_CHECKSUM_OPTION_NAME = 'jetpack_constants_sync_checksum';
5
	const CONSTANTS_AWAIT_TRANSIENT_NAME = 'jetpack_sync_constants_await';
6
7
	public function name() {
8
		return "constants";
9
	}
10
11
	private $constants_whitelist;
12
13
	public function set_defaults() {
14
		$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...
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 7 spaces

This check looks for improperly formatted assignments.

Every assignment must have exactly one space before and one space after the equals operator.

To illustrate:

$a = "a";
$ab = "ab";
$abc = "abc";

will have no issues, while

$a   = "a";
$ab  = "ab";
$abc = "abc";

will report issues in lines 1 and 2.

Loading history...
15
	}
16
17
	public function init_listeners( $callable ) {
18
		add_action( 'jetpack_sync_constant', $callable, 10, 2 );
19
20
		// full sync
21
		add_action( 'jetpack_full_sync_constants', $callable );
22
	}
23
24
	public function init_before_send() {
25
		add_action( 'jetpack_sync_before_send', array( $this, 'maybe_sync_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;
35
	}
36
37
	function get_constants_whitelist() {
38
		return $this->constants_whitelist;
39
	}
40
41
	function force_sync_constants() {
42
		delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME );
43
		delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME );
44
		$this->maybe_sync_constants();
45
	}
46
47
	function full_sync() {
48
		/**
49
		 * Tells the client to sync all constants to the server
50
		 *
51
		 * @since 4.1
52
		 *
53
		 * @param boolean Whether to expand constants (should always be true)
54
		 */
55
		do_action( 'jetpack_full_sync_constants', true );
56
		return 1; // The number of actions enqueued
57
	}
58
59 View Code Duplication
	function maybe_sync_constants() {
60
		if ( get_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ) ) {
61
			return;
62
		}
63
64
		$constants = $this->get_all_constants();
65
		if ( empty( $constants ) ) {
66
			return;
67
		}
68
69
		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...
70
		$constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() );
71
72
		foreach ( $constants as $name => $value ) {
73
			$checksum = $this->get_check_sum( $value );
74
			// explicitly not using Identical comparison as get_option returns a string
75
			if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum )  && ! is_null( $value ) ) {
76
				/**
77
				 * Tells the client to sync a constant to the server
78
				 *
79
				 * @since 4.2.0
80
				 *
81
				 * @param string The name of the constant
82
				 * @param mixed The value of the constant
83
				 */
84
				do_action( 'jetpack_sync_constant', $name, $value );
85
				$constants_checksums[ $name ] = $checksum;
86
			} else {
87
				$constants_checksums[ $name ] = $checksum;
88
			}
89
		}
90
		update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
91
	}
92
93
	// public so that we don't have to store an option for each constant
94
	function get_all_constants() {
95
		return array_combine(
96
			$this->constants_whitelist,
97
			array_map( array( $this, 'get_constant' ), $this->constants_whitelist )
98
		);
99
	}
100
101
	private function get_constant( $constant ) {
102
		return ( defined( $constant ) ) ?
103
			constant( $constant ) 
104
			: null;
105
	}
106
}