[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/plugins/content/loadmodule/ -> loadmodule.php (source)

   1  <?php
   2  
   3  /**
   4   * @package     Joomla.Plugin
   5   * @subpackage  Content.loadmodule
   6   *
   7   * @copyright   (C) 2006 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\Factory;
  14  use Joomla\CMS\Helper\ModuleHelper;
  15  use Joomla\CMS\Plugin\CMSPlugin;
  16  
  17  // phpcs:disable PSR1.Files.SideEffects
  18  \defined('_JEXEC') or die;
  19  // phpcs:enable PSR1.Files.SideEffects
  20  
  21  /**
  22   * Plugin to enable loading modules into content (e.g. articles)
  23   * This uses the {loadmodule} syntax
  24   *
  25   * @since  1.5
  26   */
  27  class PlgContentLoadmodule extends CMSPlugin
  28  {
  29      protected static $modules = array();
  30  
  31      protected static $mods = array();
  32  
  33      /**
  34       * Plugin that loads module positions within content
  35       *
  36       * @param   string   $context   The context of the content being passed to the plugin.
  37       * @param   object   &$article  The article object.  Note $article->text is also available
  38       * @param   mixed    &$params   The article params
  39       * @param   integer  $page      The 'page' number
  40       *
  41       * @return  void
  42       *
  43       * @since   1.6
  44       */
  45      public function onContentPrepare($context, &$article, &$params, $page = 0)
  46      {
  47          // Don't run this plugin when the content is being indexed
  48          if ($context === 'com_finder.indexer') {
  49              return;
  50          }
  51  
  52          // Simple performance check to determine whether bot should process further
  53          if (strpos($article->text, 'loadposition') === false && strpos($article->text, 'loadmodule') === false) {
  54              return;
  55          }
  56  
  57          // Expression to search for (positions)
  58          $regex = '/{loadposition\s(.*?)}/i';
  59          $style = $this->params->def('style', 'none');
  60  
  61          // Expression to search for(modules)
  62          $regexmod = '/{loadmodule\s(.*?)}/i';
  63          $stylemod = $this->params->def('style', 'none');
  64  
  65          // Expression to search for(id)
  66          $regexmodid = '/{loadmoduleid\s([1-9][0-9]*)}/i';
  67  
  68          // Find all instances of plugin and put in $matches for loadposition
  69          // $matches[0] is full pattern match, $matches[1] is the position
  70          preg_match_all($regex, $article->text, $matches, PREG_SET_ORDER);
  71  
  72          // No matches, skip this
  73          if ($matches) {
  74              foreach ($matches as $match) {
  75                  $matcheslist = explode(',', $match[1]);
  76  
  77                  // We may not have a module style so fall back to the plugin default.
  78                  if (!array_key_exists(1, $matcheslist)) {
  79                      $matcheslist[1] = $style;
  80                  }
  81  
  82                  $position = trim($matcheslist[0]);
  83                  $style    = trim($matcheslist[1]);
  84  
  85                  $output = $this->_load($position, $style);
  86  
  87                  // We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
  88                  if (($start = strpos($article->text, $match[0])) !== false) {
  89                      $article->text = substr_replace($article->text, $output, $start, strlen($match[0]));
  90                  }
  91  
  92                  $style = $this->params->def('style', 'none');
  93              }
  94          }
  95  
  96          // Find all instances of plugin and put in $matchesmod for loadmodule
  97          preg_match_all($regexmod, $article->text, $matchesmod, PREG_SET_ORDER);
  98  
  99          // If no matches, skip this
 100          if ($matchesmod) {
 101              foreach ($matchesmod as $matchmod) {
 102                  $matchesmodlist = explode(',', $matchmod[1]);
 103  
 104                  // We may not have a specific module so set to null
 105                  if (!array_key_exists(1, $matchesmodlist)) {
 106                      $matchesmodlist[1] = null;
 107                  }
 108  
 109                  // We may not have a module style so fall back to the plugin default.
 110                  if (!array_key_exists(2, $matchesmodlist)) {
 111                      $matchesmodlist[2] = $stylemod;
 112                  }
 113  
 114                  $module = trim($matchesmodlist[0]);
 115                  $name   = htmlspecialchars_decode(trim($matchesmodlist[1]));
 116                  $stylemod  = trim($matchesmodlist[2]);
 117  
 118                  // $match[0] is full pattern match, $match[1] is the module,$match[2] is the title
 119                  $output = $this->_loadmod($module, $name, $stylemod);
 120  
 121                  // We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
 122                  if (($start = strpos($article->text, $matchmod[0])) !== false) {
 123                      $article->text = substr_replace($article->text, $output, $start, strlen($matchmod[0]));
 124                  }
 125  
 126                  $stylemod = $this->params->def('style', 'none');
 127              }
 128          }
 129  
 130          // Find all instances of plugin and put in $matchesmodid for loadmoduleid
 131          preg_match_all($regexmodid, $article->text, $matchesmodid, PREG_SET_ORDER);
 132  
 133          // If no matches, skip this
 134          if ($matchesmodid) {
 135              foreach ($matchesmodid as $match) {
 136                  $id     = trim($match[1]);
 137                  $output = $this->_loadid($id);
 138  
 139                  // We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
 140                  if (($start = strpos($article->text, $match[0])) !== false) {
 141                      $article->text = substr_replace($article->text, $output, $start, strlen($match[0]));
 142                  }
 143  
 144                  $style = $this->params->def('style', 'none');
 145              }
 146          }
 147      }
 148  
 149      /**
 150       * Loads and renders the module
 151       *
 152       * @param   string  $position  The position assigned to the module
 153       * @param   string  $style     The style assigned to the module
 154       *
 155       * @return  mixed
 156       *
 157       * @since   1.6
 158       */
 159      protected function _load($position, $style = 'none')
 160      {
 161          self::$modules[$position] = '';
 162          $document = Factory::getDocument();
 163          $renderer = $document->loadRenderer('module');
 164          $modules  = ModuleHelper::getModules($position);
 165          $params   = array('style' => $style);
 166          ob_start();
 167  
 168          foreach ($modules as $module) {
 169              echo $renderer->render($module, $params);
 170          }
 171  
 172          self::$modules[$position] = ob_get_clean();
 173  
 174          return self::$modules[$position];
 175      }
 176  
 177      /**
 178       * This is always going to get the first instance of the module type unless
 179       * there is a title.
 180       *
 181       * @param   string  $module  The module title
 182       * @param   string  $title   The title of the module
 183       * @param   string  $style   The style of the module
 184       *
 185       * @return  mixed
 186       *
 187       * @since   1.6
 188       */
 189      protected function _loadmod($module, $title, $style = 'none')
 190      {
 191          self::$mods[$module] = '';
 192          $document = Factory::getDocument();
 193          $renderer = $document->loadRenderer('module');
 194          $mod      = ModuleHelper::getModule($module, $title);
 195  
 196          // If the module without the mod_ isn't found, try it with mod_.
 197          // This allows people to enter it either way in the content
 198          if (!isset($mod)) {
 199              $name = 'mod_' . $module;
 200              $mod  = ModuleHelper::getModule($name, $title);
 201          }
 202  
 203          $params = array('style' => $style);
 204          ob_start();
 205  
 206          if ($mod->id) {
 207              echo $renderer->render($mod, $params);
 208          }
 209  
 210          self::$mods[$module] = ob_get_clean();
 211  
 212          return self::$mods[$module];
 213      }
 214  
 215      /**
 216       * Loads and renders the module
 217       *
 218       * @param   string  $id  The id of the module
 219       *
 220       * @return  mixed
 221       *
 222       * @since   3.9.0
 223       */
 224      protected function _loadid($id)
 225      {
 226          self::$modules[$id] = '';
 227          $document = Factory::getDocument();
 228          $renderer = $document->loadRenderer('module');
 229          $modules  = ModuleHelper::getModuleById($id);
 230          $params   = array('style' => 'none');
 231          ob_start();
 232  
 233          if ($modules->id > 0) {
 234              echo $renderer->render($modules, $params);
 235          }
 236  
 237          self::$modules[$id] = ob_get_clean();
 238  
 239          return self::$modules[$id];
 240      }
 241  }


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