Skip to content

Commit

Permalink
Optimized System plugin & Clean widget data when article is deleted
Browse files Browse the repository at this point in the history
  • Loading branch information
sonvnn committed Sep 15, 2024
1 parent 5ff2d09 commit 914dd6f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 38 deletions.
16 changes: 16 additions & 0 deletions framework/library/astroid/Helper/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
use Astroid\Framework;
use Astroid\Helper;
use Joomla\CMS\Uri\Uri;
use Joomla\Filesystem\Folder;
use Joomla\Filesystem\File;
use Joomla\Filesystem\Path;
use Joomla\Registry\Registry;
use Joomla\CMS\Factory;
use Joomla\CMS\Form\Form;
Expand Down Expand Up @@ -281,6 +284,19 @@ public function onInstallerAfterInstaller($package)
return true;
}

public function onContentAfterDelete($context, $article): void
{
if ($context === 'com_content.article' && isset($article->id)) {
$templates = Folder::folders(JPATH_SITE . '/media/templates/site');
foreach ($templates as $template) {
if (file_exists(Path::clean(JPATH_SITE . '/media/templates/site/' . $template . '/astroid/article_widget_data'))) {
$files = Folder::files(JPATH_SITE . '/media/templates/site/' . $template . '/astroid/article_widget_data', '^'. $article->id . '_', false, true);
File::delete($files);
}
}
}
}

public function saveArticleElement() {
try {
// Check for request forgeries.
Expand Down
78 changes: 40 additions & 38 deletions plugins/astroid/src/Extension/AstroidPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,58 +42,52 @@ public function onAfterRoute()

public function onContentPrepareForm($form, $data)
{
if (!file_exists(JPATH_LIBRARIES . '/astroid/framework/library/astroid')) {
return false;
if (defined('_ASTROID')) {
Framework::getClient()->onContentPrepareForm($form, $data);
}
Framework::getClient()->onContentPrepareForm($form, $data);
}

public function onContentBeforeSave($context, $table, $isNew, $data = null)
{
if (!file_exists(JPATH_LIBRARIES . '/astroid/framework/library/astroid')) {
return false;
if (defined('_ASTROID')) {
return Framework::getClient()->onContentBeforeSave($context, $table, $isNew, $data);
}

return Framework::getClient()->onContentBeforeSave($context, $table, $isNew, $data);
}

public function onBeforeRender()
{
if (!file_exists(JPATH_LIBRARIES . '/astroid/framework/library/astroid')) {
return false;
if (defined('_ASTROID')) {
Framework::getClient()->onBeforeRender();
}
Framework::getClient()->onBeforeRender();
}

public function onAfterRender()
{
if (!file_exists(JPATH_LIBRARIES . '/astroid/framework/library/astroid')) {
return false;
if (defined('_ASTROID')) {
Framework::getClient()->onAfterRender();
}
Framework::getClient()->onAfterRender();
}

public function onAfterRespond()
{
if (!file_exists(JPATH_LIBRARIES . '/astroid/framework/library/astroid')) {
return false;
}
if (!(Helper::getPluginParams()->get('astroid_debug', 0)) || Framework::isAdmin()) {
return false;
}
if (defined('_ASTROID')) {
if (!(Helper::getPluginParams()->get('astroid_debug', 0)) || Framework::isAdmin()) {
return;
}

$cache = PluginHelper::getPlugin('system', 'cache');
if (Framework::isSite() && !empty($cache)) {
return false;
}
$cache = PluginHelper::getPlugin('system', 'cache');
if (Framework::isSite() && !empty($cache)) {
return;
}

// Capture output.
$contents = ob_get_contents();
// Capture output.
$contents = ob_get_contents();

if ($contents) {
ob_end_clean();
if ($contents) {
ob_end_clean();
}
echo Helper::str_lreplace('</body>', Helper::debug() . '</body>', $contents);
}
echo Helper::str_lreplace('</body>', Helper::debug() . '</body>', $contents);
}

public function onContentPrepareData($context, $user)
Expand Down Expand Up @@ -131,21 +125,22 @@ public function onUserAfterSave($user, $isnew, $success, $msg): void

public function onInstallerAfterInstaller($installmodel, $package, $installer, $result)
{
if (!$result || Framework::isSite()) {
return false;
if (defined('_ASTROID')) {
if (!$result || Framework::isSite()) {
return false;
}
Framework::getClient()->onInstallerAfterInstaller($package);
}
Framework::getClient()->onInstallerAfterInstaller($package);
}

public function onExtensionAfterSave($context, $table, $isNew)
{
if (!file_exists(JPATH_LIBRARIES . '/astroid/framework/library/astroid')) {
return false;
}
if (Framework::isAdmin() && $context == "com_templates.style" && $isNew && Template::isAstroidTemplate(JPATH_SITE . "/templates/{$table->template}/templateDetails.xml")) {
$params = \json_decode($table->params, TRUE);
$parent_id = $params['astroid'];
Template::setTemplateDefaults($table->template, $table->id, $parent_id);
if (defined('_ASTROID')) {
if (Framework::isAdmin() && $context == "com_templates.style" && $isNew && Template::isAstroidTemplate(JPATH_SITE . "/templates/{$table->template}/templateDetails.xml")) {
$params = \json_decode($table->params, TRUE);
$parent_id = $params['astroid'];
Template::setTemplateDefaults($table->template, $table->id, $parent_id);
}
}
}

Expand All @@ -156,4 +151,11 @@ public function onAfterAstroidFormLoad($template, $form)
$form->removeField('presets', 'params');
}
}

public function onContentAfterDelete($context, $article): void
{
if (defined('_ASTROID')) {
Framework::getClient()->onContentAfterDelete($context, $article);
}
}
}

0 comments on commit 914dd6f

Please sign in to comment.