* fix #1816

* fix call stack

* fix typo error
This commit is contained in:
joyqi 2025-01-07 19:42:15 +08:00 committed by GitHub
parent 7d51b210c9
commit 6ddf1c1a23
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
14 changed files with 65 additions and 37 deletions

View File

@ -8,7 +8,7 @@ $header = '<link rel="stylesheet" href="' . $options->adminStaticUrl('css', 'nor
<link rel="stylesheet" href="' . $options->adminStaticUrl('css', 'style.css', true) . '">';
/** 注册一个初始化插件 */
$header = \Typecho\Plugin::factory('admin/header.php')->call('header', $header);
$header = \Typecho\Plugin::factory('admin/header.php')->filter('header', $header);
?><!DOCTYPE HTML>
<html>

View File

@ -17,10 +17,10 @@ if (!defined('__TYPECHO_ROOT_DIR__') && !@include_once 'config.inc.php') {
\Widget\Init::alloc();
/** 注册一个初始化插件 */
\Typecho\Plugin::factory('index.php')->begin();
\Typecho\Plugin::factory('index.php')->call('begin');
/** 开始路由分发 */
\Typecho\Router::dispatch();
/** 注册一个结束插件 */
\Typecho\Plugin::factory('index.php')->end();
\Typecho\Plugin::factory('index.php')->call('end');

View File

@ -435,19 +435,47 @@ class Plugin
*/
public function call(string $component, ...$args)
{
$component = $this->handle . ':' . $component;
$last = count($args);
$args[$last] = $last > 0 ? $args[0] : false;
$componentKey = $this->handle . ':' . $component;
if (isset(self::$plugin['handles'][$component])) {
$args[$last] = null;
$this->signal = true;
foreach (self::$plugin['handles'][$component] as $callback) {
$args[$last] = call_user_func_array($callback, $args);
}
if (!isset(self::$plugin['handles'][$componentKey])) {
return null;
}
return $args[$last];
$return = null;
$this->signal = true;
foreach (self::$plugin['handles'][$componentKey] as $callback) {
$return = call_user_func_array($callback, $args);
}
return $return;
}
/**
* 过滤处理函数
*
* @param string $component 当前组件
* @param mixed $value
* @param array $args 参数
* @return mixed
*/
public function filter(string $component, $value, ...$args)
{
$componentKey = $this->handle . ':' . $component;
if (!isset(self::$plugin['handles'][$componentKey])) {
return $value;
}
$result = $value;
$this->signal = true;
foreach (self::$plugin['handles'][$componentKey] as $callback) {
$currentArgs = array_merge([$result], $args, [$result]);
$result = call_user_func_array($callback, $currentArgs);
}
return $result;
}
/**

View File

@ -967,7 +967,7 @@ class Archive extends Contents
$allows = array_merge($allows, $rules);
}
$allows = self::pluginHandle()->call('headerOptions', $allows, $this);
$allows = self::pluginHandle()->filter('headerOptions', $allows, $this);
$title = (empty($this->archiveTitle) ? '' : $this->archiveTitle . ' &raquo; ') . $this->options->title;
$header = $this->is('single') ? '<link rel="canonical" href="' . $this->archiveUrl . '" />' . "\n" : '';

View File

@ -244,7 +244,7 @@ class Comments extends Base implements QueryInterface, RowFilterInterface, Prima
$row['text'] = $row['text'] ?? '';
$row['date'] = new Date($row['created']);
return Comments::pluginHandle()->call('filter', $row, $this);
return Comments::pluginHandle()->filter('filter', $row, $this);
}
/**
@ -346,7 +346,7 @@ class Comments extends Base implements QueryInterface, RowFilterInterface, Prima
*/
public function markdown(?string $text): ?string
{
$html = Comments::pluginHandle()->trigger($parsed)->call('markdown', $text);
$html = Comments::pluginHandle()->trigger($parsed)->filter('markdown', $text);
if (!$parsed) {
$html = Markdown::convert($text);
@ -363,7 +363,7 @@ class Comments extends Base implements QueryInterface, RowFilterInterface, Prima
*/
public function autoP(?string $text): ?string
{
$html = Comments::pluginHandle()->trigger($parsed)->call('autoP', $text);
$html = Comments::pluginHandle()->trigger($parsed)->filter('autoP', $text);
if (!$parsed) {
static $parser;
@ -483,13 +483,13 @@ class Comments extends Base implements QueryInterface, RowFilterInterface, Prima
{
$text = $this->parentContent->hidden ? _t('内容被隐藏') : $this->text;
$text = Comments::pluginHandle()->trigger($plugged)->call('content', $text, $this);
$text = Comments::pluginHandle()->trigger($plugged)->filter('content', $text, $this);
if (!$plugged) {
$text = $this->options->commentsMarkdown ? $this->markdown($text)
: $this->autoP($text);
}
$text = Comments::pluginHandle()->call('contentEx', $text, $this);
$text = Comments::pluginHandle()->filter('contentEx', $text, $this);
return Common::stripTags($text, '<p><br>' . $this->options->commentsHTMLTagAllowed);
}

View File

@ -359,7 +359,7 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
$row['password'] = $row['password'] ?? '';
$row['date'] = new Date($row['created']);
return Contents::pluginHandle()->call('filter', $row, $this);
return Contents::pluginHandle()->filter('filter', $row, $this);
}
/**
@ -404,7 +404,7 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
*/
public function title(int $length = 0, string $trim = '...')
{
$title = Contents::pluginHandle()->trigger($plugged)->call('title', $this->title, $this);
$title = Contents::pluginHandle()->trigger($plugged)->filter('title', $this->title, $this);
if (!$plugged) {
echo $length > 0 ? Common::subStr($this->title, 0, $length, $trim) : $this->title;
} else {
@ -772,10 +772,10 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
return $this->text;
}
$content = Contents::pluginHandle()->call('excerpt', $this->content, $this);
$content = Contents::pluginHandle()->filter('excerpt', $this->content, $this);
[$excerpt] = explode('<!--more-->', $content);
return Common::fixHtml(Contents::pluginHandle()->call('excerptEx', $excerpt, $this));
return Common::fixHtml(Contents::pluginHandle()->filter('excerptEx', $excerpt, $this));
}
/**
@ -798,7 +798,7 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
*/
protected function markdown(?string $text): ?string
{
$html = Contents::pluginHandle()->trigger($parsed)->call('markdown', $text);
$html = Contents::pluginHandle()->trigger($parsed)->filter('markdown', $text);
if (!$parsed) {
$html = Markdown::convert($text);
@ -815,7 +815,7 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
*/
protected function autoP(?string $text): ?string
{
$html = Contents::pluginHandle()->trigger($parsed)->call('autoP', $text);
$html = Contents::pluginHandle()->trigger($parsed)->filter('autoP', $text);
if (!$parsed && $text) {
static $parser;
@ -841,14 +841,14 @@ class Contents extends Base implements QueryInterface, RowFilterInterface, Prima
return $this->text;
}
$content = Contents::pluginHandle()->trigger($plugged)->call('content', $this->text, $this);
$content = Contents::pluginHandle()->trigger($plugged)->filter('content', $this->text, $this);
if (!$plugged) {
$content = $this->isMarkdown ? $this->markdown($content)
: $this->autoP($content);
}
return Contents::pluginHandle()->call('contentEx', $content, $this);
return Contents::pluginHandle()->filter('contentEx', $content, $this);
}
/**

View File

@ -93,7 +93,7 @@ class Metas extends Base implements QueryInterface, RowFilterInterface, PrimaryK
*/
public function filter(array $row): array
{
return Metas::pluginHandle()->call('filter', $row, $this);
return Metas::pluginHandle()->filter('filter', $row, $this);
}
/**

View File

@ -64,7 +64,7 @@ class Users extends Base implements QueryInterface, RowFilterInterface, PrimaryK
*/
public function filter(array $row): array
{
return Users::pluginHandle()->call('filter', $row, $this);
return Users::pluginHandle()->filter('filter', $row, $this);
}
/**

View File

@ -300,7 +300,7 @@ class Edit extends Comments implements ActionInterface
}
/** 评论插件接口 */
$comment = self::pluginHandle()->call('edit', $comment, $this);
$comment = self::pluginHandle()->filter('edit', $comment, $this);
/** 更新评论 */
$this->update($comment, $this->db->sql()->where('coid = ?', $coid));

View File

@ -66,7 +66,7 @@ class Edit extends Contents implements ActionInterface
$contents['text'] = '<!--markdown-->' . $contents['text'];
}
$contents = self::pluginHandle()->call('write', $contents, $this);
$contents = self::pluginHandle()->filter('write', $contents, $this);
if ($this->request->is('do=publish')) {
/** 重新发布已经存在的文章 */

View File

@ -63,7 +63,7 @@ class Edit extends Contents implements ActionInterface
$contents['text'] = '<!--markdown-->' . $contents['text'];
}
$contents = self::pluginHandle()->call('write', $contents, $this);
$contents = self::pluginHandle()->filter('write', $contents, $this);
if ($this->request->is('do=publish')) {
/** 重新发布已经存在的文章 */

View File

@ -255,7 +255,7 @@ class Feedback extends Comments implements ActionInterface
/** 生成过滤器 */
try {
$comment = self::pluginHandle()->call('comment', $comment, $this->content);
$comment = self::pluginHandle()->filter('comment', $comment, $this->content);
} catch (\Typecho\Exception $e) {
Cookie::set('__typecho_remember_text', $comment['text']);
throw $e;
@ -341,7 +341,7 @@ class Feedback extends Comments implements ActionInterface
}
/** 生成过滤器 */
$trackback = self::pluginHandle()->call('trackback', $trackback, $this->content);
$trackback = self::pluginHandle()->filter('trackback', $trackback, $this->content);
/** 添加引用 */
$this->insert($trackback);

View File

@ -80,7 +80,7 @@ class Register extends Users implements ActionInterface
'group' => 'subscriber'
];
$dataStruct = self::pluginHandle()->call('register', $dataStruct);
$dataStruct = self::pluginHandle()->filter('register', $dataStruct);
$insertId = $this->insert($dataStruct);
$this->db->fetchRow($this->select()->where('uid = ?', $insertId)

View File

@ -339,7 +339,7 @@ class XmlRpc extends Contents implements ActionInterface, Hook
$input['text'] = !empty($content['mt_text_more']) ? $content['description']
. "\n<!--more-->\n" . $content['mt_text_more'] : $content['description'];
$input['text'] = self::pluginHandle()->call('textFilter', $input['text'], $this);
$input['text'] = self::pluginHandle()->filter('textFilter', $input['text'], $this);
$input['password'] = $content["wp_password"] ?? null;
$input['order'] = $content["wp_page_order"] ?? null;
@ -1719,7 +1719,7 @@ class XmlRpc extends Contents implements ActionInterface, Hook
];
/** 加入plugin */
$pingback = self::pluginHandle()->call('pingback', $pingback, $post);
$pingback = self::pluginHandle()->filter('pingback', $pingback, $post);
/** 执行插入*/
$insertId = Comments::alloc()->insert($pingback);