[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/system/languagecode/ -> languagecode.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  System.languagecode
   6   *
   7   * @copyright   (C) 2011 Open Source Matters, Inc. <https://www.joomla.org>
   8   * @license     GNU General Public License version 2 or later; see LICENSE.txt
   9  
  10   * @phpcs:disable PSR1.Classes.ClassDeclaration.MissingNamespace
  11   */
  12  
  13  use Joomla\CMS\Form\Form;
  14  use Joomla\CMS\Language\LanguageHelper;
  15  use Joomla\CMS\Language\Text;
  16  use Joomla\CMS\Plugin\CMSPlugin;
  17  
  18  // phpcs:disable PSR1.Files.SideEffects
  19  \defined('_JEXEC') or die;
  20  // phpcs:enable PSR1.Files.SideEffects
  21  
  22  /**
  23   * Language Code plugin class.
  24   *
  25   * @since  2.5
  26   */
  27  class PlgSystemLanguagecode extends CMSPlugin
  28  {
  29      /**
  30       * Application object
  31       *
  32       * @var    \Joomla\CMS\Application\CMSApplication
  33       * @since  4.0.0
  34       */
  35      protected $app;
  36  
  37      /**
  38       * Plugin that changes the language code used in the <html /> tag.
  39       *
  40       * @return  void
  41       *
  42       * @since   2.5
  43       */
  44      public function onAfterRender()
  45      {
  46          // Use this plugin only in site application.
  47          if ($this->app->isClient('site')) {
  48              // Get the response body.
  49              $body = $this->app->getBody();
  50  
  51              // Get the current language code.
  52              $code = $this->app->getDocument()->getLanguage();
  53  
  54              // Get the new code.
  55              $new_code  = $this->params->get($code);
  56  
  57              // Replace the old code by the new code in the <html /> tag.
  58              if ($new_code) {
  59                  // Replace the new code in the HTML document.
  60                  $patterns = array(
  61                      chr(1) . '(<html.*\s+xml:lang=")(' . $code . ')(".*>)' . chr(1) . 'i',
  62                      chr(1) . '(<html.*\s+lang=")(' . $code . ')(".*>)' . chr(1) . 'i',
  63                  );
  64                  $replace = array(
  65                      '$1}' . strtolower($new_code) . '$3}',
  66                      '$1}' . strtolower($new_code) . '$3}',
  67                  );
  68              } else {
  69                  $patterns = array();
  70                  $replace  = array();
  71              }
  72  
  73              // Replace codes in <link hreflang="" /> attributes.
  74              preg_match_all(chr(1) . '(<link.*\s+hreflang=")([0-9a-z\-]*)(".*\s+rel="alternate".*>)' . chr(1) . 'i', $body, $matches);
  75  
  76              foreach ($matches[2] as $match) {
  77                  $new_code = $this->params->get(strtolower($match));
  78  
  79                  if ($new_code) {
  80                      $patterns[] = chr(1) . '(<link.*\s+hreflang=")(' . $match . ')(".*\s+rel="alternate".*>)' . chr(1) . 'i';
  81                      $replace[] = '$1}' . $new_code . '$3}';
  82                  }
  83              }
  84  
  85              preg_match_all(chr(1) . '(<link.*\s+rel="alternate".*\s+hreflang=")([0-9A-Za-z\-]*)(".*>)' . chr(1) . 'i', $body, $matches);
  86  
  87              foreach ($matches[2] as $match) {
  88                  $new_code = $this->params->get(strtolower($match));
  89  
  90                  if ($new_code) {
  91                      $patterns[] = chr(1) . '(<link.*\s+rel="alternate".*\s+hreflang=")(' . $match . ')(".*>)' . chr(1) . 'i';
  92                      $replace[] = '$1}' . $new_code . '$3}';
  93                  }
  94              }
  95  
  96              // Replace codes in itemprop content
  97              preg_match_all(chr(1) . '(<meta.*\s+itemprop="inLanguage".*\s+content=")([0-9A-Za-z\-]*)(".*>)' . chr(1) . 'i', $body, $matches);
  98  
  99              foreach ($matches[2] as $match) {
 100                  $new_code = $this->params->get(strtolower($match));
 101  
 102                  if ($new_code) {
 103                      $patterns[] = chr(1) . '(<meta.*\s+itemprop="inLanguage".*\s+content=")(' . $match . ')(".*>)' . chr(1) . 'i';
 104                      $replace[] = '$1}' . $new_code . '$3}';
 105                  }
 106              }
 107  
 108              $this->app->setBody(preg_replace($patterns, $replace, $body));
 109          }
 110      }
 111  
 112      /**
 113       * Prepare form.
 114       *
 115       * @param   Form   $form  The form to be altered.
 116       * @param   mixed  $data  The associated data for the form.
 117       *
 118       * @return  boolean
 119       *
 120       * @since   2.5
 121       */
 122      public function onContentPrepareForm(Form $form, $data)
 123      {
 124          // Check we are manipulating the languagecode plugin.
 125          if ($form->getName() !== 'com_plugins.plugin' || !$form->getField('languagecodeplugin', 'params')) {
 126              return true;
 127          }
 128  
 129          // Get site languages.
 130          if ($languages = LanguageHelper::getKnownLanguages(JPATH_SITE)) {
 131              // Inject fields into the form.
 132              foreach ($languages as $tag => $language) {
 133                  $form->load('
 134                      <form>
 135                          <fields name="params">
 136                              <fieldset
 137                                  name="languagecode"
 138                                  label="PLG_SYSTEM_LANGUAGECODE_FIELDSET_LABEL"
 139                                  description="PLG_SYSTEM_LANGUAGECODE_FIELDSET_DESC"
 140                              >
 141                                  <field
 142                                      name="' . strtolower($tag) . '"
 143                                      type="text"
 144                                      label="' . $tag . '"
 145                                      description="' . htmlspecialchars(Text::sprintf('PLG_SYSTEM_LANGUAGECODE_FIELD_DESC', $language['name']), ENT_COMPAT, 'UTF-8') . '"
 146                                      translate_description="false"
 147                                      translate_label="false"
 148                                      size="7"
 149                                      filter="cmd"
 150                                  />
 151                              </fieldset>
 152                          </fields>
 153                      </form>');
 154              }
 155          }
 156  
 157          return true;
 158      }
 159  }


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