C:\xampp\htdocs\landing\wp-content\plugins\customizer-export-import\classes\class-cei-core.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php

/**
 * The main export/import class.
 *
 * @since 0.1
 */
final class CEI_Core {

    
/**
     * An array of core options that shouldn't be imported.
     *
     * @since 0.3
     * @access private
     * @var array $core_options
     */
    
static private $core_options = array(
        
'blogname',
        
'blogdescription',
        
'show_on_front',
        
'page_on_front',
        
'page_for_posts',
    );

    
/**
     * Load a translation for this plugin.
     *
     * @since 0.1
     * @return void
     */
    
static public function load_plugin_textdomain()
    {
        
load_plugin_textdomain'customizer-export-import'falsebasenameCEI_PLUGIN_DIR ) . '/lang/' );
    }

    
/**
     * Check to see if we need to do an export or import.
     * This should be called by the customize_register action.
     *
     * @since 0.1
     * @since 0.3 Passing $wp_customize to the export and import methods.
     * @param object $wp_customize An instance of WP_Customize_Manager.
     * @return void
     */
    
static public function init$wp_customize )
    {
        if ( 
current_user_can'edit_theme_options' ) ) {

            if ( isset( 
$_REQUEST['cei-export'] ) ) {
                
self::_export$wp_customize );
            }
            if ( isset( 
$_REQUEST['cei-import'] ) && isset( $_FILES['cei-import-file'] ) ) {
                
self::_import$wp_customize );
            }
        }
    }

    
/**
     * Prints scripts for the control.
     *
     * @since 0.1
     * @return void
     */
    
static public function controls_print_scripts()
    {
        global 
$cei_error;

        if ( 
$cei_error ) {
            echo 
'<script> alert("' $cei_error '"); </script>';
        }
    }

    
/**
     * Enqueues scripts for the control.
     *
     * @since 0.1
     * @return void
     */
    
static public function controls_enqueue_scripts()
    {
        
// Register
        
wp_register_style'cei-css'CEI_PLUGIN_URL 'css/customizer.css', array(), CEI_VERSION );
        
wp_register_script'cei-js'CEI_PLUGIN_URL 'js/customizer.js', array( 'jquery' ), CEI_VERSIONtrue );

        
// Localize
        
wp_localize_script'cei-js''CEIl10n', array(
            
'emptyImport'    => __'Please choose a file to import.''customizer-export-import' )
        ));

        
// Config
        
wp_localize_script'cei-js''CEIConfig', array(
            
'customizerURL'      => admin_url'customize.php' ),
            
'exportNonce'      => wp_create_nonce'cei-exporting' )
        ));

        
// Enqueue
        
wp_enqueue_style'cei-css' );
        
wp_enqueue_script'cei-js' );
    }

    
/**
     * Registers the control with the customizer.
     *
     * @since 0.1
     * @param object $wp_customize An instance of WP_Customize_Manager.
     * @return void
     */
    
static public function register$wp_customize )
    {
        require_once 
CEI_PLUGIN_DIR 'classes/class-cei-control.php';

        
// Add the export/import section.
        
$wp_customize->add_section'cei-section', array(
            
'title'       => __'Export/Import''customizer-export-import' ),
            
'priority' => 10000000
        
));

        
// Add the export/import setting.
        
$wp_customize->add_setting'cei-setting', array(
            
'default' => '',
            
'type'      => 'none'
        
));

        
// Add the export/import control.
        
$wp_customize->add_control( new CEI_Control(
            
$wp_customize,
            
'cei-setting',
            array(
                
'section'    => 'cei-section',
                
'priority'    => 1
            
)
        ));
    }

    
/**
     * Export customizer settings.
     *
     * @since 0.1
     * @since 0.3 Added $wp_customize param and exporting of options.
     * @access private
     * @param object $wp_customize An instance of WP_Customize_Manager.
     * @return void
     */
    
static private function _export$wp_customize )
    {
        if ( ! 
wp_verify_nonce$_REQUEST['cei-export'], 'cei-exporting' ) ) {
            return;
        }

        
$theme        get_stylesheet();
        
$template    get_template();
        
$charset    get_option'blog_charset' );
        
$mods        get_theme_mods();
        
$data        = array(
                          
'template'  => $template,
                          
'mods'      => $mods $mods : array(),
                          
'options'      => array()
                      );

        
// Get options from the Customizer API.
        
$settings $wp_customize->settings();

        foreach ( 
$settings as $key => $setting ) {

            if ( 
'option' == $setting->type ) {

                
// Don't save widget data.
                
if ( 'widget_' === substrstrtolower$key ), 0) ) {
                    continue;
                }

                
// Don't save sidebar data.
                
if ( 'sidebars_' === substrstrtolower$key ), 0) ) {
                    continue;
                }

                
// Don't save core options.
                
if ( in_array$keyself::$core_options ) ) {
                    continue;
                }

                
$data['options'][ $key ] = $setting->value();
            }
        }

        
// Plugin developers can specify additional option keys to export.
        
$option_keys apply_filters'cei_export_option_keys', array() );

        foreach ( 
$option_keys as $option_key ) {
            
$data['options'][ $option_key ] = get_option$option_key );
        }

        if( 
function_exists'wp_get_custom_css_post' ) ) {
            
$data['wp_css'] = wp_get_custom_css();
        }

        
// Set the download headers.
        
header'Content-disposition: attachment; filename=' $theme '-export.dat' );
        
header'Content-Type: application/octet-stream; charset=' $charset );

        
// Serialize the export data.
        
echo serialize$data );

        
// Start the download.
        
die();
    }

    
/**
     * Imports uploaded mods and calls WordPress core customize_save actions so
     * themes that hook into them can act before mods are saved to the database.
     *
     * @since 0.1
     * @since 0.3 Added $wp_customize param and importing of options.
     * @access private
     * @param object $wp_customize An instance of WP_Customize_Manager.
     * @return void
     */
    
static private function _import$wp_customize )
    {
        
// Make sure we have a valid nonce.
        
if ( ! wp_verify_nonce$_REQUEST['cei-import'], 'cei-importing' ) ) {
            return;
        }

        
// Make sure WordPress upload support is loaded.
        
if ( ! function_exists'wp_handle_upload' ) ) {
            require_once( 
ABSPATH 'wp-admin/includes/file.php' );
        }

        
// Load the export/import option class.
        
require_once CEI_PLUGIN_DIR 'classes/class-cei-option.php';

        
// Setup global vars.
        
global $wp_customize;
        global 
$cei_error;

        
// Setup internal vars.
        
$cei_error     false;
        
$template     get_template();
        
$overrides   = array( 'test_form' => false'test_type' => false'mimes' => array('dat' => 'text/plain') );
        
$file        wp_handle_upload$_FILES['cei-import-file'], $overrides );

        
// Make sure we have an uploaded file.
        
if ( isset( $file['error'] ) ) {
            
$cei_error $file['error'];
            return;
        }
        if ( ! 
file_exists$file['file'] ) ) {
            
$cei_error __'Error importing settings! Please try again.''customizer-export-import' );
            return;
        }

        
// Get the upload data.
        
$raw  file_get_contents$file['file'] );
        
$data = @unserialize$raw );

        
// Remove the uploaded file.
        
unlink$file['file'] );

        
// Data checks.
        
if ( 'array' != gettype$data ) ) {
            
$cei_error __'Error importing settings! Please check that you uploaded a customizer export file.''customizer-export-import' );
            return;
        }
        if ( ! isset( 
$data['template'] ) || ! isset( $data['mods'] ) ) {
            
$cei_error __'Error importing settings! Please check that you uploaded a customizer export file.''customizer-export-import' );
            return;
        }
        if ( 
$data['template'] != $template ) {
            
$cei_error __'Error importing settings! The settings you uploaded are not for the current theme.''customizer-export-import' );
            return;
        }

        
// Import images.
        
if ( isset( $_REQUEST['cei-import-images'] ) ) {
            
$data['mods'] = self::_import_images$data['mods'] );
        }

        
// Import custom options.
        
if ( isset( $data['options'] ) ) {

            foreach ( 
$data['options'] as $option_key => $option_value ) {

                
$option = new CEI_Option$wp_customize$option_key, array(
                    
'default'        => '',
                    
'type'            => 'option',
                    
'capability'    => 'edit_theme_options'
                
) );

                
$option->import$option_value );
            }
        }

        
// If wp_css is set then import it.
        
if( function_exists'wp_update_custom_css_post' ) && isset( $data['wp_css'] ) && '' !== $data['wp_css'] ) {
            
wp_update_custom_css_post$data['wp_css'] );
        }

        
// Call the customize_save action.
        
do_action'customize_save'$wp_customize );

        
// Loop through the mods.
        
foreach ( $data['mods'] as $key => $val ) {

            
// Call the customize_save_ dynamic action.
            
do_action'customize_save_' $key$wp_customize );

            
// Save the mod.
            
set_theme_mod$key$val );
        }

        
// Call the customize_save_after action.
        
do_action'customize_save_after'$wp_customize );
    }

    
/**
     * Imports images for settings saved as mods.
     *
     * @since 0.1
     * @access private
     * @param array $mods An array of customizer mods.
     * @return array The mods array with any new import data.
     */
    
static private function _import_images$mods )
    {
        foreach ( 
$mods as $key => $val ) {

            if ( 
self::_is_image_url$val ) ) {

                
$data self::_sideload_image$val );

                if ( ! 
is_wp_error$data ) ) {

                    
$mods$key ] = $data->url;

                    
// Handle header image controls.
                    
if ( isset( $mods$key '_data' ] ) ) {
                        
$mods$key '_data' ] = $data;
                        
update_post_meta$data->attachment_id'_wp_attachment_is_custom_header'get_stylesheet() );
                    }
                }
            }
        }

        return 
$mods;
    }

    
/**
     * Taken from the core media_sideload_image function and
     * modified to return an array of data instead of html.
     *
     * @since 0.1
     * @access private
     * @param string $file The image file path.
     * @return array An array of image data.
     */
    
static private function _sideload_image$file )
    {
        
$data = new stdClass();

        if ( ! 
function_exists'media_handle_sideload' ) ) {
            require_once( 
ABSPATH 'wp-admin/includes/media.php' );
            require_once( 
ABSPATH 'wp-admin/includes/file.php' );
            require_once( 
ABSPATH 'wp-admin/includes/image.php' );
        }
        if ( ! empty( 
$file ) ) {

            
// Set variables for storage, fix file filename for query strings.
            
preg_match'/[^\?]+\.(jpe?g|jpe|gif|png)\b/i'$file$matches );
            
$file_array = array();
            
$file_array['name'] = basename$matches[0] );

            
// Download file to temp location.
            
$file_array['tmp_name'] = download_url$file );

            
// If error storing temporarily, return the error.
            
if ( is_wp_error$file_array['tmp_name'] ) ) {
                return 
$file_array['tmp_name'];
            }

            
// Do the validation and storage stuff.
            
$id media_handle_sideload$file_array);

            
// If error storing permanently, unlink.
            
if ( is_wp_error$id ) ) {
                @
unlink$file_array['tmp_name'] );
                return 
$id;
            }

            
// Build the object to return.
            
$meta                    wp_get_attachment_metadata$id );
            
$data->attachment_id    $id;
            
$data->url                wp_get_attachment_url$id );
            
$data->thumbnail_url    wp_get_attachment_thumb_url$id );
            
$data->height            $meta['height'];
            
$data->width            $meta['width'];
        }

        return 
$data;
    }

    
/**
     * Checks to see whether a string is an image url or not.
     *
     * @since 0.1
     * @access private
     * @param string $string The string to check.
     * @return bool Whether the string is an image url or not.
     */
    
static private function _is_image_url$string '' )
    {
        if ( 
is_string$string ) ) {

            if ( 
preg_match'/\.(jpg|jpeg|png|gif)/i'$string ) ) {
                return 
true;
            }
        }

        return 
false;
    }
}
x

Windows NT KPTV 6.2 build 9200 (Windows Server 2012 Datacenter Edition) i586