-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomfields.php
More file actions
279 lines (244 loc) · 7.84 KB
/
customfields.php
File metadata and controls
279 lines (244 loc) · 7.84 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?php
/**
* @version 1.0.0
* @package PLG_Content_CustomFields
* @copyright Copyright (C) 2024. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
* @author Custom Plugin
*/
// No Direct Access
defined('_JEXEC') || die('Restricted access');
use Joomla\CMS\Factory;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\User\User;
/**
* Custom Fields Shortcode Plugin
*
* This plugin processes shortcodes like [CF fieldname] and replaces them
* with the actual value of the custom user field.
*/
class PlgContentCustomfields extends CMSPlugin
{
/**
* Application object.
*
* @var \Joomla\CMS\Application\CMSApplication
* @since 1.0.0
*/
protected $app;
/**
* Load the language file on instantiation.
*
* @var boolean
* @since 1.0.0
*/
protected $autoloadLanguage = true;
/**
* Plugin that processes custom field shortcodes
*
* @param string $context The context of the content being passed
* @param object &$article The article object
* @param object &$params The article params
* @param integer $page The 'page' number
*
* @return void
*
* @since 1.0.0
*/
public function onContentPrepare($context, &$article, &$params, $page = 0)
{
// Check if we should process this content
if (!$this->shouldProcess($context, $article)) {
return;
}
// Get the current user
$user = $this->app->getIdentity();
// Only process if user is logged in
if ($user->guest) {
// Replace shortcodes with guest text or remove them
$guestText = $this->params->get('guest_text', '');
$article->text = preg_replace('/\[CF\s+([^\]]+)\]/i', $guestText, $article->text);
return;
}
// Process shortcodes for logged in users
$article->text = preg_replace_callback(
'/\[CF\s+([^\]]+)\]/i',
[$this, 'processShortcode'],
$article->text
);
}
/**
* Check if we should process this content
*
* @param string $context The context
* @param object $article The article object
*
* @return boolean
*
* @since 1.0.0
*/
protected function shouldProcess($context, &$article)
{
// Skip if disabled
if (!$this->params->get('enabled', 1)) {
return false;
}
// Skip if no article or text to process
if (empty($article) || empty($article->text)) {
return false;
}
// Skip if no shortcodes found
if (strpos($article->text, '[CF ') === false) {
return false;
}
// Check if context is allowed
$allowedContexts = $this->getAllowedContexts();
if (!empty($allowedContexts) && !in_array($context, $allowedContexts)) {
return false;
}
return true;
}
/**
* Get allowed contexts from plugin parameters
*
* @return array
*
* @since 1.0.0
*/
protected function getAllowedContexts()
{
$contextsParam = $this->params->get('contexts', '');
if (empty($contextsParam)) {
// Return default contexts if none specified
return [
'com_content.article',
'com_content.category',
'com_users.user'
];
}
// Parse contexts from textarea (one per line)
$contexts = array_filter(
array_map('trim', explode("\n", $contextsParam)),
function($context) {
return !empty($context);
}
);
return $contexts;
}
/**
* Process individual shortcode
*
* @param array $matches The regex matches
*
* @return string The replacement text
*
* @since 1.0.0
*/
protected function processShortcode($matches)
{
$fieldName = trim($matches[1]);
if (empty($fieldName)) {
return $matches[0]; // Return original shortcode if no field name
}
try {
$user = $this->app->getIdentity();
$fieldValue = $this->getCustomFieldValue($user->id, $fieldName);
if ($fieldValue !== null && $fieldValue !== '') {
return $this->formatFieldValue($fieldValue, $fieldName);
}
// Return default text if field not found or empty
$defaultText = $this->params->get('default_text', '');
return !empty($defaultText) ? $defaultText : $matches[0];
} catch (Exception $e) {
// Log error if debug is enabled
if ($this->params->get('debug', 0)) {
Factory::getApplication()->enqueueMessage(
'Custom Fields Plugin Error: ' . $e->getMessage(),
'error'
);
}
return $matches[0]; // Return original shortcode on error
}
}
/**
* Get custom field value for a user
*
* @param integer $userId The user ID
* @param string $fieldName The field name
*
* @return mixed The field value or null if not found
*
* @since 1.0.0
*/
protected function getCustomFieldValue($userId, $fieldName)
{
try {
$db = Factory::getDbo();
// Search for field by name or title
$query = $db->getQuery(true)
->select('f.id, f.name, f.title, f.type, fv.value')
->from('#__fields AS f')
->leftJoin('#__fields_values AS fv ON f.id = fv.field_id AND fv.item_id = ' . (int) $userId)
->where('f.context = ' . $db->quote('com_users.user'))
->where('f.state = 1')
->where('(f.name = ' . $db->quote($fieldName) . ' OR f.title = ' . $db->quote($fieldName) . ')');
$result = $db->setQuery($query)->loadObject();
if ($result && isset($result->value) && $result->value !== '') {
return $result->value;
}
} catch (Exception $e) {
if ($this->params->get('debug', 0)) {
Factory::getApplication()->enqueueMessage(
'Database Error in Custom Fields Plugin: ' . $e->getMessage(),
'error'
);
}
}
return null;
}
/**
* Format field value for display
*
* @param mixed $value The field value
* @param string $fieldName The field name
*
* @return string The formatted value
*
* @since 1.0.0
*/
protected function formatFieldValue($value, $fieldName)
{
// Handle JSON values (for list fields, checkboxes, etc.)
if (is_string($value) && $this->isJson($value)) {
$decoded = json_decode($value, true);
if (is_array($decoded)) {
// Join array values with commas
return implode(', ', array_filter($decoded));
}
}
// Apply HTML output setting
$htmlOutput = $this->params->get('html_output', 0);
if ($htmlOutput) {
return $value; // Allow HTML
} else {
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); // Escape HTML
}
}
/**
* Check if string is valid JSON
*
* @param string $string The string to check
*
* @return boolean
*
* @since 1.0.0
*/
protected function isJson($string)
{
if (!is_string($string) || empty($string)) {
return false;
}
json_decode($string);
return json_last_error() === JSON_ERROR_NONE;
}
}