// plugins/content/adunlock/adunlock.php defined('_JEXEC') or die; use Joomla\CMS\Plugin\CMSPlugin; use Joomla\CMS\Factory; class PlgContentAdunlock extends CMSPlugin { public function onContentPrepare($context, &$article, &$params, $page = 0) { // 只在文章视图工作 if ($context != 'com_content.article') { return; } $user = Factory::getUser(); $articleId = $article->id; // 检查是否是锁定文章 $db = Factory::getDbo(); $query = $db->getQuery(true) ->select('params') ->from('#__modules') ->where('module = ' . $db->quote('mod_adunlock')) ->where('published = 1'); $db->setQuery($query); $modules = $db->loadObjectList(); foreach ($modules as $module) { $moduleParams = json_decode($module->params); if ($moduleParams->content_id == $articleId) { // 检查解锁状态 require_once JPATH_ROOT . '/modules/mod_adunlock/helper.php'; $unlockDuration = isset($moduleParams->unlock_duration) ? (int)$moduleParams->unlock_duration : 24; $unlocked = ModAdunlockHelper::isContentUnlocked($articleId, $user->id, $unlockDuration); if (!$unlocked) { // 替换文章内容 $article->text = '
'; $article->text .= '
'; $article->text .= '

Premium Content Locked

'; $article->text .= '

This article requires viewing an advertisement to unlock.

'; $article->text .= '
'; $article->text .= '{loadmoduleid ' . $module->id . '}'; $article->text .= '
'; } break; } } } }