Customizer Export/Import - Version 0.3

Version Description

Download this release

Release Info

Developer justinbusa
Plugin Icon 128x128 Customizer Export/Import
Version 0.3
Comparing to
See all releases

Code changes from version 0.2 to 0.3

README.md CHANGED
@@ -4,15 +4,19 @@ The Customizer Export/Import plugin allows you to export or import your WordPres
4
 
5
  Please visit our blog for more info on the [Customizer Export/Import plugin](http://www.wpbeaverbuilder.com/wordpress-customizer-export-import-plugin/?utm_source=external&utm_medium=github&utm_campaign=customizer-export-description).
6
 
 
 
 
 
7
  ## How It Works ##
8
 
9
- Exporting customizer settings is easy. Click the export button from within the customizer and a file will automatically begin downloading with your settings. Export files are named after your theme and can only be used to import settings for the theme or child theme that they came from. Export files contain a serialized dump of mods retrieved using the [get_theme_mods](http://codex.wordpress.org/Function_Reference/get_theme_mods) function.
10
 
11
  Importing customizer settings is just as easy. Choose the export file you would like to import, select whether you would like to download and import images (similar to importing posts), and finally, click the import button. Once your settings have been imported the page will refresh and your new design will be displayed.
12
 
13
  ## Exporting Custom Options ##
14
 
15
- Some plugins or themes may create controls that don't store their settings as theme mods and instead store them in the WordPress options table. These settings can also be exported and imported by adding your option key to the array of options that will be exported as shown below.
16
 
17
  ```
18
  function my_export_option_keys( $keys ) {
4
 
5
  Please visit our blog for more info on the [Customizer Export/Import plugin](http://www.wpbeaverbuilder.com/wordpress-customizer-export-import-plugin/?utm_source=external&utm_medium=github&utm_campaign=customizer-export-description).
6
 
7
+ ## New! Export Options! ##
8
+
9
+ The Customizer Export/Import plugin previously only exported options saved as theme mods using the [get_theme_mods](http://codex.wordpress.org/Function_Reference/get_theme_mods) function, but that is no more! The Customizer Export/Import plugin now exports settings saved as options as well!
10
+
11
  ## How It Works ##
12
 
13
+ Exporting customizer settings is easy. Click the export button from within the customizer and a file will automatically begin downloading with your settings. Export files are named after your theme and can only be used to import settings for the theme or child theme that they came from. Export files contain a serialized dump of mods retrieved using the [get_theme_mods](http://codex.wordpress.org/Function_Reference/get_theme_mods) function or customizer settings saved as options.
14
 
15
  Importing customizer settings is just as easy. Choose the export file you would like to import, select whether you would like to download and import images (similar to importing posts), and finally, click the import button. Once your settings have been imported the page will refresh and your new design will be displayed.
16
 
17
  ## Exporting Custom Options ##
18
 
19
+ Developers can also have arbitrary options that aren't part of the customizer exported by using the cei_export_option_keys filter. Those options can be exported and imported by adding your option key to the array of options that will be exported as shown below.
20
 
21
  ```
22
  function my_export_option_keys( $keys ) {
classes/class-cei-control.php CHANGED
@@ -1,16 +1,21 @@
1
  <?php
2
 
3
  /**
4
- * @class CEI_Control
 
 
5
  */
6
  final class CEI_Control extends WP_Customize_Control {
7
-
8
- /**
9
- * @method render_content
10
- * @protected
11
- */
12
- protected function render_content()
13
- {
14
- include CEI_PLUGIN_DIR . 'includes/control.php';
15
- }
 
 
 
16
  }
1
  <?php
2
 
3
  /**
4
+ * A customizer control for rendering the export/import form.
5
+ *
6
+ * @since 0.1
7
  */
8
  final class CEI_Control extends WP_Customize_Control {
9
+
10
+ /**
11
+ * Renders the control content.
12
+ *
13
+ * @since 0.1
14
+ * @access protected
15
+ * @return void
16
+ */
17
+ protected function render_content()
18
+ {
19
+ include CEI_PLUGIN_DIR . 'includes/control.php';
20
+ }
21
  }
classes/class-cei-core.php CHANGED
@@ -1,125 +1,191 @@
1
  <?php
2
 
3
  /**
4
- * @class CEI_Core
 
 
5
  */
6
  final class CEI_Core {
7
 
8
  /**
9
- * @method load_plugin_textdomain
10
- */
11
- static public function load_plugin_textdomain()
12
- {
13
- load_plugin_textdomain( 'customizer-export-import', false, basename( CEI_PLUGIN_DIR ) . '/lang/' );
14
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  /**
17
- * @method init
18
- */
19
- static public function init()
 
 
 
 
 
 
20
  {
21
- if ( current_user_can( 'edit_theme_options' ) ) {
22
-
23
- if ( isset( $_REQUEST['cei-export'] ) ) {
24
- self::_export();
25
- }
26
- if ( isset( $_REQUEST['cei-import'] ) && isset( $_FILES['cei-import-file'] ) ) {
27
- self::_import();
28
- }
29
- }
30
- }
31
 
32
  /**
33
- * @method controls_print_scripts
34
- */
 
 
 
35
  static public function controls_print_scripts()
36
  {
37
- global $cei_error;
38
-
39
- if ( $cei_error ) {
40
- echo '<script> alert("' . $cei_error . '"); </script>';
41
- }
42
  }
43
 
44
  /**
45
- * @method controls_enqueue_scripts
46
- */
 
 
 
47
  static public function controls_enqueue_scripts()
48
  {
49
- // Register
50
- wp_register_style( 'cei-css', CEI_PLUGIN_URL . '/css/customizer.css', array(), CEI_VERSION );
51
- wp_register_script( 'cei-js', CEI_PLUGIN_URL . '/js/customizer.js', array( 'jquery' ), CEI_VERSION, true );
52
 
53
- // Localize
54
- wp_localize_script( 'cei-js', 'CEIl10n', array(
55
- 'emptyImport' => __( 'Please choose a file to import.', 'customizer-export-import' )
56
- ));
57
-
58
- // Config
59
- wp_localize_script( 'cei-js', 'CEIConfig', array(
60
- 'customizerURL' => admin_url( 'customize.php' ),
61
- 'exportNonce' => wp_create_nonce( 'cei-exporting' )
62
- ));
63
 
64
- // Enqueue
65
- wp_enqueue_style( 'cei-css' );
66
- wp_enqueue_script( 'cei-js' );
67
  }
68
 
69
  /**
70
- * @method register
71
- */
72
- static public function register( $customizer )
 
 
 
 
73
  {
74
- require_once CEI_PLUGIN_DIR . 'classes/class-cei-control.php';
75
 
76
- // Add the export/import section.
77
- $customizer->add_section( 'cei-section', array(
78
- 'title' => __( 'Export/Import', 'customizer-export-import' ),
79
- 'priority' => 10000000
80
- ));
81
-
82
- // Add the export/import setting.
83
- $customizer->add_setting( 'cei-setting', array(
84
- 'default' => '',
85
- 'type' => 'none'
86
- ));
87
-
88
- // Add the export/import control.
89
- $customizer->add_control( new CEI_Control(
90
- $customizer,
91
- 'cei-setting',
92
- array(
93
- 'section' => 'cei-section',
94
- 'priority' => 1
95
- )
96
- ));
97
  }
98
 
99
  /**
100
- * @method _export
101
- * @private
102
- */
103
- static private function _export()
 
 
 
 
 
104
  {
105
- if ( ! wp_verify_nonce( $_REQUEST['cei-export'], 'cei-exporting' ) ) {
106
- return;
107
- }
108
-
109
- $theme = get_option( 'stylesheet' );
110
- $template = get_option( 'template' );
111
- $charset = get_option( 'blog_charset' );
112
- $mods = get_theme_mods();
113
- $data = array(
114
- 'template' => $template,
115
- 'mods' => $mods ? $mods : array(),
116
- 'options' => array()
117
- );
118
-
119
- // Plugin developers can specify option keys to export.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
120
  $option_keys = apply_filters( 'cei_export_option_keys', array() );
121
 
122
- // Add options to the data.
123
  foreach ( $option_keys as $option_key ) {
124
 
125
  $option_value = get_option( $option_key );
@@ -128,175 +194,198 @@ final class CEI_Core {
128
  $data['options'][ $option_key ] = $option_value;
129
  }
130
  }
131
-
132
- // Set the download headers.
133
- header( 'Content-disposition: attachment; filename=' . $theme . '-export.dat' );
134
  header( 'Content-Type: application/octet-stream; charset=' . $charset );
135
-
136
- // Serialize the export data.
137
- echo serialize( $data );
138
-
139
- // Start the download.
140
- die();
141
- }
142
 
143
  /**
144
- * Imports uploaded mods and calls WordPress core customize_save actions so
145
- * themes that hook into them can act before mods are saved to the database.
146
- *
147
- * @method _import
148
- * @private
149
- */
150
- static private function _import()
 
 
 
151
  {
152
- if ( ! wp_verify_nonce( $_REQUEST['cei-import'], 'cei-importing' ) ) {
153
- return;
154
- }
155
-
156
- global $wp_customize;
157
- global $cei_error;
158
-
159
- $cei_error = false;
160
- $template = get_option( 'template' );
161
- $raw = file_get_contents( $_FILES['cei-import-file']['tmp_name'] );
162
- $data = @unserialize( $raw );
163
-
164
- // Data checks.
165
- if ( 'array' != gettype( $data ) ) {
166
- $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', 'customizer-export-import' );
167
- return;
168
- }
169
- if ( ! isset( $data['template'] ) || ! isset( $data['mods'] ) ) {
170
- $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', 'customizer-export-import' );
171
- return;
172
- }
173
- if ( $data['template'] != $template ) {
174
- $cei_error = __( 'Error importing settings! The settings you uploaded are not for the current theme.', 'customizer-export-import' );
175
- return;
176
- }
177
-
178
- // Import images.
179
- if ( isset( $_REQUEST['cei-import-images'] ) ) {
180
- $data['mods'] = self::_import_images( $data['mods'] );
181
- }
182
-
183
- // Import custom options.
184
- if ( isset( $data['options'] ) ) {
185
- foreach ( $data['options'] as $option_key => $option_value ) {
186
- update_option( $option_key, $option_value );
187
- }
188
- }
189
-
190
- // Call the customize_save action.
191
- do_action( 'customize_save', $wp_customize );
192
-
193
- // Loop through the mods.
194
- foreach ( $data['mods'] as $key => $val ) {
195
-
196
- // Call the customize_save_ dynamic action.
197
- do_action( 'customize_save_' . $key, $wp_customize );
198
-
199
- // Save the mod.
200
- set_theme_mod( $key, $val );
201
- }
202
-
203
- // Call the customize_save_after action.
204
- do_action( 'customize_save_after', $wp_customize );
205
- }
 
 
 
 
 
 
 
 
 
 
206
 
207
  /**
208
- * @method _import_images
209
- * @private
210
- */
 
 
 
 
211
  static private function _import_images( $mods )
212
  {
213
- foreach ( $mods as $key => $val ) {
214
-
215
- if ( self::_is_image_url( $val ) ) {
216
-
217
- $data = self::_sideload_image( $val );
218
-
219
- if ( ! is_wp_error( $data ) ) {
220
-
221
- $mods[ $key ] = $data->url;
222
-
223
- // Handle header image controls.
224
- if ( isset( $mods[ $key . '_data' ] ) ) {
225
- $mods[ $key . '_data' ] = $data;
226
- update_post_meta( $data->attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
227
- }
228
- }
229
- }
230
- }
231
-
232
- return $mods;
233
- }
234
 
235
  /**
236
- * Taken from the core media_sideload_image function and
237
- * modified to return the url instead of html.
238
- *
239
- * @method _sideload_image
240
- * @private
241
- */
 
 
242
  static private function _sideload_image( $file )
243
  {
244
- $data = new stdClass();
245
-
246
- if ( ! function_exists( 'media_handle_sideload' ) ) {
247
- require_once( ABSPATH . 'wp-admin/includes/media.php' );
248
- require_once( ABSPATH . 'wp-admin/includes/file.php' );
249
- require_once( ABSPATH . 'wp-admin/includes/image.php' );
250
- }
251
  if ( ! empty( $file ) ) {
252
-
253
- // Set variables for storage, fix file filename for query strings.
254
- preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
255
- $file_array = array();
256
- $file_array['name'] = basename( $matches[0] );
257
-
258
- // Download file to temp location.
259
- $file_array['tmp_name'] = download_url( $file );
260
-
261
- // If error storing temporarily, return the error.
262
- if ( is_wp_error( $file_array['tmp_name'] ) ) {
263
- return $file_array['tmp_name'];
264
- }
265
-
266
- // Do the validation and storage stuff.
267
- $id = media_handle_sideload( $file_array, 0 );
268
-
269
- // If error storing permanently, unlink.
270
- if ( is_wp_error( $id ) ) {
271
- @unlink( $file_array['tmp_name'] );
272
- return $id;
273
- }
274
-
275
- // Build the object to return.
276
- $meta = wp_get_attachment_metadata( $id );
277
- $data->attachment_id = $id;
278
- $data->url = wp_get_attachment_url( $id );
279
- $data->thumbnail_url = wp_get_attachment_thumb_url( $id );
280
- $data->height = $meta['height'];
281
- $data->width = $meta['width'];
282
- }
283
-
284
- return $data;
285
- }
286
 
287
  /**
288
- * @method _is_image_url
289
- * @private
290
- */
 
 
 
 
291
  static private function _is_image_url( $string = '' )
292
  {
293
- if ( is_string( $string ) ) {
294
-
295
- if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $string ) ) {
296
- return true;
297
- }
298
- }
299
-
300
- return false;
301
- }
302
  }
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 we need to do an export or import.
38
+ * This should be called by the customize_register action.
39
+ *
40
+ * @since 0.1
41
+ * @since 0.3 Passing $wp_customize to the export and import methods.
42
+ * @param object $wp_customize An instance of WP_Customize_Manager.
43
+ * @return void
44
+ */
45
+ static public function init( $wp_customize )
46
  {
47
+ if ( current_user_can( 'edit_theme_options' ) ) {
48
+
49
+ if ( isset( $_REQUEST['cei-export'] ) ) {
50
+ self::_export( $wp_customize );
51
+ }
52
+ if ( isset( $_REQUEST['cei-import'] ) && isset( $_FILES['cei-import-file'] ) ) {
53
+ self::_import( $wp_customize );
54
+ }
55
+ }
56
+ }
57
 
58
  /**
59
+ * Prints scripts for the control.
60
+ *
61
+ * @since 0.1
62
+ * @return void
63
+ */
64
  static public function controls_print_scripts()
65
  {
66
+ global $cei_error;
67
+
68
+ if ( $cei_error ) {
69
+ echo '<script> alert("' . $cei_error . '"); </script>';
70
+ }
71
  }
72
 
73
  /**
74
+ * Enqueues scripts for the control.
75
+ *
76
+ * @since 0.1
77
+ * @return void
78
+ */
79
  static public function controls_enqueue_scripts()
80
  {
81
+ // Register
82
+ wp_register_style( 'cei-css', CEI_PLUGIN_URL . '/css/customizer.css', array(), CEI_VERSION );
83
+ wp_register_script( 'cei-js', CEI_PLUGIN_URL . '/js/customizer.js', array( 'jquery' ), CEI_VERSION, true );
84
 
85
+ // Localize
86
+ wp_localize_script( 'cei-js', 'CEIl10n', array(
87
+ 'emptyImport' => __( 'Please choose a file to import.', 'customizer-export-import' )
88
+ ));
89
+
90
+ // Config
91
+ wp_localize_script( 'cei-js', 'CEIConfig', array(
92
+ 'customizerURL' => admin_url( 'customize.php' ),
93
+ 'exportNonce' => wp_create_nonce( 'cei-exporting' )
94
+ ));
95
 
96
+ // Enqueue
97
+ wp_enqueue_style( 'cei-css' );
98
+ wp_enqueue_script( 'cei-js' );
99
  }
100
 
101
  /**
102
+ * Registers the control with the customizer.
103
+ *
104
+ * @since 0.1
105
+ * @param object $wp_customize An instance of WP_Customize_Manager.
106
+ * @return void
107
+ */
108
+ static public function register( $wp_customize )
109
  {
110
+ require_once CEI_PLUGIN_DIR . 'classes/class-cei-control.php';
111
 
112
+ // Add the export/import section.
113
+ $wp_customize->add_section( 'cei-section', array(
114
+ 'title' => __( 'Export/Import', 'customizer-export-import' ),
115
+ 'priority' => 10000000
116
+ ));
117
+
118
+ // Add the export/import setting.
119
+ $wp_customize->add_setting( 'cei-setting', array(
120
+ 'default' => '',
121
+ 'type' => 'none'
122
+ ));
123
+
124
+ // Add the export/import control.
125
+ $wp_customize->add_control( new CEI_Control(
126
+ $wp_customize,
127
+ 'cei-setting',
128
+ array(
129
+ 'section' => 'cei-section',
130
+ 'priority' => 1
131
+ )
132
+ ));
133
  }
134
 
135
  /**
136
+ * Export customizer settings.
137
+ *
138
+ * @since 0.1
139
+ * @since 0.3 Added $wp_customize param and exporting of options.
140
+ * @access private
141
+ * @param object $wp_customize An instance of WP_Customize_Manager.
142
+ * @return void
143
+ */
144
+ static private function _export( $wp_customize )
145
  {
146
+ if ( ! wp_verify_nonce( $_REQUEST['cei-export'], 'cei-exporting' ) ) {
147
+ return;
148
+ }
149
+
150
+ $theme = get_stylesheet();
151
+ $template = get_template();
152
+ $charset = get_option( 'blog_charset' );
153
+ $mods = get_theme_mods();
154
+ $data = array(
155
+ 'template' => $template,
156
+ 'mods' => $mods ? $mods : array(),
157
+ 'options' => array()
158
+ );
159
+
160
+ // Get options from the Customizer API.
161
+ $settings = $wp_customize->settings();
162
+
163
+ foreach ( $settings as $key => $setting ) {
164
+
165
+ if ( 'option' == $setting->type ) {
166
+
167
+ // Don't save widget data.
168
+ if ( stristr( $key, 'widget_' ) ) {
169
+ continue;
170
+ }
171
+
172
+ // Don't save sidebar data.
173
+ if ( stristr( $key, 'sidebars_' ) ) {
174
+ continue;
175
+ }
176
+
177
+ // Don't save core options.
178
+ if ( in_array( $key, self::$core_options ) ) {
179
+ continue;
180
+ }
181
+
182
+ $data['options'][ $key ] = $setting->value();
183
+ }
184
+ }
185
+
186
+ // Plugin developers can specify additional option keys to export.
187
  $option_keys = apply_filters( 'cei_export_option_keys', array() );
188
 
 
189
  foreach ( $option_keys as $option_key ) {
190
 
191
  $option_value = get_option( $option_key );
194
  $data['options'][ $option_key ] = $option_value;
195
  }
196
  }
197
+
198
+ // Set the download headers.
199
+ header( 'Content-disposition: attachment; filename=' . $theme . '-export.dat' );
200
  header( 'Content-Type: application/octet-stream; charset=' . $charset );
201
+
202
+ // Serialize the export data.
203
+ echo serialize( $data );
204
+
205
+ // Start the download.
206
+ die();
207
+ }
208
 
209
  /**
210
+ * Imports uploaded mods and calls WordPress core customize_save actions so
211
+ * themes that hook into them can act before mods are saved to the database.
212
+ *
213
+ * @since 0.1
214
+ * @since 0.3 Added $wp_customize param and importing of options.
215
+ * @access private
216
+ * @param object $wp_customize An instance of WP_Customize_Manager.
217
+ * @return void
218
+ */
219
+ static private function _import( $wp_customize )
220
  {
221
+ if ( ! wp_verify_nonce( $_REQUEST['cei-import'], 'cei-importing' ) ) {
222
+ return;
223
+ }
224
+
225
+ require_once CEI_PLUGIN_DIR . 'classes/class-cei-option.php';
226
+
227
+ global $wp_customize;
228
+ global $cei_error;
229
+
230
+ $cei_error = false;
231
+ $template = get_template();
232
+ $raw = file_get_contents( $_FILES['cei-import-file']['tmp_name'] );
233
+ $data = @unserialize( $raw );
234
+
235
+ // Data checks.
236
+ if ( 'array' != gettype( $data ) ) {
237
+ $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', 'customizer-export-import' );
238
+ return;
239
+ }
240
+ if ( ! isset( $data['template'] ) || ! isset( $data['mods'] ) ) {
241
+ $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', 'customizer-export-import' );
242
+ return;
243
+ }
244
+ if ( $data['template'] != $template ) {
245
+ $cei_error = __( 'Error importing settings! The settings you uploaded are not for the current theme.', 'customizer-export-import' );
246
+ return;
247
+ }
248
+
249
+ // Import images.
250
+ if ( isset( $_REQUEST['cei-import-images'] ) ) {
251
+ $data['mods'] = self::_import_images( $data['mods'] );
252
+ }
253
+
254
+ // Import custom options.
255
+ if ( isset( $data['options'] ) ) {
256
+
257
+ foreach ( $data['options'] as $option_key => $option_value ) {
258
+
259
+ $option = new CEI_Option( $wp_customize, $option_key, array(
260
+ 'default' => '',
261
+ 'type' => 'option',
262
+ 'capability' => 'edit_theme_options'
263
+ ) );
264
+
265
+ $option->import( $option_value );
266
+ }
267
+ }
268
+
269
+ // Call the customize_save action.
270
+ do_action( 'customize_save', $wp_customize );
271
+
272
+ // Loop through the mods.
273
+ foreach ( $data['mods'] as $key => $val ) {
274
+
275
+ // Call the customize_save_ dynamic action.
276
+ do_action( 'customize_save_' . $key, $wp_customize );
277
+
278
+ // Save the mod.
279
+ set_theme_mod( $key, $val );
280
+ }
281
+
282
+ // Call the customize_save_after action.
283
+ do_action( 'customize_save_after', $wp_customize );
284
+ }
285
 
286
  /**
287
+ * Imports images for settings saved as mods.
288
+ *
289
+ * @since 0.1
290
+ * @access private
291
+ * @param array $mods An array of customizer mods.
292
+ * @return array The mods array with any new import data.
293
+ */
294
  static private function _import_images( $mods )
295
  {
296
+ foreach ( $mods as $key => $val ) {
297
+
298
+ if ( self::_is_image_url( $val ) ) {
299
+
300
+ $data = self::_sideload_image( $val );
301
+
302
+ if ( ! is_wp_error( $data ) ) {
303
+
304
+ $mods[ $key ] = $data->url;
305
+
306
+ // Handle header image controls.
307
+ if ( isset( $mods[ $key . '_data' ] ) ) {
308
+ $mods[ $key . '_data' ] = $data;
309
+ update_post_meta( $data->attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
310
+ }
311
+ }
312
+ }
313
+ }
314
+
315
+ return $mods;
316
+ }
317
 
318
  /**
319
+ * Taken from the core media_sideload_image function and
320
+ * modified to return an array of data instead of html.
321
+ *
322
+ * @since 0.1
323
+ * @access private
324
+ * @param string $file The image file path.
325
+ * @return array An array of image data.
326
+ */
327
  static private function _sideload_image( $file )
328
  {
329
+ $data = new stdClass();
330
+
331
+ if ( ! function_exists( 'media_handle_sideload' ) ) {
332
+ require_once( ABSPATH . 'wp-admin/includes/media.php' );
333
+ require_once( ABSPATH . 'wp-admin/includes/file.php' );
334
+ require_once( ABSPATH . 'wp-admin/includes/image.php' );
335
+ }
336
  if ( ! empty( $file ) ) {
337
+
338
+ // Set variables for storage, fix file filename for query strings.
339
+ preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
340
+ $file_array = array();
341
+ $file_array['name'] = basename( $matches[0] );
342
+
343
+ // Download file to temp location.
344
+ $file_array['tmp_name'] = download_url( $file );
345
+
346
+ // If error storing temporarily, return the error.
347
+ if ( is_wp_error( $file_array['tmp_name'] ) ) {
348
+ return $file_array['tmp_name'];
349
+ }
350
+
351
+ // Do the validation and storage stuff.
352
+ $id = media_handle_sideload( $file_array, 0 );
353
+
354
+ // If error storing permanently, unlink.
355
+ if ( is_wp_error( $id ) ) {
356
+ @unlink( $file_array['tmp_name'] );
357
+ return $id;
358
+ }
359
+
360
+ // Build the object to return.
361
+ $meta = wp_get_attachment_metadata( $id );
362
+ $data->attachment_id = $id;
363
+ $data->url = wp_get_attachment_url( $id );
364
+ $data->thumbnail_url = wp_get_attachment_thumb_url( $id );
365
+ $data->height = $meta['height'];
366
+ $data->width = $meta['width'];
367
+ }
368
+
369
+ return $data;
370
+ }
371
 
372
  /**
373
+ * Checks to see whether a string is an image url or not.
374
+ *
375
+ * @since 0.1
376
+ * @access private
377
+ * @param string $string The string to check.
378
+ * @return bool Whether the string is an image url or not.
379
+ */
380
  static private function _is_image_url( $string = '' )
381
  {
382
+ if ( is_string( $string ) ) {
383
+
384
+ if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $string ) ) {
385
+ return true;
386
+ }
387
+ }
388
+
389
+ return false;
390
+ }
391
  }
classes/class-cei-option.php ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * A class that extends WP_Customize_Setting so we can access
5
+ * the protected updated method when importing options.
6
+ *
7
+ * @since 0.3
8
+ */
9
+ final class CEI_Option extends WP_Customize_Setting {
10
+
11
+ /**
12
+ * Import an option value for this setting.
13
+ *
14
+ * @since 0.3
15
+ * @param mixed $value The option value.
16
+ * @return void
17
+ */
18
+ public function import( $value )
19
+ {
20
+ $this->update( $value );
21
+ }
22
+ }
customizer-export-import.php CHANGED
@@ -3,7 +3,7 @@
3
  * Plugin Name: Customizer Export/Import
4
  * Plugin URI: http://www.wpbeaverbuilder.com/wordpress-customizer-export-import-plugin/?utm_source=external&utm_medium=customizer-export&utm_campaign=plugins-page
5
  * Description: Adds settings export and import functionality to the WordPress customizer.
6
- * Version: 0.1
7
  * Author: The Beaver Builder Team
8
  * Author URI: http://www.wpbeaverbuilder.com/?utm_source=external&utm_medium=customizer-export&utm_campaign=plugins-page
9
  * License: GNU General Public License v2.0
@@ -19,7 +19,7 @@ require_once CEI_PLUGIN_DIR . 'classes/class-cei-core.php';
19
 
20
  /* Actions */
21
  add_action( 'plugins_loaded', 'CEI_Core::load_plugin_textdomain' );
22
- add_action( 'init', 'CEI_Core::init' );
23
  add_action( 'customize_controls_print_scripts', 'CEI_Core::controls_print_scripts' );
24
  add_action( 'customize_controls_enqueue_scripts', 'CEI_Core::controls_enqueue_scripts' );
 
25
  add_action( 'customize_register', 'CEI_Core::register' );
3
  * Plugin Name: Customizer Export/Import
4
  * Plugin URI: http://www.wpbeaverbuilder.com/wordpress-customizer-export-import-plugin/?utm_source=external&utm_medium=customizer-export&utm_campaign=plugins-page
5
  * Description: Adds settings export and import functionality to the WordPress customizer.
6
+ * Version: 0.3
7
  * Author: The Beaver Builder Team
8
  * Author URI: http://www.wpbeaverbuilder.com/?utm_source=external&utm_medium=customizer-export&utm_campaign=plugins-page
9
  * License: GNU General Public License v2.0
19
 
20
  /* Actions */
21
  add_action( 'plugins_loaded', 'CEI_Core::load_plugin_textdomain' );
 
22
  add_action( 'customize_controls_print_scripts', 'CEI_Core::controls_print_scripts' );
23
  add_action( 'customize_controls_enqueue_scripts', 'CEI_Core::controls_enqueue_scripts' );
24
+ add_action( 'customize_register', 'CEI_Core::init', 999999 );
25
  add_action( 'customize_register', 'CEI_Core::register' );
includes/control.php CHANGED
@@ -1,25 +1,25 @@
1
  <span class="customize-control-title">
2
- <?php _e( 'Export', 'customizer-export-import' ); ?>
3
  </span>
4
  <span class="description customize-control-description">
5
- <?php _e( 'Click the button below to export the customization settings for this theme.', 'customizer-export-import' ); ?>
6
  </span>
7
  <input type="button" class="button" name="cei-export-button" value="<?php esc_attr_e( 'Export', 'customizer-export-import' ); ?>" />
8
 
9
  <hr class="cei-hr" />
10
 
11
  <span class="customize-control-title">
12
- <?php _e( 'Import', 'customizer-export-import' ); ?>
13
  </span>
14
  <span class="description customize-control-description">
15
- <?php _e( 'Upload a file to import customization settings for this theme.', 'customizer-export-import' ); ?>
16
  </span>
17
  <div class="cei-import-controls">
18
- <input type="file" name="cei-import-file" class="cei-import-file" />
19
- <label class="cei-import-images">
20
- <input type="checkbox" name="cei-import-images" value="1" /> <?php _e( 'Download and import image files?', 'customizer-export-import' ); ?>
21
- </label>
22
- <?php wp_nonce_field( 'cei-importing', 'cei-import' ); ?>
23
  </div>
24
  <div class="cei-uploading"><?php _e( 'Uploading...', 'customizer-export-import' ); ?></div>
25
  <input type="button" class="button" name="cei-import-button" value="<?php esc_attr_e( 'Import', 'customizer-export-import' ); ?>" />
1
  <span class="customize-control-title">
2
+ <?php _e( 'Export', 'customizer-export-import' ); ?>
3
  </span>
4
  <span class="description customize-control-description">
5
+ <?php _e( 'Click the button below to export the customization settings for this theme.', 'customizer-export-import' ); ?>
6
  </span>
7
  <input type="button" class="button" name="cei-export-button" value="<?php esc_attr_e( 'Export', 'customizer-export-import' ); ?>" />
8
 
9
  <hr class="cei-hr" />
10
 
11
  <span class="customize-control-title">
12
+ <?php _e( 'Import', 'customizer-export-import' ); ?>
13
  </span>
14
  <span class="description customize-control-description">
15
+ <?php _e( 'Upload a file to import customization settings for this theme.', 'customizer-export-import' ); ?>
16
  </span>
17
  <div class="cei-import-controls">
18
+ <input type="file" name="cei-import-file" class="cei-import-file" />
19
+ <label class="cei-import-images">
20
+ <input type="checkbox" name="cei-import-images" value="1" /> <?php _e( 'Download and import image files?', 'customizer-export-import' ); ?>
21
+ </label>
22
+ <?php wp_nonce_field( 'cei-importing', 'cei-import' ); ?>
23
  </div>
24
  <div class="cei-uploading"><?php _e( 'Uploading...', 'customizer-export-import' ); ?></div>
25
  <input type="button" class="button" name="cei-import-button" value="<?php esc_attr_e( 'Import', 'customizer-export-import' ); ?>" />
js/customizer.js CHANGED
@@ -1,40 +1,40 @@
1
  ( function( $ ) {
2
-
3
- var CEI = {
4
-
5
- init: function()
6
- {
7
- $( 'input[name=cei-export-button]' ).on( 'click', CEI._export );
8
- $( 'input[name=cei-import-button]' ).on( 'click', CEI._import );
9
- },
10
-
11
- _export: function()
12
- {
13
- window.location.href = CEIConfig.customizerURL + '?cei-export=' + CEIConfig.exportNonce;
14
- },
15
-
16
- _import: function()
17
- {
18
- var win = $( window ),
19
- body = $( 'body' ),
20
- form = $( '<form class="cei-form" method="POST" enctype="multipart/form-data"></form>' ),
21
- controls = $( '.cei-import-controls' ),
22
- file = $( 'input[name=cei-import-file]' ),
23
- message = $( '.cei-uploading' );
24
-
25
- if ( '' == file.val() ) {
26
- alert( CEIl10n.emptyImport );
27
- }
28
- else {
29
- win.off( 'beforeunload' );
30
- body.append( form );
31
- form.append( controls );
32
- message.show();
33
- form.submit();
34
- }
35
- }
36
- };
37
-
38
- $( CEI.init );
39
-
40
  })( jQuery );
1
  ( function( $ ) {
2
+
3
+ var CEI = {
4
+
5
+ init: function()
6
+ {
7
+ $( 'input[name=cei-export-button]' ).on( 'click', CEI._export );
8
+ $( 'input[name=cei-import-button]' ).on( 'click', CEI._import );
9
+ },
10
+
11
+ _export: function()
12
+ {
13
+ window.location.href = CEIConfig.customizerURL + '?cei-export=' + CEIConfig.exportNonce;
14
+ },
15
+
16
+ _import: function()
17
+ {
18
+ var win = $( window ),
19
+ body = $( 'body' ),
20
+ form = $( '<form class="cei-form" method="POST" enctype="multipart/form-data"></form>' ),
21
+ controls = $( '.cei-import-controls' ),
22
+ file = $( 'input[name=cei-import-file]' ),
23
+ message = $( '.cei-uploading' );
24
+
25
+ if ( '' == file.val() ) {
26
+ alert( CEIl10n.emptyImport );
27
+ }
28
+ else {
29
+ win.off( 'beforeunload' );
30
+ body.append( form );
31
+ form.append( controls );
32
+ message.show();
33
+ form.submit();
34
+ }
35
+ }
36
+ };
37
+
38
+ $( CEI.init );
39
+
40
  })( jQuery );
readme.txt CHANGED
@@ -2,7 +2,7 @@
2
  Contributors: justinbusa
3
  Tags: customizer, customizer export, customizer import, export, import, settings, customizer settings, theme settings, theme options
4
  Requires at least: 3.6
5
- Tested up to: 4.1
6
  Stable tag: trunk
7
  License: GPL2+
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
@@ -17,15 +17,19 @@ The Customizer Export/Import plugin allows you to export or import your WordPres
17
 
18
  Please visit our blog for more info on the [Customizer Export/Import plugin](https://www.wpbeaverbuilder.com/wordpress-customizer-export-import-plugin/?utm_source=external&utm_medium=wp-repo&utm_campaign=customizer-export-description).
19
 
 
 
 
 
20
  = How It Works =
21
 
22
- Exporting customizer settings is easy. Click the export button from within the customizer and a file will automatically begin downloading with your settings. Export files are named after your theme and can only be used to import settings for the theme or child theme that they came from. Export files contain a serialized dump of mods retrieved using the [get_theme_mods](http://codex.wordpress.org/Function_Reference/get_theme_mods) function.
23
 
24
  Importing customizer settings is just as easy. Choose the export file you would like to import, select whether you would like to download and import images (similar to importing posts), and finally, click the import button. Once your settings have been imported the page will refresh and your new design will be displayed.
25
 
26
  = Exporting Custom Options =
27
 
28
- Some plugins or themes may create controls that don't store their settings as theme mods and instead store them in the WordPress options table. These settings can also be exported and imported by adding your option key to the array of options that will be exported as shown below.
29
 
30
  function my_export_option_keys( $keys ) {
31
  $keys[] = 'my_option_key';
@@ -34,6 +38,10 @@ Some plugins or themes may create controls that don't store their settings as th
34
  }
35
 
36
  add_filter( 'cei_export_option_keys', 'my_export_option_keys' );
 
 
 
 
37
 
38
  = Contribute! =
39
 
@@ -64,3 +72,7 @@ Please visit our blog for more info on the [Customizer Export/Import plugin](htt
64
  = Version 0.2 =
65
 
66
  - Added cei_export_option_keys filter for exporting custom options.
 
 
 
 
2
  Contributors: justinbusa
3
  Tags: customizer, customizer export, customizer import, export, import, settings, customizer settings, theme settings, theme options
4
  Requires at least: 3.6
5
+ Tested up to: 4.2
6
  Stable tag: trunk
7
  License: GPL2+
8
  License URI: http://www.gnu.org/licenses/gpl-2.0.html
17
 
18
  Please visit our blog for more info on the [Customizer Export/Import plugin](https://www.wpbeaverbuilder.com/wordpress-customizer-export-import-plugin/?utm_source=external&utm_medium=wp-repo&utm_campaign=customizer-export-description).
19
 
20
+ = New! Export Options =
21
+
22
+ The Customizer Export/Import plugin previously only exported options saved as theme mods using the [get_theme_mods](http://codex.wordpress.org/Function_Reference/get_theme_mods) function, but that is no more! The Customizer Export/Import plugin now exports settings saved as options as well!
23
+
24
  = How It Works =
25
 
26
+ Exporting customizer settings is easy. Click the export button from within the customizer and a file will automatically begin downloading with your settings. Export files are named after your theme and can only be used to import settings for the theme or child theme that they came from. Export files contain a serialized dump of mods retrieved using the [get_theme_mods](http://codex.wordpress.org/Function_Reference/get_theme_mods) function or customizer settings saved as options.
27
 
28
  Importing customizer settings is just as easy. Choose the export file you would like to import, select whether you would like to download and import images (similar to importing posts), and finally, click the import button. Once your settings have been imported the page will refresh and your new design will be displayed.
29
 
30
  = Exporting Custom Options =
31
 
32
+ Developers can also have arbitrary options that aren't part of the customizer exported by using the cei_export_option_keys filter. Those options can be exported and imported by adding your option key to the array of options that will be exported as shown below.
33
 
34
  function my_export_option_keys( $keys ) {
35
  $keys[] = 'my_option_key';
38
  }
39
 
40
  add_filter( 'cei_export_option_keys', 'my_export_option_keys' );
41
+
42
+ = Known Issues =
43
+
44
+ This plugin currently only works for active themes, not themes that are being previewed with either the Theme Test Drive plugin or the new customizer theme preview.
45
 
46
  = Contribute! =
47
 
72
  = Version 0.2 =
73
 
74
  - Added cei_export_option_keys filter for exporting custom options.
75
+
76
+ = Version 0.3 =
77
+
78
+ - Customizer settings saved as options are now exported and imported.