Completed
Push — add/sync-rest-options ( b51d37...82a04d )
by
unknown
09:12
created

Jetpack_Options_Sync::init_option()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 5
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
require_once JETPACK__PLUGIN_DIR . 'class.json-api.php';
4
require_once JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php';
5
require_once JETPACK__PLUGIN_DIR . 'json-endpoints/jetpack/class.jetpack-json-api-endpoint.php';
6
require_once JETPACK__PLUGIN_DIR . 'json-endpoints/jetpack/class.jetpack-json-api-get-options-endpoint.php';
7
8
class Jetpack_Options_Sync {
9
10
	static $sync_all = array();
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $sync_all.

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 $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...
12
	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...
13
14
	static function init() {
15
		foreach (  Jetpack_JSON_API_Get_Options_Endpoint::$options as $option ) {
0 ignored issues
show
Bug introduced by
The property options cannot be accessed from this context as it is declared private in class Jetpack_JSON_API_Get_Options_Endpoint.

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...
16
			self::init_option( $option );
17
		}
18
	}
19
20
	static function init_option( $option ) {
21
		add_action( "delete_option_{$option}", array( __CLASS__, 'delete_option' ) );
22
		add_action( "update_option_{$option}", array( __CLASS__, 'update_option' ) );
23
		add_action( "add_option_{$option}",    array( __CLASS__, 'add_option'   ) );
24
	}
25
26
	static function init_mock_option( $mock_option, $callback ) {
27
		// The mock options get pre-fiexed with jetpack
28
		self::init_option(  'jetpack_' . $mock_option );
29
30
		add_filter( 'pre_option_jetpack_'. $mock_option, $callback );
31
		// This shouldn't happen but if it does we return the same as before.
32
		add_filter( 'option_jetpack_'. $mock_option, $callback );
33
34
35
		self::$sync_all[] = $mock_option;
36
	}
37
38
	static function sync_mock_option( $mock_option ) {
39
		self::$sync[] = $mock_option;
40
	}
41
42
	static function all() {
43
		self::$sync = array_merge( self::$sync, self::$sync_all );
44
	}
45
46
	static function init_constant( $constant ) {
47
		self::$sync_all[] = $constant;
48
	}
49
50
	static function delete_option( $option ) {
51
		self::$delete[] = $option;
52
	}
53
54 View Code Duplication
	static function update_option() {
55
		$option = current_filter();
56
		$prefix = 'update_option_';
57
		if ( 0 !== strpos( $option, $prefix ) ) {
58
			return;
59
		}
60
		$option = substr( $option, strlen( $prefix ) );
61
		self::$sync[] = $option;
62
	}
63
64
	static function add_option( $option ) {
65
		self::$sync[] = $option;
66
	}
67
68
	static function options_to_delete() {
69
		return array_unique( self::$delete );
70
	}
71
72
	static function options_to_sync() {
73
		return array_unique( self::$sync );
74
	}
75
76
	/**
77
	 * Sync all the data related to site settings
78
	 */
79
	static function sync_site_settings() {
80
81
		// set the options to sync
82
		foreach ( $options as $option ) {
0 ignored issues
show
Bug introduced by
The variable $options does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
83
			self::init_option( $option );
84
		}
85
86
	}
87
88
	static function get_settings() {
89
		error_log(self::get_settings_api_url());
90
		return self::json_api( self::get_settings_api_url(), 0 );
91
	}
92
93
	static function json_api( $url, $method = 'GET' ) {
94
		require_once JETPACK__PLUGIN_DIR . 'class.json-api.php';
95
96
		$api = WPCOM_JSON_API::init( $method, $url, null, true );
97
98
		require_once( JETPACK__PLUGIN_DIR . 'class.json-api-endpoints.php' );
99
		require_once( JETPACK__PLUGIN_DIR . 'json-endpoints.php' );
100
101
		new Jetpack_JSON_API_Get_Options_Endpoint( array (
102
			'method' => 'GET',
103
			'description' => 'Get all options.',
104
			'group' => '__do_not_document',
105
			'stat' => 'option:update',
106
			'path' => '/sites/%s/options',
107
			'path_labels' => array(
108
				'$site' => '(int|string) Site ID or domain',
109
			),
110
			'query_parameters' => array(
111
				'options' => '(array) The names of the option, mock option and constants to retrieve.',
112
			),
113
			'response_format' => array(
114
				'options' => '(array) The value of the updated option.',
115
			),
116
			'example_request' => 'https://public-api.wordpress.com/rest/v1.1/sites/82974409/options',
117
			'example_request_data' => array(
118
				'headers' => array( 'authorization' => 'Bearer YOUR_API_TOKEN' ),
119
				'body' => array(
120
					'blogname' => 'My new blog name'
121
				),
122
			),
123
		) );
124
125
		return $api->serve( false, true );
126
127
	}
128
129
	static function get_settings_api_url() {
130
		return sprintf( 'https://' . JETPACK__WPCOM_JSON_API_HOST . '/rest/v1.1/sites/%1$d/options', Jetpack_Options::get_option( 'id' ) );
131
	}
132
}
133
134
135
136
137
138