Passed
Pull Request — master (#650)
by Loz
04:12
created

src/Extensions/ElementalPageExtension.php (3 issues)

1
<?php
2
3
namespace DNADesign\Elemental\Extensions;
4
5
use Exception;
6
use DNADesign\Elemental\Models\ElementalArea;
7
use SilverStripe\Control\Controller;
8
use SilverStripe\View\Parsers\HTML4Value;
9
use SilverStripe\Core\Config\Config;
10
use SilverStripe\View\SSViewer;
11
12
class ElementalPageExtension extends ElementalAreasExtension
13
{
14
    private static $has_one = [
0 ignored issues
show
The private property $has_one is not used, and could be removed.
Loading history...
15
        'ElementalArea' => ElementalArea::class,
16
    ];
17
18
    private static $owns = [
0 ignored issues
show
The private property $owns is not used, and could be removed.
Loading history...
19
        'ElementalArea',
20
    ];
21
22
    private static $cascade_duplicates = [
0 ignored issues
show
The private property $cascade_duplicates is not used, and could be removed.
Loading history...
23
        'ElementalArea',
24
    ];
25
26
    /**
27
     * Returns the contents of each ElementalArea has_one's markup for use in Solr or Elastic search indexing
28
     *
29
     * @return string
30
     */
31
    public function getElementsForSearch()
32
    {
33
        $oldThemes = SSViewer::get_themes();
34
        SSViewer::set_themes(SSViewer::config()->get('themes'));
35
        try {
36
            $output = [];
37
            foreach ($this->owner->hasOne() as $key => $class) {
38
                if ($class !== ElementalArea::class) {
39
                    continue;
40
                }
41
                /** @var ElementalArea $area */
42
                $area = $this->owner->$key();
43
                if ($area) {
44
                    $output[] = strip_tags($area->forTemplate());
45
                }
46
            }
47
        } finally {
48
            // Reset theme if an exception occurs, if you don't have a
49
            // try / finally around code that might throw an Exception,
50
            // CMS layout can break on the response. (SilverStripe 4.1.1)
51
            SSViewer::set_themes($oldThemes);
52
        }
53
        return implode($output);
54
    }
55
56
    public function MetaTags(&$tags)
57
    {
58
        $controller = Controller::curr();
59
        $request = $controller->getRequest();
60
        if ($request->getVar('ElementalPreview') !== null) {
61
            $html = HTML4Value::create($tags);
62
            $xpath = "//meta[@name='x-page-id' or @name='x-cms-edit-link']";
63
            $removeTags = $html->query($xpath);
64
            $body = $html->getBody();
65
            foreach ($removeTags as $tag) {
66
                $body->removeChild($tag);
67
            }
68
            $tags = $html->getContent();
69
        }
70
    }
71
}
72