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; |
|
|
|
|
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 ); |
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: