CreateDemoContent
5 years ago
CustomizerImporter.php
5 years ago
CustomizerOption.php
5 years ago
Downloader.php
5 years ago
Helpers.php
5 years ago
ImportActions.php
5 years ago
Importer.php
5 years ago
Logger.php
5 years ago
OneClickDemoImport.php
5 years ago
PluginInstaller.php
5 years ago
PluginInstallerSkin.php
5 years ago
PluginInstallerSkinSilent.php
5 years ago
ReduxImporter.php
5 years ago
ViewHelpers.php
5 years ago
WPCLICommands.php
5 years ago
WXRImporter.php
5 years ago
WidgetImporter.php
5 years ago
CustomizerImporter.php
242 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class for the customizer importer used in the One Click Demo Import plugin. |
| 4 | * |
| 5 | * Code is mostly from the Customizer Export/Import plugin. |
| 6 | * |
| 7 | * @see https://wordpress.org/plugins/customizer-export-import/ |
| 8 | * @package ocdi |
| 9 | */ |
| 10 | |
| 11 | namespace OCDI; |
| 12 | |
| 13 | class CustomizerImporter { |
| 14 | /** |
| 15 | * Import customizer from a DAT file, generated by the Customizer Export/Import plugin. |
| 16 | * |
| 17 | * @param string $customizer_import_file_path path to the customizer import file. |
| 18 | */ |
| 19 | public static function import( $customizer_import_file_path ) { |
| 20 | $ocdi = OneClickDemoImport::get_instance(); |
| 21 | $log_file_path = $ocdi->get_log_file_path(); |
| 22 | |
| 23 | // Try to import the customizer settings. |
| 24 | $results = self::import_customizer_options( $customizer_import_file_path ); |
| 25 | |
| 26 | // Check for errors, else write the results to the log file. |
| 27 | if ( is_wp_error( $results ) ) { |
| 28 | $error_message = $results->get_error_message(); |
| 29 | |
| 30 | // Add any error messages to the frontend_error_messages variable in OCDI main class. |
| 31 | $ocdi->append_to_frontend_error_messages( $error_message ); |
| 32 | |
| 33 | // Write error to log file. |
| 34 | Helpers::append_to_file( |
| 35 | $error_message, |
| 36 | $log_file_path, |
| 37 | esc_html__( 'Importing customizer settings', 'one-click-demo-import' ) |
| 38 | ); |
| 39 | } |
| 40 | else { |
| 41 | // Add this message to log file. |
| 42 | $log_added = Helpers::append_to_file( |
| 43 | esc_html__( 'Customizer settings import finished!', 'one-click-demo-import' ), |
| 44 | $log_file_path, |
| 45 | esc_html__( 'Importing customizer settings' , 'one-click-demo-import' ) |
| 46 | ); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | |
| 51 | /** |
| 52 | * Imports uploaded mods and calls WordPress core customize_save actions so |
| 53 | * themes that hook into them can act before mods are saved to the database. |
| 54 | * |
| 55 | * Update: WP core customize_save actions were removed, because of some errors. |
| 56 | * |
| 57 | * @since 1.1.1 |
| 58 | * @param string $import_file_path Path to the import file. |
| 59 | * @return void|WP_Error |
| 60 | */ |
| 61 | public static function import_customizer_options( $import_file_path ) { |
| 62 | // Setup global vars. |
| 63 | global $wp_customize; |
| 64 | |
| 65 | // Setup internal vars. |
| 66 | $template = get_template(); |
| 67 | |
| 68 | // Make sure we have an import file. |
| 69 | if ( ! file_exists( $import_file_path ) ) { |
| 70 | return new \WP_Error( |
| 71 | 'missing_cutomizer_import_file', |
| 72 | sprintf( /* translators: %s - file path */ |
| 73 | esc_html__( 'Error: The customizer import file is missing! File path: %s', 'one-click-demo-import' ), |
| 74 | $import_file_path |
| 75 | ) |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | // Get the upload data. |
| 80 | $raw = Helpers::data_from_file( $import_file_path ); |
| 81 | |
| 82 | // Make sure we got the data. |
| 83 | if ( is_wp_error( $raw ) ) { |
| 84 | return $raw; |
| 85 | } |
| 86 | |
| 87 | $data = unserialize( $raw ); |
| 88 | |
| 89 | // Data checks. |
| 90 | if ( ! is_array( $data ) && ( ! isset( $data['template'] ) || ! isset( $data['mods'] ) ) ) { |
| 91 | return new \WP_Error( |
| 92 | 'customizer_import_data_error', |
| 93 | esc_html__( 'Error: The customizer import file is not in a correct format. Please make sure to use the correct customizer import file.', 'one-click-demo-import' ) |
| 94 | ); |
| 95 | } |
| 96 | if ( $data['template'] !== $template ) { |
| 97 | return new \WP_Error( |
| 98 | 'customizer_import_wrong_theme', |
| 99 | esc_html__( 'Error: The customizer import file is not suitable for current theme. You can only import customizer settings for the same theme or a child theme.', 'one-click-demo-import' ) |
| 100 | ); |
| 101 | } |
| 102 | |
| 103 | // Import images. |
| 104 | if ( Helpers::apply_filters( 'ocdi/customizer_import_images', true ) ) { |
| 105 | $data['mods'] = self::import_customizer_images( $data['mods'] ); |
| 106 | } |
| 107 | |
| 108 | // Import custom options. |
| 109 | if ( isset( $data['options'] ) ) { |
| 110 | // Require modified customizer options class. |
| 111 | if ( ! class_exists( '\WP_Customize_Setting' ) ) { |
| 112 | require_once ABSPATH . 'wp-includes/class-wp-customize-setting.php'; |
| 113 | } |
| 114 | |
| 115 | foreach ( $data['options'] as $option_key => $option_value ) { |
| 116 | $option = new CustomizerOption( $wp_customize, $option_key, array( |
| 117 | 'default' => '', |
| 118 | 'type' => 'option', |
| 119 | 'capability' => 'edit_theme_options', |
| 120 | ) ); |
| 121 | |
| 122 | $option->import( $option_value ); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // Should the customizer import use the WP customize_save* hooks? |
| 127 | $use_wp_customize_save_hooks = Helpers::apply_filters( 'ocdi/enable_wp_customize_save_hooks', false ); |
| 128 | |
| 129 | if ( $use_wp_customize_save_hooks ) { |
| 130 | do_action( 'customize_save', $wp_customize ); |
| 131 | } |
| 132 | |
| 133 | // Loop through the mods and save the mods. |
| 134 | foreach ( $data['mods'] as $key => $val ) { |
| 135 | if ( $use_wp_customize_save_hooks ) { |
| 136 | do_action( 'customize_save_' . $key, $wp_customize ); |
| 137 | } |
| 138 | |
| 139 | set_theme_mod( $key, $val ); |
| 140 | } |
| 141 | |
| 142 | if ( $use_wp_customize_save_hooks ) { |
| 143 | do_action( 'customize_save_after', $wp_customize ); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * Helper function: Customizer import - imports images for settings saved as mods. |
| 149 | * |
| 150 | * @since 1.1.1 |
| 151 | * @param array $mods An array of customizer mods. |
| 152 | * @return array The mods array with any new import data. |
| 153 | */ |
| 154 | private static function import_customizer_images( $mods ) { |
| 155 | foreach ( $mods as $key => $val ) { |
| 156 | if ( self::customizer_is_image_url( $val ) ) { |
| 157 | $data = self::customizer_sideload_image( $val ); |
| 158 | if ( ! is_wp_error( $data ) ) { |
| 159 | $mods[ $key ] = $data->url; |
| 160 | |
| 161 | // Handle header image controls. |
| 162 | if ( isset( $mods[ $key . '_data' ] ) ) { |
| 163 | $mods[ $key . '_data' ] = $data; |
| 164 | update_post_meta( $data->attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() ); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | return $mods; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Helper function: Customizer import |
| 175 | * Taken from the core media_sideload_image function and |
| 176 | * modified to return an array of data instead of html. |
| 177 | * |
| 178 | * @since 1.1.1. |
| 179 | * @param string $file The image file path. |
| 180 | * @return array An array of image data. |
| 181 | */ |
| 182 | private static function customizer_sideload_image( $file ) { |
| 183 | $data = new \stdClass(); |
| 184 | |
| 185 | if ( ! function_exists( 'media_handle_sideload' ) ) { |
| 186 | require_once( ABSPATH . 'wp-admin/includes/media.php' ); |
| 187 | require_once( ABSPATH . 'wp-admin/includes/file.php' ); |
| 188 | require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
| 189 | } |
| 190 | if ( ! empty( $file ) ) { |
| 191 | // Set variables for storage, fix file filename for query strings. |
| 192 | preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches ); |
| 193 | $file_array = array(); |
| 194 | $file_array['name'] = basename( $matches[0] ); |
| 195 | |
| 196 | // Download file to temp location. |
| 197 | $file_array['tmp_name'] = download_url( $file ); |
| 198 | |
| 199 | // If error storing temporarily, return the error. |
| 200 | if ( is_wp_error( $file_array['tmp_name'] ) ) { |
| 201 | return $file_array['tmp_name']; |
| 202 | } |
| 203 | |
| 204 | // Do the validation and storage stuff. |
| 205 | $id = media_handle_sideload( $file_array, 0 ); |
| 206 | |
| 207 | // If error storing permanently, unlink. |
| 208 | if ( is_wp_error( $id ) ) { |
| 209 | unlink( $file_array['tmp_name'] ); |
| 210 | return $id; |
| 211 | } |
| 212 | |
| 213 | // Build the object to return. |
| 214 | $meta = wp_get_attachment_metadata( $id ); |
| 215 | $data->attachment_id = $id; |
| 216 | $data->url = wp_get_attachment_url( $id ); |
| 217 | $data->thumbnail_url = wp_get_attachment_thumb_url( $id ); |
| 218 | $data->height = $meta['height']; |
| 219 | $data->width = $meta['width']; |
| 220 | } |
| 221 | |
| 222 | return $data; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Checks to see whether a string is an image url or not. |
| 227 | * |
| 228 | * @since 1.1.1 |
| 229 | * @param string $string The string to check. |
| 230 | * @return bool Whether the string is an image url or not. |
| 231 | */ |
| 232 | private static function customizer_is_image_url( $string = '' ) { |
| 233 | if ( is_string( $string ) ) { |
| 234 | if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $string ) ) { |
| 235 | return true; |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 |