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; |
|
|
|
|
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
|
|
|
// full sync |
28
|
|
|
add_filter( 'jetpack_sync_before_send_jetpack_full_sync_constants', array( $this, 'expand_constants' ) ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
public function reset_data() { |
32
|
|
|
delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME ); |
33
|
|
|
delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
function set_constants_whitelist( $constants ) { |
37
|
|
|
$this->constants_whitelist = $constants; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
function get_constants_whitelist() { |
41
|
|
|
return $this->constants_whitelist; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
function force_sync_constants() { |
45
|
|
|
delete_option( self::CONSTANTS_CHECKSUM_OPTION_NAME ); |
46
|
|
|
delete_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME ); |
47
|
|
|
$this->maybe_sync_constants(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
View Code Duplication |
function enqueue_full_sync_actions() { |
51
|
|
|
/** |
52
|
|
|
* Tells the client to sync all constants to the server |
53
|
|
|
* |
54
|
|
|
* @since 4.2.0 |
55
|
|
|
* |
56
|
|
|
* @param boolean Whether to expand constants (should always be true) |
57
|
|
|
*/ |
58
|
|
|
do_action( 'jetpack_full_sync_constants', true ); |
59
|
|
|
remove_action( 'jetpack_sync_before_send', array( $this, 'maybe_sync_constants' ) ); |
60
|
|
|
set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time ); |
|
|
|
|
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
|
|
|
|
79
|
|
|
$constants_checksums = (array) get_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, array() ); |
80
|
|
|
if ( empty( $constants_checksums ) ) { |
81
|
|
|
$this->enqueue_full_sync_actions(); |
82
|
|
|
return; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
foreach ( $constants as $name => $value ) { |
86
|
|
|
$checksum = $this->get_check_sum( $value ); |
87
|
|
|
// explicitly not using Identical comparison as get_option returns a string |
88
|
|
|
if ( ! $this->still_valid_checksum( $constants_checksums, $name, $checksum ) && ! is_null( $value ) ) { |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Tells the client to sync a constant to the server |
92
|
|
|
* |
93
|
|
|
* @since 4.2.0 |
94
|
|
|
* |
95
|
|
|
* @param string The name of the constant |
96
|
|
|
* @param mixed The value of the constant |
97
|
|
|
*/ |
98
|
|
|
do_action( 'jetpack_sync_constant', $name, $value ); |
99
|
|
|
$constants_checksums[ $name ] = $checksum; |
100
|
|
|
} else { |
101
|
|
|
$constants_checksums[ $name ] = $checksum; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums ); |
105
|
|
|
set_transient( self::CONSTANTS_AWAIT_TRANSIENT_NAME, microtime( true ), Jetpack_Sync_Defaults::$default_sync_constants_wait_time ); |
|
|
|
|
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
// public so that we don't have to store an option for each constant |
109
|
|
|
function get_all_constants() { |
110
|
|
|
return array_combine( |
111
|
|
|
$this->constants_whitelist, |
112
|
|
|
array_map( array( $this, 'get_constant' ), $this->constants_whitelist ) |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function get_constant( $constant ) { |
117
|
|
|
return ( defined( $constant ) ) ? |
118
|
|
|
constant( $constant ) |
119
|
|
|
: null; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
View Code Duplication |
public function expand_constants( $args ) { |
123
|
|
|
if ( $args[0] ) { |
124
|
|
|
$constants = $this->get_all_constants(); |
125
|
|
|
|
126
|
|
|
// Update the callable checksums on full sync. |
127
|
|
|
$constants_checksums = array(); |
128
|
|
|
foreach ( $constants as $name => $value ) { |
129
|
|
|
$constants_checksums[ $name ] = $this->get_check_sum( $value ); |
130
|
|
|
} |
131
|
|
|
update_option( self::CONSTANTS_CHECKSUM_OPTION_NAME, $constants_checksums ); |
132
|
|
|
return $constants; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return $args; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
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.