PluginProbe ʕ •ᴥ•ʔ
VikAppointments Services Booking Calendar / 1.2.21
VikAppointments Services Booking Calendar v1.2.21
1.2.21 1.2.20 trunk 1.2.17 1.2.18 1.2.19
vikappointments / site / helpers / libraries / view / contents.php
vikappointments / site / helpers / libraries / view Last commit date
classes 3 days ago contents.php 3 days ago index.html 3 days ago
contents.php
401 lines
1 <?php
2 /**
3 * @package VikAppointments
4 * @subpackage core
5 * @author E4J s.r.l.
6 * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved.
7 * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
8 * @link https://vikwp.com
9 */
10
11 // No direct access
12 defined('ABSPATH') or die('No script kiddies please!');
13
14 /**
15 * VikAppointments view contents handler.
16 * This class provides helpful tools to enhance the
17 * <head> of the pages.
18 *
19 * @since 1.6
20 */
21 #[\AllowDynamicProperties]
22 class VAPViewContents
23 {
24 /**
25 * A list of instances.
26 *
27 * @var array
28 */
29 protected static $instances = array();
30
31 /**
32 * The view object.
33 *
34 * @var JView
35 */
36 protected $page;
37
38 /**
39 * A registry of params.
40 *
41 * @var JRegistry
42 */
43 protected $params;
44
45 /**
46 * The name of the current view (set in query string).
47 *
48 * @var string
49 */
50 protected $currentView;
51
52 /**
53 * The name of the active page (active menu item).
54 *
55 * @var string
56 */
57 protected $activeView;
58
59 /**
60 * Returns a new instance of this object, only creating it
61 * if it doesn't already exist.
62 *
63 * @param mixed $page The view object.
64 *
65 * @return self A new instance of this object.
66 *
67 * @uses getViewHandler()
68 */
69 public static function getInstance($page)
70 {
71 $sign = get_class($page);
72
73 if (!isset(static::$instances[$sign]))
74 {
75 $obj = static::getViewHandler($sign, $page);
76
77 if (!$obj)
78 {
79 $obj = new static($page);
80 }
81
82 static::$instances[$sign] = $obj;
83 }
84
85 return static::$instances[$sign];
86 }
87
88 /**
89 * Helper method used to detect if a sub-handler
90 * should be used.
91 *
92 * @param string $name The view class name.
93 * @param string $view The view object.
94 *
95 * @return mixed The handler object if exists, otherwise null.
96 */
97 protected static function getViewHandler($name, $view)
98 {
99 if (!preg_match("/VikAppointmentsView(.+)$/i", $name, $match))
100 {
101 // the view is not part of VikAppointments
102 return null;
103 }
104
105 $file = strtolower($match[1]);
106 $class = 'VAPViewContents' . ucwords($match[1]);
107
108 if (!VAPLoader::import('libraries.view.classes.' . $file))
109 {
110 // the view is not supported
111 return null;
112 }
113
114 $handler = new $class($view, $file);
115
116 if (!$handler instanceof VAPViewContents)
117 {
118 // the handler doesn't inherit the default class
119 return null;
120 }
121
122 return $handler;
123 }
124
125 /**
126 * Class constructor.
127 *
128 * @param mixed $page The view object.
129 * @param string $type The view file/type. If not provided
130 * it will be calculated from the $page class name.
131 */
132 public function __construct($page, $type = null)
133 {
134 $this->page = $page;
135
136 $menu = JFactory::getApplication()->getMenu()->getActive();
137 $params = null;
138
139 /**
140 * Workaround for issue related to MenuItem object.
141 *
142 * @since 3.7.0
143 */
144 if (isset($menu->params))
145 {
146 $this->params = $menu->params;
147 }
148 else if ($menu && method_exists($menu, 'getParams'))
149 {
150 $this->params = $menu->getParams();
151 }
152 else
153 {
154 $this->params = new JRegistry;
155 }
156
157 if ($type)
158 {
159 $this->currentView = $type;
160 }
161 else if (preg_match("/View(.+)$/i", get_class($page), $match))
162 {
163 $this->currentView = strtolower($match[1]);
164 }
165
166 if (isset($menu->query) && isset($menu->query['view']))
167 {
168 $this->activeView = $menu->query['view'];
169 }
170 }
171
172 /**
173 * Magic method to access protected properties.
174 *
175 * @param string $name The property name.
176 *
177 * @return mixed The property value.
178 */
179 public function __get($name)
180 {
181 // do not check if the property exists as it should
182 // be raised a warning in case of missing property
183 return $this->{$name};
184 }
185
186 /**
187 * Returns the HTML used to render the page heading.
188 *
189 * @param boolean $echo True to display the HTML, false to return it.
190 *
191 * @return string The heading HTML.
192 */
193 public function getPageHeading($echo = false)
194 {
195 $html = '';
196
197 if ((int) $this->params->get('show_page_heading') == 1 && $this->params->get('page_heading'))
198 {
199 $data = array(
200 'suffix' => $this->params->get('pageclass_sfx', ''),
201 'title' => $this->params->get('page_heading'),
202 );
203
204 $html = JLayoutHelper::render('document.heading', $data);
205 }
206
207 if (!$echo)
208 {
209 return $html;
210 }
211
212 echo $html;
213 }
214
215 /**
216 * Sets the browser page title.
217 *
218 * @return boolean True if set, otherwise false.
219 *
220 * @since 1.6.1
221 */
222 public function setPageTitle()
223 {
224 // do nothing on base class
225
226 return false;
227 }
228
229 /**
230 * Sets the meta description according to the settings of the page.
231 *
232 * @return boolean True if set, otherwise false.
233 */
234 public function setMetaDescription()
235 {
236 $desc = $this->params->get('menu-meta_description');
237
238 if ($desc)
239 {
240 $this->page->document->setDescription($desc);
241
242 return true;
243 }
244
245 return false;
246 }
247
248 /**
249 * Sets the meta keywords according to the settings of the page.
250 *
251 * @return self This object to support chaining.
252 *
253 * @return boolean True if set, otherwise false.
254 */
255 public function setMetaKeywords()
256 {
257 return $this->setMetaData('keywords', 'menu-meta_keywords');
258 }
259
260 /**
261 * Sets the meta robots according to the settings of the page.
262 *
263 * @return self This object to support chaining.
264 *
265 * @return boolean True if set, otherwise false.
266 */
267 public function setMetaRobots()
268 {
269 if ($this->setMetaData('robots', 'robots'))
270 {
271 return true;
272 }
273
274 $app = JFactory::getApplication();
275
276 // fallback to include global robots
277 if ($robot = $app->get('robots'))
278 {
279 $this->page->document->setMetaData('robots', $app->get('robots'));
280
281 return true;
282 }
283
284 return false;
285 }
286
287 /**
288 * Creates the OPEN GRAPH protocol according to the
289 * entity of the current page.
290 *
291 * @return self This object to support chaining.
292 */
293 public function buildOpenGraph()
294 {
295 $doc = $this->page->document;
296
297 // generic metadata
298 $locale = str_replace('-', '_', JFactory::getLanguage()->getTag());
299 $doc->setMetaData('og:locale', $locale);
300
301 foreach (VikAppointments::getKnownLanguages() as $tag)
302 {
303 $tag = str_replace('-', '_', $tag);
304
305 if ($tag != $locale)
306 {
307 $doc->setMetaData('og:locale:alternate', $tag);
308 }
309 }
310
311 $doc->setMetaData('og:site_name', JFactory::getApplication()->get('sitename'));
312
313 /**
314 * Trigger event to allow the plugins to add meta data according to
315 * OPEN GRAPH protocol.
316 *
317 * @param JDocument $doc The application document object.
318 * @param VAPViewContents $this The view content handler.
319 *
320 * @return void
321 *
322 * @since 1.6
323 */
324 VAPFactory::getEventDispatcher()->trigger('onBuildOpenGraph', array($doc, $this));
325
326 return $this;
327 }
328
329 /**
330 * Builds mircodata information using the application/ld+json format.
331 *
332 * @return void
333 *
334 * @uses getMicrodataObject()
335 */
336 public function buildMicrodata()
337 {
338 // init empty object
339 $json = new stdClass;
340
341 // fill microdata object
342 $res = $this->getMicrodataObject($json);
343
344 /**
345 * Trigger event to allow the plugins to manipulate the JSON object
346 * that will be used by search engines.
347 *
348 * @param object &$json The JSON object.
349 * @param boolean &$res True whether the JSON object should be included into the document.
350 * @param VAPViewContents $handler The view content handler.
351 *
352 * @return void
353 *
354 * @since 1.6
355 */
356 VAPFactory::getEventDispatcher()->trigger('onBeforeAddMicrodata', array(&$json, &$res, $this));
357
358 if ($res)
359 {
360 // attach only in case of successful response
361 $this->page->document->addScriptDeclaration(json_encode($json), 'application/ld+json');
362 }
363 }
364
365 /**
366 * Returns the microdata object to include within the head of the page.
367 * Inherits this method to dispatch the attachment of the properties to
368 * children classes.
369 *
370 * @param object &$json The root object used to attach microdata.
371 *
372 * @return boolean True in case the object should be attached, otherwise false.
373 */
374 protected function getMicrodataObject(&$json)
375 {
376 return false;
377 }
378
379 /**
380 * Sets a generic meta data according to the settings of the page.
381 *
382 * @param string $key The meta key.
383 * @param string $param The parameter name.
384 *
385 * @return boolean True if set, otherwise false.
386 */
387 protected function setMetaData($key, $param)
388 {
389 $value = $this->params->get($param);
390
391 if ($value)
392 {
393 $this->page->document->setMetaData($key, $value);
394
395 return true;
396 }
397
398 return false;
399 }
400 }
401