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