Customizer Export/Import - Version 0.1

Version Description

Download this release

Release Info

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

Version 0.1

README.md ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Customizer Export/Import #
2
+
3
+ The Customizer Export/Import plugin allows you to export or import your WordPress customizer settings from directly within the customizer interface! If your theme makes use of the WordPress customizer for its settings, this plugin is for you!
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
+ ## Contribute! ##
14
+
15
+ We'd love to hear your feedback as to how we could improve the Customizer Export/Import plugin, or better yet, see theme developers actively contribute! Don't hesitate to let us know if you're interested in contributing as we would gladly have others on board.
classes/class-cei-control.php ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ }
classes/class-cei-core.php ADDED
@@ -0,0 +1,277 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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( CEI_TD, 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.', CEI_TD )
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', CEI_TD ),
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
+
114
+ header( 'Content-disposition: attachment; filename=' . $theme . '-export.dat' );
115
+ header( 'Content-Type: application/octet-stream; charset=' . $charset );
116
+
117
+ echo serialize( array(
118
+ 'template' => $template,
119
+ 'mods' => $mods ? $mods : array()
120
+ ));
121
+
122
+ die();
123
+ }
124
+
125
+ /**
126
+ * Imports uploaded mods and calls WordPress core customize_save actions so
127
+ * themes that hook into them can act before mods are saved to the database.
128
+ *
129
+ * @method _import
130
+ * @private
131
+ */
132
+ static private function _import()
133
+ {
134
+ if ( ! wp_verify_nonce( $_REQUEST['cei-import'], 'cei-importing' ) ) {
135
+ return;
136
+ }
137
+
138
+ global $wp_customize;
139
+ global $cei_error;
140
+
141
+ $cei_error = false;
142
+ $template = get_option( 'template' );
143
+ $raw = file_get_contents( $_FILES['cei-import-file']['tmp_name'] );
144
+ $data = @unserialize( $raw );
145
+
146
+ // Data checks.
147
+ if ( 'array' != gettype( $data ) ) {
148
+ $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', CEI_TD );
149
+ return;
150
+ }
151
+ if ( ! isset( $data['template'] ) || ! isset( $data['mods'] ) ) {
152
+ $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', CEI_TD );
153
+ return;
154
+ }
155
+ if ( $data['template'] != $template ) {
156
+ $cei_error = __( 'Error importing settings! The settings you uploaded are not for the current theme.', CEI_TD );
157
+ return;
158
+ }
159
+
160
+ // Import images.
161
+ if ( isset( $_REQUEST['cei-import-images'] ) ) {
162
+ $data['mods'] = self::_import_images( $data['mods'] );
163
+ }
164
+
165
+ // Call the customize_save action.
166
+ do_action( 'customize_save', $wp_customize );
167
+
168
+ // Loop through the mods.
169
+ foreach ( $data['mods'] as $key => $val ) {
170
+
171
+ // Call the customize_save_ dynamic action.
172
+ do_action( 'customize_save_' . $key, $wp_customize );
173
+
174
+ // Save the mod.
175
+ set_theme_mod( $key, $val );
176
+ }
177
+
178
+ // Call the customize_save_after action.
179
+ do_action( 'customize_save_after', $wp_customize );
180
+ }
181
+
182
+ /**
183
+ * @method _import_images
184
+ * @private
185
+ */
186
+ static private function _import_images( $mods )
187
+ {
188
+ foreach ( $mods as $key => $val ) {
189
+
190
+ if ( self::_is_image_url( $val ) ) {
191
+
192
+ $data = self::_sideload_image( $val );
193
+
194
+ if ( ! is_wp_error( $data ) ) {
195
+
196
+ $mods[ $key ] = $data->url;
197
+
198
+ // Handle header image controls.
199
+ if ( isset( $mods[ $key . '_data' ] ) ) {
200
+ $mods[ $key . '_data' ] = $data;
201
+ update_post_meta( $data->attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
202
+ }
203
+ }
204
+ }
205
+ }
206
+
207
+ return $mods;
208
+ }
209
+
210
+ /**
211
+ * Taken from the core media_sideload_image function and
212
+ * modified to return the url instead of html.
213
+ *
214
+ * @method _sideload_image
215
+ * @private
216
+ */
217
+ static private function _sideload_image( $file )
218
+ {
219
+ $data = new stdClass();
220
+
221
+ if ( ! function_exists( 'media_handle_sideload' ) ) {
222
+ require_once( ABSPATH . 'wp-admin/includes/media.php' );
223
+ require_once( ABSPATH . 'wp-admin/includes/file.php' );
224
+ require_once( ABSPATH . 'wp-admin/includes/image.php' );
225
+ }
226
+ if ( ! empty( $file ) ) {
227
+
228
+ // Set variables for storage, fix file filename for query strings.
229
+ preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
230
+ $file_array = array();
231
+ $file_array['name'] = basename( $matches[0] );
232
+
233
+ // Download file to temp location.
234
+ $file_array['tmp_name'] = download_url( $file );
235
+
236
+ // If error storing temporarily, return the error.
237
+ if ( is_wp_error( $file_array['tmp_name'] ) ) {
238
+ return $file_array['tmp_name'];
239
+ }
240
+
241
+ // Do the validation and storage stuff.
242
+ $id = media_handle_sideload( $file_array, 0 );
243
+
244
+ // If error storing permanently, unlink.
245
+ if ( is_wp_error( $id ) ) {
246
+ @unlink( $file_array['tmp_name'] );
247
+ return $id;
248
+ }
249
+
250
+ // Build the object to return.
251
+ $meta = wp_get_attachment_metadata( $id );
252
+ $data->attachment_id = $id;
253
+ $data->url = wp_get_attachment_url( $id );
254
+ $data->thumbnail_url = wp_get_attachment_thumb_url( $id );
255
+ $data->height = $meta['height'];
256
+ $data->width = $meta['width'];
257
+ }
258
+
259
+ return $data;
260
+ }
261
+
262
+ /**
263
+ * @method _is_image_url
264
+ * @private
265
+ */
266
+ static private function _is_image_url( $string = '' )
267
+ {
268
+ if ( is_string( $string ) ) {
269
+
270
+ if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $string ) ) {
271
+ return true;
272
+ }
273
+ }
274
+
275
+ return false;
276
+ }
277
+ }
css/customizer.css ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ .cei-form {
2
+ position: absolute;
3
+ left: -99999px;
4
+ }
5
+ .cei-hr {
6
+ margin: 20px 0px 10px;
7
+ }
8
+ .cei-import-file {
9
+ background: #eeeeee;
10
+ width: 100%;
11
+ margin: 10px 0;
12
+ padding: 10px;
13
+ font-size: 12px;
14
+ }
15
+ .cei-import-images {
16
+ display: block;
17
+ margin-bottom: 10px;
18
+ }
19
+ .cei-uploading {
20
+ background: #eeeeee;
21
+ display: none;
22
+ margin: 10px 0;
23
+ padding: 10px;
24
+ font-size: 12px;
25
+ }
customizer-export-import.php ADDED
@@ -0,0 +1,26 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
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
10
+ * License URI: http://www.gnu.org/licenses/gpl-2.0.html
11
+ * Text Domain: customizer-export-import
12
+ */
13
+ define( 'CEI_VERSION', '0.1' );
14
+ define( 'CEI_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
15
+ define( 'CEI_PLUGIN_URL', plugins_url( '/', __FILE__ ) );
16
+ define( 'CEI_TD', 'customizer-export-import' );
17
+
18
+ /* Classes */
19
+ require_once CEI_PLUGIN_DIR . 'classes/class-cei-core.php';
20
+
21
+ /* Actions */
22
+ add_action( 'plugins_loaded', 'CEI_Core::load_plugin_textdomain' );
23
+ add_action( 'init', 'CEI_Core::init' );
24
+ add_action( 'customize_controls_print_scripts', 'CEI_Core::controls_print_scripts' );
25
+ add_action( 'customize_controls_enqueue_scripts', 'CEI_Core::controls_enqueue_scripts' );
26
+ add_action( 'customize_register', 'CEI_Core::register' );
includes/control.php ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <span class="customize-control-title">
2
+ <?php _e( 'Export', CEI_TD ); ?>
3
+ </span>
4
+ <span class="description customize-control-description">
5
+ <?php _e( 'Click the button below to export the customization settings for this theme.', CEI_TD ); ?>
6
+ </span>
7
+ <input type="button" class="button" name="cei-export-button" value="<?php esc_attr_e( 'Export', CEI_TD ); ?>" />
8
+
9
+ <hr class="cei-hr" />
10
+
11
+ <span class="customize-control-title">
12
+ <?php _e( 'Import', CEI_TD ); ?>
13
+ </span>
14
+ <span class="description customize-control-description">
15
+ <?php _e( 'Upload a file to import customization settings for this theme.', CEI_TD ); ?>
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?', CEI_TD ); ?>
21
+ </label>
22
+ <?php wp_nonce_field( 'cei-importing', 'cei-import' ); ?>
23
+ </div>
24
+ <div class="cei-uploading"><?php _e( 'Uploading...', CEI_TD ); ?></div>
25
+ <input type="button" class="button" name="cei-import-button" value="<?php esc_attr_e( 'Import', CEI_TD ); ?>" />
js/customizer.js ADDED
@@ -0,0 +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 );
readme.txt ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === Customizer Export/Import ===
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
9
+
10
+ Easily export or import your WordPress customizer settings!
11
+
12
+ == Description ==
13
+
14
+ = Customizer Export/Import =
15
+
16
+ The Customizer Export/Import plugin allows you to export or import your WordPress customizer settings from directly within the customizer interface! If your theme makes use of the WordPress customizer for its settings, this plugin is for you!
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
+ = Contribute! =
27
+
28
+ We'd love to hear your feedback as to how we could improve the Customizer Export/Import plugin, or better yet, see theme developers actively contribute! Don't hesitate to let us know if you're interested in contributing as we would gladly have others on board.
29
+
30
+ The Customizer Export/Import plugin is brought to you by the fine folks at [Beaver Builder](https://www.wpbeaverbuilder.com/?utm_source=external&utm_medium=wp-repo&utm_campaign=customizer-export-description).
31
+
32
+ == Installation ==
33
+
34
+ 1. Install the Customizer Export/Import plugin either via the WordPress plugin directory, or by uploading the files to your server at wp-content/plugins.
35
+
36
+ 2. After activating, the export/import functionality will be available as a separate section within the WordPress customizer.
37
+
38
+ == Frequently Asked Questions ==
39
+
40
+ 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-faq).
41
+
42
+ == Screenshots ==
43
+
44
+ 1. The export/import customizer section.
45
+
46
+ == Changelog ==
47
+
48
+ = Version 0.1 =
49
+
50
+ Initial release.