*/ public static function post_type_choices(): array { $choices = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $type ) { if ( post_type_supports( $type->name, 'comments' ) || 'product' === $type->name ) { $choices[ $type->name ] = $type->labels->name; } } return $choices; } /** * Types that keep their comments. * * @return string[] */ private static function kept(): array { return array_values( array_intersect( (array) aiou_get_setting( 'performance', Module::SECTION, 'except', array( 'product' ) ), array_keys( get_post_types() ) ) ); } /** * Whether a post's comments are off. * * @param int|\WP_Post|null $post Post. * @return bool */ private static function off_for( $post ): bool { $type = $post ? get_post_type( $post ) : false; return ! $type || ! in_array( $type, self::kept(), true ); } /** * Closed, except on kept types. * * @param bool $open Whether open. * @param int|\WP_Post $post_id Post. * @return bool */ public function closed( $open, $post_id = null ) { return self::off_for( $post_id ) ? false : $open; } /** * Hide posted comments. * * @param array $comments Comments. * @param int $post_id Post ID. * @return array */ public function hide_existing( $comments, $post_id = 0 ) { return self::off_for( $post_id ) && aiou_get_setting( 'performance', Module::SECTION, 'hide_existing', 0 ) ? array() : $comments; } /** * With "Existing comments" hidden, keep every front-end comment query * (the site-wide comment feed, Latest Comments block, Recent Comments * widget, REST for visitors) to the post types that keep comments, so * hidden comments don't leak out beside the post page. Admin screens, * and REST requests from users who can moderate, still see everything. * * @param \WP_Comment_Query $query Comment query. * @return void */ public function limit_comment_queries( \WP_Comment_Query $query ): void { if ( is_admin() || wp_doing_cron() || ( defined( 'WP_CLI' ) && WP_CLI ) || ! aiou_get_setting( 'performance', Module::SECTION, 'hide_existing', 0 ) ) { return; } if ( wp_is_serving_rest_request() && current_user_can( 'moderate_comments' ) ) { return; } $kept = self::kept(); $requested = array_filter( (array) ( $query->query_vars['post_type'] ?? '' ) ); $allowed = $requested ? array_values( array_intersect( $requested, $kept ) ) : $kept; if ( ! $allowed ) { $query->query_vars['comment__in'] = array( 0 ); return; } $query->query_vars['post_type'] = $allowed; } /** * The same limit for comment feeds, which WP_Query builds with its own * SQL rather than WP_Comment_Query (both the site-wide feed and a * single post's comment feed). * * @param string $where WHERE clause. * @return string */ public function limit_comment_feed( $where ) { if ( ! aiou_get_setting( 'performance', Module::SECTION, 'hide_existing', 0 ) ) { return $where; } global $wpdb; $kept = self::kept(); if ( ! $kept ) { return $where . ' AND 1=0'; } $types = implode( ', ', array_map( static fn( string $type ): string => "'" . esc_sql( $type ) . "'", $kept ) ); return $where . " AND {$wpdb->comments}.comment_post_ID IN ( SELECT ID FROM {$wpdb->posts} WHERE post_type IN ( {$types} ) )"; } /** * Report no comments where they're hidden. * * @param int|string $count Count. * @param int $post_id Post ID. * @return int|string */ public function count( $count, $post_id = 0 ) { return self::off_for( $post_id ) && aiou_get_setting( 'performance', Module::SECTION, 'hide_existing', 0 ) ? 0 : $count; } /** * Drop comment support from the other types (hides the editor panels). * * @return void */ public function remove_support(): void { foreach ( get_post_types() as $type ) { if ( ! in_array( $type, self::kept(), true ) && post_type_supports( $type, 'comments' ) ) { remove_post_type_support( $type, 'comments' ); remove_post_type_support( $type, 'trackbacks' ); } } } /** * Remove the X-Pingback header. * * @param array $headers Headers. * @return array */ public function remove_pingback_header( $headers ) { unset( $headers['X-Pingback'] ); return $headers; } /** * Remove the Recent Comments widget. * * @return void */ public function remove_widget(): void { unregister_widget( 'WP_Widget_Recent_Comments' ); } /** * Remove the Recent Comments dashboard box. * * @return void */ public function remove_dashboard_box(): void { remove_meta_box( 'dashboard_recent_comments', 'dashboard', 'normal' ); } /** * Remove the toolbar's comments node. * * @param \WP_Admin_Bar $bar Toolbar. * @return void */ public function remove_toolbar_node( $bar ): void { $bar->remove_node( 'comments' ); } /** * Remove XML-RPC's comment-writing methods. * * @param array $methods Methods. * @return array */ public function remove_xmlrpc_methods( $methods ) { unset( $methods['wp.newComment'], $methods['wp.editComment'], $methods['pingback.ping'] ); return $methods; } /** * Remove the Comments menu and Discussion settings. * * @return void */ public function remove_menu(): void { if ( self::kept() ) { return; } remove_menu_page( 'edit-comments.php' ); } /** * Send the Comments screen to the dashboard. * * @return void */ public function redirect_screen(): void { global $pagenow; if ( self::kept() ) { return; } if ( 'edit-comments.php' === $pagenow || 'comment.php' === $pagenow ) { wp_safe_redirect( admin_url() ); exit; } } /** * Remove the comments REST routes. * * @param array $endpoints Routes. * @return array */ public function remove_rest_routes( $endpoints ) { if ( self::kept() ) { return $endpoints; } foreach ( array_keys( (array) $endpoints ) as $route ) { if ( str_starts_with( (string) $route, '/wp/v2/comments' ) ) { unset( $endpoints[ $route ] ); } } return $endpoints; } /** * Hide the comments feed link when no type keeps comments. * * @param bool $show Whether to show it. * @return bool */ public function show_comments_feed( $show ) { return self::kept() ? $show : false; } /** * Send comment feeds to the homepage. * * @return void */ public function redirect_feed(): void { if ( ! self::kept() && is_comment_feed() ) { wp_safe_redirect( home_url( '/' ), 301 ); exit; } } }