НАСТРОЙКИ / Open Graph

Заметка создана: 06 июня 2024 г.

#Вариант 1

Обязательно добавить в head префиксы для валидности
<head prefix=
    "og: http://ogp.me/ns#
     fb: http://ogp.me/ns/fb#  
     product: http://ogp.me/ns/product#">
Сам код OpenGraph
<?php
$bOpenGraph = FALSE;
// Open Graph
if (is_object(Core_Page::instance()->object))
{
    $bInformationItem = Core_Page::instance()->object instanceof Informationsystem_Controller_Show;
    $bShopItem = Core_Page::instance()->object instanceof Shop_Controller_Show;
   
    if ($bInformationItem || $bShopItem)
    {
        echo'<meta property="og:site_name" content="inikSite.ru">';    // ЗАМЕНИТЬ НА URL СВОЕГО САЙТА
        if (Core_Page::instance()->object->item)
        {
   $bOpenGraph = TRUE;
         
            $aOpenGraph = array();            
            $oEntity = $bInformationItem
                ? Core_Entity::factory('Informationsystem_Item', Core_Page::instance()->object->item)
                : Core_Entity::factory('Shop_Item', Core_Page::instance()->object->item);
            
            $type = $bInformationItem
                ? 'article'
                : 'product';
            
            $aOpenGraph['og:type'] = $type;
            $aOpenGraph['og:title'] =  $oEntity->name;
            $aOpenGraph['og:description'] = strip_tags(Core_Str::cutSentences($oEntity->description));


            $oSite = Core_Entity::factory('Site', CURRENT_SITE);
            $oSite_Alias = $oSite->getCurrentAlias();
        
            if ($oSite_Alias)
            {
                $sSiteURL = $oSite_Alias->name;
                
                $protocol = Core_Page::instance()->structure->https
                    ? 'https://'
                    : 'http://';
            
                $aOpenGraph['og:url'] = $protocol . $sSiteURL
                    . Core_Page::instance()->structure->getPath()
                    . $oEntity->getPath();
            
                if ($oEntity->image_large != '')
                {
                    $aOpenGraph['og:image'] = $protocol . $sSiteURL . $oEntity->getLargeFileHref();
                              $aOpenGraph['og:image:width'] = $oEntity->image_large_width;
                              $aOpenGraph['og:image:height'] = $oEntity->image_large_height;
                }
               
               if ($bShopItem)
               {
                  $aOpenGraph['product:price:amount'] = $oEntity->price;
                  $aOpenGraph['product:price:currency'] = $oEntity->shop_currency->code;
               }
                     
            }
            
            foreach ($aOpenGraph as $sProperty => $sContent)
            {
         ?><meta property="<?php echo htmlspecialchars($sProperty)?>" content="<?php echo htmlspecialchars($sContent)?>" /><?php
      
         echo PHP_EOL;
            }
      }
      elseif (Core_Page::instance()->object->group)
      {
         $bOpenGraph = TRUE;


            $aOpenGraph = array();
            
            $oEntity = $bInformationItem
                ? Core_Entity::factory('Informationsystem_Group', Core_Page::instance()->object->group)
                : Core_Entity::factory('Shop_Group', Core_Page::instance()->object->group);
            
            $type = $bInformationItem
                ? 'article'
                : 'website';
            
            $aOpenGraph['og:type'] = $type;
            $aOpenGraph['og:title'] = $oEntity->name;
            $aOpenGraph['og:description'] = $oEntity->seo_description;


            $oSite = Core_Entity::factory('Site', CURRENT_SITE);
            $oSite_Alias = $oSite->getCurrentAlias();
        
            if ($oSite_Alias)
            {
                $sSiteURL = $oSite_Alias->name;
                
                $protocol = Core_Page::instance()->structure->https
                    ? 'https://'
                    : 'http://';
            
                $aOpenGraph['og:url'] = $protocol . $sSiteURL
                    . Core_Page::instance()->structure->getPath()
                    . $oEntity->getPath();
            
                if ($oEntity->image_large != '')
                {
                    $aOpenGraph['og:image'] = $protocol . $sSiteURL . $oEntity->getLargeFileHref();
                }else{
                    $aOpenGraph['og:image'] = 'https://inikSite.ru/images/logo.jpg';   // ЗАМЕНИТЬ НА ПУТЬ К ЛОГОТИПУ СВОЕГО САЙТА
                }
            }
            
            foreach ($aOpenGraph as $sProperty => $sContent)
            {
         ?><meta property="<?php echo htmlspecialchars($sProperty)?>" content="<?php echo htmlspecialchars($sContent)?>" /><?php
      
         echo PHP_EOL;
         }
      }
   }
}
   
if (!$bOpenGraph && Core_Page::instance()->structure)
{
   $aOpenGraph = array();
   
   $oEntity = Core_Page::instance()->structure;
   
   $type = 'website';
   
   $aOpenGraph['og:type'] = $type;
   $aOpenGraph['og:title'] = $oEntity->Site->name;
   $aOpenGraph['og:description'] = strip_tags(Core_Str::cutSentences($oEntity->seo_description));


   $oSite = Core_Entity::factory('Site', CURRENT_SITE);
   $oSite_Alias = $oSite->getCurrentAlias();


   if ($oSite_Alias)
   {
      $sSiteURL = $oSite_Alias->name;
      
      $protocol = Core_Page::instance()->structure->https
         ? 'https://'
         : 'http://';
   
      $aOpenGraph['og:url'] = $protocol . $sSiteURL
         . $oEntity->getPath();
   
     $aOpenGraph['og:image'] = 'https://inikSite.ru/images/logo.jpg';   // ЗАМЕНИТЬ НА ПУТЬ К ЛОГОТИПУ СВОЕГО САЙТА
               
   }
   
   foreach ($aOpenGraph as $sProperty => $sContent)
   {
      ?><meta property="<?php echo htmlspecialchars($sProperty)?>" content="<?php echo htmlspecialchars($sContent)?>" /><?php


      echo PHP_EOL;
   }
}
?>

#Вариант 2

<?php
    // Open Graph
    if (is_object(Core_Page::instance()->object))
    {
        $bInformationItem = Core_Page::instance()->object instanceof Informationsystem_Controller_Show;
        $bShopItem = Core_Page::instance()->object instanceof Shop_Controller_Show;
        if ($bInformationItem || $bShopItem)
        {
            if (Core_Page::instance()->object->item)
            {
                $aOpenGraph = array();
                
                $oEntity = $bInformationItem
                    ? Core_Entity::factory('Informationsystem_Item', Core_Page::instance()->object->item)
                    : Core_Entity::factory('Shop_Item', Core_Page::instance()->object->item);
                
                $type = $bInformationItem
                    ? 'article'
                    : 'website';
                
                $aOpenGraph['og:type'] = $type;
                $aOpenGraph['og:title'] = $oEntity->name;
                $aOpenGraph['og:description'] = strip_tags(Core_Str::cutSentences($oEntity->description));
                                

                if ($oEntity->image_large != '')
                {
                    $oSite = Core_Entity::factory('Site', CURRENT_SITE);
                    $oSite_Alias = $oSite->getCurrentAlias();
                
                    if ($oSite_Alias)
                    {
                        $sSiteURL = $oSite_Alias->name;
                        
                        $protocol = Core_Page::instance()->structure->https
                            ? 'https://'
                            : 'http://';
                            
                        $aOpenGraph['og:image'] = $protocol . $sSiteURL . $oEntity->getLargeFileHref();
                    }
                }
                
                foreach ($aOpenGraph as $sProperty => $sContent)
                {
                    ?><meta property="<?php echo htmlspecialchars($sProperty)?>" content="<?php echo htmlspecialchars($sContent)?>" /><?php
                    echo PHP_EOL;
                }
            }
        }
    }
?>