system/modules/pct_seo_helper/PCT/SEO/ContaoCallbacks.php line 76

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\SEO;
  17. /**
  18.  * Class file
  19.  * ContaoCallbacks
  20.  */
  21. class ContaoCallbacks
  22. {
  23.     /**
  24.      * Add the 'loading' attribute to <img> elements
  25.      * @param object
  26.      */
  27.     public function parseFrontendTemplateCallback($strBuffer$strTemplate)
  28.     {
  29.         $strLoading \Contao\Config::get('pct_seo_image_loading');
  30.         if( empty($strLoading) === true )
  31.         {
  32.             return $strBuffer;
  33.         }
  34.         // find images
  35.         if( preg_match_all('/(<img[^>]+>)/i'$strBuffer$arrMatches) )
  36.         {
  37.             foreach( $arrMatches as $match )
  38.             {
  39.                 if( empty($match) === true )
  40.                 {
  41.                     continue;
  42.                 }
  43.                 $image $match[0];
  44.                 // skip images already processed or containing an loading attribute
  45.                 if( in_array($image,$GLOBALS['PCT_SEO']['processedImages'] ?? array() ) == true || strpos($image,'loading=') !== false )
  46.                 {
  47.                     continue;
  48.                 }
  49.                 // insert loading attribute
  50.                 $new_image str_replace('<img','<img loading="'.$strLoading.'"',$image);
  51.                 // replace in html string
  52.                 $strBuffer str_replace($image,$new_image,$strBuffer);
  53.                 // mark as being processed
  54.                 $GLOBALS['PCT_SEO']['processedImages'][] = $image;
  55.                 $GLOBALS['PCT_SEO']['processedImages'][] = $new_image;
  56.             }
  57.         }
  58.         return $strBuffer;
  59.     }
  60.     /**
  61.      * Add variables to fe_page Template
  62.      * @param mixed $objTemplate 
  63.      * @return void 
  64.      */
  65.     public function parseTemplateCallback($objTemplate)
  66.     {
  67.         if ( \strpos($objTemplate->getName(), 'fe_page') === false 
  68.         {
  69.             $GLOBALS['PCT_SEO']['FE_PAGE_PROCESSED'] = true;
  70.             return;
  71.         }
  72.         global $objPage;
  73.         if( strlen($objPage->pct_structured_data) > )
  74.         {
  75.             $objTemplate->pct_structured_data \Contao\StringUtil::decodeEntities($objPage->pct_structured_data);
  76.         }
  77.         // HTTP1/2
  78.         if( empty(\Contao\Config::get('pct_seo_protocol')) === false )
  79.         {
  80.             // add a template variable
  81.             $objTemplate->seo_protocol \PCT\SEO::getProtocol(); // http1, http2
  82.         }
  83.         
  84.         // SEO scripte single src files
  85.         $objTemplate->seo_scripts_files = array();
  86.         
  87.         // if ThemeDesigner is active, place all SEO SCRIPT FILES in TL_JAVASCRIPT
  88.         if( (boolean)\Contao\Config::get('pct_themedesigner_hidden') === false && isset($GLOBALS['SEO_SCRIPTS_FILE']) && !empty($GLOBALS['SEO_SCRIPTS_FILE']) )
  89.         {
  90.             $tmp \array_unique\array_filter($GLOBALS['SEO_SCRIPTS_FILE']) );
  91.             foreach($tmp as $k => $file)
  92.             {
  93.                 #$GLOBALS['TL_JQUERY'][]    = '<script src="'.$file.'"></script>';
  94.                 $GLOBALS['TL_JAVASCRIPT'][]    = $file;
  95.             }
  96.             unset($GLOBALS['SEO_SCRIPTS_FILE']);
  97.         }
  98.         
  99.         
  100.         if( isset($GLOBALS['SEO_SCRIPTS_FILE']) && !empty($GLOBALS['SEO_SCRIPTS_FILE']) )
  101.         {
  102.             $tmp \array_unique\array_filter($GLOBALS['SEO_SCRIPTS_FILE']) );
  103.             if( empty($GLOBALS['SEO_SCRIPTS_FILE']) === false )
  104.             {
  105.                 foreach($tmp as $k => $file)
  106.                 {
  107.                     $arr parse_url($file);
  108.                     if( isset($arr['path']) && !empty($arr['path']) )
  109.                     {
  110.                         $file $arr['path'];
  111.                     }
  112.                     $file str_replace(array('|static','|print','|async'), ''$file);
  113.                     $tmp[$k] = $file;
  114.                 }
  115.                 $objTemplate->seo_scripts_files $tmp;
  116.                 unset($tmp);
  117.             }
  118.         }
  119.     }
  120.     
  121.     /**
  122.      * Find comment section in templates to extract
  123.      * @param mixed $objTemplate 
  124.      * @return void 
  125.      */
  126.     public function collectScriptSections($strBuffer$strTemplate)
  127.     {
  128.         $arrMatches = array();
  129.         $preg preg_match_all('/SEO-SCRIPT-START(.*?)SEO-SCRIPT-STOP/s'$strBuffer,$arrMatches);
  130.         if( !$preg )
  131.         {
  132.             return $strBuffer;
  133.         }
  134.         
  135.         $block_start '<!-- SEO-SCRIPT-START -->';
  136.         $block_stop '<!-- SEO-SCRIPT-STOP -->';
  137.         
  138.         foreach($arrMatches[1] as $match)
  139.         {
  140.             $match ltrim(ltrim($match),'-->');
  141.             $match rtrim(rtrim($match),'<!--');
  142.             // add to end of body
  143.             $GLOBALS['TL_BODY'][] = $match;
  144.             // remove script from source code
  145.             $strBuffer str_replace(array($block_start,$block_stop,$match), ''$strBuffer);
  146.         }
  147.         
  148.         return $strBuffer;
  149.     }
  150.     /**
  151.      * Add page record information directly to the fe_page template
  152.      * @param object
  153.      * @param object
  154.      */
  155.     public function generatePageCallback($objPage$objLayout$objPageRegular)
  156.     {    
  157.         // HTTP1/2
  158.         if( empty(\Contao\Config::get('pct_seo_protocol')) === false )
  159.         {
  160.             $strProtocol \PCT\SEO::getProtocol(); // http1, http2
  161.             
  162.             $version str_replace(array('http','https'),'',$strProtocol);
  163.             // http1.x
  164.             if( version_compare($version,'2','<') )
  165.             {
  166.                 // add |static
  167.                 if( !empty($GLOBALS['TL_JAVASCRIPT']) && !\is_array($GLOBALS['TL_JAVASCRIPT']) )
  168.                 {
  169.                     $GLOBALS['TL_JAVASCRIPT'] = array_unique($GLOBALS['TL_JAVASCRIPT']);
  170.                     foreach( $GLOBALS['TL_JAVASCRIPT'] as $i => $v )
  171.                     {
  172.                         if( strpos($v,'|static') === false && file_exists(TL_ROOT.'/'.$v) )
  173.                         {
  174.                             $GLOBALS['TL_JAVASCRIPT'][$i] = $v.'|static';
  175.                         }
  176.                     }
  177.                 }
  178.                 if( !empty($GLOBALS['TL_CSS']) && !\is_array($GLOBALS['TL_CSS']) )
  179.                 {
  180.                     $GLOBALS['TL_CSS'] = array_unique($GLOBALS['TL_CSS']);
  181.                     foreach( $GLOBALS['TL_CSS'] as $i => $v )
  182.                     {
  183.                         if( strpos($v,'|static') === false && file_exists(TL_ROOT.'/'.$v) )
  184.                         {
  185.                             $GLOBALS['TL_CSS'][$i] = $v.'|static';
  186.                         }
  187.                     }
  188.                 }
  189.             }
  190.             // http2.x
  191.             else if( version_compare($version,'2','>=') )
  192.             {
  193.                 // strip |static
  194.                 if( !empty($GLOBALS['TL_JAVASCRIPT']) && !\is_array($GLOBALS['TL_JAVASCRIPT']) )
  195.                 {
  196.                     foreach( $GLOBALS['TL_JAVASCRIPT'] as $i => $v )
  197.                     {
  198.                         $GLOBALS['TL_JAVASCRIPT'][$i] = str_replace('|static','',$v);
  199.                     }
  200.                 }
  201.                 if( !empty($GLOBALS['TL_CSS']) && !\is_array($GLOBALS['TL_CSS']) )
  202.                 {
  203.                     $GLOBALS['TL_CSS'] = array_unique($GLOBALS['TL_CSS']);
  204.                     foreach( $GLOBALS['TL_CSS'] as $i => $v )
  205.                     {
  206.                         $GLOBALS['TL_CSS'][$i] = str_replace('|static','',$v);
  207.                     }
  208.                 }
  209.             }
  210.         }
  211.     }
  212. }