settings = Plugin::get_settings( 'posts' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !current_user_can( 'edit_posts' ) ) { return; } if( empty( $this->settings['disable_gutenberg'] ) ) { return; } add_action( 'init', [ $this, 'init' ] ); add_action( 'admin_init', [ $this, 'restore_classic_editor' ] ); add_filter( 'gutenberg_use_widgets_block_editor', '__return_false' ); add_filter( 'use_widgets_block_editor', '__return_false' ); } /** * Adds additional settings fields to the provided settings array. * Extends the settings array with specific options related to disabling Gutenberg. * * @param array $fields An existing array of settings fields, organized by category. * * @return array The modified array of settings fields with additional options. */ public function adminease_settings_fields( array $fields ): array { $fields['posts']['fields'][] = [ 'type' => 'switch', 'id' => 'disable-gutenberg', 'name' => 'adminease[posts][disable_gutenberg]', 'value' => $this->settings['disable_gutenberg'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Disable Gutenberg', 'adminease' ), 'risk' => 'careful', 'breaks' => __( 'The classic editor replaces the block editor, and widgets use the old screen.', 'adminease' ), 'description' => __( 'Disable the Gutenberg block editor and revert to the classic editor for all post types. This is useful for users who prefer the classic editing experience or have compatibility issues with Gutenberg.', 'adminease' ), 'field_description' => __( 'Enable to disable the Gutenberg block editor and use the classic editor instead.', 'adminease' ), ]; return $fields; } /** * Initializes the process to disable Gutenberg editor for specific post types. * Applies a filter to allow customization of the post types for which Gutenberg editor is disabled. * Adds filters to prevent the use of the block editor for the specified post types. * @return void */ public function init(): void { $this->post_types = apply_filters( 'adminease_disable_gutenberg_post_types', Utils::get_post_types( [ 'base' ] ) ); add_filter( 'use_block_editor_for_post_type', [ $this, 'disable_gutenberg_for_post_type' ], 100, 2 ); add_filter( 'gutenberg_can_edit_post_type', [ $this, 'disable_gutenberg_for_post_type' ], 100, 2 ); } /** * Disables the Gutenberg editor for specified post types. * Checks if the given post type is included in the list of post types for which the Gutenberg editor is disabled. * If the post type matches, it prevents the editor from being enabled. * * @param bool $can_edit Whether the Gutenberg editor can be used for the post type. * @param string $post_type The post type being checked. * * @return bool Returns false to disable Gutenberg if the post type matches; otherwise, returns the original $can_edit value. */ public function disable_gutenberg_for_post_type( bool $can_edit, string $post_type ): bool { if( in_array( $post_type, array_keys( $this->post_types ) ) ) { return false; } return $can_edit; } /** * Restores the classic editor by removing Gutenberg-specific menu and redirection actions. * Adds a filter to disable the block editor for posts if the 'classic-editor' query parameter is set. * @return void */ public function restore_classic_editor() { remove_action( 'admin_menu', 'gutenberg_menu' ); remove_action( 'admin_init', 'gutenberg_redirect_demo' ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading GET parameter to conditionally disable block editor, no sensitive processing if( isset( $_GET['classic-editor'] ) ) { add_filter( 'use_block_editor_for_post', '__return_false', 100 ); } } }