PluginProbe
Borderless – Addons and Templates for Elementor / trunk
Borderless – Addons and Templates for Elementor vtrunk
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.2 1.2.3 All 78 releases
borderless / includes / library / inc / CustomizerImporter.php

CustomizerImporter.php in Borderless – Addons and Templates for Elementor trunk, at includes/library/inc/CustomizerImporter.php

173 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace LIBRARY;
4
5 class CustomizerImporter {
6 public static function import( $customizer_import_file_path ) {
7 $library = BorderlessLibraryImporter::get_instance();
8 $log_file_path = $library->get_log_file_path();
9
10 $results = self::import_customizer_options( $customizer_import_file_path );
11
12 if ( is_wp_error( $results ) ) {
13 $error_message = $results->get_error_message();
14
15 $library->append_to_frontend_error_messages( $error_message );
16
17 Helpers::append_to_file(
18 $error_message,
19 $log_file_path,
20 esc_html__( 'Importing customizer settings', 'borderless' )
21 );
22 }
23 else {
24 $log_added = Helpers::append_to_file(
25 esc_html__( 'Customizer settings import finished!', 'borderless' ),
26 $log_file_path,
27 esc_html__( 'Importing customizer settings' , 'borderless' )
28 );
29 }
30 }
31
32
33 public static function import_customizer_options( $import_file_path ) {
34 global $wp_customize;
35
36 $template = get_template();
37
38 if ( ! file_exists( $import_file_path ) ) {
39 return new \WP_Error(
40 'missing_cutomizer_import_file',
41 sprintf(
42 esc_html__( 'Error: The customizer import file is missing! File path: %s', 'borderless' ),
43 $import_file_path
44 )
45 );
46 }
47
48 $raw = Helpers::data_from_file( $import_file_path );
49
50 if ( is_wp_error( $raw ) ) {
51 return $raw;
52 }
53
54 $data = unserialize( $raw );
55
56 if ( ! is_array( $data ) && ( ! isset( $data['template'] ) || ! isset( $data['mods'] ) ) ) {
57 return new \WP_Error(
58 'customizer_import_data_error',
59 esc_html__( 'Error: The customizer import file is not in a correct format. Please make sure to use the correct customizer import file.', 'borderless' )
60 );
61 }
62 if ( $data['template'] !== $template ) {
63 return new \WP_Error(
64 'customizer_import_wrong_theme',
65 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.', 'borderless' )
66 );
67 }
68
69 if ( Helpers::apply_filters( 'library/customizer_import_images', true ) ) {
70 $data['mods'] = self::import_customizer_images( $data['mods'] );
71 }
72
73 if ( isset( $data['options'] ) ) {
74 if ( ! class_exists( '\WP_Customize_Setting' ) ) {
75 require_once ABSPATH . 'wp-includes/class-wp-customize-setting.php';
76 }
77
78 foreach ( $data['options'] as $option_key => $option_value ) {
79 $option = new CustomizerOption( $wp_customize, $option_key, array(
80 'default' => '',
81 'type' => 'option',
82 'capability' => 'edit_theme_options',
83 ) );
84
85 $option->import( $option_value );
86 }
87 }
88
89 $use_wp_customize_save_hooks = Helpers::apply_filters( 'library/enable_wp_customize_save_hooks', false );
90
91 if ( $use_wp_customize_save_hooks ) {
92 do_action( 'customize_save', $wp_customize );
93 }
94
95 foreach ( $data['mods'] as $key => $val ) {
96 if ( $use_wp_customize_save_hooks ) {
97 do_action( 'customize_save_' . $key, $wp_customize );
98 }
99
100 set_theme_mod( $key, $val );
101 }
102
103 if ( $use_wp_customize_save_hooks ) {
104 do_action( 'customize_save_after', $wp_customize );
105 }
106 }
107
108 private static function import_customizer_images( $mods ) {
109 foreach ( $mods as $key => $val ) {
110 if ( self::customizer_is_image_url( $val ) ) {
111 $data = self::customizer_sideload_image( $val );
112 if ( ! is_wp_error( $data ) ) {
113 $mods[ $key ] = $data->url;
114
115 if ( isset( $mods[ $key . '_data' ] ) ) {
116 $mods[ $key . '_data' ] = $data;
117 update_post_meta( $data->attachment_id, '_wp_attachment_is_custom_header', get_stylesheet() );
118 }
119 }
120 }
121 }
122
123 return $mods;
124 }
125
126 private static function customizer_sideload_image( $file ) {
127 $data = new \stdClass();
128
129 if ( ! function_exists( 'media_handle_sideload' ) ) {
130 require_once( ABSPATH . 'wp-admin/includes/media.php' );
131 require_once( ABSPATH . 'wp-admin/includes/file.php' );
132 require_once( ABSPATH . 'wp-admin/includes/image.php' );
133 }
134 if ( ! empty( $file ) ) {
135 preg_match( '/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches );
136 $file_array = array();
137 $file_array['name'] = basename( $matches[0] );
138
139 $file_array['tmp_name'] = download_url( $file );
140
141 if ( is_wp_error( $file_array['tmp_name'] ) ) {
142 return $file_array['tmp_name'];
143 }
144
145 $id = media_handle_sideload( $file_array, 0 );
146
147 if ( is_wp_error( $id ) ) {
148 unlink( $file_array['tmp_name'] );
149 return $id;
150 }
151
152 $meta = wp_get_attachment_metadata( $id );
153 $data->attachment_id = $id;
154 $data->url = wp_get_attachment_url( $id );
155 $data->thumbnail_url = wp_get_attachment_thumb_url( $id );
156 $data->height = $meta['height'];
157 $data->width = $meta['width'];
158 }
159
160 return $data;
161 }
162
163 private static function customizer_is_image_url( $string = '' ) {
164 if ( is_string( $string ) ) {
165 if ( preg_match( '/\.(jpg|jpeg|png|gif)/i', $string ) ) {
166 return true;
167 }
168 }
169
170 return false;
171 }
172 }
173