-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend-block-visibility.php
124 lines (112 loc) · 4.8 KB
/
backend-block-visibility.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
/**
* Plugin Name: Backend Block Visibility
* Description: Hide unneeded blocks from the post editor
* Version: 1.0.0
* Author: Kevin Marsden
* Author URI: https://kmarsden.com
* Text Domain: km-block-visibility
* Contributors: kevmarsden
* Tags: block, blocks, visibility
* Requires at least: 5.8.0
* Tested up to: 6.5
* Stable tag: 1.0.0
* Requires PHP: 7.0
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*/
defined( 'ABSPATH' ) || exit;
if ( is_admin() ) {
add_filter( 'plugin_action_links_backend-block-visibility/backend-block-visibility.php', 'km_block_visibility_add_plugin_settings_link');
add_filter( 'allowed_block_types_all', 'km_block_visibility_remove_blocks', 10, 2 );
add_action( 'admin_init', 'km_block_visibility_register_settings' );
add_action( 'admin_menu', 'km_block_visibility_settings_page' );
}
/**
* Add settings link to the main plugin page
*
* @param array $links Plugin action links
*
* @return array The list of plugin action links
*/
function km_block_visibility_add_plugin_settings_link( $links ) {
$settings_link = '<a href="' . esc_url( admin_url( 'options-general.php?page=km_block_visibility' ) ) . '">' . __( 'Settings', 'km-block-visibility' ) . '</a>';
array_unshift( $links, $settings_link );
return $links;
}
/**
* Custom sanitization callback function to ensure input is an array.
*/
function km_block_visibility_register_settings() {
register_setting( 'km_block_visibility_options', 'km_block_visibility_options', array(
'type' => 'array',
'sanitize_callback' => 'km_block_visibility_sanitize_block_selection',
'default' => NULL
) );
}
/**
* Add the "Block Visibility" options page under "Settings".
*/
function km_block_visibility_settings_page() {
add_options_page( 'Block Visibility', 'Block Visibility', 'manage_options', 'km_block_visibility', 'km_block_visibility_callback' );
}
/**
* Filters the list of allowed block types based the block visibility settings.
*
* @param array $allowed_block_types array of block type slugs
* @param object $block_editor_context The current block editor context.
*
* @return array The filtered list of allowed block types.
*/
function km_block_visibility_remove_blocks( $allowed_block_types, $block_editor_context ) {
$disallowed_blocks = get_option( 'km_block_visibility_options', array() );
// Get all registered blocks if $allowed_block_types is not already set.
if ( ! is_array( $allowed_block_types ) || empty( $allowed_block_types ) ) {
$registered_blocks = WP_Block_Type_Registry::get_instance()->get_all_registered();
$allowed_block_types = array_keys( $registered_blocks );
}
$filtered_blocks = array_values( array_diff( $allowed_block_types, $disallowed_blocks ) );
return $filtered_blocks;
}
/**
* Display the block visibility settings page content.
*/
function km_block_visibility_callback() {
?>
<div class="wrap">
<h2><?php _e( 'Block Visibility Settings', 'km-block-visibility' );?></h2>
<p><?php _e( 'Select the blocks to hide from the post editor. If a block is hidden that is already in use, it will still be visible on the frontend and backend.', 'km-block-visibility' );?></p>
<form action="options.php" method="post" >
<?php
settings_fields( 'km_block_visibility_options' );
$blocks_to_hide = get_option( 'km_block_visibility_options' ) ?? array();
$block_types = \WP_Block_Type_Registry::get_instance()->get_all_registered();
?>
<ul class="registered-blocks" style="display: flex; flex-direction: row; flex-wrap: wrap;">
<?php foreach ( $block_types as $block_type ) : ?>
<li style="width:30%;">
<label>
<input type="checkbox" name="km_block_visibility_options[]" value="<?php echo esc_attr( $block_type->name ); ?>" <?php if ( in_array( $block_type->name, $blocks_to_hide ) ) { echo "checked"; }?>>
<?php echo esc_html( $block_type->name ); ?>
</label>
</li>
<?php endforeach; ?>
</ul>
<?php submit_button( __( 'Save Changes', 'km-block-visibility' ), 'primary'); ?>
</form>
</div>
<?php
}
/**
* Custom sanitization callback function to ensure input is an array.
*
* @param array $input Array of selected blocks.
*
* @return array The sanitized list of selected blocks.
*/
function km_block_visibility_sanitize_block_selection( $input ) {
if ( ! is_array( $input ) ) {
$input = array();
}
return $input;
}