|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Integrations\Elementor; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Modules\SchemaParser as Parser; |
|
8
|
|
|
|
|
9
|
|
|
class SchemaParser extends Parser |
|
10
|
|
|
{ |
|
11
|
|
|
public function generate(): array |
|
12
|
|
|
{ |
|
13
|
|
|
if (!class_exists('Elementor\Plugin')) { |
|
14
|
|
|
return []; |
|
15
|
|
|
} |
|
16
|
|
|
$document = \Elementor\Plugin::$instance->documents->get((int) get_the_ID()); |
|
17
|
|
|
if (false === $document || !$document->is_built_with_elementor()) { |
|
18
|
|
|
return []; |
|
19
|
|
|
} |
|
20
|
|
|
$data = Arr::consolidate($document->get_elements_data()); |
|
21
|
|
|
$data = $this->parseElementorData($data); |
|
22
|
|
|
foreach ($data as $shortcode => $args) { |
|
23
|
|
|
if ('site_reviews' === $shortcode) { |
|
24
|
|
|
return $this->buildReviewSchema($args); |
|
25
|
|
|
} |
|
26
|
|
|
if ('site_reviews_summary' === $shortcode) { |
|
27
|
|
|
return $this->buildSummarySchema($args); |
|
28
|
|
|
} |
|
29
|
|
|
} |
|
30
|
|
|
return []; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
protected function parseElementorData(array $elements, array $result = []): array |
|
34
|
|
|
{ |
|
35
|
|
|
foreach ($elements as $data) { |
|
36
|
|
|
$element = \Elementor\Plugin::$instance->elements_manager->create_element_instance($data); |
|
37
|
|
|
if (!$element) { |
|
38
|
|
|
continue; |
|
39
|
|
|
} |
|
40
|
|
|
$name = $element->get_name(); |
|
41
|
|
|
if ('template' === $name) { |
|
42
|
|
|
$postId = Cast::toInt($element->get_settings('template_id')); |
|
43
|
|
|
if ($document = \Elementor\Plugin::$instance->documents->get($postId)) { |
|
44
|
|
|
$templateElements = Arr::consolidate($document->get_elements_data()); |
|
45
|
|
|
$result = $this->parseElementorData($templateElements, $result); |
|
46
|
|
|
} |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
$children = $element->get_data('elements'); |
|
50
|
|
|
if (!empty($children)) { |
|
51
|
|
|
$result = $this->parseElementorData($children, $result); |
|
52
|
|
|
continue; |
|
53
|
|
|
} |
|
54
|
|
|
if (!in_array($name, ['site_reviews', 'site_reviews_summary'])) { |
|
55
|
|
|
continue; |
|
56
|
|
|
} |
|
57
|
|
|
if (!Cast::toBool($element->get_settings('schema'))) { |
|
58
|
|
|
continue; |
|
59
|
|
|
} |
|
60
|
|
|
if (array_key_exists($name, $result)) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
$result[$name] = $element->get_data('settings'); |
|
64
|
|
|
} |
|
65
|
|
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|