Completed
Push — fix/videopress-missing-from-se... ( 60564b )
by
unknown
22:41 queued 12:31
created

sync/class.jetpack-sync-module-themes.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
class Jetpack_Sync_Module_Themes extends Jetpack_Sync_Module {
4
	function name() {
5
		return 'themes';
6
	}
7
8
	public function init_listeners( $callable ) {
9
		add_action( 'switch_theme', array( $this, 'sync_theme_support' ) );
10
		add_action( 'jetpack_sync_current_theme_support', $callable );
11
12
		// Sidebar updates.
13
		add_action( 'update_option_sidebars_widgets', array( $this, 'sync_sidebar_widgets_actions' ), 10, 2 );
14
		add_action( 'jetpack_widget_added', $callable, 10, 2 );
15
		add_action( 'jetpack_widget_removed', $callable, 10, 2 );
16
		add_action( 'jetpack_widget_moved_to_inactive', $callable );
17
		add_action( 'jetpack_cleared_inactive_widgets', $callable );
18
		add_action( 'jetpack_widget_reordered', $callable );
19
	}
20
21
	public function init_full_sync_listeners( $callable ) {
22
		add_action( 'jetpack_full_sync_theme_data', $callable );
23
	}
24
25
	public function sync_theme_support() {
26
		/**
27
		 * Fires when the client needs to sync theme support info
28
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
29
		 *
30
		 * @since 4.2.0
31
		 *
32
		 * @param object the theme support hash
33
		 */
34
		do_action( 'jetpack_sync_current_theme_support' , $this->get_theme_support_info() );
35
	}
36
37
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
38
		/**
39
		 * Tells the client to sync all theme data to the server
40
		 *
41
		 * @since 4.2.0
42
		 *
43
		 * @param boolean Whether to expand theme data (should always be true)
44
		 */
45
		do_action( 'jetpack_full_sync_theme_data', true );
46
47
		// The number of actions enqueued, and next module state (true == done)
48
		return array( 1, true );
49
	}
50
51
	public function estimate_full_sync_actions( $config ) {
52
		return 1;
53
	}
54
	
55
	public function init_before_send() {
56
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_theme_data', array( $this, 'expand_theme_data' ) );
57
	}
58
59
	function get_full_sync_actions() {
60
		return array( 'jetpack_full_sync_theme_data' );
61
	}
62
63
	function expand_theme_data() {
64
		return array( $this->get_theme_support_info() );
65
	}
66
67
	function sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ) {
68
		$added_widgets = array_diff( $new_widgets, $old_widgets );
69
		if ( empty( $added_widgets ) ) {
70
			return array();
71
		}
72
		$moved_to_sidebar = array();
73
		foreach ( $added_widgets as $added_widget ) {
74
			$moved_to_sidebar[] = $added_widget;
75
			/**
76
			 * Helps Sync log that a widget got added
77
			 *
78
			 * @since 4.9.0
79
			 *
80
			 * @param string $sidebar, Sidebar id got changed
81
			 * @param string $added_widget, Widget id got added
82
			 */
83
			do_action( 'jetpack_widget_added', $sidebar, $added_widget );
84
		}
85
		return $moved_to_sidebar;
86
	}
87
88
	function sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $inactive_widgets  ) {
89
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
90
91
		if ( empty( $removed_widgets ) ) {
92
			return array();
93
		}
94
95
		$moved_to_inactive = array();
96
97
		foreach( $removed_widgets as $removed_widget ) {
98
			// Lets check if we didn't move the widget to in_active_widgets
99
			if ( isset( $inactive_widgets ) && ! in_array( $removed_widget, $inactive_widgets ) ) {
100
				/**
101
				 * Helps Sync log that a widgte got removed
102
				 *
103
				 * @since 4.9.0
104
				 *
105
				 * @param string $sidebar, Sidebar id got changed
106
				 * @param string $removed_widget, Widget id got removed
107
				 */
108
				do_action( 'jetpack_widget_removed', $sidebar, $removed_widget );
109
			} else {
110
				$moved_to_inactive[] = $removed_widget;
111
			}
112
		}
113
		return $moved_to_inactive;
114
115
	}
116
117
	function sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ) {
118
		$added_widgets = array_diff( $new_widgets, $old_widgets );
119
		if ( ! empty( $added_widgets ) ) {
120
			return;
121
		}
122
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
123
		if ( ! empty( $removed_widgets ) ) {
124
			return;
125
		}
126
127
		if ( serialize( $old_widgets ) !== serialize( $new_widgets ) ) {
128
			/**
129
			 * Helps Sync log that a sidebar id got reordered
130
			 *
131
			 * @since 4.9.0
132
			 *
133
			 * @param string $sidebar, Sidebar id got changed
134
			 */
135
			do_action( 'jetpack_widget_reordered', $sidebar );
136
		}
137
138
	}
139
140
	function sync_sidebar_widgets_actions( $old_value, $new_value ) {
141
142
		// Don't really know how to deal with different array_values yet.
143
		if ( $old_value['array_version'] !== 3 || $new_value['array_version'] !== 3 ) {
144
			return;
145
		}
146
147
		$moved_to_inactive = array();
148
		$moved_to_sidebar = array();
149
150
		foreach ( $new_value as $sidebar => $new_widgets ) {
151
			if ( in_array( $sidebar, array( 'array_version', 'wp_inactive_widgets' ) ) ) {
152
				continue;
153
			}
154
			$old_widgets = $old_value[ $sidebar ];
155
156
			$moved_to_inactive_recently = $this->sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $new_value['wp_inactive_widgets'] );
157
			$moved_to_inactive = array_merge( $moved_to_inactive, $moved_to_inactive_recently );
158
159
160
			$moved_to_sidebar_recently = $this->sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar );
161
			$moved_to_sidebar = array_merge( $moved_to_sidebar, $moved_to_sidebar_recently );
162
163
			$this->sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar );
164
165
		}
166
167
		// Treat inactive sidebar a bit differently
168
		if ( ! empty( $moved_to_inactive ) ) {
169
			/**
170
			 * Helps Sync log that a widgets IDs got moved to in active
171
			 *
172
			 * @since 4.9.0
173
			 *
174
			 * @param array $sidebar, Sidebar id got changed
175
			 */
176
			do_action( 'jetpack_widget_moved_to_inactive', $moved_to_inactive );
177
		} elseif ( empty( $moved_to_sidebar ) &&
178
		           empty( $new_value['wp_inactive_widgets']) &&
179
		           ! empty( $old_value['wp_inactive_widgets'] ) ) {
180
			/**
181
			 * Helps Sync log that a got cleared from inactive.
182
			 *
183
			 * @since 4.9.0
184
			 */
185
			do_action( 'jetpack_cleared_inactive_widgets' );
186
		} 
187
	}
188
189
	private function get_theme_support_info() {
190
		global $_wp_theme_features;
191
192
		$theme_support = array();
193
194
		foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) {
0 ignored issues
show
The property default_theme_support_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...
195
			$has_support = current_theme_supports( $theme_feature );
196
			if ( $has_support ) {
197
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
198
			}
199
		}
200
201
		$theme = wp_get_theme();
202
		$theme_support['name'] = $theme->name;
203
		$theme_support['version'] =  $theme->version;
204
205
		return $theme_support;
206
	}
207
}
208