XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
Legacy_HeaderScript.class.php
1<?php
13
14if (!defined('XOOPS_ROOT_PATH')) {
15 exit();
16}
17
19{
20 protected $_mType = 'google';
21 protected $_mCore = '1';
22 protected $_mUi = '1';
23
24 protected $_mLibrary = [];
25 protected $_mScript = [];
26 protected $_mMeta = [
27 'keywords' =>'',
28 'description' =>'',
29 'robots' =>'',
30 'rating' =>'',
31 'author' =>'',
32 'copyright' =>'',
33 'msvalidate.01' =>'',
34 'google-site-verification' =>'',
35 'yandex-verification' =>'',
36 'fb:app_id' =>'',
37 'twitter:site' =>''
38 ];
39 protected $_mOnloadScript = [];
40 protected $_mStylesheet = [];
41 protected $_mLink = [];
42
43 public $mUsePrototype = false; //use prototype.js ?
44 public $mFuncNamePrefix = ''; //jQuery $() function's name prefix for compatibility with prototype.js
45
53 public function __construct()
54 {
55 $root = XCube_Root::getSingleton();
56
57 //setup jQuery library location
58 $this->_mCore = $this->_getRenderConfig('jquery_core');
59 $this->_mUi = $this->_getRenderConfig('jquery_ui');
60 $core = str_replace('.', '', $this->_mCore);
61 $this->_mType = is_numeric($core) ? 'google' : 'local';
62
63 //use compatibility mode with prototype.js ?
64 if (1 == $root->getSiteConfig('jQuery', 'usePrototype')) {
65 $this->mUsePrototype = true;
66 $this->mPrototypeUrl = $root->getSiteConfig('jQuery', 'prototypeUrl');
67 $this->mFuncNamePrefix = $root->getSiteConfig('jQuery', 'funcNamePrefix');
68 }
69
71 }
72
80 public function _setupDefaultStylesheet()
81 {
82 if ($this->_getRenderConfig('css_file')) {
83 $this->addStylesheet($this->_getRenderConfig('css_file'), false);
84 }
85 }
86
95 public function addLibrary( string $url, bool $xoopsUrl=true )
96 {
97 $libUrl = (true == $xoopsUrl) ? XOOPS_URL . $url : $url;
98 if (!in_array($libUrl, $this->_mLibrary, true)) {
99 $this->_mLibrary[] = $libUrl;
100 }
101 }
102
111 public function addStylesheet( string $url, bool $xoopsUrl=true )
112 {
113 $libUrl = (true == $xoopsUrl) ? XOOPS_URL . $url : $url;
114 if (!in_array($libUrl, $this->_mStylesheet, true)) {
115 $this->_mStylesheet[] = $libUrl;
116 }
117 }
118
127 public function addScript( string $script, bool $isOnloadFunction=true )
128 {
129 if (true == $isOnloadFunction) {
130 $this->_mOnloadScript[] = $script;
131 } else {
132 $this->_mScript[] = $script;
133 }
134 }
135
143 public function getLibraryArr()
144 {
145 return $this->_mLibrary;
146 }
147
155 public function getScriptArr( bool $isOnloadFunction=true )
156 {
157 if (true == $isOnloadFunction) {
158 return $this->_mOnloadScript;
159 } else {
160 return $this->_mScript;
161 }
162 }
163
174 public function addLink( string $rel, string $href, string $type, string $title=null )
175 {
176 $this->_mLink[] = ['rel' =>$rel, 'type' =>$type, 'title' =>$title, 'href' =>$href];
177 }
178
187 public function addMeta( string $name, string $content )
188 {
189 $this->_mMeta[$name] = $content;
190 }
191
199 public function getMeta( string $name )
200 {
201 return $this->_mMeta[$name];
202 }
203
211 public function createLibraryTag()
212 {
213 $html = '';
214
215 //prototype.js compatibility
216 if ($this->mUsePrototype) {
217 $html .= '<script src="'. $this->mPrototypeUrl .'"></script>';
218 }
219
220 // load main library
221 if ('google' == $this->_mType) {
222 $html .= $this->_loadGoogleJQueryLibrary();
223 } elseif ('local' == $this->_mType) {
224 $html .= $this->_loadLocalJQueryLibrary();
225 }
226
227 // load plugin libraries
228 // Preload Since XCL 2.3.x
229 foreach ($this->_mLibrary as $lib) {
230 //$html .= '<link rel="preload" as="script" src="' . $lib . "\">\n";
231 $html .= "<link rel=\"preload\" as=\"script\" href=" . $lib . " onload=\"this.onload=null;this.rel='script'\">\n";
232 $html .= "<noscript><link rel=\"stylesheet\" href=" . $lib . "></noscript>\n";
233
234 $html .= '<script defer src="' . $lib . "\"></script>\n";
235 }
236
237 // load css
238 // Preload Since XCL 2.3.x
239 foreach ($this->_mStylesheet as $css) {
240 $html .= "<link rel=\"preload\" as=\"style\" href=" . $css . " onload=\"this.onload=null;this.rel='stylesheet'\">\n";
241 $html .= "<noscript><link rel=\"stylesheet\" href=" . $css . "></noscript>\n";
242 //<noscript><link rel=\"stylesheet\" href=" . $css . "></noscript><!-- test -->\n";
243 }
244
245 // load link
246 // Preload Since XCL 2.3.x
247 foreach ($this->_mLink as $link) {
248 $title = $link['title'] ? 'title="'.$link['title'].'" ' : null;
249 $html .= sprintf("<link rel=\"preload\" as=\"%s\" onload=\"this.onload=null;this.rel=\'%s'\" href=\"%s\" $title>\n", $link['type'], $link['rel'], $link['href']);
250 }
251
252 //set rss auto-discovery
253 if ($this->_getRenderConfig('feed_url')) {
254 $html .= sprintf('<link rel="alternate" type="application/rss+xml" title="rss" href="%s" />'."\n", $this->_getRenderConfig('feed_url'));
255 }
256 return $html;
257 }
258
266 protected function _loadGoogleJQueryLibrary()
267 {
268 $apiKey = XCube_Root::getSingleton()->getSiteConfig('jQuery', 'GoogleApiKey');
269 $apiKey = (isset($apiKey)) ? '?key='.$apiKey : null;
270 return '<script src="//www.google.com/jsapi'.$apiKey.'" crossorigin="anonymous"></script>
271<script crossorigin="anonymous">
272google.load("language", "1");
273google.load("jquery", "'. $this->_mCore .'");
274google.load("jqueryui", "'. $this->_mUi .'");
275</script>
276';
277 }
278
286 protected function _loadLocalJQueryLibrary()
287 {
288 $html = '';
289 if ($this->_mCore) {
290 $html .= '<script src="'. $this->_mCore .'"></script>';
291 }
292 if ($this->_mUi) {
293 $html .= '<script defer src="'. $this->_mUi .'"></script>';
294 }
295
296 return $html;
297 }
298
306 public function createOnloadFunctionTag()
307 {
308 $html = null;
309 if (count($this->_mOnloadScript)>0||count($this->_mScript)>0) {
310 $html = "<script crossorigin=\"anonymous\">\n";
311 if ('google' == $this->_mType) {
312 $html .= "google.setOnLoadCallback(function() {\n";
313 }
314 if (true == $this->mUsePrototype) {
315 $html .= "jQuery.noConflict();\n";
316 }
317 $html .= "jQuery(function($){\n";
318 $html .= $this->_makeScript(true);
319 if ('google' == $this->_mType) {
320 $html .= "\n});\n";
321 }
322 $html .= "\n});\n";
323 $html .= $this->_makeScript(false);
324 $html .= '</script>' . "\n";
325 }
326 return $html;
327 }
328
336 protected function _makeScript(bool $isOnloadFunction=true)
337 {
338 $html = null;
339 $scriptArr = (true === $isOnloadFunction) ? $this->_mOnloadScript : $this->_mScript;
340 foreach ($scriptArr as $script) {
341 $html .= $this->_convertFuncName($script);
342 }
343 return $html;
344 }
345
353 protected function _convertFuncName(string $script)
354 {
355 if ($this->mFuncNamePrefix) {
356 $script = str_replace('$(', $this->mFuncNamePrefix . '$(', $script);
357 }
358 return $script;
359 }
360
368 protected function _getRenderConfig(string $key)
369 {
370 $handler =& xoops_gethandler('config');
371 $configArr =& $handler->getConfigsByDirname('legacyRender');
372
373 return $configArr[$key] ?? '';
374 }
375}
addLibrary(string $url, bool $xoopsUrl=true)
_makeScript(bool $isOnloadFunction=true)
addMeta(string $name, string $content)
getScriptArr(bool $isOnloadFunction=true)
addScript(string $script, bool $isOnloadFunction=true)
addStylesheet(string $url, bool $xoopsUrl=true)
addLink(string $rel, string $href, string $type, string $title=null)