Completed
Push — fix/production-builds ( 301e61...f5e011 )
by
unknown
288:28 queued 279:29
created

Constants::maybe_sync_constants()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 34

Duplication

Lines 18
Ratio 52.94 %

Importance

Changes 0
Metric Value
cc 6
nc 5
nop 0
dl 18
loc 34
rs 8.7537
c 0
b 0
f 0
1
<?php
2
3
namespace Automattic\Jetpack\Sync\Modules;
4
5
class 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
	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 View Code Duplication
		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
			$constants           = $this->get_all_constants();
116
			$constants_checksums = array();
117
			foreach ( $constants as $name => $value ) {
118
				$constants_checksums[ $name ] = $this->get_check_sum( $value );
119
			}
120
			update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums );
121
			return $constants;
122
		}
123
		return $args;
124
	}
125
}
126