Test Failed
Push — develop ( 31453c...07ca16 )
by Paul
08:20
created

SchemaParser::parseElementorData()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 33
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 33
ccs 0
cts 10
cp 0
rs 8.0555
c 0
b 0
f 0
cc 9
nc 9
nop 2
crap 90
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