diff --git a/admin/header.php b/admin/header.php index 6e1bf3d1..11f33f45 100644 --- a/admin/header.php +++ b/admin/header.php @@ -8,7 +8,7 @@ $header = ''; /** 注册一个初始化插件 */ -$header = \Typecho\Plugin::factory('admin/header.php')->call('header', $header); +$header = \Typecho\Plugin::factory('admin/header.php')->filter('header', $header); ?> diff --git a/index.php b/index.php index 25f2e738..982fba6f 100644 --- a/index.php +++ b/index.php @@ -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'); diff --git a/var/Typecho/Plugin.php b/var/Typecho/Plugin.php index 9357ad26..ce07da4b 100644 --- a/var/Typecho/Plugin.php +++ b/var/Typecho/Plugin.php @@ -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; } /** diff --git a/var/Widget/Archive.php b/var/Widget/Archive.php index 3b1e726e..ab370fab 100644 --- a/var/Widget/Archive.php +++ b/var/Widget/Archive.php @@ -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 . ' » ') . $this->options->title; $header = $this->is('single') ? '' . "\n" : ''; diff --git a/var/Widget/Base/Comments.php b/var/Widget/Base/Comments.php index 10a4948c..87cd9e92 100644 --- a/var/Widget/Base/Comments.php +++ b/var/Widget/Base/Comments.php @@ -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, '


' . $this->options->commentsHTMLTagAllowed); } diff --git a/var/Widget/Base/Contents.php b/var/Widget/Base/Contents.php index 91238962..a28ccec7 100644 --- a/var/Widget/Base/Contents.php +++ b/var/Widget/Base/Contents.php @@ -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('', $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); } /** diff --git a/var/Widget/Base/Metas.php b/var/Widget/Base/Metas.php index 77be56fb..30ba2d24 100644 --- a/var/Widget/Base/Metas.php +++ b/var/Widget/Base/Metas.php @@ -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); } /** diff --git a/var/Widget/Base/Users.php b/var/Widget/Base/Users.php index 5dbd6376..1c1a51fb 100644 --- a/var/Widget/Base/Users.php +++ b/var/Widget/Base/Users.php @@ -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); } /** diff --git a/var/Widget/Comments/Edit.php b/var/Widget/Comments/Edit.php index 271552c9..cfb0776f 100644 --- a/var/Widget/Comments/Edit.php +++ b/var/Widget/Comments/Edit.php @@ -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)); diff --git a/var/Widget/Contents/Page/Edit.php b/var/Widget/Contents/Page/Edit.php index 7d8f0f3c..9591c4b5 100644 --- a/var/Widget/Contents/Page/Edit.php +++ b/var/Widget/Contents/Page/Edit.php @@ -66,7 +66,7 @@ class Edit extends Contents implements ActionInterface $contents['text'] = '' . $contents['text']; } - $contents = self::pluginHandle()->call('write', $contents, $this); + $contents = self::pluginHandle()->filter('write', $contents, $this); if ($this->request->is('do=publish')) { /** 重新发布已经存在的文章 */ diff --git a/var/Widget/Contents/Post/Edit.php b/var/Widget/Contents/Post/Edit.php index ae50cbed..a88f1d08 100644 --- a/var/Widget/Contents/Post/Edit.php +++ b/var/Widget/Contents/Post/Edit.php @@ -63,7 +63,7 @@ class Edit extends Contents implements ActionInterface $contents['text'] = '' . $contents['text']; } - $contents = self::pluginHandle()->call('write', $contents, $this); + $contents = self::pluginHandle()->filter('write', $contents, $this); if ($this->request->is('do=publish')) { /** 重新发布已经存在的文章 */ diff --git a/var/Widget/Feedback.php b/var/Widget/Feedback.php index 291a2ed6..fd4674ed 100644 --- a/var/Widget/Feedback.php +++ b/var/Widget/Feedback.php @@ -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); diff --git a/var/Widget/Register.php b/var/Widget/Register.php index 1ca23606..cbc44f4f 100644 --- a/var/Widget/Register.php +++ b/var/Widget/Register.php @@ -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) diff --git a/var/Widget/XmlRpc.php b/var/Widget/XmlRpc.php index f31329fa..3a1b296d 100644 --- a/var/Widget/XmlRpc.php +++ b/var/Widget/XmlRpc.php @@ -339,7 +339,7 @@ class XmlRpc extends Contents implements ActionInterface, Hook $input['text'] = !empty($content['mt_text_more']) ? $content['description'] . "\n\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);