'onAfterInitialise', 'onExtensionAfterSave' => 'onExtensionAfterSave', ]; } /** * Apply dev mode settings at runtime. */ public function onAfterInitialise(): void { if (!$this->params->get('dev_mode', 0)) { return; } $config = Factory::getConfig(); $config->set('caching', 0); $config->set('debug', 1); // Show offline page on primary domain $primaryDomain = $this->params->get('primary_domain', ''); $currentHost = $_SERVER['HTTP_HOST'] ?? ''; if (!empty($primaryDomain) && $currentHost === $primaryDomain) { $config->set('offline', 1); } // Suppress hit recording by disabling the content hit counter $config->set('record_hits', 0); } /** * Handle maintenance actions when this plugin's params are saved. */ public function onExtensionAfterSave($event): void { // Joomla 6: single event object; Joomla 5: individual args if (is_object($event) && method_exists($event, 'getArgument')) { $context = $event->getArgument('context', $event->getArgument(0, '')); $table = $event->getArgument('subject', $event->getArgument(1, null)); } else { $context = $event; $table = func_get_arg(1); } if ($context !== 'com_plugins.plugin' || !$table) { return; } // Only process saves to this plugin if (($table->element ?? '') !== 'mokosuiteclient_devtools' || ($table->folder ?? '') !== 'system') { return; } $params = new \Joomla\Registry\Registry($table->params ?? '{}'); // Reset hits on save if toggled on if ($params->get('reset_hits', 0)) { $this->resetAllHits(); $params->set('reset_hits', 0); } // Delete versions on save if toggled on if ($params->get('delete_versions', 0)) { $this->deleteAllVersions(); $params->set('delete_versions', 0); } // Reset download keys on save if toggled on if ($params->get('reset_download_keys', 0)) { $this->resetDownloadKeys(); $params->set('reset_download_keys', 0); } // Reset the one-shot toggles if ($table->params !== $params->toString()) { $db = Factory::getDbo(); $db->setQuery( $db->getQuery(true) ->update($db->quoteName('#__extensions')) ->set($db->quoteName('params') . ' = ' . $db->quote($params->toString())) ->where($db->quoteName('extension_id') . ' = ' . (int) $table->extension_id) )->execute(); } } private function resetAllHits(): int { $db = Factory::getDbo(); $db->setQuery( $db->getQuery(true) ->update($db->quoteName('#__content')) ->set($db->quoteName('hits') . ' = 0') ->where($db->quoteName('hits') . ' > 0') )->execute(); $count = $db->getAffectedRows(); $this->getApplication()->enqueueMessage(\sprintf('Reset hits on %d articles.', $count), 'message'); return $count; } private function deleteAllVersions(): int { $db = Factory::getDbo(); $db->setQuery( $db->getQuery(true)->delete($db->quoteName('#__history')) )->execute(); $count = $db->getAffectedRows(); $this->getApplication()->enqueueMessage(\sprintf('Deleted %d version history records.', $count), 'message'); return $count; } private function resetDownloadKeys(): int { $db = Factory::getDbo(); // Find update sites that have a dlid in extra_query $db->setQuery( $db->getQuery(true) ->select([$db->quoteName('update_site_id'), $db->quoteName('extra_query')]) ->from($db->quoteName('#__update_sites')) ->where($db->quoteName('extra_query') . ' LIKE ' . $db->quote('%dlid=%')) ); $sites = $db->loadObjectList(); $count = 0; foreach ($sites as $site) { // Parse the query string, remove dlid, rebuild parse_str($site->extra_query, $parsed); unset($parsed['dlid']); $newQuery = http_build_query($parsed); $db->setQuery( $db->getQuery(true) ->update($db->quoteName('#__update_sites')) ->set($db->quoteName('extra_query') . ' = ' . $db->quote($newQuery)) ->where($db->quoteName('update_site_id') . ' = ' . (int) $site->update_site_id) )->execute(); $count++; } $this->getApplication()->enqueueMessage(\sprintf('Cleared download keys from %d update sites.', $count), 'message'); return $count; } }