/libraries/joomla/microdata/microdata.php

https://github.com/pjwiseman/joomla-cms · PHP · 927 lines · 436 code · 106 blank · 385 comment · 57 complexity · 6a286c6564cfda5a3a89c510224f0bf8 MD5 · raw file

  1. <?php
  2. /**
  3. * @package Joomla.Platform
  4. * @subpackage Microdata
  5. *
  6. * @copyright Copyright (C) 2005 - 2015 Open Source Matters, Inc. All rights reserved.
  7. * @license GNU General Public License version 2 or later; see LICENSE
  8. */
  9. defined('JPATH_PLATFORM') or die;
  10. /**
  11. * Joomla Platform class for interacting with Microdata semantics.
  12. *
  13. * @since 3.2
  14. */
  15. class JMicrodata
  16. {
  17. /**
  18. * Array with all available Types and Properties
  19. *
  20. * @var array
  21. * @since 3.2
  22. */
  23. protected static $types = null;
  24. /**
  25. * The Schema.org Type
  26. *
  27. * @var string
  28. * @since 3.2
  29. */
  30. protected $type = null;
  31. /**
  32. * The Property
  33. *
  34. * @var string
  35. * @since 3.2
  36. */
  37. protected $property = null;
  38. /**
  39. * The Human value or Machine value
  40. *
  41. * @var string
  42. * @since 3.2
  43. */
  44. protected $content = null;
  45. /**
  46. * The Machine value
  47. *
  48. * @var string
  49. * @since 3.2
  50. */
  51. protected $machineContent = null;
  52. /**
  53. * Fallback Type
  54. *
  55. * @var string
  56. * @since 3.2
  57. */
  58. protected $fallbackType = null;
  59. /**
  60. * Fallback Property
  61. *
  62. * @var string
  63. * @since 3.2
  64. */
  65. protected $fallbackProperty = null;
  66. /**
  67. * Used to check if a Fallback must be used
  68. *
  69. * @var string
  70. * @since 3.2
  71. */
  72. protected $fallback = false;
  73. /**
  74. * Used to check if the Microdata semantics output are enabled or disabled
  75. *
  76. * @var boolean
  77. * @since 3.2
  78. */
  79. protected $enabled = true;
  80. /**
  81. * Initialize the class and setup the default Type
  82. *
  83. * @param string $type Optional, Fallback to Thing Type
  84. * @param boolean $flag Enable or disable microdata output
  85. *
  86. * @since 3.2
  87. */
  88. public function __construct($type = '', $flag = true)
  89. {
  90. if ($this->enabled = (boolean) $flag)
  91. {
  92. // Fallback to Thing Type
  93. if (!$type)
  94. {
  95. $type = 'Thing';
  96. }
  97. $this->setType($type);
  98. }
  99. }
  100. /**
  101. * Load all Types and Properties from the types.json file
  102. *
  103. * @return void
  104. *
  105. * @since 3.2
  106. */
  107. protected static function loadTypes()
  108. {
  109. // Load the JSON
  110. if (!static::$types)
  111. {
  112. $path = JPATH_PLATFORM . '/joomla/microdata/types.json';
  113. static::$types = json_decode(file_get_contents($path), true);
  114. }
  115. }
  116. /**
  117. * Reset all params
  118. *
  119. * @return void
  120. *
  121. * @since 3.2
  122. */
  123. protected function resetParams()
  124. {
  125. $this->content = null;
  126. $this->machineContent = null;
  127. $this->property = null;
  128. $this->fallbackProperty = null;
  129. $this->fallbackType = null;
  130. $this->fallback = false;
  131. }
  132. /**
  133. * Enable or Disable Microdata semantics output
  134. *
  135. * @param boolean $flag Enable or disable microdata output
  136. *
  137. * @return JMicrodata Instance of $this
  138. *
  139. * @since 3.2
  140. */
  141. public function enable($flag = true)
  142. {
  143. $this->enabled = (boolean) $flag;
  144. return $this;
  145. }
  146. /**
  147. * Return true if Microdata semantics output are enabled
  148. *
  149. * @return boolean
  150. *
  151. * @since 3.2
  152. */
  153. public function isEnabled()
  154. {
  155. return $this->enabled;
  156. }
  157. /**
  158. * Set a new Schema.org Type
  159. *
  160. * @param string $type The Type to be setup
  161. *
  162. * @return JMicrodata Instance of $this
  163. *
  164. * @since 3.2
  165. */
  166. public function setType($type)
  167. {
  168. if (!$this->enabled)
  169. {
  170. return $this;
  171. }
  172. // Sanitize the Type
  173. $this->type = static::sanitizeType($type);
  174. // If the given Type isn't available, fallback to Thing
  175. if (!static::isTypeAvailable($this->type))
  176. {
  177. $this->type = 'Thing';
  178. }
  179. return $this;
  180. }
  181. /**
  182. * Return the current Type name
  183. *
  184. * @return string
  185. *
  186. * @since 3.2
  187. */
  188. public function getType()
  189. {
  190. return $this->type;
  191. }
  192. /**
  193. * Setup a Property
  194. *
  195. * @param string $property The Property
  196. *
  197. * @return JMicrodata Instance of $this
  198. *
  199. * @since 3.2
  200. */
  201. public function property($property)
  202. {
  203. if (!$this->enabled)
  204. {
  205. return $this;
  206. }
  207. // Sanitize the Property
  208. $property = static::sanitizeProperty($property);
  209. // Control if the Property exist in the given Type and setup it, if not leave NULL
  210. if (static::isPropertyInType($this->type, $property))
  211. {
  212. $this->property = $property;
  213. }
  214. else
  215. {
  216. $this->fallback = true;
  217. }
  218. return $this;
  219. }
  220. /**
  221. * Return the property variable
  222. *
  223. * @return string
  224. *
  225. * @since 3.2
  226. */
  227. public function getProperty()
  228. {
  229. return $this->property;
  230. }
  231. /**
  232. * Setup a Text value or Content value for the Microdata
  233. *
  234. * @param string $value The human value or marchine value to be used
  235. * @param string $machineValue The machine value
  236. *
  237. * @return JMicrodata Instance of $this
  238. *
  239. * @since 3.2
  240. */
  241. public function content($value, $machineValue = null)
  242. {
  243. $this->content = $value;
  244. $this->machineContent = $machineValue;
  245. return $this;
  246. }
  247. /**
  248. * Return the content variable
  249. *
  250. * @return string
  251. *
  252. * @since 3.2
  253. */
  254. public function getContent()
  255. {
  256. return $this->content;
  257. }
  258. /**
  259. * Setup a Fallback Type and Property
  260. *
  261. * @param string $type The Fallback Type
  262. * @param string $property The Fallback Property
  263. *
  264. * @return JMicrodata Instance of $this
  265. *
  266. * @since 3.2
  267. */
  268. public function fallback($type, $property)
  269. {
  270. if (!$this->enabled)
  271. {
  272. return $this;
  273. }
  274. // Sanitize the Type
  275. $this->fallbackType = static::sanitizeType($type);
  276. // If the given Type isn't available, fallback to Thing
  277. if (!static::isTypeAvailable($this->fallbackType))
  278. {
  279. $this->fallbackType = 'Thing';
  280. }
  281. // Control if the Property exist in the given Type and setup it, if not leave NULL
  282. if (static::isPropertyInType($this->fallbackType, $property))
  283. {
  284. $this->fallbackProperty = $property;
  285. }
  286. else
  287. {
  288. $this->fallbackProperty = null;
  289. }
  290. return $this;
  291. }
  292. /**
  293. * Return the fallbackType variable
  294. *
  295. * @return string
  296. *
  297. * @since 3.2
  298. */
  299. public function getFallbackType()
  300. {
  301. return $this->fallbackType;
  302. }
  303. /**
  304. * Return the fallbackProperty variable
  305. *
  306. * @return string
  307. *
  308. * @since 3.2
  309. */
  310. public function getFallbackProperty()
  311. {
  312. return $this->fallbackProperty;
  313. }
  314. /**
  315. * This function handle the logic of a Microdata intelligent display.
  316. * Check if the Type, Property are available, if not check for a Fallback,
  317. * then reset all params for the next use and
  318. * return the Microdata HTML
  319. *
  320. * @param string $displayType Optional, 'inline', available ['inline'|'span'|'div'|meta]
  321. * @param boolean $emptyOutput Return an empty string if the microdata output is disabled and there is a $content value
  322. *
  323. * @return string
  324. *
  325. * @since 3.2
  326. */
  327. public function display($displayType = '', $emptyOutput = false)
  328. {
  329. // Initialize the HTML to output
  330. $html = ($this->content !== null) ? $this->content : '';
  331. // Control if the Microdata output is enabled, otherwise return the content or empty string
  332. if (!$this->enabled)
  333. {
  334. // Reset params
  335. $this->resetParams();
  336. return ($emptyOutput) ? '' : $html;
  337. }
  338. // If the property is wrong for the current Type check if Fallback available, otherwise return empty HTML
  339. if ($this->property && !$this->fallback)
  340. {
  341. // Process and return the HTML the way the user expects to
  342. if ($displayType)
  343. {
  344. switch ($displayType)
  345. {
  346. case 'span':
  347. $html = static::htmlSpan($html, $this->property);
  348. break;
  349. case 'div':
  350. $html = static::htmlDiv($html, $this->property);
  351. break;
  352. case 'meta':
  353. $html = ($this->machineContent !== null) ? $this->machineContent : $html;
  354. $html = static::htmlMeta($html, $this->property);
  355. break;
  356. default:
  357. // Default $displayType = 'inline'
  358. $html = static::htmlProperty($this->property);
  359. break;
  360. }
  361. }
  362. else
  363. {
  364. /*
  365. * Process and return the HTML in an automatic way,
  366. * with the Property expected Types and display the Microdata in the right way,
  367. * check if the Property is normal, nested or must be rendered in a metadata tag
  368. */
  369. switch (static::getExpectedDisplayType($this->type, $this->property))
  370. {
  371. case 'nested':
  372. // Retrive the expected nested Type of the Property
  373. $nestedType = static::getExpectedTypes($this->type, $this->property);
  374. $nestedProperty = '';
  375. // If there is a Fallback Type then probably it could be the expectedType
  376. if (in_array($this->fallbackType, $nestedType))
  377. {
  378. $nestedType = $this->fallbackType;
  379. if ($this->fallbackProperty)
  380. {
  381. $nestedProperty = $this->fallbackProperty;
  382. }
  383. }
  384. else
  385. {
  386. $nestedType = $nestedType[0];
  387. }
  388. // Check if a Content is available, otherwise Fallback to an 'inline' display type
  389. if ($this->content !== null)
  390. {
  391. if ($nestedProperty)
  392. {
  393. $html = static::htmlSpan(
  394. $this->content,
  395. $nestedProperty
  396. );
  397. }
  398. $html = static::htmlSpan(
  399. $html,
  400. $this->property,
  401. $nestedType,
  402. true
  403. );
  404. }
  405. else
  406. {
  407. $html = static::htmlProperty($this->property) . ' ' . static::htmlScope($nestedType);
  408. if ($nestedProperty)
  409. {
  410. $html .= ' ' . static::htmlProperty($nestedProperty);
  411. }
  412. }
  413. break;
  414. case 'meta':
  415. // Check if the Content value is available, otherwise Fallback to an 'inline' display Type
  416. if ($this->content !== null)
  417. {
  418. $html = ($this->machineContent !== null) ? $this->machineContent : $this->content;
  419. $html = static::htmlMeta($html, $this->property) . $this->content;
  420. }
  421. else
  422. {
  423. $html = static::htmlProperty($this->property);
  424. }
  425. break;
  426. default:
  427. /*
  428. * Default expected display type = 'normal'
  429. * Check if the Content value is available,
  430. * otherwise Fallback to an 'inline' display Type
  431. */
  432. if ($this->content !== null)
  433. {
  434. $html = static::htmlSpan($this->content, $this->property);
  435. }
  436. else
  437. {
  438. $html = static::htmlProperty($this->property);
  439. }
  440. break;
  441. }
  442. }
  443. }
  444. elseif ($this->fallbackProperty)
  445. {
  446. // Process and return the HTML the way the user expects to
  447. if ($displayType)
  448. {
  449. switch ($displayType)
  450. {
  451. case 'span':
  452. $html = static::htmlSpan($html, $this->fallbackProperty, $this->fallbackType);
  453. break;
  454. case 'div':
  455. $html = static::htmlDiv($html, $this->fallbackProperty, $this->fallbackType);
  456. break;
  457. case 'meta':
  458. $html = ($this->machineContent !== null) ? $this->machineContent : $html;
  459. $html = static::htmlMeta($html, $this->fallbackProperty, $this->fallbackType);
  460. break;
  461. default:
  462. // Default $displayType = 'inline'
  463. $html = static::htmlScope($type::scope()) . ' ' . static::htmlProperty($this->fallbackProperty);
  464. break;
  465. }
  466. }
  467. else
  468. {
  469. /*
  470. * Process and return the HTML in an automatic way,
  471. * with the Property expected Types an display the Microdata in the right way,
  472. * check if the Property is nested or must be rendered in a metadata tag
  473. */
  474. switch (static::getExpectedDisplayType($this->fallbackType, $this->fallbackProperty))
  475. {
  476. case 'meta':
  477. // Check if the Content value is available, otherwise Fallback to an 'inline' display Type
  478. if ($this->content !== null)
  479. {
  480. $html = ($this->machineContent !== null) ? $this->machineContent : $this->content;
  481. $html = static::htmlMeta($html, $this->fallbackProperty, $this->fallbackType);
  482. }
  483. else
  484. {
  485. $html = static::htmlScope($this->fallbackType) . ' ' . static::htmlProperty($this->fallbackProperty);
  486. }
  487. break;
  488. default:
  489. /*
  490. * Default expected display type = 'normal'
  491. * Check if the Content value is available,
  492. * otherwise Fallback to an 'inline' display Type
  493. */
  494. if ($this->content !== null)
  495. {
  496. $html = static::htmlSpan($this->content, $this->fallbackProperty);
  497. $html = static::htmlSpan($html, '', $this->fallbackType);
  498. }
  499. else
  500. {
  501. $html = static::htmlScope($this->fallbackType) . ' ' . static::htmlProperty($this->fallbackProperty);
  502. }
  503. break;
  504. }
  505. }
  506. }
  507. elseif (!$this->fallbackProperty && $this->fallbackType !== null)
  508. {
  509. $html = static::htmlScope($this->fallbackType);
  510. }
  511. // Reset params
  512. $this->resetParams();
  513. return $html;
  514. }
  515. /**
  516. * Return the HTML of the current Scope
  517. *
  518. * @return string
  519. *
  520. * @since 3.2
  521. */
  522. public function displayScope()
  523. {
  524. // Control if the Microdata output is enabled, otherwise return the content or empty string
  525. if (!$this->enabled)
  526. {
  527. return '';
  528. }
  529. return static::htmlScope($this->type);
  530. }
  531. /**
  532. * Return the sanitized Type
  533. *
  534. * @param string $type The Type to sanitize
  535. *
  536. * @return string
  537. *
  538. * @since 3.2
  539. */
  540. public static function sanitizeType($type)
  541. {
  542. return ucfirst(trim($type));
  543. }
  544. /**
  545. * Return the sanitized Property
  546. *
  547. * @param string $property The Property to sanitize
  548. *
  549. * @return string
  550. *
  551. * @since 3.2
  552. */
  553. public static function sanitizeProperty($property)
  554. {
  555. return lcfirst(trim($property));
  556. }
  557. /**
  558. * Return an array with all Types and Properties
  559. *
  560. * @return array
  561. *
  562. * @since 3.2
  563. */
  564. public static function getTypes()
  565. {
  566. static::loadTypes();
  567. return static::$types;
  568. }
  569. /**
  570. * Return an array with all available Types
  571. *
  572. * @return array
  573. *
  574. * @since 3.2
  575. */
  576. public static function getAvailableTypes()
  577. {
  578. static::loadTypes();
  579. return array_keys(static::$types);
  580. }
  581. /**
  582. * Return the expected types of the Property
  583. *
  584. * @param string $type The Type to process
  585. * @param string $property The Property to process
  586. *
  587. * @return array
  588. *
  589. * @since 3.2
  590. */
  591. public static function getExpectedTypes($type, $property)
  592. {
  593. static::loadTypes();
  594. $tmp = static::$types[$type]['properties'];
  595. // Check if the Property is in the Type
  596. if (isset($tmp[$property]))
  597. {
  598. return $tmp[$property]['expectedTypes'];
  599. }
  600. // Check if the Property is inherit
  601. $extendedType = static::$types[$type]['extends'];
  602. // Recursive
  603. if (!empty($extendedType))
  604. {
  605. return static::getExpectedTypes($extendedType, $property);
  606. }
  607. return array();
  608. }
  609. /**
  610. * Return the expected display type of the [normal|nested|meta]
  611. * In wich way to display the Property:
  612. * normal -> itemprop="name"
  613. * nested -> itemprop="director" itemscope itemtype="https://schema.org/Person"
  614. * meta -> <meta itemprop="datePublished" content="1991-05-01">
  615. *
  616. * @param string $type The Type where to find the Property
  617. * @param string $property The Property to process
  618. *
  619. * @return string
  620. *
  621. * @since 3.2
  622. */
  623. protected static function getExpectedDisplayType($type, $property)
  624. {
  625. $expectedTypes = static::getExpectedTypes($type, $property);
  626. // Retrieve the first expected type
  627. $type = $expectedTypes[0];
  628. // Check if it's a meta display
  629. if ($type === 'Date' || $type === 'DateTime' || $property === 'interactionCount')
  630. {
  631. return 'meta';
  632. }
  633. // Check if it's a normal display
  634. if ($type === 'Text' || $type === 'URL' || $type === 'Boolean' || $type === 'Number')
  635. {
  636. return 'normal';
  637. }
  638. // Otherwise it's a nested display
  639. return 'nested';
  640. }
  641. /**
  642. * Recursive function, control if the given Type has the given Property
  643. *
  644. * @param string $type The Type where to check
  645. * @param string $property The Property to check
  646. *
  647. * @return boolean
  648. *
  649. * @since 3.2
  650. */
  651. public static function isPropertyInType($type, $property)
  652. {
  653. if (!static::isTypeAvailable($type))
  654. {
  655. return false;
  656. }
  657. // Control if the Property exists, and return true
  658. if (array_key_exists($property, static::$types[$type]['properties']))
  659. {
  660. return true;
  661. }
  662. // Recursive: Check if the Property is inherit
  663. $extendedType = static::$types[$type]['extends'];
  664. if (!empty($extendedType))
  665. {
  666. return static::isPropertyInType($extendedType, $property);
  667. }
  668. return false;
  669. }
  670. /**
  671. * Control if the given Type class is available
  672. *
  673. * @param string $type The Type to check
  674. *
  675. * @return boolean
  676. *
  677. * @since 3.2
  678. */
  679. public static function isTypeAvailable($type)
  680. {
  681. static::loadTypes();
  682. return (array_key_exists($type, static::$types)) ? true : false;
  683. }
  684. /**
  685. * Return the microdata in a <meta> tag with content for machines.
  686. *
  687. * @param string $content The machine content to display
  688. * @param string $property The Property
  689. * @param string $scope Optional, the Type scope to display
  690. * @param boolean $inverse Optional, default = false, inverse the $scope with the $property
  691. *
  692. * @return string
  693. *
  694. * @since 3.2
  695. */
  696. public static function htmlMeta($content, $property, $scope = '', $inverse = false)
  697. {
  698. // Control if the Property has allready the itemprop
  699. if (stripos($property, 'itemprop') !== 0)
  700. {
  701. $property = static::htmlProperty($property);
  702. }
  703. // Control if the Scope have allready the itemtype
  704. if (!empty($scope) && stripos($scope, 'itemscope') !== 0)
  705. {
  706. $scope = static::htmlScope($scope);
  707. }
  708. if ($inverse)
  709. {
  710. $tmp = join(' ', array($property, $scope));
  711. }
  712. else
  713. {
  714. $tmp = join(' ', array($scope, $property));
  715. }
  716. $tmp = trim($tmp);
  717. return "<meta $tmp content='$content'/>";
  718. }
  719. /**
  720. * Return the microdata in a <span> tag.
  721. *
  722. * @param string $content The human value
  723. * @param string $property Optional, the human value to display
  724. * @param string $scope Optional, the Type scope to display
  725. * @param boolean $inverse Optional, default = false, inverse the $scope with the $property
  726. *
  727. * @return string
  728. *
  729. * @since 3.2
  730. */
  731. public static function htmlSpan($content, $property = '', $scope = '', $inverse = false)
  732. {
  733. // Control if the Property has allready the itemprop
  734. if (!empty($property) && stripos($property, 'itemprop') !== 0)
  735. {
  736. $property = static::htmlProperty($property);
  737. }
  738. // Control if the Scope have allready the itemtype
  739. if (!empty($scope) && stripos($scope, 'itemscope') !== 0)
  740. {
  741. $scope = static::htmlScope($scope);
  742. }
  743. if ($inverse)
  744. {
  745. $tmp = join(' ', array($property, $scope));
  746. }
  747. else
  748. {
  749. $tmp = join(' ', array($scope, $property));
  750. }
  751. $tmp = trim($tmp);
  752. $tmp = ($tmp) ? ' ' . $tmp : '';
  753. return "<span$tmp>$content</span>";
  754. }
  755. /**
  756. * Return the microdata in an <div> tag.
  757. *
  758. * @param string $content The human value
  759. * @param string $property Optional, the human value to display
  760. * @param string $scope Optional, the Type scope to display
  761. * @param boolean $inverse Optional, default = false, inverse the $scope with the $property
  762. *
  763. * @return string
  764. *
  765. * @since 3.2
  766. */
  767. public static function htmlDiv($content, $property = '', $scope = '', $inverse = false)
  768. {
  769. // Control if the Property has allready the itemprop
  770. if (!empty($property) && stripos($property, 'itemprop') !== 0)
  771. {
  772. $property = static::htmlProperty($property);
  773. }
  774. // Control if the Scope have allready the itemtype
  775. if (!empty($scope) && stripos($scope, 'itemscope') !== 0)
  776. {
  777. $scope = static::htmlScope($scope);
  778. }
  779. if ($inverse)
  780. {
  781. $tmp = join(' ', array($property, $scope));
  782. }
  783. else
  784. {
  785. $tmp = join(' ', array($scope, $property));
  786. }
  787. $tmp = trim($tmp);
  788. $tmp = ($tmp) ? ' ' . $tmp : '';
  789. return "<div$tmp>$content</div>";
  790. }
  791. /**
  792. * Return the HTML Scope
  793. *
  794. * @param string $scope The Scope to process
  795. *
  796. * @return string
  797. *
  798. * @since 3.2
  799. */
  800. public static function htmlScope($scope)
  801. {
  802. if (stripos($scope, 'http') !== 0)
  803. {
  804. $scope = 'https://schema.org/' . ucfirst($scope);
  805. }
  806. return "itemscope itemtype='$scope'";
  807. }
  808. /**
  809. * Return the HTML Property
  810. *
  811. * @param string $property The Property to process
  812. *
  813. * @return string
  814. *
  815. * @since 3.2
  816. */
  817. public static function htmlProperty($property)
  818. {
  819. return "itemprop='$property'";
  820. }
  821. }