settings = Plugin::get_settings( 'posts' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( empty( $this->settings['disable_comments_enabled'] ) ) { return; } add_filter( 'comments_open', [ $this, 'filter_comments_status' ], 20, 2 ); add_filter( 'pings_open', [ $this, 'filter_comments_status' ], 20, 2 ); add_filter( 'get_comments_number', [ $this, 'filter_comments_number' ], 20, 2 ); add_action( 'init', [ $this, 'remove_post_types_support' ], 100 ); add_action( 'wp_before_admin_bar_render', [ $this, 'remove_admin_bar_comments' ] ); add_filter( 'comments_template', [ $this, 'disable_comments_template' ], 20, 1 ); add_filter( 'comments_array', [ $this, 'disable_comments_array' ], 20, 2 ); if( ! empty( $this->settings['disable_comments_remove_screens'] ) ) { add_action( 'admin_menu', [ $this, 'remove_comments_menu' ], 999 ); add_action( 'load-edit-comments.php', [ $this, 'redirect_comments_screen' ] ); add_action( 'widgets_init', [ $this, 'remove_recent_comments_widget' ], 20 ); add_filter( 'rest_endpoints', [ $this, 'remove_comment_routes' ] ); } } /** * Adds custom settings fields for the AdminEase plugin configuration. * * @param array $fields The existing settings fields to which custom fields will be appended. * * @return array The modified settings fields including the added custom fields. */ public function adminease_settings_fields( array $fields ): array { $fields['posts']['fields'][] = [ 'type' => 'switch', 'id' => 'disable-comments-enabled', 'name' => 'adminease[posts][disable_comments_enabled]', 'value' => $this->settings['disable_comments_enabled'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Disable Comments', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'Comments disappear from the content types you pick, including product reviews if you pick Products.', 'adminease' ), 'description' => __( 'Disable new comments and pings on the content types you choose. This hides the discussion metabox in the editor and closes comments on the frontend, but keeps existing data accessible in the admin menu.', 'adminease' ), 'field_description' => __( 'Enable to disable comment functionality for the content types you choose.', 'adminease' ), 'conflicts' => [ 'woocommerce/woocommerce.php' => __( 'WooCommerce is active. Ticking Products here turns off product reviews.', 'adminease' ), ], 'child_fields' => [ [ 'type' => 'select', 'id' => 'disable-comments-post-types', 'name' => 'adminease[posts][disable_comments_post_types][]', 'value' => $this->get_saved_post_types(), 'options' => $this->get_post_type_options(), 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input adminease-choices', 'wrapper_class' => 'form-group-child', 'label' => __( 'Content types to close comments on', 'adminease' ), 'field_description' => __( 'Comments and pings are closed on every type listed here. Products are WooCommerce reviews.', 'adminease' ), 'attributes' => [ 'multiple' => 'multiple', 'data-allow_clear' => true, 'data-parent' => 'disable-comments-enabled', ], ], [ 'type' => 'switch', 'id' => 'disable-comments-remove-screens', 'name' => 'adminease[posts][disable_comments_remove_screens]', 'value' => $this->settings['disable_comments_remove_screens'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Also remove the Comments screens', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'Moderators lose the Comments screen.', 'adminease' ), 'field_description' => __( 'Works only when every type in the list above is ticked. Moderators will no longer see the Comments screen.', 'adminease' ), 'attributes' => [ 'data-parent' => 'disable-comments-enabled', ], ], ], ]; return $fields; } /** * Returns the saved list of post types, or the default of posts and pages. * * @return array */ private function get_saved_post_types(): array { $saved = $this->settings['disable_comments_post_types'] ?? []; if( ! is_array( $saved ) || empty( $saved ) ) { return [ 'post', 'page' ]; } return array_values( array_map( 'strval', $saved ) ); } /** * Lists every public post type that supports comments, plus every type already saved. * * Saved types stay in the list after their comment support is removed, so saving never refuses them. * * @return array Post type name => label. */ private function get_post_type_options(): array { $options = []; foreach( get_post_types( [ 'public' => true ], 'objects' ) as $name => $object ) { if( post_type_supports( $name, 'comments' ) ) { $options[ $name ] = $object->labels->name ?? $name; } } foreach( $this->get_saved_post_types() as $name ) { if( ! isset( $options[ $name ] ) ) { $object = get_post_type_object( $name ); $options[ $name ] = $object ? ( $object->labels->name ?? $name ) : $name; } } if( isset( $options['product'] ) ) { $options['product'] = __( 'Products (reviews)', 'adminease' ); } if( isset( $options['attachment'] ) ) { $options['attachment'] = __( 'Media files', 'adminease' ); } return $options; } /** * Determines if comments should be open based on the post type. * * Untyped on purpose: other plugins and themes run these filters with a post * object, null or a string id, and a type error would be fatal. * * @param bool $open Whether comments are open. * @param int|string|\WP_Post|null $post_id Post ID or post object. * * @return bool */ public function filter_comments_status( $open, $post_id = null ) { $post_type = get_post_type( $post_id ); if( !$post_type ) { return $open; } $disabled_post_types = $this->get_disabled_post_types(); if( in_array( $post_type, $disabled_post_types, true ) ) { return false; } return $open; } /** * Reports zero comments for the chosen post types. * * @param int|string $count The comment count. * @param int|string|\WP_Post|null $post_id Post ID or post object. * * @return int|string */ public function filter_comments_number( $count, $post_id = null ) { $post_type = get_post_type( $post_id ); if( $post_type && in_array( $post_type, $this->get_disabled_post_types(), true ) ) { return 0; } return $count; } /** * Removes support for comments and trackbacks from specified post types. * @return void */ public function remove_post_types_support(): void { foreach( $this->get_disabled_post_types() as $post_type ) { remove_post_type_support( $post_type, 'comments' ); remove_post_type_support( $post_type, 'trackbacks' ); } } /** * Retrieves the list of post types that should have comments disabled. * This can be filtered to allow customization of the post types, for example, * excluding specific ones based on user settings in the Pro version. * @return array Array of post type names that have comments disabled. */ private function get_disabled_post_types(): array { /** * Filters the post types that should have comments disabled. * The Pro version can use this to exclude post types (like 'product') * based on user settings. * * @param array $post_types Array of post type names. */ return (array) apply_filters( 'adminease_disable_comments_post_types', $this->get_saved_post_types() ); } /** * Tells whether no public post type supports comments any more. * * Only meaningful after init priority 100, once support has been removed. * * @return bool */ private function comments_are_off_everywhere(): bool { foreach( array_keys( get_post_types( [ 'public' => true ] ) ) as $post_type ) { if( post_type_supports( $post_type, 'comments' ) ) { return false; } } return true; } /** * Removes the Comments menu page when no content type has comments left. * @return void */ public function remove_comments_menu(): void { if( $this->comments_are_off_everywhere() ) { remove_menu_page( 'edit-comments.php' ); } } /** * Sends visitors of the Comments screen to the Dashboard when no content type has comments left. * @return void */ public function redirect_comments_screen(): void { if( $this->comments_are_off_everywhere() ) { wp_safe_redirect( admin_url() ); exit; } } /** * Removes the Recent Comments widget when no content type has comments left. * @return void */ public function remove_recent_comments_widget(): void { if( $this->comments_are_off_everywhere() ) { unregister_widget( 'WP_Widget_Recent_Comments' ); } } /** * Hides the comment REST routes from visitors who are not logged in. * * @param array $endpoints The registered REST routes. * * @return array */ public function remove_comment_routes( $endpoints ) { if( ! is_array( $endpoints ) || is_user_logged_in() || ! $this->comments_are_off_everywhere() ) { return $endpoints; } foreach( array_keys( $endpoints ) as $route ) { if( 0 === strpos( (string) $route, '/wp/v2/comments' ) ) { unset( $endpoints[ $route ] ); } } return $endpoints; } /** * Removes the "Comments" menu item from the WordPress admin bar. * @return void */ public function remove_admin_bar_comments(): void { global $wp_admin_bar; if( $wp_admin_bar ) { $wp_admin_bar->remove_menu( 'comments' ); } } /** * Returns an empty template to prevent comment form display. * * @param string $template The path to the comments' template. * * @return string */ public function disable_comments_template( $template ) { global $post; if( $post && in_array( $post->post_type, $this->get_disabled_post_types(), true ) ) { return ADMINEASE_DIR . '/index.php'; } return $template; } /** * Returns an empty comments array to prevent display. * * @param array $comments Array of comments. * @param int|string|\WP_Post|null $post_id Post ID or post object. * * @return array */ public function disable_comments_array( $comments, $post_id = null ) { $post_type = get_post_type( $post_id ); if( $post_type && in_array( $post_type, $this->get_disabled_post_types(), true ) ) { return []; } return $comments; } }