PluginProbe ʕ •ᴥ•ʔ
Customizer Export/Import / 0.1
Customizer Export/Import v0.1
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 11 years ago class-cei-core.php 11 years ago
class-cei-core.php
278 lines
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 }
278