class-cei-core.php
422 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 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 | $data['options'][ $option_key ] = get_option( $option_key ); |
| 191 | } |
| 192 | |
| 193 | if( function_exists( 'wp_get_custom_css_post' ) ) { |
| 194 | $data['wp_css'] = wp_get_custom_css(); |
| 195 | } |
| 196 | |
| 197 | // Set the download headers. |
| 198 | header( 'Content-disposition: attachment; filename=' . $theme . '-export.dat' ); |
| 199 | header( 'Content-Type: application/octet-stream; charset=' . $charset ); |
| 200 | |
| 201 | // Serialize the export data. |
| 202 | echo serialize( $data ); |
| 203 | |
| 204 | // Start the download. |
| 205 | die(); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Imports uploaded mods and calls WordPress core customize_save actions so |
| 210 | * themes that hook into them can act before mods are saved to the database. |
| 211 | * |
| 212 | * @since 0.1 |
| 213 | * @since 0.3 Added $wp_customize param and importing of options. |
| 214 | * @access private |
| 215 | * @param object $wp_customize An instance of WP_Customize_Manager. |
| 216 | * @return void |
| 217 | */ |
| 218 | static private function _import( $wp_customize ) |
| 219 | { |
| 220 | // Make sure we have a valid nonce. |
| 221 | if ( ! wp_verify_nonce( $_REQUEST['cei-import'], 'cei-importing' ) ) { |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | // Make sure WordPress upload support is loaded. |
| 226 | if ( ! function_exists( 'wp_handle_upload' ) ) { |
| 227 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 228 | } |
| 229 | |
| 230 | // Load the export/import option class. |
| 231 | require_once CEI_PLUGIN_DIR . 'classes/class-cei-option.php'; |
| 232 | |
| 233 | // Setup global vars. |
| 234 | global $wp_customize; |
| 235 | global $cei_error; |
| 236 | |
| 237 | // Setup internal vars. |
| 238 | $cei_error = false; |
| 239 | $template = get_template(); |
| 240 | $overrides = array( 'test_form' => false, 'test_type' => false, 'mimes' => array('dat' => 'text/plain') ); |
| 241 | $file = wp_handle_upload( $_FILES['cei-import-file'], $overrides ); |
| 242 | |
| 243 | // Make sure we have an uploaded file. |
| 244 | if ( isset( $file['error'] ) ) { |
| 245 | $cei_error = $file['error']; |
| 246 | return; |
| 247 | } |
| 248 | if ( ! file_exists( $file['file'] ) ) { |
| 249 | $cei_error = __( 'Error importing settings! Please try again.', 'customizer-export-import' ); |
| 250 | return; |
| 251 | } |
| 252 | |
| 253 | // Get the upload data. |
| 254 | $raw = file_get_contents( $file['file'] ); |
| 255 | $data = @unserialize( $raw ); |
| 256 | |
| 257 | // Remove the uploaded file. |
| 258 | unlink( $file['file'] ); |
| 259 | |
| 260 | // Data checks. |
| 261 | if ( 'array' != gettype( $data ) ) { |
| 262 | $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', 'customizer-export-import' ); |
| 263 | return; |
| 264 | } |
| 265 | if ( ! isset( $data['template'] ) || ! isset( $data['mods'] ) ) { |
| 266 | $cei_error = __( 'Error importing settings! Please check that you uploaded a customizer export file.', 'customizer-export-import' ); |
| 267 | return; |
| 268 | } |
| 269 | if ( $data['template'] != $template ) { |
| 270 | $cei_error = __( 'Error importing settings! The settings you uploaded are not for the current theme.', 'customizer-export-import' ); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | // Import images. |
| 275 | if ( isset( $_REQUEST['cei-import-images'] ) ) { |
| 276 | $data['mods'] = self::_import_images( $data['mods'] ); |
| 277 | } |
| 278 | |
| 279 | // Import custom options. |
| 280 | if ( isset( $data['options'] ) ) { |
| 281 | |
| 282 | foreach ( $data['options'] as $option_key => $option_value ) { |
| 283 | |
| 284 | $option = new CEI_Option( $wp_customize, $option_key, array( |
| 285 | 'default' => '', |
| 286 | 'type' => 'option', |
| 287 | 'capability' => 'edit_theme_options' |
| 288 | ) ); |
| 289 | |
| 290 | $option->import( $option_value ); |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | // If wp_css is set then import it. |
| 295 | if( function_exists( 'wp_update_custom_css_post' ) && isset( $data['wp_css'] ) && '' !== $data['wp_css'] ) { |
| 296 | wp_update_custom_css_post( $data['wp_css'] ); |
| 297 | } |
| 298 | |
| 299 | // Call the customize_save action. |
| 300 | do_action( 'customize_save', $wp_customize ); |
| 301 | |
| 302 | // Loop through the mods. |
| 303 | foreach ( $data['mods'] as $key => $val ) { |
| 304 | |
| 305 | // Call the customize_save_ dynamic action. |
| 306 | do_action( 'customize_save_' . $key, $wp_customize ); |
| 307 | |
| 308 | // Save the mod. |
| 309 | set_theme_mod( $key, $val ); |
| 310 | } |
| 311 | |
| 312 | // Call the customize_save_after action. |
| 313 | do_action( 'customize_save_after', $wp_customize ); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Imports images for settings saved as mods. |
| 318 | * |
| 319 | * @since 0.1 |
| 320 | * @access private |
| 321 | * @param array $mods An array of customizer mods. |
| 322 | * @return array The mods array with any new import data. |
| 323 | */ |
| 324 | static private function _import_images( $mods ) |
| 325 | { |
| 326 | foreach ( $mods as $key => $val ) { |
| 327 | |
| 328 | if ( self::_is_image_url( $val ) ) { |
| 329 | |
| 330 | $data = self::_sideload_image( $val ); |
| 331 | |
| 332 | if ( ! is_wp_error( $data ) ) { |
| 333 | |
| 334 | $mods[ $key ] = $data->url; |
| 335 | |
| 336 | // Handle header image controls. |
| 337 | if ( isset( $mods[ $key . '_data' ] ) ) { |
| 338 | $mods[ $key . '_data' ] = $data; |
| 339 | update_post_meta( $data->attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() ); |
| 340 | } |
| 341 | } |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return $mods; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Taken from the core media_sideload_image function and |
| 350 | * modified to return an array of data instead of html. |
| 351 | * |
| 352 | * @since 0.1 |
| 353 | * @access private |
| 354 | * @param string $file The image file path. |
| 355 | * @return array An array of image data. |
| 356 | */ |
| 357 | static private function _sideload_image( $file ) |
| 358 | { |
| 359 | $data = new stdClass(); |
| 360 | |
| 361 | if ( ! function_exists( 'media_handle_sideload' ) ) { |
| 362 | require_once( ABSPATH . 'wp-admin/includes/media.php' ); |
| 363 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 364 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 365 | } |
| 366 | if ( ! empty( $file ) ) { |
| 367 | |
| 368 | // Set variables for storage, fix file filename for query strings. |
| 369 | preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); |
| 370 | $file_array = array(); |
| 371 | $file_array['name'] = basename( $matches[0] ); |
| 372 | |
| 373 | // Download file to temp location. |
| 374 | $file_array['tmp_name'] = download_url( $file ); |
| 375 | |
| 376 | // If error storing temporarily, return the error. |
| 377 | if ( is_wp_error( $file_array['tmp_name'] ) ) { |
| 378 | return $file_array['tmp_name']; |
| 379 | } |
| 380 | |
| 381 | // Do the validation and storage stuff. |
| 382 | $id = media_handle_sideload( $file_array, 0 ); |
| 383 | |
| 384 | // If error storing permanently, unlink. |
| 385 | if ( is_wp_error( $id ) ) { |
| 386 | @unlink( $file_array['tmp_name'] ); |
| 387 | return $id; |
| 388 | } |
| 389 | |
| 390 | // Build the object to return. |
| 391 | $meta = wp_get_attachment_metadata( $id ); |
| 392 | $data->attachment_id = $id; |
| 393 | $data->url = wp_get_attachment_url( $id ); |
| 394 | $data->thumbnail_url = wp_get_attachment_thumb_url( $id ); |
| 395 | $data->height = $meta['height']; |
| 396 | $data->width = $meta['width']; |
| 397 | } |
| 398 | |
| 399 | return $data; |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Checks to see whether a string is an image url or not. |
| 404 | * |
| 405 | * @since 0.1 |
| 406 | * @access private |
| 407 | * @param string $string The string to check. |
| 408 | * @return bool Whether the string is an image url or not. |
| 409 | */ |
| 410 | static private function _is_image_url( $string = '' ) |
| 411 | { |
| 412 | if ( is_string( $string ) ) { |
| 413 | |
| 414 | if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $string ) ) { |
| 415 | return true; |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | return false; |
| 420 | } |
| 421 | } |
| 422 |