PluginProbe ʕ •ᴥ•ʔ
Customizer Export/Import / trunk
Customizer Export/Import vtrunk
trunk 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.9.1 0.9.2 0.9.7 0.9.7.1 0.9.7.2 0.9.7.3 0.9.8
customizer-export-import / classes / class-cei-core.php
customizer-export-import / classes Last commit date
class-cei-control.php 7 years ago class-cei-core.php 1 year ago class-cei-option.php 7 years ago
class-cei-core.php
466 lines
1 <?php
2
3 /**
4 * The main export/import class.
5 *
6 * @since 0.1
7 */
8 final class CEI_Core {
9
10 /**
11 * An array of core options that shouldn't be imported.
12 *
13 * @since 0.3
14 * @access private
15 * @var array $core_options
16 */
17 static private $core_options = array(
18 'blogname',
19 'blogdescription',
20 'show_on_front',
21 'page_on_front',
22 'page_for_posts',
23 );
24
25 /**
26 * Load a translation for this plugin.
27 *
28 * @since 0.1
29 * @return void
30 */
31 static public function load_plugin_textdomain()
32 {
33 load_plugin_textdomain( 'customizer-export-import', false, basename( CEI_PLUGIN_DIR ) . '/lang/' );
34 }
35
36 /**
37 * Check to see if the current user is allowed to import.
38 *
39 * @since 0.9.6
40 * @return bool
41 */
42 static public function can_import()
43 {
44 return apply_filters( 'cei_allow_unsafe_imports', current_user_can( 'install_plugins' ) );
45 }
46
47 /**
48 * Check to see if we need to do an export or import.
49 * This should be called by the customize_register action.
50 *
51 * @since 0.1
52 * @since 0.3 Passing $wp_customize to the export and import methods.
53 * @param object $wp_customize An instance of WP_Customize_Manager.
54 * @return void
55 */
56 static public function init( $wp_customize )
57 {
58 if ( current_user_can( 'edit_theme_options' ) ) {
59
60 if ( isset( $_REQUEST['cei-export'] ) ) {
61 self::_export( $wp_customize );
62 }
63 if ( isset( $_REQUEST['cei-import'] ) && isset( $_FILES['cei-import-file'] ) ) {
64 self::_import( $wp_customize );
65 }
66 }
67 }
68
69 /**
70 * Prints scripts for the control.
71 *
72 * @since 0.1
73 * @return void
74 */
75 static public function controls_print_scripts()
76 {
77 global $cei_error;
78
79 if ( $cei_error ) {
80 echo '<script> alert("' . $cei_error . '"); </script>';
81 }
82 }
83
84 /**
85 * Enqueues scripts for the control.
86 *
87 * @since 0.1
88 * @return void
89 */
90 static public function controls_enqueue_scripts()
91 {
92 // Register
93 wp_register_style( 'cei-css', CEI_PLUGIN_URL . 'css/customizer.css', array(), CEI_VERSION );
94 wp_register_script( 'cei-js', CEI_PLUGIN_URL . 'js/customizer.js', array( 'jquery' ), CEI_VERSION, true );
95
96 // Localize
97 wp_localize_script( 'cei-js', 'CEIl10n', array(
98 'emptyImport' => __( 'Please choose a file to import.', 'customizer-export-import' )
99 ));
100
101 // Config
102 wp_localize_script( 'cei-js', 'CEIConfig', array(
103 'customizerURL' => admin_url( 'customize.php' ),
104 'exportNonce' => wp_create_nonce( 'cei-exporting' )
105 ));
106
107 // Enqueue
108 wp_enqueue_style( 'cei-css' );
109 wp_enqueue_script( 'cei-js' );
110 }
111
112 /**
113 * Registers the control with the customizer.
114 *
115 * @since 0.1
116 * @param object $wp_customize An instance of WP_Customize_Manager.
117 * @return void
118 */
119 static public function register( $wp_customize )
120 {
121 require_once CEI_PLUGIN_DIR . 'classes/class-cei-control.php';
122
123 $description = "<p class='cei-description'>";
124
125 $description .= __( 'Assistant makes tasks like this easier!', 'customizer-export-import' );
126
127 $description .= sprintf( '<br /><a href="%1$s" class="external-link" target="_blank">%2$s<span class="screen-reader-text"> %3$s</span></a>',
128 esc_url( __( 'https://assistant.pro/' ) ),
129 __( 'Get started with a Free Account', 'customizer-export-import' ),
130 /* translators: Accessibility text. */
131 __( '(opens in a new tab)', 'customizer-export-import' )
132 );
133
134 $description .= '</p>';
135
136 // Add the export/import section.
137 $wp_customize->add_section( 'cei-section', array(
138 'title' => __( 'Export/Import', 'customizer-export-import' ),
139 'priority' => 10000000,
140 'description' => $description,
141 ));
142
143 // Add the export/import setting.
144 $wp_customize->add_setting( 'cei-setting', array(
145 'default' => '',
146 'type' => 'none'
147 ));
148
149 // Add the export/import control.
150 $wp_customize->add_control( new CEI_Control(
151 $wp_customize,
152 'cei-setting',
153 array(
154 'section' => 'cei-section',
155 'priority' => 1
156 )
157 ));
158 }
159
160 /**
161 * Export customizer settings.
162 *
163 * @since 0.1
164 * @since 0.3 Added $wp_customize param and exporting of options.
165 * @access private
166 * @param object $wp_customize An instance of WP_Customize_Manager.
167 * @return void
168 */
169 static private function _export( $wp_customize )
170 {
171 if ( ! wp_verify_nonce( $_REQUEST['cei-export'], 'cei-exporting' ) ) {
172 return;
173 }
174
175 $theme = get_stylesheet();
176 $template = get_template();
177 $charset = get_option( 'blog_charset' );
178 $mods = get_theme_mods();
179 $data = array(
180 'template' => $template,
181 'mods' => $mods ? $mods : array(),
182 'options' => array()
183 );
184
185 // Get options from the Customizer API.
186 $settings = $wp_customize->settings();
187
188 foreach ( $settings as $key => $setting ) {
189
190 if ( 'option' == $setting->type ) {
191
192 // Don't save widget data.
193 if ( 'widget_' === substr( strtolower( $key ), 0, 7 ) ) {
194 continue;
195 }
196
197 // Don't save sidebar data.
198 if ( 'sidebars_' === substr( strtolower( $key ), 0, 9 ) ) {
199 continue;
200 }
201
202 // Don't save core options.
203 if ( in_array( $key, self::$core_options ) ) {
204 continue;
205 }
206
207 $data['options'][ $key ] = $setting->value();
208 }
209 }
210
211 // Plugin developers can specify additional option keys to export.
212 $option_keys = apply_filters( 'cei_export_option_keys', array() );
213
214 foreach ( $option_keys as $option_key ) {
215 $data['options'][ $option_key ] = get_option( $option_key );
216 }
217
218 if( function_exists( 'wp_get_custom_css_post' ) ) {
219 $data['wp_css'] = wp_get_custom_css();
220 }
221
222 // Set the download headers.
223 header( 'Content-disposition: attachment; filename=' . $theme . '-export.dat' );
224 header( 'Content-Type: application/octet-stream; charset=' . $charset );
225
226 // Serialize the export data.
227 echo serialize( $data );
228
229 // Start the download.
230 die();
231 }
232
233 /**
234 * Imports uploaded mods and calls WordPress core customize_save actions so
235 * themes that hook into them can act before mods are saved to the database.
236 *
237 * @since 0.1
238 * @since 0.3 Added $wp_customize param and importing of options.
239 * @access private
240 * @param object $wp_customize An instance of WP_Customize_Manager.
241 * @return void
242 */
243 static private function _import( $wp_customize )
244 {
245 // Make sure the current user can import.
246 if ( ! self::can_import() ) {
247 return;
248 }
249
250 // Make sure we have a valid nonce.
251 if ( ! wp_verify_nonce( $_REQUEST['cei-import'], 'cei-importing' ) ) {
252 return;
253 }
254
255 // Make sure WordPress upload support is loaded.
256 if ( ! function_exists( 'wp_handle_upload' ) ) {
257 require_once( ABSPATH . 'wp-admin/includes/file.php' );
258 }
259
260 // Load the export/import option class.
261 require_once CEI_PLUGIN_DIR . 'classes/class-cei-option.php';
262
263 // Setup global vars.
264 global $wp_customize;
265 global $cei_error;
266
267 // Setup internal vars.
268 $cei_error = false;
269 $template = get_template();
270
271 $validate = wp_check_filetype( $_FILES['cei-import-file']['name'], array( 'dat' => 'application/octet-stream' ) );
272 if ( 'application/octet-stream' !== $validate['type'] ) {
273 $cei_error = __( 'File type is not allowed', 'customizer-export-import' );
274 unlink( $_FILES['cei-import-file']['tmp_name'] );
275 return;
276 }
277
278 $overrides = array( 'test_form' => false, 'test_type' => false, 'mimes' => array( 'dat' => 'application/octet-stream' ) );
279 $file = wp_handle_upload( $_FILES['cei-import-file'], $overrides );
280
281 // Make sure we have an uploaded file.
282 if ( isset( $file['error'] ) ) {
283 $cei_error = $file['error'];
284 return;
285 }
286
287 if ( ! file_exists( $file['file'] ) ) {
288 $cei_error = __( 'Error importing settings! Please try again.', 'customizer-export-import' );
289 return;
290 }
291
292 // Get the upload data.
293 $raw = file_get_contents( $file['file'] );
294 $data = @unserialize( trim( $raw ), array( 'allowed_classes' => false ) );
295
296 // Remove the uploaded file.
297 unlink( $file['file'] );
298
299 // Data checks.
300 if ( 'array' != gettype( $data ) ) {
301 $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', 'customizer-export-import' );
302 return;
303 }
304 if ( ! isset( $data['template'] ) || ! isset( $data['mods'] ) ) {
305 $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', 'customizer-export-import' );
306 return;
307 }
308 if ( $data['template'] != $template ) {
309 $cei_error = __( 'Error importing settings! The settings you uploaded are not for the current theme.', 'customizer-export-import' );
310 return;
311 }
312
313 // Import images.
314 if ( isset( $_REQUEST['cei-import-images'] ) ) {
315 $data['mods'] = self::_import_images( $data['mods'] );
316 }
317
318 // Import custom options.
319 if ( isset( $data['options'] ) ) {
320
321 foreach ( $data['options'] as $option_key => $option_value ) {
322
323 $option = new CEI_Option( $wp_customize, $option_key, array(
324 'default' => '',
325 'type' => 'option',
326 'capability' => 'edit_theme_options'
327 ) );
328
329 $option->import( $option_value );
330 }
331 }
332
333 // If wp_css is set then import it.
334 if( function_exists( 'wp_update_custom_css_post' ) && isset( $data['wp_css'] ) && '' !== $data['wp_css'] ) {
335 wp_update_custom_css_post( $data['wp_css'] );
336 }
337
338 // Call the customize_save action.
339 do_action( 'customize_save', $wp_customize );
340
341 // Loop through the mods.
342 foreach ( $data['mods'] as $key => $val ) {
343
344 // Call the customize_save_ dynamic action.
345 do_action( 'customize_save_' . $key, $wp_customize );
346
347 // Save the mod.
348 set_theme_mod( $key, $val );
349 }
350
351 // Call the customize_save_after action.
352 do_action( 'customize_save_after', $wp_customize );
353 }
354
355 public static function add_mime_for_upload( $mimes ) {
356 $mimes['dat'] = 'application/dat';
357 return $mimes;
358 }
359
360 /**
361 * Imports images for settings saved as mods.
362 *
363 * @since 0.1
364 * @access private
365 * @param array $mods An array of customizer mods.
366 * @return array The mods array with any new import data.
367 */
368 static private function _import_images( $mods )
369 {
370 foreach ( $mods as $key => $val ) {
371
372 if ( self::_is_image_url( $val ) ) {
373
374 $data = self::_sideload_image( $val );
375
376 if ( ! is_wp_error( $data ) ) {
377
378 $mods[ $key ] = $data->url;
379
380 // Handle header image controls.
381 if ( isset( $mods[ $key . '_data' ] ) ) {
382 $mods[ $key . '_data' ] = $data;
383 update_post_meta( $data->attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
384 }
385 }
386 }
387 }
388
389 return $mods;
390 }
391
392 /**
393 * Taken from the core media_sideload_image function and
394 * modified to return an array of data instead of html.
395 *
396 * @since 0.1
397 * @access private
398 * @param string $file The image file path.
399 * @return array An array of image data.
400 */
401 static private function _sideload_image( $file )
402 {
403 $data = new stdClass();
404
405 if ( ! function_exists( 'media_handle_sideload' ) ) {
406 require_once( ABSPATH . 'wp-admin/includes/media.php' );
407 require_once( ABSPATH . 'wp-admin/includes/file.php' );
408 require_once( ABSPATH . 'wp-admin/includes/image.php' );
409 }
410 if ( ! empty( $file ) ) {
411
412 // Set variables for storage, fix file filename for query strings.
413 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
414 $file_array = array();
415 $file_array['name'] = basename( $matches[0] );
416
417 // Download file to temp location.
418 $file_array['tmp_name'] = download_url( $file );
419
420 // If error storing temporarily, return the error.
421 if ( is_wp_error( $file_array['tmp_name'] ) ) {
422 return $file_array['tmp_name'];
423 }
424
425 // Do the validation and storage stuff.
426 $id = media_handle_sideload( $file_array, 0 );
427
428 // If error storing permanently, unlink.
429 if ( is_wp_error( $id ) ) {
430 @unlink( $file_array['tmp_name'] );
431 return $id;
432 }
433
434 // Build the object to return.
435 $meta = wp_get_attachment_metadata( $id );
436 $data->attachment_id = $id;
437 $data->url = wp_get_attachment_url( $id );
438 $data->thumbnail_url = wp_get_attachment_thumb_url( $id );
439 $data->height = $meta['height'];
440 $data->width = $meta['width'];
441 }
442
443 return $data;
444 }
445
446 /**
447 * Checks to see whether a string is an image url or not.
448 *
449 * @since 0.1
450 * @access private
451 * @param string $string The string to check.
452 * @return bool Whether the string is an image url or not.
453 */
454 static private function _is_image_url( $string = '' )
455 {
456 if ( is_string( $string ) ) {
457
458 if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $string ) ) {
459 return true;
460 }
461 }
462
463 return false;
464 }
465 }
466