PluginProbe
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More / 2.6.1
Content Control – The Ultimate Content Restriction Plugin! Restrict Content, Create Conditional Blocks & More v2.6.1
trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.1.10 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 2.0.0 2.0.1 2.0.10 2.0.11 2.0.12 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 47 releases
← All changes | classes/Controllers/Frontend/Blocks.php +49 -35 2.0.122.6.1 View file →
@@ -19,9 +19,11 @@
19 19 */
20 20 class Blocks extends Controller {
21 21
22 22 /**
23 - * Initialize Hooks & Filters
23 + * Initialize Hooks & Filters.
24 + *
25 + * @return void
24 26 */
25 27 public function init() {
26 28 if ( is_admin() ) {
27 29 return;
@@ -27,9 +29,9 @@
27 29 return;
28 30 }
29 31
30 32 add_action( 'wp_loaded', [ $this, 'register_block_attributes' ], 100 );
31 - add_filter( 'pre_render_block', [ $this, 'pre_render_block' ], 11, 3 );
33 + add_filter( 'pre_render_block', [ $this, 'pre_render_block' ], 999, 3 );
32 34 add_filter( 'render_block', [ $this, 'render_block' ], 10, 2 );
33 35 add_filter( 'content_control/should_hide_block', [ $this, 'block_user_rules' ], 10, 2 );
34 36 add_action( 'wp_print_styles', [ $this, 'print_block_styles' ] );
35 37 }
@@ -67,9 +69,9 @@
67 69 $controls = wp_parse_args( $block['attrs']['contentControls'], [
68 70 'enabled' => false,
69 71 ] );
70 72
71 - return ! ! $controls['enabled'];
73 + return (bool) $controls['enabled'];
72 74 }
73 75
74 76 /**
75 77 * Get blocks controls if enabled.
@@ -81,50 +83,41 @@
81 83 if ( ! $this->has_block_controls( $block ) ) {
82 84 return null;
83 85 }
84 86
85 - return wp_parse_args( $block['attrs']['contentControls'], [
87 + /**
88 + * Controls for the block.
89 + *
90 + * @var array{enabled:bool,rules:array<string,mixed>} $controls
91 + */
92 + $controls = wp_parse_args( $block['attrs']['contentControls'], [
86 93 'enabled' => false,
87 94 'rules' => [],
88 95 ] );
96 +
97 + return $controls;
89 98 }
90 99
91 100 /**
92 - * Short curcuit block rendering for hidden blocks.
101 + * Check block rules to see if it should be hidden from user.
93 102 *
94 - * @param string|null $pre_render The pre-rendered content. Default null.
95 - * @param array<string,mixed> $parsed_block The block being rendered.
96 - * @param \WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
103 + * @param array{attrs:array<string,mixed>} $block Block to get controls for.
97 104 *
98 - * @return string|null
105 + * @return boolean Whether the block should be hidden.
99 106 */
100 - public function pre_render_block( $pre_render, $parsed_block, $parent_block ) {
101 - if ( 'core/navigation' === $parsed_block['blockName'] ) {
102 - $nav_menu_ref = ! empty( $parsed_block['attrs']['ref'] ) ? $parsed_block['attrs']['ref'] : 0;
103 -
104 - if ( $nav_menu_ref ) {
105 - $navigation_post = get_post( $nav_menu_ref );
106 - $parsed_blocks = parse_blocks( $navigation_post->post_content );
107 - }
108 -
109 - // TODO for some reason, controls applied to core/navigation-link are not being saved, or not appearing in attrs.
107 + public function should_hide_block( $block ) {
108 + if ( protection_is_disabled() ) {
109 + return false;
110 110 }
111 111
112 - if ( ! isset( $parsed_block['attrs']['contentControls'] ) ) {
113 - return $pre_render;
112 + if ( ! $this->has_block_controls( $block ) ) {
113 + return false;
114 114 }
115 115
116 - if ( protection_is_disabled() ) {
117 - return $pre_render;
118 - }
116 + $controls = $this->get_block_controls( $block );
119 117
120 - $controls = wp_parse_args( $parsed_block['attrs']['contentControls'], [
121 - 'enabled' => false,
122 - 'rules' => [],
123 - ] );
124 -
125 118 if ( ! $controls['enabled'] ) {
126 - return $pre_render;
119 + return false;
127 120 }
128 121
129 122 /**
130 123 * Filter whether to hide the block.
@@ -130,9 +123,9 @@
130 123 * Filter whether to hide the block.
131 124 *
132 125 * @param bool $should_hide Whether the block should be hidden.
133 126 * @param array $rules Rules to check.
134 - * @param array $parsed_block The block being rendered.
127 + * @param array $block The block being rendered.
135 128 * @return bool
136 129 */
137 130 $should_hide = apply_filters(
138 131 'content_control/should_hide_block',
@@ -137,15 +130,28 @@
137 130 $should_hide = apply_filters(
138 131 'content_control/should_hide_block',
139 132 false,
140 133 $controls['rules'],
141 - $parsed_block
134 + $block
142 135 );
143 136
144 - return $should_hide ? '' : $pre_render;
137 + return $should_hide;
145 138 }
146 139
147 140 /**
141 + * Short curcuit block rendering for hidden blocks.
142 + *
143 + * @param string|null $pre_render The pre-rendered content. Default null.
144 + * @param array<string,mixed> $parsed_block The block being rendered.
145 + * @param \WP_Block|null $parent_block If this is a nested block, a reference to the parent block.
146 + *
147 + * @return string|null
148 + */
149 + public function pre_render_block( $pre_render, $parsed_block, $parent_block ) {
150 + return $this->should_hide_block( $parsed_block ) ? '' : $pre_render;
151 + }
152 +
153 + /**
148 154 * Check block rules to see if it should be hidden from user.
149 155 *
150 156 * @param bool $should_hide Whether the block should be hidden.
151 157 * @param array<string,array<string,mixed>|null> $rules Rules to check.
@@ -173,9 +179,9 @@
173 179
174 180 /**
175 181 * Get any classes to be added to the outer block element.
176 182 *
177 - * @param array<string,mixed> $block Block to get classes for.
183 + * @param array{attrs:array<string,mixed>} $block Block to get controls for.
178 184 * @return null|string[]
179 185 */
180 186 public function get_block_control_classes( $block ) {
181 187 if ( ! $this->has_block_controls( $block ) ) {
@@ -207,13 +213,14 @@
207 213 /**
208 214 * Filter the classes to be added to the block.
209 215 *
210 216 * @param array $classes Classes to be added.
217 + * @param array $controls Controls for the block.
211 218 * @param array $block Block to get classes for.
212 219 *
213 220 * @return string[]
214 221 */
215 - $classes = apply_filters( 'content_control/get_block_control_classes', $classes, $block );
222 + $classes = apply_filters( 'content_control/get_block_control_classes', $classes, $controls, $block );
216 223
217 224 return array_unique( $classes );
218 225 }
219 226
@@ -232,8 +239,15 @@
232 239 */
233 240 public function render_block( $block_content, $block ) {
234 241 if ( ! $this->has_block_controls( $block ) ) {
235 242 return $block_content;
243 + }
244 +
245 + // If the block should be hidden, return an empty string.
246 + // This catches blocks that should be hidden but were missed by pre_render_block.
247 + // This applies to core/navigation-link blocks for example.
248 + if ( $this->should_hide_block( $block ) ) {
249 + return '';
236 250 }
237 251
238 252 $classes = $this->get_block_control_classes( $block );
239 253