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