Completed
Push — add/sync-rest-options ( c51f1f...c0c257 )
by
unknown
21:05 queued 12:26
created

Jetpack_Sync_Options::update_option()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 9
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 2
eloc 7
nc 2
nop 0
1
<?php
2
3
4
class Jetpack_Sync_Options {
5
6
	static $options = array(
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $options.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
7
		'blogname',
8
	);
9
10
	static $sync = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sync.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
11
	static $delete = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $delete.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
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 );
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned correctly; expected 1 space but found 11 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...
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