Completed
Push — add/google-maps/api-key ( 5599f7...90239a )
by
unknown
26:57 queued 16:47
created

Jetpack_Sync_Module_Options   A

Complexity

Total Complexity 27

Size/Duplication

Total Lines 134
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 134
rs 10
wmc 27
lcom 1
cbo 3

15 Methods

Rating   Name   Duplication   Size   Complexity  
A init_before_send() 0 4 1
A name() 0 3 1
A update_options_whitelist() 0 4 1
A set_options_whitelist() 0 3 1
A get_options_whitelist() 0 3 1
A init_listeners() 0 19 1
A set_defaults() 0 3 1
A get_full_sync_actions() 0 3 1
A get_all_options() 0 14 2
A whitelist_options() 0 15 4
A is_whitelisted_option() 0 3 2
A filter_theme_mods() 0 5 3
B jetpack_sync_core_icon() 0 16 5
A expand_options() 0 7 2
A enqueue_full_sync_actions() 0 12 1
1
<?php
2
3
class Jetpack_Sync_Module_Options extends Jetpack_Sync_Module {
4
	private $options_whitelist;
5
6
	public function name() {
7
		return 'options';
8
	}
9
10
	public function init_listeners( $callable ) {
11
		// options
12
		add_action( 'added_option', $callable, 10, 2 );
13
		add_action( 'updated_option', $callable, 10, 3 );
14
		add_action( 'deleted_option', $callable, 10, 1 );
15
16
		// Sync Core Icon: Detect changes in Core's Site Icon and make it syncable.
17
		add_action( 'add_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
18
		add_action( 'update_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
19
		add_action( 'delete_option_site_icon', array( $this, 'jetpack_sync_core_icon' ) );
20
21
		// full sync
22
		add_action( 'jetpack_full_sync_options', $callable );
23
24
		$whitelist_option_handler = array( $this, 'whitelist_options' );
25
		add_filter( 'jetpack_sync_before_enqueue_deleted_option', $whitelist_option_handler );
26
		add_filter( 'jetpack_sync_before_enqueue_added_option', $whitelist_option_handler );
27
		add_filter( 'jetpack_sync_before_enqueue_updated_option', $whitelist_option_handler );
28
	}
29
30
	public function init_before_send() {
31
		// full sync
32
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_options', array( $this, 'expand_options' ) );
33
	}
34
35
	public function set_defaults() {
36
		$this->update_options_whitelist();
37
	}
38
39
	function enqueue_full_sync_actions() {
40
		/**
41
		 * Tells the client to sync all options to the server
42
		 *
43
		 * @since 4.2.0
44
		 *
45
		 * @param boolean Whether to expand options (should always be true)
46
		 */
47
		do_action( 'jetpack_full_sync_options', true );
48
49
		return 1; // The number of actions enqueued
50
	}
51
52
	function get_full_sync_actions() {
53
		return array( 'jetpack_full_sync_options' );
54
	}
55
56
	// Is public so that we don't have to store so much data all the options twice.
57
	function get_all_options() {
58
		$options = array();
59
		foreach ( $this->options_whitelist as $option ) {
60
			$options[ $option ] = get_option( $option );
61
		}
62
63
		// add theme mods
64
		$theme_mods_option = 'theme_mods_'.get_option( 'stylesheet' );
65
		$theme_mods_value  = get_option( $theme_mods_option );
66
		$this->filter_theme_mods( $theme_mods_value );
67
		$options[ $theme_mods_option ] = $theme_mods_value;
68
69
		return $options;
70
	}
71
72
	function update_options_whitelist() {
73
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
74
		$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...
75
	}
76
77
	function set_options_whitelist( $options ) {
78
		$this->options_whitelist = $options;
79
	}
80
81
	function get_options_whitelist() {
82
		return $this->options_whitelist;
83
	}
84
85
	// reject non-whitelisted options
86
	function whitelist_options( $args ) {
87
		if ( ! $this->is_whitelisted_option( $args[0] ) ) {
88
			return false;
89
		}
90
91
		// filter our weird array( false ) value for theme_mods_*
92
		if ( 'theme_mods_' === substr( $args[0], 0, 11 ) ) {
93
			$this->filter_theme_mods( $args[1] );
94
			if ( isset( $args[2] ) ) { 
95
				$this->filter_theme_mods( $args[2] );
96
			}
97
		}
98
99
		return $args;
100
	}
101
102
	function is_whitelisted_option( $option ) {
103
		return in_array( $option, $this->options_whitelist ) || 'theme_mods_' === substr( $option, 0, 11 );
104
	}
105
106
	private function filter_theme_mods( &$value ) {
107
		if ( is_array( $value ) && isset( $value[0] ) ) {
108
			unset( $value[0] ); 
109
		}
110
	}
111
112
	function jetpack_sync_core_icon() {
113
		if ( function_exists( 'get_site_icon_url' ) ) {
114
			$url = get_site_icon_url();
115
		} else {
116
			return;
117
		}
118
119
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
120
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
121
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
122
			// This is the option that is synced with dotcom
123
			Jetpack_Options::update_option( 'site_icon_url', $url );
124
		} else if ( empty( $url ) ) {
125
			Jetpack_Options::delete_option( 'site_icon_url' );
126
		}
127
	}
128
129
	public function expand_options( $args ) {
130
		if ( $args[0] ) {
131
			return $this->get_all_options();
132
		}
133
134
		return $args;
135
	}
136
}
137