diff --git a/comments.php b/comments.php index 9a08a2a4..1f4e2f41 100644 --- a/comments.php +++ b/comments.php @@ -1,206 +1,14 @@ 'desc', - 'nested_depth' => 3, - 'require_login' => false, - 'allow_reply' => true, - 'avatar_size' => 48, - 'empty_text' => __('暂无评论,来做第一个评论的人吧~', 'aksrt'), - ]; -$require_login = !empty($comment_settings['require_login']); -$default_comment_order = ($comment_settings['order'] ?? 'desc') === 'asc' ? 'oldest' : 'newest'; -$comment_order = isset($_GET['aksrt_comment_order']) ? sanitize_key((string) wp_unslash($_GET['aksrt_comment_order'])) : $default_comment_order; -$comment_order = in_array($comment_order, ['newest', 'oldest', 'hottest'], true) ? $comment_order : 'newest'; -$reverse_comments = ($comment_order === 'newest'); -$is_hottest = ($comment_order === 'hottest'); - -if ($is_hottest) { - add_filter('comments_clauses', function ($clauses) { - global $wpdb; - $clauses['join'] .= " LEFT JOIN {$wpdb->commentmeta} AS aksrt_cm ON ({$wpdb->comments}.comment_ID = aksrt_cm.comment_id AND aksrt_cm.meta_key = 'aksrt_comment_likes')"; - $clauses['orderby'] = 'CAST(aksrt_cm.meta_value AS UNSIGNED) DESC, ' . $clauses['orderby']; - return $clauses; - }); -} -$author_only = isset($_GET['aksrt_author_only']) && (int) $_GET['aksrt_author_only'] === 1; -$post_author_id = (int) get_the_author_meta('ID'); -$avatar_size = max(24, (int) ($comment_settings['avatar_size'] ?? 48)); -$max_depth = max(1, (int) ($comment_settings['nested_depth'] ?? 3)); -$allow_reply = !empty($comment_settings['allow_reply']); -$empty_text = (string) ($comment_settings['empty_text'] ?? __('暂无评论,来做第一个评论的人吧~', 'aksrt')); -$comment_toolbar = is_array($comment_settings['toolbar'] ?? null) ? $comment_settings['toolbar'] : ['smilie' => true, 'code' => true, 'image' => true]; -$comment_upload_enabled = !empty($comment_settings['toolbar']['upload_img']); -$comment_box_placeholder = (string) ($comment_settings['box_placeholder'] ?? __('说点什么吧...', 'aksrt')); -$comment_submit_text = (string) ($comment_settings['submit_text'] ?? __('提交评论', 'aksrt')); -$comment_title_text = (string) ($comment_settings['title'] ?? __('评论', 'aksrt')); -set_query_var('aksrt_comment_order', $comment_order); -set_query_var('aksrt_comment_settings', $comment_settings); -?> - -get_auth_providers_data(); -} -?> - -
-
-
- - -
-
- - - -
- - - - -
- - - -
- -
- -
-
-
-
-
- - - -
- -
- -
- - -
- -
- - - - - -
-
- -
- -
- -
-
-
- - - - -
-
- - - - - - -
- - 0) : ?> - - -
- - -
-
    - 'ol', - 'short_ping' => true, - 'avatar_size' => $avatar_size, - 'max_depth' => $max_depth, - 'callback' => 'aksrt_comment_callback', - 'reverse_top_level' => $reverse_comments, - 'reverse_children' => $reverse_comments, - ]); - ?> -
-
- - - - -
-
- -
-

-

-
- - - -

- -
diff --git a/inc/bootstrap.php b/inc/bootstrap.php index ba1491f5..e57c0a12 100644 --- a/inc/bootstrap.php +++ b/inc/bootstrap.php @@ -30,6 +30,7 @@ require_once AKSRT_DIR . '/inc/helpers/aksrt-homepage.php'; require_once AKSRT_DIR . '/inc/helpers/aksrt-single.php'; require_once AKSRT_DIR . '/inc/helpers/aksrt-archive.php'; require_once AKSRT_DIR . '/inc/helpers/aksrt-archive-filters.php'; +require_once AKSRT_DIR . '/inc/helpers/aksrt-comments.php'; require_once AKSRT_DIR . '/inc/helpers/aksrt-slider.php'; require_once AKSRT_DIR . '/inc/helpers/aksrt-vip.php'; require_once AKSRT_DIR . '/inc/helpers/aksrt-i18n-switch.php'; diff --git a/inc/helpers/aksrt-comments.php b/inc/helpers/aksrt-comments.php new file mode 100644 index 00000000..dfb5917c --- /dev/null +++ b/inc/helpers/aksrt-comments.php @@ -0,0 +1,322 @@ + + */ + function aksrt_comments_build_context(): array { + $settings = class_exists('AKSRT_Settings') + ? AKSRT_Settings::get_comment_display_settings() + : [ + 'order' => 'desc', + 'nested_depth' => 3, + 'require_login' => false, + 'allow_reply' => true, + 'avatar_size' => 48, + 'empty_text' => __('暂无评论,来做第一个评论的人吧~', 'aksrt'), + ]; + + $default_order = (($settings['order'] ?? 'desc') === 'asc') ? 'oldest' : 'newest'; + $order = isset($_GET['aksrt_comment_order']) + ? sanitize_key((string) wp_unslash($_GET['aksrt_comment_order'])) + : $default_order; + if (!in_array($order, ['newest', 'oldest', 'hottest'], true)) { + $order = 'newest'; + } + + $avatar_size = max(24, (int) ($settings['avatar_size'] ?? 48)); + $toolbar = is_array($settings['toolbar'] ?? null) + ? $settings['toolbar'] + : ['smilie' => true, 'code' => true, 'image' => true]; + + return [ + 'settings' => $settings, + 'comment_count' => (int) get_comments_number(), + 'is_logged_in' => is_user_logged_in(), + 'current_user' => wp_get_current_user(), + 'require_login' => !empty($settings['require_login']), + 'order' => $order, + 'reverse_comments' => $order === 'newest', + 'is_hottest' => $order === 'hottest', + 'author_only' => isset($_GET['aksrt_author_only']) && (int) $_GET['aksrt_author_only'] === 1, + 'post_author_id' => (int) get_the_author_meta('ID'), + 'avatar_size' => $avatar_size, + 'max_depth' => max(1, (int) ($settings['nested_depth'] ?? 3)), + 'allow_reply' => !empty($settings['allow_reply']), + 'empty_text' => (string) ($settings['empty_text'] ?? __('暂无评论,来做第一个评论的人吧~', 'aksrt')), + 'toolbar' => $toolbar, + 'upload_enabled' => !empty($toolbar['upload_img']), + 'box_placeholder' => (string) ($settings['box_placeholder'] ?? __('说点什么吧...', 'aksrt')), + 'submit_text' => (string) ($settings['submit_text'] ?? __('提交评论', 'aksrt')), + 'title_text' => (string) ($settings['title'] ?? __('评论', 'aksrt')), + 'corderby_enabled' => !empty($settings['corderby']), + 'oauth_providers' => class_exists('AKSRT_OAuth') + ? AKSRT_OAuth::get_instance()->get_auth_providers_data() + : [], + ]; + } +endif; + +if (!function_exists('aksrt_comments_apply_hottest_order')) : + /** + * Apply hottest-order SQL once for the current comments query. + */ + function aksrt_comments_apply_hottest_order(): void { + static $applied = false; + if ($applied) { + return; + } + $applied = true; + + add_filter('comments_clauses', static function ($clauses) { + global $wpdb; + $clauses['join'] .= " LEFT JOIN {$wpdb->commentmeta} AS aksrt_cm ON ({$wpdb->comments}.comment_ID = aksrt_cm.comment_id AND aksrt_cm.meta_key = 'aksrt_comment_likes')"; + $clauses['orderby'] = 'CAST(aksrt_cm.meta_value AS UNSIGNED) DESC, ' . $clauses['orderby']; + return $clauses; + }); + } +endif; + +if (!function_exists('aksrt_comments_render_signarea')) : + /** + * Login-required gate. + * + * @param array $ctx Comments context. + */ + function aksrt_comments_render_signarea(array $ctx): void { + $oauth_providers = is_array($ctx['oauth_providers'] ?? null) ? $ctx['oauth_providers'] : []; + ?> +
+ + + + +
+ + + +
+ +
+ $ctx Comments context. + */ + function aksrt_comments_render_form(array $ctx): void { + $is_logged_in = !empty($ctx['is_logged_in']); + $current_user = $ctx['current_user'] ?? null; + $avatar_size = (int) $ctx['avatar_size']; + $toolbar = is_array($ctx['toolbar'] ?? null) ? $ctx['toolbar'] : []; + $upload_enabled = !empty($ctx['upload_enabled']); + ?> +
+
+
+
+
+ + + +
+ +
+ +
+ + +
+ +
+ + + + + +
+
+ +
+ +
+ +
+
+
+ + + + +
+
+ $ctx Comments context. + */ + function aksrt_comments_render_filter(array $ctx): void { + if (empty($ctx['corderby_enabled'])) { + return; + } + $order = (string) $ctx['order']; + $author_only = !empty($ctx['author_only']); + $post_author_id = (int) $ctx['post_author_id']; + ?> +
+ + 0) : ?> + + +
+ $ctx Comments context. + */ + function aksrt_comments_render_list(array $ctx): void { + ?> +
+
    + 'ol', + 'short_ping' => true, + 'avatar_size' => (int) $ctx['avatar_size'], + 'max_depth' => (int) $ctx['max_depth'], + 'callback' => 'aksrt_comment_callback', + 'reverse_top_level' => !empty($ctx['reverse_comments']), + 'reverse_children' => !empty($ctx['reverse_comments']), + ]); + ?> +
+
+ $ctx Comments context. + */ + function aksrt_comments_render_empty(array $ctx): void { + ?> +
+
+ +
+

+

+
+ +
+
+
+ + +
+
+ + + + + + + + + + + + + + + + + +

+ +
+