PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / 1.4.1
Starter Sites & Templates by Neve v1.4.1
1.4.1 1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 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.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / includes / Importers / Theme_Mods_Importer.php
templates-patterns-collection / includes / Importers Last commit date
Cleanup 3 months ago Helpers 3 months ago WP 3 months ago Content_Importer.php 3 months ago Plugin_Importer.php 1 year ago Theme_Mods_Importer.php 3 months ago Widgets_Importer.php 2 months ago Zelle_Importer.php 2 years ago
Theme_Mods_Importer.php
202 lines
1 <?php
2 /**
3 * Theme Mods Importer.
4 *
5 * @package templates-patterns-collection
6 */
7
8 namespace TIOB\Importers;
9
10 use TIOB\Importers\Cleanup\Active_State;
11 use TIOB\Importers\Helpers\Helper;
12 use TIOB\Importers\Helpers\Slug_Mapping;
13 use WP_REST_Request;
14 use WP_REST_Response;
15
16 /**
17 * Class Theme_Mods_Importer
18 */
19 class Theme_Mods_Importer {
20 use Helper;
21
22 /**
23 * Log
24 *
25 * @var
26 */
27 private $log = '';
28
29 /**
30 * Source URL.
31 *
32 * @var string
33 */
34 private $source_url = '';
35
36 /**
37 * Theme mods array.
38 *
39 * @var array
40 */
41 private $theme_mods = array();
42
43 /**
44 * Options array.
45 *
46 * @var array
47 */
48 private $options = array();
49
50 /**
51 * Import theme mods.
52 *
53 * @param WP_REST_Request $request the async request.
54 *
55 * @return WP_REST_Response
56 */
57 public function import_theme_mods( WP_REST_Request $request ) {
58 if ( ! current_user_can( 'customize' ) ) {
59 return new WP_REST_Response(
60 array(
61 'data' => 'ti__ob_permission_err_2',
62 'success' => false,
63 )
64 );
65 }
66
67 do_action( 'themeisle_ob_before_customizer_import' );
68
69 $data = $request->get_json_params();
70
71 if ( ! isset( $data['source_url'] ) || empty( $data['source_url'] ) ) {
72
73 return new WP_REST_Response(
74 array(
75 'data' => 'ti__ob_theme_mods_err_1',
76 'success' => false,
77 )
78 );
79 }
80
81 if ( ! isset( $data['theme_mods'] ) || empty( $data['theme_mods'] ) ) {
82 return new WP_REST_Response(
83 array(
84 'data' => 'ti__ob_theme_mods_err_2',
85 'success' => false,
86 )
87 );
88 }
89 $this->source_url = $data['source_url'];
90 Slug_Mapping::register_source_url( $this->source_url );
91 $this->theme_mods = apply_filters( 'ti_tpc_theme_mods_pre_import', $data['theme_mods'] );
92 array_walk( $this->theme_mods, array( $this, 'change_theme_mods_root_url' ) );
93
94 foreach ( $this->theme_mods as $mod => $value ) {
95 if ( $mod === '__ti_import_menus_location' ) {
96 continue;
97 }
98 if ( $value === 'true' ) {
99 $value = true;
100 }
101
102 if ( $value === 'false' ) {
103 $value = false;
104 }
105 $previous_value = get_theme_mod( $mod );
106 do_action(
107 'themeisle_cl_add_item_to_property_state',
108 Active_State::THEME_MODS_NSP,
109 array(
110 'mod' => $mod,
111 'value' => $previous_value,
112 )
113 );
114 set_theme_mod( $mod, $value );
115 }
116
117 $this->options = isset( $data['wp_options'] ) ? $data['wp_options'] : array();
118 foreach ( $this->options as $key => $value ) {
119 $value = Slug_Mapping::rewrite_value( $value );
120 if ( is_array( $value ) ) {
121 array_walk_recursive(
122 $value,
123 function ( &$item ) {
124 if ( $item == 'true' ) {
125 $item = true;
126 } elseif ( $item == 'false' ) {
127 $item = false;
128 } elseif ( is_numeric( $item ) ) {
129 $item = intval( $item );
130 }
131 }
132 );
133 }
134 update_option( $key, $value );
135 }
136
137 // Set nav menu locations.
138 if ( isset( $this->theme_mods['__ti_import_menus_location'] ) ) {
139 $menus = $this->theme_mods['__ti_import_menus_location'];
140 $this->setup_nav_menus( $menus );
141 }
142
143 do_action( 'themeisle_ob_after_customizer_import' );
144
145 return new WP_REST_Response(
146 array(
147 'data' => 'success',
148 'success' => true,
149 'log' => $this->log,
150 )
151 );
152 }
153
154 /**
155 * Set up the `nav_menu_locations` theme mod.
156 *
157 * @param array $menus represents the menu data as as [location => slug] retrieved from the API.
158 */
159 public function setup_nav_menus( $menus ) {
160 do_action( 'themeisle_ob_before_nav_menus_setup' );
161
162 if ( empty( $menus ) || ! is_array( $menus ) ) {
163 return;
164 }
165
166 $setup_menus = array();
167 foreach ( $menus as $location => $menu_slug ) {
168
169 $menu_object = wp_get_nav_menu_object( $menu_slug );
170 $term_id = $menu_object->term_id;
171 $setup_menus[ $location ] = $term_id;
172 }
173 if ( empty( $setup_menus ) ) {
174 $this->log .= 'No menus to set up locations for.' . "\n";
175
176 return;
177 }
178 $previous_menu_locations = get_theme_mod( 'nav_menu_locations' );
179 do_action( 'themeisle_cl_add_property_state', Active_State::MENUS_NSP, $previous_menu_locations );
180 set_theme_mod( 'nav_menu_locations', $setup_menus );
181
182 $this->log .= 'Menus are set up.' . "\n";
183
184 do_action( 'themeisle_ob_after_nav_menus_setup' );
185 }
186
187 /**
188 * Change the theme mods root url.
189 *
190 * @param string $item theme mod.
191 *
192 * @return void
193 */
194 private function change_theme_mods_root_url( &$item ) {
195 do_action( 'themeisle_ob_before_change_theme_mods_root_url' );
196 $item = $this->replace_image_urls( $item );
197 $item = Slug_Mapping::rewrite_value( $item );
198 do_action( 'themeisle_ob_after_change_theme_mods_root_url' );
199 }
200
201 }
202