Passed
Pull Request — 4 (#911)
by Steve
03:37
created

ElementalPageExtension::updateAnchorsOnPage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 7
rs 10
1
<?php
2
3
namespace DNADesign\Elemental\Extensions;
4
5
use DNADesign\Elemental\Models\ElementalArea;
6
use SilverStripe\CMS\Model\SiteTree;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\View\Parsers\HTML4Value;
9
use SilverStripe\View\SSViewer;
10
11
/**
12
 * @method ElementalArea ElementalArea()
13
 * @property int ElementalAreaID
14
 */
15
class ElementalPageExtension extends ElementalAreasExtension
16
{
17
    private static $has_one = [
0 ignored issues
show
introduced by
The private property $has_one is not used, and could be removed.
Loading history...
18
        'ElementalArea' => ElementalArea::class,
19
    ];
20
21
    private static $owns = [
0 ignored issues
show
introduced by
The private property $owns is not used, and could be removed.
Loading history...
22
        'ElementalArea',
23
    ];
24
25
    private static $cascade_duplicates = [
0 ignored issues
show
introduced by
The private property $cascade_duplicates is not used, and could be removed.
Loading history...
26
        'ElementalArea',
27
    ];
28
29
    /**
30
     * Returns the contents of each ElementalArea has_one's markup for use in Solr or Elastic search indexing
31
     *
32
     * @return string
33
     */
34
    public function getElementsForSearch()
35
    {
36
        return strip_tags($this->createHtml());
37
    }
38
39
    /**
40
     * @param array $anchors
41
     *
42
     * @see SiteTree::getAnchorsOnPage()
43
     */
44
    public function updateAnchorsOnPage(array &$anchors): void
45
    {
46
        if (!($this->owner instanceof SiteTree)) {
47
            return;
48
        }
49
        $content = $this->createHtml();
50
        $anchors = array_merge($anchors, $this->owner->getAnchorsInContent($content));
51
    }
52
53
    private function createHtml()
54
    {
55
        $oldThemes = SSViewer::get_themes();
56
        SSViewer::set_themes(SSViewer::config()->get('themes'));
57
        try {
58
            $output = [];
59
            foreach ($this->owner->hasOne() as $key => $class) {
60
                if ($class !== ElementalArea::class) {
61
                    continue;
62
                }
63
                /** @var ElementalArea $area */
64
                $area = $this->owner->$key();
65
                if ($area) {
66
                    $output[] = $area->forTemplate();
67
                }
68
            }
69
        } finally {
70
            // Reset theme if an exception occurs, if you don't have a
71
            // try / finally around code that might throw an Exception,
72
            // CMS layout can break on the response. (SilverStripe 4.1.1)
73
            SSViewer::set_themes($oldThemes);
74
        }
75
        return implode($output);
76
    }
77
78
    public function MetaTags(&$tags)
79
    {
80
        if (!Controller::has_curr()) {
81
            return;
82
        }
83
        $controller = Controller::curr();
84
        $request = $controller->getRequest();
85
        if ($request->getVar('ElementalPreview') !== null) {
86
            $html = HTML4Value::create($tags);
87
            $xpath = "//meta[@name='x-page-id' or @name='x-cms-edit-link']";
88
            $removeTags = $html->query($xpath);
89
            $body = $html->getBody();
90
            foreach ($removeTags as $tag) {
91
                $body->removeChild($tag);
92
            }
93
            $tags = $html->getContent();
94
        }
95
    }
96
}
97