SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/SocialMeta.php

  1. <?php
  2. /*
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  */
  13.  
  14. namespace Sprout\Helpers;
  15.  
  16. use DateTime;
  17.  
  18. use Kohana;
  19.  
  20.  
  21. /**
  22.  * Management of social meta data tags, such as Facebook OpenGraph and Twitter Cards
  23.  */
  24. class SocialMeta
  25. {
  26. /**
  27.   * Tags of the form <meta property=":key" content=":val">
  28.   */
  29. private static $meta_property = [];
  30.  
  31. /**
  32.   * Tags of the form <meta name=":key" content=":val">
  33.   */
  34. private static $meta_name = [];
  35.  
  36.  
  37. /**
  38.   * Set a title for this page. Don't include the site name
  39.   *
  40.   * @param string $val
  41.   */
  42. public static function setTitle($val)
  43. {
  44. self::$meta_property['og:title'] = $val;
  45. }
  46.  
  47.  
  48. /**
  49.   * Has a page title been set?
  50.   *
  51.   * @return bool True if a title has been set
  52.   */
  53. public static function hasTitle()
  54. {
  55. return !empty(self::$meta_property['og:title']);
  56. }
  57.  
  58.  
  59. /**
  60.   * Set the image for this page - should be unique to this page; not shared across the site
  61.   *
  62.   * @param string $url Relative or absolute url
  63.   */
  64. public static function setImage($url)
  65. {
  66. if (preg_match('!^[0-9]+$!', $url)) {
  67. $url = File::url($url);
  68. }
  69. if (!preg_match('!^https?://!', $url)) {
  70. $url = Sprout::absRoot() . $url;
  71. }
  72. self::$meta_property['og:image'] = $url;
  73. }
  74.  
  75.  
  76. /**
  77.   * Set a short (up to two sentence) description for this page
  78.   *
  79.   * @param string $val
  80.   */
  81. public static function setDescription($val)
  82. {
  83. self::$meta_property['og:description'] = $val;
  84. }
  85.  
  86.  
  87. /**
  88.   * Set the canonical url for this page
  89.   *
  90.   * @param string $url Relative or absolute url
  91.   */
  92. public static function setUrl($url)
  93. {
  94. if (!preg_match('!^https?://!', $url)) {
  95. $url = Sprout::absRoot() . $url;
  96. }
  97. self::$meta_property['og:url'] = $url;
  98. }
  99.  
  100.  
  101. /**
  102.   * The page type, i.e. "og:type" property
  103.   *
  104.   * Current options are:
  105.   * article, book, profile, website, music.song, music.album, music.playlist,
  106.   * music.radio_station, video.movie, video.episode, video.tv_show, video.other
  107.   *
  108.   * @param string $val
  109.   */
  110. public static function setPageType($val)
  111. {
  112. self::$meta_property['og:type'] = $val;
  113. }
  114.  
  115.  
  116. /**
  117.   * The twitter card type, i.e. "twitter:card" property
  118.   *
  119.   * Current options are:
  120.   * summary, summary_large_image, app, player
  121.   * Note that the player card requires approval from Twitter prior to use
  122.   *
  123.   * @param string $val
  124.   */
  125. public static function setTwitterCardType($val)
  126. {
  127. self::$meta_property['twitter:card'] = $val;
  128. }
  129.  
  130.  
  131. /**
  132.   * Set a custom OpenGraph string property
  133.   *
  134.   * @param string Property name, e.g. 'article:section'
  135.   * @param string $value Property value, e.g. 'Technology'
  136.   */
  137. public static function setOpenGraphString($property, $value)
  138. {
  139. self::$meta_property[$property] = trim($value);
  140. }
  141.  
  142.  
  143. /**
  144.   * Set a custom OpenGraph integer property
  145.   *
  146.   * @param string Property name, e.g. 'music:album:track'
  147.   * @param int $value Property value, e.g. 4
  148.   */
  149. public static function setOpenGraphInteger($property, $value)
  150. {
  151. self::$meta_property[$property] = (int)$value;
  152. }
  153.  
  154.  
  155. /**
  156.   * Set a custom OpenGraph date property
  157.   *
  158.   * @param string $property Property name, e.g. 'article:published_time'
  159.   * @param DateTime|string $value A DateTime or anything paresable by the DateTime constructor
  160.   */
  161. public static function setOpenGraphDate($property, $value)
  162. {
  163. if (!($value instanceof DateTime)) {
  164. $value = new DateTime($value);
  165. }
  166. self::$meta_property[$property] = $value->format('c');
  167. }
  168.  
  169.  
  170. /**
  171.   * Auto-generate missing OpenGraph and Twitter cards meta data.
  172.   */
  173. protected static function autoGenerateMissing()
  174. {
  175. if (empty(self::$meta_property['og:url'])) {
  176. self::$meta_property['og:url'] = Sprout::absRoot() . Url::current(true);
  177. }
  178. if (empty(self::$meta_property['og:site_name'])) {
  179. self::$meta_property['og:site_name'] = Kohana::config('sprout.site_title');
  180. }
  181. if (empty(self::$meta_property['og:type'])) {
  182. self::$meta_property['og:type'] = 'website';
  183. }
  184. if (empty(self::$meta_name['twitter:card'])) {
  185. self::$meta_name['twitter:card'] = 'summary';
  186. }
  187. if (empty(self::$meta_name['twitter:site'])) {
  188. $acct = Kohana::config('sprout.site_twitter');
  189. if ($acct) {
  190. self::$meta_name['twitter:site'] = '@' . ltrim($acct, '@');
  191. }
  192. }
  193. }
  194.  
  195.  
  196. /**
  197.   * Render the social metadata
  198.   *
  199.   * @return string HTML
  200.   */
  201. public static function render()
  202. {
  203. static::autoGenerateMissing();
  204. $out = '';
  205. foreach (self::$meta_property as $property => $content) {
  206. $out .= '<meta property="' . Enc::html($property) . '" content="' . Enc::html($content) . '">' . PHP_EOL;
  207. }
  208. foreach (self::$meta_name as $name => $content) {
  209. $out .= '<meta name="' . Enc::html($name) . '" content="' . Enc::html($content) . '">' . PHP_EOL;
  210. }
  211. return $out;
  212. }
  213.  
  214.  
  215. /**
  216.   *
  217.   * @return array [ properties[], names[] ]
  218.   */
  219. public static function get()
  220. {
  221. return [
  222. 'properties' => self::$meta_property,
  223. 'names' => self::$meta_name,
  224. ];
  225. }
  226.  
  227. }
  228.