|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class Jetpack_Sync_Options { |
|
5
|
|
|
|
|
6
|
|
|
static $options = array( |
|
|
|
|
|
|
7
|
|
|
'blogname', |
|
8
|
|
|
); |
|
9
|
|
|
|
|
10
|
|
|
static $sync = array(); |
|
|
|
|
|
|
11
|
|
|
static $delete = array(); |
|
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
static function init() { |
|
14
|
|
|
foreach ( self::$options as $option ) { |
|
15
|
|
|
self::register( $option ); |
|
16
|
|
|
} |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
static function register( $option ) { |
|
20
|
|
|
add_action( "add_option_{$option}", array( __CLASS__, 'add_option' ) ); |
|
21
|
|
|
add_action( "update_option_{$option}", array( __CLASS__, 'update_option' ) ); |
|
22
|
|
|
add_action( "delete_option_{$option}", array( __CLASS__, 'delete_option' ) ); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
static function add_option( $option ) { |
|
26
|
|
|
self::$sync[] = $option; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
View Code Duplication |
static function update_option() { |
|
30
|
|
|
$option = current_filter(); |
|
31
|
|
|
$prefix = 'update_option_'; |
|
32
|
|
|
if ( 0 !== strpos( $option, $prefix ) ) { |
|
33
|
|
|
return; |
|
34
|
|
|
} |
|
35
|
|
|
$option = substr( $option, strlen( $prefix ) ); |
|
36
|
|
|
self::$sync[] = $option; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
static function delete_option( $option ) { |
|
40
|
|
|
self::$delete[] = $option; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
static function sync() { |
|
44
|
|
|
return self::values( self::get_options_to_sync() ); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
static function sync_sometimes() { |
|
48
|
|
|
|
|
49
|
|
|
// Since there are option in the sync we know that things have changed. |
|
50
|
|
|
if( ! empty ( self::$sync ) ) { |
|
51
|
|
|
return self::sync_all(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
$values = self::values( self::$options ); |
|
55
|
|
|
$check_sum = self::get_check_sum( $values ); |
|
56
|
|
|
|
|
57
|
|
|
if ( Jetpack_Options::get_option( 'options_check_sum' ) !== $check_sum ) { |
|
58
|
|
|
return self::sync_all( $values, $check_sum ); |
|
59
|
|
|
} |
|
60
|
|
|
return null; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
static function sync_all( $values = null, $check_sum = null ) { |
|
64
|
|
|
if ( is_null( $values ) ) { |
|
65
|
|
|
$values = self::values( self::$options ); |
|
|
|
|
|
|
66
|
|
|
} |
|
67
|
|
|
if( is_null( $check_sum ) ) { |
|
68
|
|
|
$check_sum = self::get_check_sum( $values ); |
|
69
|
|
|
} |
|
70
|
|
|
Jetpack_Options::update_option( 'options_check_sum', $check_sum ); |
|
71
|
|
|
return $values; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
static function options_to_sync() { |
|
75
|
|
|
return array_unique( self::$sync ); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
static function get_check_sum( $values ) { |
|
79
|
|
|
return crc32( self::get_query_string( $values ) ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
static function get_query_string( $values ) { |
|
83
|
|
|
return build_query( $values ); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
static function values( $sync = array() ) { |
|
87
|
|
|
$values = array(); |
|
88
|
|
|
if ( ! empty( $sync ) ) { |
|
89
|
|
|
foreach ( $sync as $key ) { |
|
90
|
|
|
$values[ $key ] = self::get( $key ); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
return $values; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
static function get_options_to_sync() { |
|
97
|
|
|
return array_unique( self::$sync ); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
static function sync_delete() { |
|
101
|
|
|
return array_unique( self::$delete ); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
static function get( $constant ) { |
|
105
|
|
|
return get_option( $constant ); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.