Issues (281)

Branch: master

src/Frontend/Core/Engine/Footer.php (1 issue)

1
<?php
2
3
namespace Frontend\Core\Engine;
4
5
use ForkCMS\App\KernelLoader;
6
use Symfony\Component\HttpKernel\KernelInterface;
7
use Frontend\Core\Engine\Navigation as FrontendNavigation;
8
9
/**
10
 * This class will be used to alter the footer-part of the HTML-document that will be created by the frontend.
11
 */
12
class Footer extends KernelLoader
13
{
14
    /**
15
     * TwigTemplate instance
16
     *
17
     * @var TwigTemplate
18
     */
19
    protected $template;
20
21
    /**
22
     * URL instance
23
     *
24
     * @var Url
25
     */
26
    protected $url;
27
28 26
    public function __construct(KernelInterface $kernel)
29
    {
30 26
        parent::__construct($kernel);
31
32 26
        $this->template = $this->getContainer()->get('templating');
33 26
        $this->url = $this->getContainer()->get('url');
34
35 26
        $this->getContainer()->set('footer', $this);
36 26
    }
37
38
    /**
39
     * Parse the footer into the template
40
     */
41 26
    public function parse(): void
42
    {
43 26
        $footerLinks = (array) Navigation::getFooterLinks();
44 26
        $this->template->assignGlobal('footerLinks', $footerLinks);
45
46 26
        $siteHTMLEndOfBody = (string) $this->get('fork.settings')->get('Core', 'site_html_end_of_body', $this->get('fork.settings')->get('Core', 'site_html_footer', null));
47
48
        // @deprecated remove this in Fork 6, Facebook should not be added automaticall
49 26
        $facebookAppId = $this->get('fork.settings')->get('Core', 'facebook_app_id', null);
50 26
        if ($facebookAppId !== null) {
51
            // add Facebook container
52
            $siteHTMLEndOfBody .= $this->getFacebookHtml($facebookAppId);
0 ignored issues
show
Deprecated Code introduced by
The function Frontend\Core\Engine\Footer::getFacebookHtml() has been deprecated: remove this in Fork 6, Facebook should be added with respect to the given consent. In essence: add Facebook yourself if needed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

52
            $siteHTMLEndOfBody .= /** @scrutinizer ignore-deprecated */ $this->getFacebookHtml($facebookAppId);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
53
        }
54
55
        // add Google sitelinks search box code if wanted.
56 26
        if ($this->get('fork.settings')->get('Search', 'use_sitelinks_search_box', true)) {
57 26
            $searchUrl = FrontendNavigation::getUrlForBlock('Search');
58 26
            $url404 = FrontendNavigation::getUrl(Model::ERROR_PAGE_ID);
59 26
            if ($searchUrl !== $url404) {
60 26
                $siteHTMLEndOfBody .= $this->getSiteLinksCode($searchUrl);
61
            }
62
        }
63
64
        // assign site wide html
65 26
        $this->template->assignGlobal('siteHTMLEndOfBody', $siteHTMLEndOfBody);
66
67
        // @deprecated remove this in Fork 6, use siteHTMLEndOfBody
68 26
        $this->template->assignGlobal('siteHTMLFooter', $siteHTMLEndOfBody);
69 26
    }
70
71
    /**
72
     * Builds the HTML needed for Facebook to be initialized
73
     * @deprecated remove this in Fork 6, Facebook should be added with respect
74
     *             to the given consent. In essence: add Facebook yourself if
75
     *             needed.
76
     *
77
     * @param string $facebookAppId The application id used to interact with FB
78
     *
79
     * @return string  HTML and JS needed to initialize FB JavaScript
80
     */
81
    protected function getFacebookHtml(string $facebookAppId): string
82
    {
83
        // add the fb-root div
84
        $facebookHtml = "\n" . '<div id="fb-root"></div>' . "\n";
85
86
        // add facebook JavaScript
87
        $facebookHtml .= '<script>' . "\n";
88
        if (!empty($facebookAppId)) {
89
            $facebookHtml .= '    window.fbAsyncInit = function() {' . "\n";
90
            $facebookHtml .= '        FB.init({' . "\n";
91
            $facebookHtml .= '            appId: "' . $facebookAppId . '",' . "\n";
92
            $facebookHtml .= '            status: true,' . "\n";
93
            $facebookHtml .= '            cookie: true,' . "\n";
94
            $facebookHtml .= '            xfbml: true,' . "\n";
95
            $facebookHtml .= '            oauth: true' . "\n";
96
            $facebookHtml .= '        });' . "\n";
97
            $facebookHtml .= '        jsFrontend.facebook.afterInit();' . "\n";
98
            $facebookHtml .= '    };' . "\n";
99
        }
100
101
        $facebookHtml .= '    (function(d, s, id){' . "\n";
102
        $facebookHtml .= '        var js, fjs = d.getElementsByTagName(s)[0];' . "\n";
103
        $facebookHtml .= '        if (d.getElementById(id)) {return;}' . "\n";
104
        $facebookHtml .= '        js = d.createElement(s); js.id = id;' . "\n";
105
        $facebookHtml .= '        js.src = "//connect.facebook.net/' . $this->getFacebookLocale() . '/all.js";' . "\n";
106
        $facebookHtml .= '        fjs.parentNode.insertBefore(js, fjs);' . "\n";
107
        $facebookHtml .= '    }(document, \'script\', \'facebook-jssdk\'));' . "\n";
108
        $facebookHtml .= '</script>';
109
110
        return $facebookHtml;
111
    }
112
113
    /**
114
     * @deprecated remove this in Fork 6, Facebook should be added with respect
115
     *             to the given consent. In essence: add Facebook yourself if
116
     *             needed.
117
     */
118
    private function getFacebookLocale(): string
119
    {
120
        $specialCases = [
121
            'en' => 'en_US', // sorry uk :( I prefer you too
122
            'zh' => 'zh_CN',
123
            'cs' => 'cs_CZ',
124
            'el' => 'el_GR',
125
            'ja' => 'ja_JP',
126
            'sv' => 'sv_SE',
127
            'uk' => 'uk_UA',
128
        ];
129
130
        // check if it is a special case, otherwise return [language]_[language]
131
        return $specialCases[LANGUAGE] ?? mb_strtolower(LANGUAGE) . '_' . mb_strtoupper(LANGUAGE);
132
    }
133
134
    /**
135
     * Returns the code needed to get a site links search box in Google.
136
     * More information can be found on the offical Google documentation:
137
     * https://developers.google.com/webmasters/richsnippets/sitelinkssearch
138
     *
139
     * @param string $searchUrl The url to the search page
140
     *
141
     * @return string The script needed for google
142
     */
143 26
    protected function getSiteLinksCode(string $searchUrl): string
144
    {
145 26
        $siteLinksCode = '<script type="application/ld+json">' . "\n";
146 26
        $siteLinksCode .= '{' . "\n";
147 26
        $siteLinksCode .= '    "@context": "https://schema.org",' . "\n";
148 26
        $siteLinksCode .= '    "@type": "WebSite",' . "\n";
149 26
        $siteLinksCode .= '    "url": "' . SITE_URL . '",' . "\n";
150 26
        $siteLinksCode .= '    "potentialAction": {' . "\n";
151 26
        $siteLinksCode .= '        "@type": "SearchAction",' . "\n";
152 26
        $siteLinksCode .= '        "target": "' . SITE_URL . $searchUrl . '?form=search&q_widget={q_widget}",' . "\n";
153 26
        $siteLinksCode .= '        "query-input": "name=q_widget"' . "\n";
154 26
        $siteLinksCode .= '    }' . "\n";
155 26
        $siteLinksCode .= '}' . "\n";
156 26
        $siteLinksCode .= '</script>';
157
158 26
        return $siteLinksCode;
159
    }
160
}
161