Completed
Push — fix/gulp-env ( ec4107...cf0b47 )
by
unknown
133:51 queued 124:05
created

Jetpack_Sync_Module_Options::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
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
		// theme mod varies from theme to theme.
38
		$this->options_whitelist[] = 'theme_mods_' . get_option( 'stylesheet' );
39
	}
40
41
	function enqueue_full_sync_actions() {
42
		/**
43
		 * Tells the client to sync all options to the server
44
		 *
45
		 * @since 4.2.0
46
		 *
47
		 * @param boolean Whether to expand options (should always be true)
48
		 */
49
		do_action( 'jetpack_full_sync_options', true );
50
		return 1; // The number of actions enqueued
51
	}
52
53
	function get_full_sync_actions() {
54
		return array( 'jetpack_full_sync_options' );
55
	}
56
57
	// Is public so that we don't have to store so much data all the options twice.
58
	function get_all_options() {
59
		$options = array();
60
		foreach ( $this->options_whitelist as $option ) {
61
			$options[ $option ] = get_option( $option );
62
		}
63
64
		return $options;
65
	}
66
67
	function update_options_whitelist() {
68
		/** This filter is already documented in json-endpoints/jetpack/class.wpcom-json-api-get-option-endpoint.php */
69
		$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...
70
	}
71
72
	function set_options_whitelist( $options ) {
73
		$this->options_whitelist = $options;
74
	}
75
76
	function get_options_whitelist() {
77
		return $this->options_whitelist;
78
	}
79
80
	// reject non-whitelisted options
81
	function whitelist_options( $args ) {
82
		if ( ! $this->is_whitelisted_option( $args[0] ) ) {
83
			return false;
84
		}
85
86
		return $args;
87
	}
88
89
	function is_whitelisted_option( $option ) {
90
		return in_array( $option, $this->options_whitelist );
91
	}
92
93
	function jetpack_sync_core_icon() {
94
		if ( function_exists( 'get_site_icon_url' ) ) {
95
			$url = get_site_icon_url();
96
		} else {
97
			return;
98
		}
99
100
		require_once( JETPACK__PLUGIN_DIR . 'modules/site-icon/site-icon-functions.php' );
101
		// If there's a core icon, maybe update the option.  If not, fall back to Jetpack's.
102
		if ( ! empty( $url ) && $url !== jetpack_site_icon_url() ) {
103
			// This is the option that is synced with dotcom
104
			Jetpack_Options::update_option( 'site_icon_url', $url );
105
		} else if ( empty( $url ) ) {
106
			Jetpack_Options::delete_option( 'site_icon_url' );
107
		}
108
	}
109
110
	public function expand_options( $args ) {
111
		if ( $args[0] ) {
112
			return $this->get_all_options();
113
		}
114
115
		return $args;
116
	}
117
}
118