Completed
Push — add/sync_theme ( c0480a )
by
unknown
11:53
created

Jetpack_Sync_Module_Themes::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
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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
		add_action( 'upgrader_process_complete', array( $this, 'check_upgrader'), 10, 2 );
12
13
		// Sidebar updates.
14
		add_action( 'update_option_sidebars_widgets', array( $this, 'sync_sidebar_widgets_actions' ), 10, 2 );
15
		add_action( 'jetpack_widget_added', $callable, 10, 2 );
16
		add_action( 'jetpack_widget_removed', $callable, 10, 2 );
17
		add_action( 'jetpack_widget_moved_to_inactive', $callable );
18
		add_action( 'jetpack_cleared_inactive_widgets', $callable );
19
		add_action( 'jetpack_widget_reordered', $callable );
20
	}
21
22
	public function check_upgrader( $upgrader, $details) {
23
		error_log(get_class($upgrader));
24
		error_log(print_r($details, true));
25
26
		return;
27
28 View Code Duplication
		if ( ! isset( $details['type'] ) ||
0 ignored issues
show
Unused Code introduced by
if (!isset($details['typ...info')) { return; } does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
29
			'plugin' !== $details['type'] ||
30
			is_wp_error( $upgrader->skin->result ) ||
31
			! method_exists( $upgrader, 'plugin_info' )
32
		) {
33
			return;
34
		}
35
36 View Code Duplication
		if ( 'install' === $details['action'] ) {
37
			$plugin_path = $upgrader->plugin_info();
38
			$plugins = get_plugins();
39
			$plugin_info = $plugins[ $plugin_path ];
40
41
			/**
42
			 * Signals to the sync listener that a plugin was installed and a sync action
43
			 * reflecting the installation and the plugin info should be sent
44
			 *
45
			 * @since 4.9.0
46
			 *
47
			 * @param string $plugin_path Path of plugin installed
48
			 * @param mixed $plugin_info Array of info describing plugin installed
49
			 */
50
			do_action( 'jetpack_installed_plugin', $plugin_path, $plugin_info );
51
		}
52
	}
53
54
	public function init_full_sync_listeners( $callable ) {
55
		add_action( 'jetpack_full_sync_theme_data', $callable );
56
	}
57
58
	public function sync_theme_support() {
59
		/**
60
		 * Fires when the client needs to sync theme support info
61
		 * Only sends theme support attributes whitelisted in Jetpack_Sync_Defaults::$default_theme_support_whitelist
62
		 *
63
		 * @since 4.2.0
64
		 *
65
		 * @param object the theme support hash
66
		 */
67
		do_action( 'jetpack_sync_current_theme_support' , $this->get_theme_support_info() );
68
	}
69
70
	public function enqueue_full_sync_actions( $config, $max_items_to_enqueue, $state ) {
71
		/**
72
		 * Tells the client to sync all theme data to the server
73
		 *
74
		 * @since 4.2.0
75
		 *
76
		 * @param boolean Whether to expand theme data (should always be true)
77
		 */
78
		do_action( 'jetpack_full_sync_theme_data', true );
79
80
		// The number of actions enqueued, and next module state (true == done)
81
		return array( 1, true );
82
	}
83
84
	public function estimate_full_sync_actions( $config ) {
85
		return 1;
86
	}
87
	
88
	public function init_before_send() {
89
		add_filter( 'jetpack_sync_before_send_jetpack_full_sync_theme_data', array( $this, 'expand_theme_data' ) );
90
	}
91
92
	function get_full_sync_actions() {
93
		return array( 'jetpack_full_sync_theme_data' );
94
	}
95
96
	function expand_theme_data() {
97
		return array( $this->get_theme_support_info() );
98
	}
99
100
	function sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar ) {
101
		$added_widgets = array_diff( $new_widgets, $old_widgets );
102
		if ( empty( $added_widgets ) ) {
103
			return array();
104
		}
105
		$moved_to_sidebar = array();
106
		foreach ( $added_widgets as $added_widget ) {
107
			$moved_to_sidebar[] = $added_widget;
108
			/**
109
			 * Helps Sync log that a widget got added
110
			 *
111
			 * @since 4.9.0
112
			 *
113
			 * @param string $sidebar, Sidebar id got changed
114
			 * @param string $added_widget, Widget id got added
115
			 */
116
			do_action( 'jetpack_widget_added', $sidebar, $added_widget );
117
		}
118
		return $moved_to_sidebar;
119
	}
120
121
	function sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $inactive_widgets  ) {
122
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
123
124
		if ( empty( $removed_widgets ) ) {
125
			return array();
126
		}
127
128
		$moved_to_inactive = array();
129
130
		foreach( $removed_widgets as $removed_widget ) {
131
			// Lets check if we didn't move the widget to in_active_widgets
132
			if ( isset( $inactive_widgets ) && ! in_array( $removed_widget, $inactive_widgets ) ) {
133
				/**
134
				 * Helps Sync log that a widgte got removed
135
				 *
136
				 * @since 4.9.0
137
				 *
138
				 * @param string $sidebar, Sidebar id got changed
139
				 * @param string $removed_widget, Widget id got removed
140
				 */
141
				do_action( 'jetpack_widget_removed', $sidebar, $removed_widget );
142
			} else {
143
				$moved_to_inactive[] = $removed_widget;
144
			}
145
		}
146
		return $moved_to_inactive;
147
148
	}
149
150
	function sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar ) {
151
		$added_widgets = array_diff( $new_widgets, $old_widgets );
152
		if ( ! empty( $added_widgets ) ) {
153
			return;
154
		}
155
		$removed_widgets = array_diff( $old_widgets, $new_widgets );
156
		if ( ! empty( $removed_widgets ) ) {
157
			return;
158
		}
159
160
		if ( serialize( $old_widgets ) !== serialize( $new_widgets ) ) {
161
			/**
162
			 * Helps Sync log that a sidebar id got reordered
163
			 *
164
			 * @since 4.9.0
165
			 *
166
			 * @param string $sidebar, Sidebar id got changed
167
			 */
168
			do_action( 'jetpack_widget_reordered', $sidebar );
169
		}
170
171
	}
172
173
	function sync_sidebar_widgets_actions( $old_value, $new_value ) {
174
175
		// Don't really know how to deal with different array_values yet.
176
		if ( $old_value['array_version'] !== 3 || $new_value['array_version'] !== 3 ) {
177
			return;
178
		}
179
180
		$moved_to_inactive = array();
181
		$moved_to_sidebar = array();
182
183
		foreach ( $new_value as $sidebar => $new_widgets ) {
184
			if ( in_array( $sidebar, array( 'array_version', 'wp_inactive_widgets' ) ) ) {
185
				continue;
186
			}
187
			$old_widgets = $old_value[ $sidebar ];
188
189
			$moved_to_inactive_recently = $this->sync_remove_widgets_from_sidebar( $new_widgets, $old_widgets, $sidebar, $new_value['wp_inactive_widgets'] );
190
			$moved_to_inactive = array_merge( $moved_to_inactive, $moved_to_inactive_recently );
191
192
193
			$moved_to_sidebar_recently = $this->sync_add_widgets_to_sidebar( $new_widgets, $old_widgets, $sidebar );
194
			$moved_to_sidebar = array_merge( $moved_to_sidebar, $moved_to_sidebar_recently );
195
196
			$this->sync_widgets_reordered( $new_widgets, $old_widgets, $sidebar );
197
198
		}
199
200
		// Treat inactive sidebar a bit differently
201
		if ( ! empty( $moved_to_inactive ) ) {
202
			/**
203
			 * Helps Sync log that a widgets IDs got moved to in active
204
			 *
205
			 * @since 4.9.0
206
			 *
207
			 * @param array $sidebar, Sidebar id got changed
208
			 */
209
			do_action( 'jetpack_widget_moved_to_inactive', $moved_to_inactive );
210
		} elseif ( empty( $moved_to_sidebar ) &&
211
		           empty( $new_value['wp_inactive_widgets']) &&
212
		           ! empty( $old_value['wp_inactive_widgets'] ) ) {
213
			/**
214
			 * Helps Sync log that a got cleared from inactive.
215
			 *
216
			 * @since 4.9.0
217
			 */
218
			do_action( 'jetpack_cleared_inactive_widgets' );
219
		} 
220
	}
221
222
	private function get_theme_support_info() {
223
		global $_wp_theme_features;
224
225
		$theme_support = array();
226
227
		foreach ( Jetpack_Sync_Defaults::$default_theme_support_whitelist as $theme_feature ) {
0 ignored issues
show
Bug introduced by
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...
228
			$has_support = current_theme_supports( $theme_feature );
229
			if ( $has_support ) {
230
				$theme_support[ $theme_feature ] = $_wp_theme_features[ $theme_feature ];
231
			}
232
		}
233
234
		$theme = wp_get_theme();
235
		$theme_support['name'] = $theme->name;
236
		$theme_support['version'] =  $theme->version;
237
238
		return $theme_support;
239
	}
240
}
241