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 +59 -43 2.0.22.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' ], 10, 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 }
@@ -35,8 +37,10 @@
35 37 }
36 38
37 39 /**
38 40 * Adds custom attributes to allowed block attributes.
41 + *
42 + * @return void
39 43 */
40 44 public function register_block_attributes() {
41 45 $registered_blocks = \WP_Block_Type_Registry::get_instance()->get_all_registered();
42 46
@@ -53,9 +57,9 @@
53 57
54 58 /**
55 59 * Check if block has controls enabled.
56 60 *
57 - * @param array $block Block to be checked.
61 + * @param array<string,mixed> $block Block to be checked.
58 62 * @return boolean Whether the block has Controls enabled.
59 63 */
60 64 public function has_block_controls( $block ) {
61 65 if ( ! isset( $block['attrs']['contentControls'] ) ) {
@@ -65,16 +69,16 @@
65 69 $controls = wp_parse_args( $block['attrs']['contentControls'], [
66 70 'enabled' => false,
67 71 ] );
68 72
69 - return ! ! $controls['enabled'];
73 + return (bool) $controls['enabled'];
70 74 }
71 75
72 76 /**
73 77 * Get blocks controls if enabled.
74 78 *
75 - * @param array $block Block to get controls from.
76 - * @return array|null Controls if enabled.
79 + * @param array{attrs:array<string,mixed>} $block Block to get controls for.
80 + * @return array{enabled:bool,rules:array<string,mixed>}|null Controls if enabled.
77 81 */
78 82 public function get_block_controls( $block ) {
79 83 if ( ! $this->has_block_controls( $block ) ) {
80 84 return null;
@@ -79,50 +83,41 @@
79 83 if ( ! $this->has_block_controls( $block ) ) {
80 84 return null;
81 85 }
82 86
83 - 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'], [
84 93 'enabled' => false,
85 94 'rules' => [],
86 95 ] );
96 +
97 + return $controls;
87 98 }
88 99
89 100 /**
90 - * Short curcuit block rendering for hidden blocks.
101 + * Check block rules to see if it should be hidden from user.
91 102 *
92 - * @param string|null $pre_render The pre-rendered content. Default null.
93 - * @param array $parsed_block The block being rendered.
94 - * @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.
95 104 *
96 - * @return string|null
105 + * @return boolean Whether the block should be hidden.
97 106 */
98 - public function pre_render_block( $pre_render, $parsed_block, $parent_block ) {
99 - if ( 'core/navigation' === $parsed_block['blockName'] ) {
100 - $nav_menu_ref = ! empty( $parsed_block['attrs']['ref'] ) ? $parsed_block['attrs']['ref'] : 0;
101 -
102 - if ( $nav_menu_ref ) {
103 - $navigation_post = get_post( $nav_menu_ref );
104 - $parsed_blocks = parse_blocks( $navigation_post->post_content );
105 - }
106 -
107 - // 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;
108 110 }
109 111
110 - if ( ! isset( $parsed_block['attrs']['contentControls'] ) ) {
111 - return $pre_render;
112 + if ( ! $this->has_block_controls( $block ) ) {
113 + return false;
112 114 }
113 115
114 - if ( protection_is_disabled() ) {
115 - return $pre_render;
116 - }
116 + $controls = $this->get_block_controls( $block );
117 117
118 - $controls = wp_parse_args( $parsed_block['attrs']['contentControls'], [
119 - 'enabled' => false,
120 - 'rules' => [],
121 - ] );
122 -
123 118 if ( ! $controls['enabled'] ) {
124 - return $pre_render;
119 + return false;
125 120 }
126 121
127 122 /**
128 123 * Filter whether to hide the block.
@@ -128,9 +123,9 @@
128 123 * Filter whether to hide the block.
129 124 *
130 125 * @param bool $should_hide Whether the block should be hidden.
131 126 * @param array $rules Rules to check.
132 - * @param array $parsed_block The block being rendered.
127 + * @param array $block The block being rendered.
133 128 * @return bool
134 129 */
135 130 $should_hide = apply_filters(
136 131 'content_control/should_hide_block',
@@ -135,19 +130,32 @@
135 130 $should_hide = apply_filters(
136 131 'content_control/should_hide_block',
137 132 false,
138 133 $controls['rules'],
139 - $parsed_block
134 + $block
140 135 );
141 136
142 - return $should_hide ? '' : $pre_render;
137 + return $should_hide;
143 138 }
144 139
145 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 + /**
146 154 * Check block rules to see if it should be hidden from user.
147 155 *
148 - * @param bool $should_hide Whether the block should be hidden.
149 - * @param array $rules Rules to check.
156 + * @param bool $should_hide Whether the block should be hidden.
157 + * @param array<string,array<string,mixed>|null> $rules Rules to check.
150 158 * @return bool
151 159 */
152 160 public function block_user_rules( $should_hide, $rules ) {
153 161 $rules = wp_parse_args( $rules, [
@@ -171,10 +179,10 @@
171 179
172 180 /**
173 181 * Get any classes to be added to the outer block element.
174 182 *
175 - * @param array $block Block to get classes for.
176 - * @return null|array
183 + * @param array{attrs:array<string,mixed>} $block Block to get controls for.
184 + * @return null|string[]
177 185 */
178 186 public function get_block_control_classes( $block ) {
179 187 if ( ! $this->has_block_controls( $block ) ) {
180 188 return null;
@@ -205,13 +213,14 @@
205 213 /**
206 214 * Filter the classes to be added to the block.
207 215 *
208 216 * @param array $classes Classes to be added.
217 + * @param array $controls Controls for the block.
209 218 * @param array $block Block to get classes for.
210 219 *
211 220 * @return string[]
212 221 */
213 - $classes = apply_filters( 'content_control/get_block_control_classes', $classes, $block );
222 + $classes = apply_filters( 'content_control/get_block_control_classes', $classes, $controls, $block );
214 223
215 224 return array_unique( $classes );
216 225 }
217 226
@@ -222,10 +231,10 @@
222 231 * - https://github.com/WordPress/gutenberg/blob/9aab0c4f60c78d19aae0af3351a2b66f8fa4c162/lib/block-supports/layout.php#L317
223 232 * - https://github.com/WordPress/gutenberg/blob/e776b4f00f690ce9cf21c027ebf5e7442420d716/lib/block-supports/duotone.php#L503
224 233 * - https://github.com/WordPress/gutenberg/blob/9aab0c4f60c78d19aae0af3351a2b66f8fa4c162/lib/block-supports/elements.php#L54-L72
225 234 *
226 - * @param string $block_content Blocks rendered html.
227 - * @param array $block Array of block properties.
235 + * @param string $block_content Blocks rendered html.
236 + * @param array<string,mixed> $block Array of block properties.
228 237 *
229 238 * @return string
230 239 */
231 240 public function render_block( $block_content, $block ) {
@@ -230,8 +239,15 @@
230 239 */
231 240 public function render_block( $block_content, $block ) {
232 241 if ( ! $this->has_block_controls( $block ) ) {
233 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 '';
234 250 }
235 251
236 252 $classes = $this->get_block_control_classes( $block );
237 253