system/modules/pct_seo_helper/PCT/SEO.php line 181

Open in your IDE?
  1. <?php
  2. /**
  3.  * Contao Open Source CMS
  4.  *
  5.  * Copyright (c) 2005-2019 Leo Feyer
  6.  *
  7.  * @copyright    Tim Gatzky 2019, Premium-Contao-Themes
  8.  * @author        Tim Gatzky <info@tim-gatzky.de>
  9.  * @package     pct_seo_helper
  10.  * @link        https://contao.org, https://www.premium-contao-themes.com
  11.  * @license     http://www.gnu.org/licenses/lgpl-3.0.html LGPL
  12.  */
  13. /**
  14.  * Namespace
  15.  */
  16. namespace PCT;
  17. /**
  18.  * Imports
  19.  */
  20. use Contao\System;
  21. use Contao\Environment;
  22. use Contao\UserModel;
  23. use Contao\NewsModel;
  24. use Contao\CalendarEventsModel;
  25. /**
  26.  * Class file
  27.  * SEO
  28.  */
  29. class SEO
  30. {
  31.     /**
  32.      * Return the seo protocol depending on system settings
  33.      * @return string
  34.      */
  35.     public static function getProtocol()
  36.     {
  37.         if( empty(\Contao\Config::get('pct_seo_protocol')) === true)
  38.         {
  39.             return '';
  40.         }
  41.         
  42.         $strReturn strtolower\Contao\Config::get('pct_seo_protocol') );
  43.         // by SERVER
  44.         if( \Contao\Config::get('pct_seo_protocol') == 'auto' )
  45.         {
  46.             $strReturn str_replace(array('http','https','/'),'',strtolower$_SERVER['SERVER_PROTOCOL'] ));
  47.             $strReturn 'http'.explode('.',$strReturn)[0];
  48.         }
  49.         return $strReturn;
  50.     }
  51.     /**
  52.      * Generate a google json ld string for structured data depending on an incoming template||stdclass object
  53.      * @param object
  54.      * @param array
  55.      * @return string JSON
  56.      */
  57.     public static function writeJSON($objTemplate$arrCustom=array())
  58.     {
  59.         $strTemplate $objTemplate->strTemplate;
  60.         
  61.         if( \method_exists($objTemplate,'getName') )
  62.         {
  63.             $strTemplate $objTemplate->getName();
  64.         }
  65.         
  66.         $tmp \explode('_',$strTemplate);
  67.         switch($tmp[0])
  68.         {
  69.             // Events
  70.             case 'event':
  71.                 $arrJSON = static::getJsonByEvents($objTemplate);
  72.                 break;
  73.             // News
  74.             case 'news':
  75.                 $arrJSON = static::getJsonByNews($objTemplate);
  76.                 break;
  77.             default: 
  78.                 break;
  79.         }
  80.         unset($tmp);
  81.         // custom data
  82.         if( empty($arrCustom) === false && \is_array($arrCustom) )
  83.         {
  84.             $arrJSON \array_merge($arrJSON$arrCustom);
  85.         }
  86.         $strJSON \json_encode($arrJSON,JSON_UNESCAPED_SLASHES);
  87.         // HOOK
  88.         if (empty($GLOBALS['SEO']['writeJSON']) === false && \is_array($GLOBALS['SEO']['writeJSON']) === true )
  89.         {
  90.             foreach($GLOBALS['SEO']['writeJSON'] as $callback)
  91.             {
  92.                 $strJSON \Contao\System::importStatic($callback[0])->{$callback[1]}($objTemplate,$arrJSON);
  93.             }
  94.         }
  95.     
  96.         // add json to page
  97.         $GLOBALS['TL_HEAD'][] = '<script type="application/ld+json">'$strJSON '</script>';
  98.     }
  99.     /**
  100.      * Prepare a google json structured data array based on an events template object
  101.      * @param object
  102.      * @param array
  103.      * @return string JSON
  104.      */
  105.     protected static function getJsonByEvents($objTemplate)
  106.     {
  107.         if( empty($objTemplate) === true )
  108.         {
  109.             return '';
  110.         }
  111.         // @var object
  112.         $objRow CalendarEventsModel::findByPk($objTemplate->id);
  113.         // @var object
  114.         $objAuthor UserModel::findByPk($objRow->author);
  115.         
  116.         $intStart $objRow->startDate;
  117.         $intStop $objRow->endDate;
  118.         if( (boolean)$objRow->addTime === true )
  119.         {
  120.             $intStart $objRow->startTime;
  121.             $intStop $objRow->endTime;
  122.         }
  123.         
  124.         $arrReturn = array
  125.         (
  126.             '@context'     => 'https://schema.org',
  127.             '@type'     => 'Event',
  128.             'mainEntityOfPage' => array
  129.             (
  130.                 '@type' => 'WebPage',
  131.                 '@id'     => Environment::get('base').Environment::get('request'),
  132.             ),
  133.             'name' => \strip_tags$objTemplate->title ),
  134.             'description' => \strip_tags($objTemplate->teaser),
  135.             'startDate' => System::parseDate('c',$intStart),
  136.             'endDate' => System::parseDate('c',$intStop),
  137.             'author' => array
  138.             (
  139.                 "@type" => "Person",
  140.                 "name" => $objAuthor->name,
  141.             ),
  142.             'location' => array
  143.             (
  144.                 '@type' => 'Place',
  145.                 'name' => $objTemplate->location,
  146.             ),
  147.         );
  148.         // image
  149.         if( $objTemplate->addImage )
  150.         {
  151.             $arrReturn['image'] = \Environment::get('base').$objTemplate->singleSRC;
  152.         }
  153.         
  154.         return $arrReturn;
  155.     }
  156.     /**
  157.      * Prepare a google json structured data array based on a news template object
  158.      * @param object
  159.      * @param array
  160.      * @return array
  161.      */
  162.     protected static function getJsonByNews($objTemplate)
  163.     {
  164.         if( empty($objTemplate) === true )
  165.         {
  166.             return '';
  167.         }
  168.         // @var object
  169.         $objRow NewsModel::findByPk($objTemplate->id);
  170.         // @var object
  171.         $objAuthor UserModel::findByPk($objRow->author);
  172.         $arrReturn = array
  173.         (
  174.             '@context'     => 'https://schema.org',
  175.             '@type'     => 'NewsArticle',
  176.             'mainEntityOfPage' => array
  177.             (
  178.                 '@type' => 'WebPage',
  179.                 '@id'     => Environment::get('base').Environment::get('request'),
  180.             ),
  181.             'headline' => \strip_tags$objTemplate->headline ?: $objTemplate->name ),
  182.             'description' => \strip_tags($objTemplate->teaser),
  183.             'datePublished' => System::parseDate('c',$objTemplate->timestamp),
  184.             'dateModified' => System::parseDate('c',$objTemplate->tstamp),
  185.             'author' => array
  186.             (
  187.                 "@type" => "Person",
  188.                 "name" => $objAuthor->name,
  189.             )
  190.         );
  191.         // image
  192.         if( $objTemplate->addImage )
  193.         {
  194.             $arrReturn['image'] = \Environment::get('base').$objTemplate->singleSRC;
  195.         }
  196.         return $arrReturn;
  197.     }
  198. }