/plugins/content/loadmodule/loadmodule.php

https://github.com/joomla/joomla-cms · PHP · 237 lines · 116 code · 42 blank · 79 comment · 15 complexity · 2223d161a853eafaacfd34c0c7a4ee37 MD5 · raw file

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