PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / full-site-import / Utils / ElementorHelper.php

ElementorHelper.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at modules/full-site-import/Utils/ElementorHelper.php

570 lines 19.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\FullSiteImport\Utils;
4
5 use Templately\Modules\ThemeBuilder\Types\BaseTemplate;
6 use Templately\Utils\Helper;
7
8 class ElementorHelper extends ImportHelper {
9 protected $content;
10
11 protected $post_id;
12
13 private $nav_menus = [];
14
15 private $ea_post_widgets = [
16 "eael-post-grid",
17 "eael-post-list",
18 "eael-post-timeline",
19 "eael-content-timeline",
20 "eael-dynamic-filterable-gallery",
21 "eael-post-carousel",
22 "eael-post-block",
23 "eael-woo-product-carousel",
24 "eael-woo-product-slider"
25 ];
26
27 private $el_post_widgets = [
28 'posts',
29 'portfolio',
30 'archive-posts',
31 'woocommerce-products'
32 ];
33
34 /**
35 * Source site URL of the pack being imported (from manifest `site`). Absolute
36 * links in Elementor data that point here are rewritten to this site on import.
37 *
38 * @var string
39 */
40 private $source_url = '';
41
42 /**
43 * Path component of the source site URL, un-trailing-slashed — i.e. the
44 * subdirectory/subsite prefix of a multisite demo (e.g. `/readspire`). Empty
45 * when the source site lives at the domain root. Used to strip the demo
46 * subdirectory off relative links before resolving their slug.
47 *
48 * @var string
49 */
50 private $source_path = '';
51
52 /**
53 * Map of the pack's ORIGINAL page slug => the imported page's permalink on THIS
54 * site, e.g. `[ 'about-us' => 'https://mysite.test/about-us-2/' ]`. Built by the
55 * Finalizer (where map_post_ids + permalinks are final) and pushed in via
56 * set_source_pages(). Keyed by the demo slug because that is what stored links
57 * still carry; the value reflects the imported slug even when WordPress changed
58 * it on conflict — which is the whole point of resolving by slug.
59 *
60 * @var array
61 */
62 private $source_pages = [];
63
64 /**
65 * Number of link URLs rewritten by the most recent replace_source_urls() call.
66 *
67 * @var int
68 */
69 private $url_rewrite_count = 0;
70
71 /**
72 * @param $template_json
73 * @param $template_settings
74 * @param $extra_content
75 *
76 * @return ElementorHelper
77 */
78 public function prepare( $template_json, $template_settings, $extra_content = [], $request_params = []) {
79
80 $extraContent = $extra_content;
81 $_data = [];
82 $template_settings['data'] = $template_settings['data'] ?? [];
83 foreach ( $template_settings['data'] as $type => $type_data ) {
84 if ( empty( $type_data ) ) {
85 continue;
86 }
87
88 if ( $type == 'nav_menus' ) {
89 $this->nav_menus = $type_data;
90 }
91
92 if ( $type == 'form' || $type == 'post_type' ) {
93 foreach ( $type_data as $plugin => $plugin_data ) {
94 if ( $type == 'post_type' ) {
95 $_data[ $plugin_data['id'] ] = [
96 'type' => $type,
97 'query' => $plugin_data['query']
98 ];
99 } else {
100 foreach ( $plugin_data as $value ) {
101 if ( empty( $value['settings'] ) ) {
102 continue;
103 }
104
105 foreach ( $value['settings'] as $key => $v ) {
106 if ( ! isset( $extraContent[ $plugin ] ) ) {
107 continue;
108 }
109 $_data[ $value['id'] ][ $key ] = $extraContent[ $plugin ][ $value['id'] ];
110 }
111 }
112 }
113 // $this->sse_log('prepare', 'Preparing output for finalize, just a moment...' . $plugin, 1, 'eventLog');
114 }
115 }
116 // $this->sse_log('prepare', 'Preparing output for finalize, just a moment...' . $type, 1, 'eventLog');
117 }
118
119 // $content = $template_json;
120 $this->json_prepare( $template_json['content'], $_data, $request_params );
121 $this->replace_source_urls( $template_json['content'] );
122 $this->post_id = $this->map_post_ids[ $template_settings['post_id'] ];
123 $this->content = $template_json;
124 unset($template_settings['conditions']);
125 $this->content['import_settings'] = $template_settings;
126
127 return $this;
128 }
129
130 public function update() {
131 /**
132 * @var BaseTemplate $template
133 */
134 if(!empty($this->post_id)){
135 $template = templately()->theme_builder::$templates_manager->get( $this->post_id );
136 // $this->sse_log('update', 'Updating prepared data, just a moment...', 1, 'eventLog');
137 $template->import( $this->content );
138 }
139 else{
140 Helper::log(__("Templately Error code: T001", "templately"));
141 }
142
143 $this->nav_menus = [];
144 $this->content = [];
145 }
146
147 private function json_prepare( &$elements, $data, $request_params = []) {
148 foreach ( $elements as &$element ) {
149 if ( ! empty( $data ) ) {
150 foreach ( $data as $id => $settings ) {
151 if ( $element['id'] == $id ) {
152 if ( isset( $settings['type'] ) && $settings['type'] == 'post_type' ) {
153 $this->replace_query_data( $element, $settings['query'] );
154 } else {
155 foreach ( $settings as $key => $value ) {
156 $element['settings'][ $key ] = $value;
157 }
158 }
159 unset( $data[ $id ] );
160 }
161 }
162 }
163
164 /**
165 * Menu Update if needed.
166 */
167 if ( ! empty( $this->nav_menus ) ) {
168 $this->nav_menu_update( $element );
169 }
170
171 if ( ! empty( $element['elements'] ) ) {
172 $this->json_prepare( $element['elements'], $data, $request_params );
173 }
174 else if(!empty($request_params['logo']) && isset($request_params['logo_size']) && $request_params['logo_size'] && $element['elType'] === "widget" && $element['widgetType'] === "tl-site-logo"){
175 $element['settings']['width'] = [
176 'size' => $request_params['logo_size'],
177 'unit' => 'px'
178 ];
179 $element['settings']['max-width'] = [
180 'size' => '100',
181 'unit' => '%'
182 ];
183 $this->sse_log('prepare', 'Site Logo', 1, 'eventLog');
184 }
185
186 // $this->sse_log('prepare', 'Preparing output for finalize, just a moment..Preparing output for finalize.', 1, 'eventLog');
187 }
188 }
189
190 private function nav_menu_update( &$element ) {
191 // $this->sse_log('nav-menu', 'Updating nav menus, just a moment...', 1, 'eventLog');
192 if ( ! isset( $element['widgetType'] ) ) {
193 return;
194 }
195
196 switch ( $element['widgetType'] ) {
197 case 'eael-simple-menu':
198 if(!empty($element['settings']['eael_simple_menu_menu']) && isset($this->map_term_ids[ $element['settings']['eael_simple_menu_menu'] ])){
199 $element['settings']['eael_simple_menu_menu'] = $this->map_term_ids[ $element['settings']['eael_simple_menu_menu'] ];
200 }
201 break;
202 case 'eael-advanced-menu':
203 if(!empty($element['settings']['eael_advanced_menu_menu']) && isset($this->map_term_ids[ $element['settings']['eael_advanced_menu_menu'] ])){
204 $element['settings']['eael_advanced_menu_menu'] = $this->map_term_ids[ $element['settings']['eael_advanced_menu_menu'] ];
205 }
206 break;
207 }
208 }
209
210 /**
211 * Set the source site URL (pack origin) used by replace_source_urls().
212 *
213 * @param string $url Usually the manifest `site` value.
214 *
215 * @return self
216 */
217 public function set_source_url( $url ) {
218 $this->source_url = is_string( $url ) ? $url : '';
219
220 // Demo subdirectory/subsite prefix (e.g. "/readspire" for a subdirectory
221 // multisite demo). Stripped off relative links before slug resolution.
222 $path = '' !== $this->source_url ? (string) wp_parse_url( $this->source_url, PHP_URL_PATH ) : '';
223 $this->source_path = untrailingslashit( $path );
224
225 return $this;
226 }
227
228 /**
229 * Provide the ORIGINAL-slug => imported-permalink map used to resolve links to
230 * the actual imported page (see $source_pages). Without it, replace_source_urls()
231 * still works — it just falls back to the plain home_url() domain swap.
232 *
233 * @param array $map original page slug (lowercase) => permalink on this site.
234 *
235 * @return self
236 */
237 public function set_source_pages( $map ) {
238 $this->source_pages = is_array( $map ) ? $map : [];
239
240 return $this;
241 }
242
243 /**
244 * Number of link URLs the last replace_source_urls() call rewrote.
245 *
246 * @return int
247 */
248 public function get_url_rewrite_count() {
249 return $this->url_rewrite_count;
250 }
251
252 /**
253 * Rewrite links that point at the imported pack's source site so they point at
254 * the matching imported page on THIS site instead.
255 *
256 * Walks the Elementor element tree — child `elements` AND each element's
257 * `settings` recursively, so repeater rows like a Mega Menu's `menu_items[*]`
258 * (and EA Info Box / Button link controls) are all covered — and rewrites every
259 * link-control `url` that originates from the source site.
260 *
261 * For each such link the URL is first reduced to a path relative to the source
262 * site, covering all three forms a demo produces:
263 * - absolute / protocol-relative pointing at the source host
264 * (`https://demo.site/readspire/about-us`);
265 * - root-relative carrying the demo's subdirectory/subsite prefix
266 * (`/readspire/about-us` — common on subdirectory-multisite demos);
267 * - already-local (a prior pass turned it into `home_url()/about-us`).
268 * Its last path segment is taken as the page slug, then:
269 * 1. if that slug is in the source_pages map → the link becomes the imported
270 * page's real permalink (slug-change-proof: get_permalink() reflects the
271 * post_name WordPress actually assigned, even after a conflict rename);
272 * 2. otherwise it falls back to the plain home_url() domain swap.
273 *
274 * Matching is:
275 * - scheme-insensitive — http/https (or protocol-relative) drift still matches;
276 * - boundary-safe — the source host/subdirectory must end at a `/`, `?`, `#` or
277 * end-of-string, so `example.com` never matches `example.com.evil.com` and the
278 * `/readspire` prefix never eats `/readspire-blog`;
279 * - foreign-link-safe — absolute URLs on other domains are left alone, even if
280 * their last segment happens to collide with an imported slug;
281 * - idempotent — a link already resolved to a local permalink is left untouched
282 * on a second pass.
283 *
284 * @param array $elements Elementor `content` array (by reference, mutated in place).
285 * @param string|null $source_url Override source URL; defaults to set_source_url().
286 *
287 * @return int Number of URLs rewritten.
288 */
289 public function replace_source_urls( &$elements, $source_url = null ) {
290 $this->url_rewrite_count = 0;
291
292 $source_url = null !== $source_url ? $source_url : $this->source_url;
293
294 if ( empty( $source_url ) || ! is_array( $elements ) ) {
295 return 0;
296 }
297
298 // Scheme-relative forms ("//host/path") so http/https drift still matches.
299 $source_sr = $this->scheme_relative( $source_url );
300 $target_base = untrailingslashit( home_url() );
301
302 if ( '' === $source_sr || '//' === $source_sr ) {
303 return 0;
304 }
305
306 // Demo subdirectory prefix (e.g. "/readspire"); honoured even when the source
307 // URL is passed in here rather than via set_source_url().
308 $source_path = null !== $source_url
309 ? untrailingslashit( (string) wp_parse_url( $source_url, PHP_URL_PATH ) )
310 : $this->source_path;
311
312 $this->rewrite_elements_urls( $elements, $source_sr, $target_base, $source_path );
313
314 return $this->url_rewrite_count;
315 }
316
317 /**
318 * Strip the http(s) scheme from a URL, returning its scheme-relative form
319 * ("//host/path") with any trailing slash removed.
320 *
321 * @param string $url
322 *
323 * @return string
324 */
325 private function scheme_relative( $url ) {
326 $url = untrailingslashit( (string) $url );
327 $url = preg_replace( '#^https?:#i', '', $url );
328
329 if ( '' === $url ) {
330 return '';
331 }
332
333 // Normalise a bare "host/path" (no scheme at all) to "//host/path".
334 return strpos( $url, '//' ) === 0 ? $url : '//' . ltrim( $url, '/' );
335 }
336
337 private function rewrite_elements_urls( &$elements, $source_sr, $target_base, $source_path ) {
338 foreach ( $elements as &$element ) {
339 if ( ! is_array( $element ) ) {
340 continue;
341 }
342
343 if ( ! empty( $element['settings'] ) && is_array( $element['settings'] ) ) {
344 $this->rewrite_settings_urls( $element['settings'], $source_sr, $target_base, $source_path );
345 }
346
347 if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) {
348 $this->rewrite_elements_urls( $element['elements'], $source_sr, $target_base, $source_path );
349 }
350 }
351 unset( $element );
352 }
353
354 private function rewrite_settings_urls( &$node, $source_sr, $target_base, $source_path ) {
355 // Elementor link controls carry a `url` string ({ url, is_external, nofollow,
356 // custom_attributes } — or, for some controls, just { url }). Media/attachment
357 // controls also carry a `url`, but always alongside an `id`/`source`; excluding
358 // those leaves images untouched while still covering link controls that omit
359 // `is_external`.
360 if ( isset( $node['url'] ) && is_string( $node['url'] )
361 && ! isset( $node['id'] ) && ! isset( $node['source'] ) ) {
362 $node['url'] = $this->swap_source_url( $node['url'], $source_sr, $target_base, $source_path );
363 }
364
365 foreach ( $node as &$value ) {
366 if ( is_array( $value ) ) {
367 $this->rewrite_settings_urls( $value, $source_sr, $target_base, $source_path );
368 }
369 }
370 unset( $value );
371 }
372
373 /**
374 * Rewrite a single link URL from the source site to the matching imported page
375 * on this site. Returns the URL unchanged when it does not originate from the
376 * source site, so foreign links, mailto:/tel:/#fragment links and already-local
377 * links are safe.
378 *
379 * @param string $url The control URL to (maybe) rewrite.
380 * @param string $source_sr Scheme-relative source base ("//host/subdir").
381 * @param string $target_base This site's base (home_url(), with scheme, no trailing slash).
382 * @param string $source_path Demo subdirectory prefix ("/readspire") or '' at root.
383 *
384 * @return string
385 */
386 private function swap_source_url( $url, $source_sr, $target_base, $source_path ) {
387 if ( ! is_string( $url ) || '' === $url ) {
388 return $url;
389 }
390
391 $stripped = preg_replace( '#^https?:#i', '', $url );
392 $target_sr = $this->scheme_relative( $target_base );
393
394 // (1) Absolute / protocol-relative URL pointing at the SOURCE host (+subdir).
395 if ( strpos( $stripped, $source_sr ) === 0 ) {
396 $rest = substr( $stripped, strlen( $source_sr ) );
397
398 // Boundary so "//example.com" never matches "//example.com.evil.com".
399 if ( ! $this->is_url_boundary( $rest ) ) {
400 return $url;
401 }
402
403 return $this->resolve_rest( $rest, $target_base, true, $url );
404 }
405
406 // (2) Absolute URL already on THIS site (e.g. a prior pass rewrote it) —
407 // only slug-resolve (to fix a changed slug); never blind-swap it again.
408 if ( strpos( $stripped, $target_sr ) === 0 ) {
409 $rest = substr( $stripped, strlen( $target_sr ) );
410
411 if ( ! $this->is_url_boundary( $rest ) ) {
412 return $url;
413 }
414
415 return $this->resolve_rest( $rest, $target_base, false, $url );
416 }
417
418 // (3) Root-relative path ("/...", not protocol-relative "//...").
419 if ( '/' === $url[0] && ( ! isset( $url[1] ) || '/' !== $url[1] ) ) {
420 // Carries the demo subdirectory prefix → strip it (boundary-safe) and
421 // treat as a source link so an unmatched slug still loses the prefix.
422 if ( '' !== $source_path && strpos( $url, $source_path ) === 0
423 && $this->is_url_boundary( substr( $url, strlen( $source_path ) ) ) ) {
424 return $this->resolve_rest( substr( $url, strlen( $source_path ) ), $target_base, true, $url );
425 }
426
427 // Plain site-relative path (no demo prefix) — already resolves to this
428 // host, so only slug-resolve; leave it untouched if there is no match.
429 return $this->resolve_rest( $url, $target_base, false, $url );
430 }
431
432 // Foreign absolute URL, mailto:/tel:/#fragment, etc. — leave untouched.
433 return $url;
434 }
435
436 /**
437 * Resolve a source-relative path ("/about-us", "/about-us?x=1#y") to a final URL.
438 * Looks up the path's last segment in the source_pages slug map first; on a miss
439 * either domain-swaps to home_url() ($blind_fallback) or returns the URL as-is.
440 *
441 * @param string $rest Path relative to the source/target origin (leading "/").
442 * @param string $target_base This site's base (home_url(), no trailing slash).
443 * @param bool $blind_fallback When no slug matches, swap to $target_base . $rest.
444 * @param string $original_url The URL to return when nothing applies.
445 *
446 * @return string
447 */
448 private function resolve_rest( $rest, $target_base, $blind_fallback, $original_url ) {
449 // Split off ?query / #fragment so they can be carried onto the permalink.
450 $cut = strcspn( $rest, '?#' );
451 $path = substr( $rest, 0, $cut );
452 $suffix = substr( $rest, $cut );
453
454 // Last non-empty path segment = the page slug (post_name).
455 $segments = array_filter( explode( '/', $path ), 'strlen' );
456 $slug = empty( $segments ) ? '' : strtolower( (string) end( $segments ) );
457
458 if ( '' !== $slug && isset( $this->source_pages[ $slug ] ) ) {
459 $this->url_rewrite_count++;
460
461 // Permalink keeps its own trailing slash; ?query/#fragment (if any) ride along.
462 return '' === $suffix
463 ? $this->source_pages[ $slug ]
464 : untrailingslashit( $this->source_pages[ $slug ] ) . $suffix;
465 }
466
467 if ( $blind_fallback ) {
468 $this->url_rewrite_count++;
469
470 return $target_base . $rest;
471 }
472
473 return $original_url;
474 }
475
476 /**
477 * True when $rest is empty or starts at a URL path/query/fragment boundary.
478 *
479 * @param string $rest
480 *
481 * @return bool
482 */
483 private function is_url_boundary( $rest ) {
484 return '' === $rest || strpos( '/?#', $rest[0] ) !== false;
485 }
486
487 private function replace_query_data( &$element, $data ) {
488 // $this->sse_log('query', 'Finalizing query data, just a moment...', 1, 'eventLog');
489 if ( ! empty( $element['widgetType'] ) ) {
490 if ( in_array( $element['widgetType'], $this->ea_post_widgets ) ) {
491 if ( ! empty( $data['tax_query'] ) ) {
492 foreach ( $data['tax_query'] as $key => $tax_query ) {
493 if ( $key === 'relation' ) {
494 continue;
495 }
496 if ( isset( $element['settings'][ $tax_query['taxonomy'] . '_ids' ] ) ) {
497 $new_ids = [];
498 foreach ( $element['settings'][ $tax_query['taxonomy'] . '_ids' ] as $id ) {
499 if ( isset( $this->map_term_ids[ $id ] ) ) {
500 $new_ids[] = $this->map_term_ids[ $id ];
501 }
502 }
503 $element['settings'][ $tax_query['taxonomy'] . '_ids' ] = $new_ids;
504 }
505 }
506 }
507 if ( ! empty( $element['settings']['authors'] ) ) {
508 $element['settings']['authors'] = [ get_current_user_id() ];
509 }
510 }
511 if ( in_array( $element['widgetType'], $this->el_post_widgets ) ) {
512 if ( ! empty( $data['tax_query'] ) ) {
513 $this->replace_term_ids( $element, [
514 'posts_include_term_ids',
515 'posts_exclude_term_ids',
516 'query_include_term_ids',
517 'query_exclude_term_ids',
518 ] );
519
520 }
521 if ( ! empty( $data['author__in'] ) ) {
522 $keys = [ 'posts_include_authors', 'query_include_authors' ];
523 foreach ( $keys as $key ) {
524 if ( isset( $element['settings'][ $key ] ) ) {
525 $element['settings'][ $key ] = [ get_current_user_id() ];
526 }
527 }
528 }
529 }
530 if ( $element['widgetType'] == 'eael-woo-product-gallery' ) {
531 $this->replace_term_ids( $element, [ 'eael_product_gallery_categories', 'eael_product_gallery_tags' ] );
532
533 }
534 if ( $element['widgetType'] == 'eicon-woocommerce' ) {
535 $this->replace_term_ids( $element, [ 'eael_product_grid_categories' ] );
536 }
537 }
538 }
539
540 private function replace_term_ids( &$element, $keys ) {
541 foreach ( $keys as $key ) {
542 // $this->sse_log('terms', 'Finalizing term ids, just a moment...', 1, 'eventLog');
543 if ( ! empty( $element['settings'][ $key ] ) ) {
544 $new_ids = [];
545 foreach ( $element['settings'][ $key ] as $id ) {
546 if ( isset( $this->map_term_ids[ $id ] ) ) {
547 $new_ids[] = $this->map_term_ids[ $id ];
548 }
549 }
550 if(!empty($new_ids)){
551 $element['settings'][ $key ] = $new_ids;
552 }
553 }
554
555 }
556 }
557
558 public function parse_images($post_content) {
559 // Defensive stub — Elementor has no image-parsing pass (Gutenberg does). This
560 // "should never be called" guard previously ran an UNCONDITIONAL
561 // debug_backtrace() + bare error_log(print_r(...)) on every call in production.
562 // Route it through Helper::log (WP_DEBUG_LOG-gated) and only compute the
563 // backtrace when logging is actually enabled.
564 if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) {
565 Helper::log( debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS, 3 ), 'ElementorHelper::parse_images called unexpectedly', 'warning' );
566 }
567
568 return [];
569 }
570 }