[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/src/Document/Renderer/Html/ -> MetasRenderer.php (source)

   1  <?php
   2  
   3  /**
   4   * Joomla! Content Management System
   5   *
   6   * @copyright   (C) 2005 Open Source Matters, Inc. <https://www.joomla.org>
   7   * @license     GNU General Public License version 2 or later; see LICENSE
   8   */
   9  
  10  namespace Joomla\CMS\Document\Renderer\Html;
  11  
  12  use Joomla\CMS\Document\DocumentRenderer;
  13  use Joomla\CMS\Factory;
  14  use Joomla\CMS\Helper\TagsHelper;
  15  use Joomla\CMS\Uri\Uri;
  16  use Joomla\CMS\WebAsset\WebAssetAttachBehaviorInterface;
  17  use Joomla\Utilities\ArrayHelper;
  18  
  19  // phpcs:disable PSR1.Files.SideEffects
  20  \defined('JPATH_PLATFORM') or die;
  21  // phpcs:enable PSR1.Files.SideEffects
  22  
  23  /**
  24   * JDocument metas renderer
  25   *
  26   * @since  4.0.0
  27   */
  28  class MetasRenderer extends DocumentRenderer
  29  {
  30      /**
  31       * Renders the document metas and returns the results as a string
  32       *
  33       * @param   string  $head     (unused)
  34       * @param   array   $params   Associative array of values
  35       * @param   string  $content  The script
  36       *
  37       * @return  string  The output of the script
  38       *
  39       * @since   4.0.0
  40       */
  41      public function render($head, $params = array(), $content = null)
  42      {
  43          // Convert the tagids to titles
  44          if (isset($this->_doc->_metaTags['name']['tags'])) {
  45              $tagsHelper = new TagsHelper();
  46              $this->_doc->_metaTags['name']['tags'] = implode(', ', $tagsHelper->getTagNames($this->_doc->_metaTags['name']['tags']));
  47          }
  48  
  49          /** @var \Joomla\CMS\Application\CMSApplication $app */
  50          $app = Factory::getApplication();
  51          $wa  = $this->_doc->getWebAssetManager();
  52  
  53          // Check for AttachBehavior and web components
  54          foreach ($wa->getAssets('script', true) as $asset) {
  55              if ($asset instanceof WebAssetAttachBehaviorInterface) {
  56                  $asset->onAttachCallback($this->_doc);
  57              }
  58          }
  59  
  60          // Trigger the onBeforeCompileHead event
  61          $app->triggerEvent('onBeforeCompileHead');
  62  
  63          // Add Script Options as inline asset
  64          $scriptOptions = $this->_doc->getScriptOptions();
  65  
  66          if ($scriptOptions) {
  67              $prettyPrint = (JDEBUG && \defined('JSON_PRETTY_PRINT') ? JSON_PRETTY_PRINT : false);
  68              $jsonOptions = json_encode($scriptOptions, $prettyPrint);
  69              $jsonOptions = $jsonOptions ?: '{}';
  70  
  71              $wa->addInlineScript(
  72                  $jsonOptions,
  73                  ['name' => 'joomla.script.options', 'position' => 'before'],
  74                  ['type' => 'application/json', 'class' => 'joomla-script-options new'],
  75                  ['core']
  76              );
  77          }
  78  
  79          // Lock the AssetManager
  80          $wa->lock();
  81  
  82          // Get line endings
  83          $lnEnd        = $this->_doc->_getLineEnd();
  84          $tab          = $this->_doc->_getTab();
  85          $buffer       = '';
  86  
  87          // Generate charset when using HTML5 (should happen first)
  88          if ($this->_doc->isHtml5()) {
  89              $buffer .= $tab . '<meta charset="' . $this->_doc->getCharset() . '">' . $lnEnd;
  90          }
  91  
  92          // Generate base tag (need to happen early)
  93          $base = $this->_doc->getBase();
  94  
  95          if (!empty($base)) {
  96              $buffer .= $tab . '<base href="' . $base . '">' . $lnEnd;
  97          }
  98  
  99          $noFavicon = true;
 100          $searchFor = 'image/vnd.microsoft.icon';
 101  
 102          array_map(function ($value) use (&$noFavicon, $searchFor) {
 103              if (isset($value['attribs']['type']) && $value['attribs']['type'] === $searchFor) {
 104                  $noFavicon = false;
 105              }
 106          }, array_values((array)$this->_doc->_links));
 107  
 108          if ($noFavicon) {
 109              $client   = $app->isClient('administrator') === true ? 'administrator/' : 'site/';
 110              $template = $app->getTemplate(true);
 111  
 112              // Try to find a favicon by checking the template and root folder
 113              $icon = '/favicon.ico';
 114              $foldersToCheck = [
 115                  JPATH_BASE,
 116                  JPATH_ROOT . '/media/templates/' . $client . $template->template,
 117                  JPATH_BASE . '/templates/' . $template->template,
 118              ];
 119  
 120              foreach ($foldersToCheck as $base => $dir) {
 121                  if (
 122                      $template->parent !== ''
 123                      && $base === 1
 124                      && !is_file(JPATH_ROOT . '/media/templates/' . $client . $template->template . $icon)
 125                  ) {
 126                      $dir = JPATH_ROOT . '/media/templates/' . $client . $template->parent;
 127                  }
 128  
 129                  if (is_file($dir . $icon)) {
 130                      $urlBase = in_array($base, [0, 2]) ? Uri::base(true) : Uri::root(true);
 131                      $base    = in_array($base, [0, 2]) ? JPATH_BASE : JPATH_ROOT;
 132                      $path    = str_replace($base, '', $dir);
 133                      $path    = str_replace('\\', '/', $path);
 134                      $this->_doc->addFavicon($urlBase . $path . $icon);
 135                      break;
 136                  }
 137              }
 138          }
 139  
 140          // Generate META tags (needs to happen as early as possible in the head)
 141          foreach ($this->_doc->_metaTags as $type => $tag) {
 142              foreach ($tag as $name => $contents) {
 143                  if ($type === 'http-equiv' && !($this->_doc->isHtml5() && $name === 'content-type')) {
 144                      $buffer .= $tab . '<meta http-equiv="' . $name . '" content="'
 145                          . htmlspecialchars($contents, ENT_COMPAT, 'UTF-8') . '">' . $lnEnd;
 146                  } elseif ($type !== 'http-equiv' && !empty($contents)) {
 147                      $buffer .= $tab . '<meta ' . $type . '="' . $name . '" content="'
 148                          . htmlspecialchars($contents, ENT_COMPAT, 'UTF-8') . '">' . $lnEnd;
 149                  }
 150              }
 151          }
 152  
 153          // Don't add empty descriptions
 154          $documentDescription = $this->_doc->getDescription();
 155  
 156          if ($documentDescription) {
 157              $buffer .= $tab . '<meta name="description" content="' . htmlspecialchars($documentDescription, ENT_COMPAT, 'UTF-8') . '">' . $lnEnd;
 158          }
 159  
 160          // Don't add empty generators
 161          $generator = $this->_doc->getGenerator();
 162  
 163          if ($generator) {
 164              $buffer .= $tab . '<meta name="generator" content="' . htmlspecialchars($generator, ENT_COMPAT, 'UTF-8') . '">' . $lnEnd;
 165          }
 166  
 167          $buffer .= $tab . '<title>' . htmlspecialchars($this->_doc->getTitle(), ENT_COMPAT, 'UTF-8') . '</title>' . $lnEnd;
 168  
 169          // Generate link declarations
 170          foreach ($this->_doc->_links as $link => $linkAtrr) {
 171              $buffer .= $tab . '<link href="' . $link . '" ' . $linkAtrr['relType'] . '="' . $linkAtrr['relation'] . '"';
 172  
 173              if (\is_array($linkAtrr['attribs'])) {
 174                  if ($temp = ArrayHelper::toString($linkAtrr['attribs'])) {
 175                      $buffer .= ' ' . $temp;
 176                  }
 177              }
 178  
 179              $buffer .= '>' . $lnEnd;
 180          }
 181  
 182          return ltrim($buffer, $tab);
 183      }
 184  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer