Skip to content

Commit

Permalink
Fix preg_match_all(): Passing null to parameter #2 ($subject) of type…
Browse files Browse the repository at this point in the history
… string is deprecated

Search only for modules in the articletext if it is not empty
  • Loading branch information
LadySolveig committed Oct 7, 2024
1 parent edf53e8 commit 54224b8
Showing 1 changed file with 62 additions and 60 deletions.
122 changes: 62 additions & 60 deletions administrator/components/com_content/src/Model/ArticleModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -447,85 +447,87 @@ public function getItem($pk = null)
}
}

// Expression to search for (positions)
$regex = '/{loadposition\s(.*?)}/i';
if ($item->articletext) {
// Expression to search for (positions)
$regex = '/{loadposition\s(.*?)}/i';

// Expression to search for (modules)
$regexmod = '/{loadmodule\s(.*?)}/i';
// Expression to search for (modules)
$regexmod = '/{loadmodule\s(.*?)}/i';

// Expression to search for (id)
$regexmodid = '/{loadmoduleid\s([1-9][0-9]*)}/i';
// 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, $item->articletext, $matches, PREG_SET_ORDER);
$item->importedPositions = [];
/**
* 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, $item->articletext, $matches, PREG_SET_ORDER);
$item->importedPositions = [];

// No matches, skip this
if ($matches) {
foreach ($matches as $match) {
$matcheslist = explode(',', $match[1]);
// No matches, skip this
if ($matches) {
foreach ($matches as $match) {
$matcheslist = explode(',', $match[1]);

$position = trim($matcheslist[0]);
$style = array_key_exists(1, $matcheslist) ? trim($matcheslist[1]) : 'none';
$position = trim($matcheslist[0]);
$style = array_key_exists(1, $matcheslist) ? trim($matcheslist[1]) : 'none';

$item->importedPositions[] = array('name' => $position, 'style' => $style, 'editorText' => $match[0]);
$item->importedPositions[] = array('name' => $position, 'style' => $style, 'editorText' => $match[0]);
}
}
}

// Find all instances of plugin and put in $matchesmod for loadmodule
preg_match_all($regexmod, $item->articletext, $matchesmod, PREG_SET_ORDER);
// Find all instances of plugin and put in $matchesmod for loadmodule
preg_match_all($regexmod, $item->articletext, $matchesmod, PREG_SET_ORDER);

// If no matches, skip this
if ($matchesmod) {
foreach ($matchesmod as $matchmod) {
$matchesmodlist = explode(',', $matchmod[1]);
// If no matches, skip this
if ($matchesmod) {
foreach ($matchesmod as $matchmod) {
$matchesmodlist = explode(',', $matchmod[1]);

$module = trim($matchesmodlist[0]);
$name = array_key_exists(1, $matchesmodlist) ? htmlspecialchars_decode(trim($matchesmodlist[1])) : null;
$module = trim($matchesmodlist[0]);
$name = array_key_exists(1, $matchesmodlist) ? htmlspecialchars_decode(trim($matchesmodlist[1])) : null;

$mod = ModuleHelper::getModule($module, $name);
$mod = ModuleHelper::getModule($module, $name);

/**
* 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)) {
$mod = ModuleHelper::getModule('mod_' . $module, $name);
}
/**
* 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)) {
$mod = ModuleHelper::getModule('mod_' . $module, $name);
}

if (isset($mod)) {
$mod->editorText = $matchmod[0];
$item->importedModuleTypes[] = $mod;
if (isset($mod)) {
$mod->editorText = $matchmod[0];
$item->importedModuleTypes[] = $mod;
}
}
}
}

// Find all instances of loadmoduleid and store it in $matchesmodid
preg_match_all($regexmodid, $item->articletext, $matchesmodid, PREG_SET_ORDER);
$importedModules = [];
// Find all instances of loadmoduleid and store it in $matchesmodid
preg_match_all($regexmodid, $item->articletext, $matchesmodid, PREG_SET_ORDER);
$importedModules = [];

// If no matches, skip this
if ($matchesmodid) {
foreach ($matchesmodid as $match) {
$importedModules[] = $match[1];
}
// If no matches, skip this
if ($matchesmodid) {
foreach ($matchesmodid as $match) {
$importedModules[] = $match[1];
}

$db = $this->getDbo();
$query = $db->getQuery(true)
->select(
[
$db->quoteName('id'),
$db->quoteName('title'),
$db->quoteName('published'),
]
)
->from($db->quoteName('#__modules'));
$query->where($db->quoteName('id') . ' IN (' . implode(',', $query->bindArray(array_values($importedModules))) . ')');
$db = $this->getDbo();
$query = $db->getQuery(true)
->select(
[
$db->quoteName('id'),
$db->quoteName('title'),
$db->quoteName('published'),
]
)
->from($db->quoteName('#__modules'));
$query->where($db->quoteName('id') . ' IN (' . implode(',', $query->bindArray(array_values($importedModules))) . ')');

$item->importedModules = $db->setQuery($query)->loadObjectList();
$item->importedModules = $db->setQuery($query)->loadObjectList();
}
}

return $item;
Expand Down

0 comments on commit 54224b8

Please sign in to comment.