Completed
Push — try/refactor-sync-client-class ( 71e59e...d33bd5 )
by
unknown
11:05 queued 02:07
created

Jetpack_Sync_Module_Options::get_all_options()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 8
rs 9.4285
1
<?php
2
3
class Jetpack_Sync_Module_Options extends Jetpack_Sync_Module {
4
	private $options_whitelist;
5
	private $network_options_whitelist;
6
7
	public function name() {
8
		return "options";
9
	}
10
11
	public function init_listeners( $callable ) {
12
		// options
13
		add_action( 'added_option', $callable, 10, 2 );
14
		add_action( 'updated_option', $callable, 10, 3 );
15
		add_action( 'deleted_option', $callable, 10, 1 );
16
17
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
18
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
19
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
20
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
21
22
		// full sync
23
		add_action( 'jetpack_full_sync_options', $callable );
24
25
		// multi site network options
26
		if ( is_multisite() ) {
27
			add_action( 'add_site_option', $callable, 10, 2 );
28
			add_action( 'update_site_option', $callable, 10, 3 );
29
			add_action( 'delete_site_option', $callable, 10, 1 );
30
31
			// full sync
32
			add_action( 'jetpack_full_sync_network_options', $callable );
33
		}
34
35
		$whitelist_option_handler = array( $this, 'whitelist_options' );
36
		add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler );
37
		add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler );
38
		add_filter( 'jetpack_sync_before_enqueue_updated_option', $whitelist_option_handler );
39
40
		$whitelist_network_option_handler = array( $this, 'whitelist_network_options' );
41
		add_filter( 'jetpack_sync_before_enqueue_delete_site_option', $whitelist_network_option_handler );
42
		add_filter( 'jetpack_sync_before_enqueue_add_site_option', $whitelist_network_option_handler );
43
		add_filter( 'jetpack_sync_before_enqueue_update_site_option', $whitelist_network_option_handler );
44
	}
45
46
	public function set_defaults() {
47
		$this->update_options_whitelist();
48
		$this->network_options_whitelist = Jetpack_Sync_Defaults::$default_network_options_whitelist;
0 ignored issues
show
Bug introduced by
The property default_network_options_whitelist cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

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...
49
		// theme mod varies from theme to theme.
50
		$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' );
51
	}
52
53
	// TODO: force sync for whole module as interface method?
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
54
	function full_sync() {
55
		
56
57
		/**
58
		 * Tells the client to sync all options to the server
59
		 *
60
		 * @since 4.2.0
61
		 *
62
		 * @param boolean Whether to expand options (should always be true)
63
		 */
64
		do_action( 'jetpack_full_sync_options', true );
65
		
66
		return 1; // The number of actions enqueued
67
	}
68
69
	function full_sync_network() {
70
		/**
71
		 * Tells the client to sync all network options to the server
72
		 *
73
		 * @since 4.2.0
74
		 *
75
		 * @param boolean Whether to expand options (should always be true)
76
		 */
77
		do_action( 'jetpack_full_sync_network_options', true );
78
		return 1; // The number of actions enqueued
79
	}
80
81
	// Is public so that we don't have to store so much data all the options twice.
82
	function get_all_options() {
83
		$options = array();
84
		foreach ( $this->options_whitelist as $option ) {
85
			$options[ $option ] = get_option( $option );
86
		}
87
88
		return $options;
89
	}
90
91
	function get_all_network_options() {
92
		$options = array();
93
		foreach ( $this->network_options_whitelist as $option ) {
94
			$options[ $option ] = get_site_option( $option );
95
		}
96
97
		return $options;
98
	}
99
100
	function set_network_options_whitelist( $options ) {
101
		$this->network_options_whitelist = $options;
102
	}
103
104
	function get_network_options_whitelist() {
105
		return $this->network_options_whitelist;
106
	}
107
108
	function update_options_whitelist() {
109
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
110
		$this->options_whitelist = apply_filters( 'jetpack_options_whitelist', Jetpack_Sync_Defaults::$default_options_whitelist );
0 ignored issues
show
Bug introduced by
The property default_options_whitelist cannot be accessed from this context as it is declared private in class Jetpack_Sync_Defaults.

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...
111
	}
112
113
	function set_options_whitelist( $options ) {
114
		$this->options_whitelist = $options;
115
	}
116
117
	function get_options_whitelist() {
118
		return $this->options_whitelist;
119
	}
120
121
	// reject non-whitelisted options
122
	function whitelist_options( $args ) {
123
		if ( ! $this->is_whitelisted_option( $args[0] ) ) {
124
			return false;
125
		}
126
127
		return $args;
128
	}
129
130
	function is_whitelisted_option( $option ) {
131
		return in_array( $option, $this->options_whitelist );
132
	}
133
134
	// reject non-whitelisted network options
135
	function whitelist_network_options( $args ) {
136
		if ( ! $this->is_whitelisted_network_option( $args[0] ) ) {
137
			return false;
138
		}
139
140
		return $args;
141
	}
142
143
	function is_whitelisted_network_option( $option ) {
144
		return is_multisite() && in_array( $option, $this->network_options_whitelist );
145
	}
146
147
	function jetpack_sync_core_icon() {
148
		if ( function_exists( 'get_site_icon_url' ) ) {
149
			$url = get_site_icon_url();
150
		} else {
151
			return;
152
		}
153
154
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
155
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
156
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
157
			// This is the option that is synced with dotcom
158
			Jetpack_Options::update_option( 'site_icon_url', $url );
159
		} else if ( empty( $url ) ) {
160
			Jetpack_Options::delete_option( 'site_icon_url' );
161
		}
162
	}
163
}