PluginProbe ʕ •ᴥ•ʔ
GenerateBlocks / 2.3.0
GenerateBlocks v2.3.0
trunk 1.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.2.0 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.7.0 1.7.1 1.7.2 1.7.3 1.8.0 1.8.1 1.8.2 1.8.3 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.1.2 2.2.0 2.2.1 2.3.0
generateblocks / includes / general.php
generateblocks / includes Last commit date
blocks 1 week ago dynamic-tags 1 week ago pattern-library 1 week ago utils 2 years ago class-do-css.php 2 years ago class-dynamic-content.php 1 week ago class-dynamic-tag-security.php 6 months ago class-enqueue-css.php 1 year ago class-legacy-attributes.php 4 years ago class-map-deprecated-attributes.php 2 years ago class-meta-handler.php 3 months ago class-plugin-update.php 1 year ago class-query-loop.php 2 years ago class-query-utils.php 1 week ago class-render-blocks.php 1 year ago class-rest.php 1 year ago class-settings.php 1 year ago dashboard.php 1 year ago defaults.php 1 year ago deprecated.php 1 year ago functions.php 1 week ago general.php 1 week ago
general.php
735 lines
1 <?php
2 /**
3 * General actions and filters.
4 *
5 * @package GenerateBlocks
6 */
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 add_action( 'enqueue_block_editor_assets', 'generateblocks_do_block_editor_assets' );
13 /**
14 * Enqueue Gutenberg block assets for backend editor.
15 *
16 * @uses {wp-blocks} for block type registration & related functions.
17 * @uses {wp-element} for WP Element abstraction — structure of blocks.
18 * @uses {wp-i18n} to internationalize the block's text.
19 * @uses {wp-editor} for WP editor styles.
20 * @since 0.1
21 */
22 function generateblocks_do_block_editor_assets() {
23 wp_localize_script(
24 'generateblocks-media-editor-script',
25 'generateblocksBlockMedia',
26 [
27 'standardPlaceholder' => GENERATEBLOCKS_DIR_URL . 'assets/images/placeholder1280x720.png',
28 'squarePlaceholder' => GENERATEBLOCKS_DIR_URL . 'assets/images/placeholder800x.png',
29 ]
30 );
31
32 wp_localize_script(
33 'generateblocks-text-editor-script',
34 'generateblocksBlockText',
35 [
36 'defaultButtonAttributes' => apply_filters(
37 'generateblocks_default_button_attributes',
38 [
39 'styles' => [
40 'display' => 'inline-flex',
41 'alignItems' => 'center',
42 'backgroundColor' => '#215bc2',
43 'color' => '#ffffff',
44 'paddingTop' => '1rem',
45 'paddingRight' => '2rem',
46 'paddingBottom' => '1rem',
47 'paddingLeft' => '2rem',
48 'textDecoration' => 'none',
49 '&:is(:hover, :focus)' => [
50 'backgroundColor' => '#1a4a9b',
51 'color' => '#ffffff',
52 ],
53 ],
54 ]
55 ),
56 ]
57 );
58
59 global $pagenow;
60
61 $generateblocks_deps = array( 'wp-blocks', 'wp-i18n', 'wp-editor', 'wp-element', 'wp-compose', 'wp-data' );
62
63 if ( 'widgets.php' === $pagenow ) {
64 unset( $generateblocks_deps[2] );
65 }
66
67 $assets_file = GENERATEBLOCKS_DIR . 'dist/blocks.asset.php';
68 $compiled_assets = file_exists( $assets_file )
69 ? require $assets_file
70 : false;
71
72 $assets =
73 isset( $compiled_assets['dependencies'] ) &&
74 isset( $compiled_assets['version'] )
75 ? $compiled_assets
76 : [
77 'dependencies' => $generateblocks_deps,
78 'version' => filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.js' ),
79 ];
80
81 wp_enqueue_script(
82 'generateblocks',
83 GENERATEBLOCKS_DIR_URL . 'dist/blocks.js',
84 $assets['dependencies'],
85 $assets['version'],
86 true
87 );
88
89 if ( function_exists( 'wp_set_script_translations' ) ) {
90 wp_set_script_translations( 'generateblocks', 'generateblocks' );
91 }
92
93 wp_enqueue_style(
94 'generateblocks',
95 GENERATEBLOCKS_DIR_URL . 'dist/blocks.css',
96 array( 'wp-edit-blocks', 'generateblocks-packages' ),
97 filemtime( GENERATEBLOCKS_DIR . 'dist/blocks.css' )
98 );
99
100 $image_sizes = get_intermediate_image_sizes();
101 $image_sizes = array_diff( $image_sizes, array( '1536x1536', '2048x2048' ) );
102 $image_sizes[] = 'full';
103
104 wp_localize_script(
105 'generateblocks',
106 'generateBlocksInfo',
107 array(
108 'imageSizes' => $image_sizes,
109 'svgShapes' => generateblocks_get_svg_shapes(),
110 'syncResponsivePreviews' => generateblocks_get_option( 'sync_responsive_previews' ),
111 'excerptLength' => apply_filters( 'excerpt_length', 55 ), // phpcs:ignore -- Core filter.
112 'excerptMore' => apply_filters( 'excerpt_more', ' ' . '[&hellip;]' ), // phpcs:ignore -- Core filter.
113 'imagePlaceholders' => array(
114 'standard' => GENERATEBLOCKS_DIR_URL . 'assets/images/image-placeholder.png',
115 'square' => GENERATEBLOCKS_DIR_URL . 'assets/images/square-image-placeholder.png',
116 ),
117 'globalContainerWidth' => generateblocks_get_global_container_width(),
118 'queryLoopEditorPostsCap' => apply_filters( 'generateblocks_query_loop_editor_posts_cap', 50 ),
119 'disableGoogleFonts' => generateblocks_get_option( 'disable_google_fonts' ),
120 'typographyFontFamilyList' => generateblocks_get_font_family_list(),
121 'useV1Blocks' => generateblocks_use_v1_blocks(),
122 )
123 );
124
125 if ( function_exists( 'generate_get_color_defaults' ) ) {
126 $color_settings = wp_parse_args(
127 get_option( 'generate_settings', array() ),
128 generate_get_color_defaults()
129 );
130
131 $generatepressDefaultStyling = apply_filters(
132 'generateblocks_gp_default_styling',
133 array(
134 'buttonBackground' => $color_settings['form_button_background_color'],
135 'buttonBackgroundHover' => $color_settings['form_button_background_color_hover'],
136 'buttonText' => $color_settings['form_button_text_color'],
137 'buttonTextHover' => $color_settings['form_button_text_color_hover'],
138 'buttonPaddingTop' => '10px',
139 'buttonPaddingRight' => '20px',
140 'buttonPaddingBottom' => '10px',
141 'buttonPaddingLeft' => '20px',
142 )
143 );
144
145 $css = sprintf(
146 '.gb-button.button {
147 background-color: %1$s;
148 color: %2$s;
149 padding-top: %3$s;
150 padding-right: %4$s;
151 padding-bottom: %5$s;
152 padding-left: %6$s;
153 }',
154 $generatepressDefaultStyling['buttonBackground'],
155 $generatepressDefaultStyling['buttonText'],
156 $generatepressDefaultStyling['buttonPaddingTop'],
157 $generatepressDefaultStyling['buttonPaddingRight'],
158 $generatepressDefaultStyling['buttonPaddingBottom'],
159 $generatepressDefaultStyling['buttonPaddingLeft']
160 );
161
162 $css .= sprintf(
163 '.gb-button.button:active, .gb-button.button:hover, .gb-button.button:focus {
164 background-color: %1$s;
165 color: %2$s;
166 }',
167 $generatepressDefaultStyling['buttonBackgroundHover'],
168 $generatepressDefaultStyling['buttonTextHover']
169 );
170
171 wp_add_inline_style( 'generateblocks', $css );
172 }
173
174 $defaults = generateblocks_get_block_defaults();
175
176 wp_localize_script(
177 'generateblocks',
178 'generateBlocksDefaults',
179 $defaults
180 );
181
182 wp_localize_script(
183 'generateblocks',
184 'generateBlocksStyling',
185 generateblocks_get_default_styles()
186 );
187
188 wp_localize_script(
189 'generateblocks',
190 'generateBlocksLegacyDefaults',
191 array(
192 'v_1_4_0' => GenerateBlocks_Legacy_Attributes::get_defaults( '1.4.0' ),
193 )
194 );
195
196 $editor_sidebar_assets = generateblocks_get_enqueue_assets( 'editor-sidebar' );
197
198 wp_enqueue_script(
199 'generateblocks-editor-sidebar',
200 GENERATEBLOCKS_DIR_URL . 'dist/editor-sidebar.js',
201 $editor_sidebar_assets['dependencies'],
202 $editor_sidebar_assets['version'],
203 true
204 );
205
206 if ( function_exists( 'wp_set_script_translations' ) ) {
207 wp_set_script_translations( 'generateblocks-editor-sidebar', 'generateblocks' );
208 }
209
210 wp_enqueue_style(
211 'generateblocks-editor-sidebar',
212 GENERATEBLOCKS_DIR_URL . 'dist/editor-sidebar.css',
213 array( 'wp-components' ),
214 filemtime( GENERATEBLOCKS_DIR . 'dist/editor-sidebar.css' )
215 );
216
217 $packages_asset_info = generateblocks_get_enqueue_assets( 'packages' );
218 wp_register_style(
219 'generateblocks-packages',
220 GENERATEBLOCKS_DIR_URL . 'dist/packages.css',
221 '',
222 $packages_asset_info['version']
223 );
224
225 $edge22_packages = [
226 'block-styles',
227 'components',
228 'styles-builder',
229 ];
230
231 foreach ( $edge22_packages as $name ) {
232 $package_asset_info = generateblocks_get_enqueue_assets( "{$name}-imported" );
233 $script_asset_info = generateblocks_get_enqueue_assets( $name );
234
235 wp_register_script(
236 "generateblocks-$name",
237 GENERATEBLOCKS_DIR_URL . 'dist/' . $name . '.js',
238 $package_asset_info['dependencies'],
239 $script_asset_info['version'],
240 true
241 );
242
243 wp_register_style(
244 "generateblocks-$name",
245 false,
246 [ 'generateblocks-packages' ],
247 $packages_asset_info['version']
248 );
249 }
250
251 $editor_assets = generateblocks_get_enqueue_assets( 'editor' );
252
253 wp_enqueue_script(
254 'generateblocks-editor',
255 GENERATEBLOCKS_DIR_URL . 'dist/editor.js',
256 $editor_assets['dependencies'],
257 $editor_assets['version'],
258 true
259 );
260
261 $tags = GenerateBlocks_Register_Dynamic_Tag::get_tags();
262 $tag_list = [];
263
264 foreach ( $tags as $tag => $data ) {
265 $relevant_data = $data;
266 unset( $relevant_data['return'] );
267 if ( $data ) {
268 $tag_list[] = $relevant_data;
269 }
270 }
271
272 wp_localize_script(
273 'generateblocks-editor',
274 'generateBlocksEditor',
275 [
276 'useV1Blocks' => generateblocks_use_v1_blocks(),
277 /**
278 * Filters whether WordPress 7.0's core "Additional CSS" textarea is
279 * shown on GenerateBlocks blocks. We disable it by default because our
280 * blocks have their own CSS editing; return true to re-enable it.
281 *
282 * @since 2.3.0
283 *
284 * @param bool $enabled Whether to allow the core Additional CSS field.
285 */
286 'enableCoreAdditionalCss' => (bool) apply_filters( 'generateblocks_enable_core_additional_css', false ),
287 'dynamicTags' => $tag_list,
288 'hasGPFontLibrary' => function_exists( 'generatepress_is_module_active' )
289 ? generatepress_is_module_active( 'generate_package_font_library', 'GENERATE_FONT_LIBRARY' )
290 : false,
291 'dateFormat' => get_option( 'date_format' ),
292 'wpContentUrl' => content_url(),
293 'typographyFontFamilyList' => generateblocks_get_font_family_list(),
294 'dynamicTagsPreview' => apply_filters( 'generateblocks_dynamic_tags_preview', true ) ? 'enabled' : 'disabled',
295 ]
296 );
297
298 wp_enqueue_style(
299 'generateblocks-editor',
300 GENERATEBLOCKS_DIR_URL . 'dist/editor.css',
301 array( 'wp-edit-blocks', 'generateblocks-packages' ),
302 filemtime( GENERATEBLOCKS_DIR . 'dist/editor.css' )
303 );
304 }
305
306 add_filter( 'block_categories_all', 'generateblocks_do_category' );
307 /**
308 * Add GeneratePress category to Gutenberg.
309 *
310 * @param array $categories Existing categories.
311 * @since 0.1
312 */
313 function generateblocks_do_category( $categories ) {
314 array_unshift(
315 $categories,
316 [
317 'slug' => 'generateblocks',
318 'title' => __( 'GenerateBlocks', 'generateblocks' ),
319 ]
320 );
321
322 return $categories;
323 }
324
325 add_action( 'wp_enqueue_scripts', 'generateblocks_do_google_fonts' );
326 add_action( 'enqueue_block_editor_assets', 'generateblocks_do_google_fonts' );
327 /**
328 * Do Google Fonts.
329 *
330 * @since 0.1
331 */
332 function generateblocks_do_google_fonts() {
333 if ( generateblocks_get_option( 'disable_google_fonts' ) ) {
334 return;
335 }
336
337 $fonts_url = generateblocks_get_google_fonts_uri();
338
339 if ( $fonts_url ) {
340 wp_enqueue_style( 'generateblocks-google-fonts', $fonts_url, array(), null, 'all' ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
341 }
342 }
343
344 add_filter( 'generateblocks_css_print_method', 'generateblocks_set_css_print_method' );
345 /**
346 * Set our CSS print method.
347 *
348 * @param string $method Existing method.
349 */
350 function generateblocks_set_css_print_method( $method ) {
351 $method = generateblocks_get_option( 'css_print_method' );
352
353 if ( is_single() ) {
354 $method = 'inline';
355 }
356
357 return $method;
358 }
359
360 add_filter( 'excerpt_allowed_blocks', 'generateblocks_set_excerpt_allowed_blocks' );
361 /**
362 * Add blocks that can be displayed in post excerpts.
363 *
364 * @param array $allowed Existing allowed blocks.
365 * @since 1.0
366 */
367 function generateblocks_set_excerpt_allowed_blocks( $allowed ) {
368 $allowed[] = 'generateblocks/headline';
369 $allowed[] = 'generateblocks/container';
370 $allowed[] = 'generateblocks/text';
371 $allowed[] = 'generateblocks/element';
372
373 return $allowed;
374 }
375
376 add_filter( 'excerpt_allowed_wrapper_blocks', 'generateblocks_set_excerpt_allowed_wrapper_blocks' );
377 /**
378 * Allows excerpts to be generated from the `innerBlocks` of these wrappers.
379 *
380 * @param array $allowed Existing allowed wrapper blocks.
381 * @since 1.5.0
382 */
383 function generateblocks_set_excerpt_allowed_wrapper_blocks( $allowed ) {
384 $allowed[] = 'generateblocks/container';
385 $allowed[] = 'generateblocks/element';
386
387 return $allowed;
388 }
389
390 add_filter( 'generateblocks_before_container_close', 'generateblocks_do_shape_divider', 10, 2 );
391 /**
392 * Add shape divider to Container.
393 *
394 * @since 1.2.0
395 * @param string $output The current block output.
396 * @param array $attributes The current block attributes.
397 */
398 function generateblocks_do_shape_divider( $output, $attributes ) {
399 $defaults = generateblocks_get_block_defaults();
400
401 $settings = wp_parse_args(
402 $attributes,
403 $defaults['container']
404 );
405
406 if ( ! empty( $settings['shapeDividers'] ) ) {
407 $shapes = generateblocks_get_svg_shapes();
408 $shape_values = array();
409
410 foreach ( $shapes as $group => $data ) {
411 if ( ! empty( $data['svgs'] ) && is_array( $data['svgs'] ) ) {
412 foreach ( $data['svgs'] as $key => $shape ) {
413 $shape_values[ $key ] = $shape['icon'];
414 }
415 }
416 }
417
418 $output .= '<div class="gb-shapes">';
419
420 foreach ( (array) $settings['shapeDividers'] as $index => $option ) {
421 if ( ! empty( $option['shape'] ) ) {
422 if ( isset( $shape_values[ $option['shape'] ] ) ) {
423 $shapeNumber = $index + 1;
424
425 $output .= sprintf(
426 '<div class="gb-shape gb-shape-' . $shapeNumber . '">%s</div>',
427 $shape_values[ $option['shape'] ]
428 );
429 }
430 }
431 }
432
433 $output .= '</div>';
434 }
435
436 return $output;
437 }
438
439 add_filter( 'generateblocks_do_content', 'generateblocks_do_widget_styling' );
440 /**
441 * Process all widget content for potential styling.
442 *
443 * @since 1.3.4
444 * @param string $content The existing content to process.
445 */
446 function generateblocks_do_widget_styling( $content ) {
447 $widget_blocks = get_option( 'widget_block' );
448
449 foreach ( (array) $widget_blocks as $block ) {
450 if ( isset( $block['content'] ) ) {
451 $content .= $block['content'];
452 }
453 }
454
455 return $content;
456 }
457
458 add_filter( 'generateblocks_attr_container', 'generateblocks_set_inline_background_style', 10, 2 );
459 /**
460 * Add our background image attribute to the Container.
461 *
462 * @since 1.5.0
463 * @param array $attributes Existing attributes.
464 * @param array $settings Block settings.
465 */
466 function generateblocks_set_inline_background_style( $attributes, $settings ) {
467 if ( generateblocks_has_background_image( $settings ) && $settings['bgImageInline'] ) {
468 $url = generateblocks_get_background_image_url( $settings );
469
470 if ( $url ) {
471 $attribute_name = 'background-image';
472
473 if ( 'element' !== $settings['bgOptions']['selector'] ) {
474 $attribute_name = '--' . $attribute_name;
475 }
476
477 $attributes['style'] = $attribute_name . ': url(' . esc_url( $url ) . ');';
478 }
479 }
480
481 return $attributes;
482 }
483
484 add_filter( 'generateblocks_block_css_selector', 'generateblocks_set_block_css_selectors', 10, 3 );
485 /**
486 * Change our block selectors if needed.
487 *
488 * @param string $selector Existing selector.
489 * @param string $name The block name.
490 * @param array $attributes The block attributes.
491 */
492 function generateblocks_set_block_css_selectors( $selector, $name, $attributes ) {
493 $blockVersion = ! empty( $attributes['blockVersion'] ) ? $attributes['blockVersion'] : 1;
494 $defaults = generateblocks_get_block_defaults();
495
496 if ( 'button' === $name ) {
497 $settings = wp_parse_args(
498 $attributes,
499 $defaults['button']
500 );
501
502 if ( $blockVersion < 3 ) {
503 // Old versions of the this block used this backwards logic
504 // to determine whether to remove the "a" to the selector.
505 $clean_selector = $selector;
506 $selector = 'a' . $selector;
507
508 if ( isset( $attributes['hasUrl'] ) && ! $attributes['hasUrl'] ) {
509 $selector = $clean_selector;
510 }
511 } else {
512 $is_link = (
513 ! empty( $settings['hasUrl'] ) ||
514 ! empty( $settings['dynamicLinkType'] )
515 ) && 'link' === $settings['buttonType'];
516
517 if ( $is_link ) {
518 $selector = 'a' . $selector;
519 }
520
521 if ( 'button' === $settings['buttonType'] ) {
522 $selector = 'button' . $selector;
523 }
524 }
525
526 if ( $settings['hasButtonContainer'] || $blockVersion < 3 ) {
527 $selector = '.gb-button-wrapper ' . $selector;
528 } elseif ( isset( $settings['isPagination'] ) && $settings['isPagination'] ) {
529 $selector = '.gb-query-loop-pagination ' . $selector;
530 }
531 }
532
533 if ( 'headline' === $name ) {
534 $settings = wp_parse_args(
535 $attributes,
536 $defaults['headline']
537 );
538
539 if ( apply_filters( 'generateblocks_headline_selector_tagname', true, $attributes ) ) {
540 $selector = $settings['element'] . $selector;
541 }
542 }
543
544 return $selector;
545 }
546
547 add_action( 'init', 'generateblocks_register_user_meta' );
548 /**
549 * Register GenerateBlocks custom user meta fields.
550 *
551 * @return void
552 */
553 function generateblocks_register_user_meta() {
554 register_meta(
555 'user',
556 GenerateBlocks_Rest::ONBOARDING_META_KEY,
557 array(
558 'type' => 'object',
559 'single' => true,
560 'show_in_rest' => array(
561 'schema' => array(
562 'type' => 'object',
563 'properties' => array(
564 'insert_inner_container' => array( 'type' => 'boolean' ),
565 ),
566 'additionalProperties' => array(
567 'type' => 'boolean',
568 ),
569 ),
570 ),
571 )
572 );
573 }
574
575 add_filter( 'block_editor_settings_all', 'generateblocks_do_block_css_reset', 15 );
576 /**
577 * This resets the `max-width`, `margin-left`, and `margin-right` properties for our blocks in the editor.
578 * We have to do this as most themes use `.wp-block` to set a `max-width` and auto margins.
579 *
580 * We used to do this directly in the block CSS if those block attributes didn't exist, but this allows us
581 * to overwrite the reset in the `block_editor_settings_all` filter with a later priority.
582 *
583 * @param array $editor_settings The existing editor settings.
584 */
585 function generateblocks_do_block_css_reset( $editor_settings ) {
586 $css = '.gb-container, .gb-headline, .gb-button {max-width:unset;margin-left:0;margin-right:0;}';
587 $editor_settings['styles'][] = [ 'css' => $css ];
588
589 $blocks_to_reset = [
590 '.editor-styles-wrapper .wp-block-generateblocks-text:where(:not(h1, h2, h3, h4, h5, h6, p))',
591 '.editor-styles-wrapper .wp-block-generateblocks-element',
592 '.editor-styles-wrapper .wp-block-generateblocks-shape',
593 '.editor-styles-wrapper .wp-block-generateblocks-media',
594 '.editor-styles-wrapper .wp-block-generateblocks-query',
595 '.editor-styles-wrapper .wp-block-generateblocks-query-no-results',
596 '.editor-styles-wrapper .wp-block-generateblocks-query-page-numbers',
597 '.editor-styles-wrapper .wp-block-generateblocks-looper',
598 '.editor-styles-wrapper .wp-block-generateblocks-loop-item',
599 ];
600
601 $heading_blocks_to_reset = [
602 '.editor-styles-wrapper .wp-block-generateblocks-text:where(h1, h2, h3, h4, h5, h6, p)',
603 ];
604
605 $css = implode( ',', $blocks_to_reset ) . '{max-width:unset;margin:0;}';
606 $css .= implode( ',', $heading_blocks_to_reset ) . '{max-width:unset;margin-left:0;margin-right:0;}';
607 $editor_settings['styles'][] = [ 'css' => $css ];
608
609 return $editor_settings;
610 }
611
612 add_filter( 'generateblocks_css_output', 'generateblocks_add_general_css' );
613 /**
614 * Add general CSS that doesn't apply to our own blocks.
615 *
616 * @param string $css Existing CSS.
617 */
618 function generateblocks_add_general_css( $css ) {
619 $container_width = generateblocks_get_global_container_width();
620
621 if ( $container_width ) {
622 $css .= ':root{--gb-container-width:' . $container_width . ';}';
623 }
624
625 $css .= '.gb-container .wp-block-image img{vertical-align:middle;}';
626 $css .= '.gb-grid-wrapper .wp-block-image{margin-bottom:0;}';
627 $css .= '.gb-highlight{background:none;}';
628 $css .= '.gb-shape{line-height:0;}';
629
630 return $css;
631 }
632
633 add_filter( 'block_editor_settings_all', 'generateblocks_do_block_editor_styles', 15 );
634 /**
635 * Add our block editor styles.
636 *
637 * @param array $editor_settings The existing editor settings.
638 */
639 function generateblocks_do_block_editor_styles( $editor_settings ) {
640 $container_width = generateblocks_get_global_container_width();
641
642 $editor_settings['styles'][] = array(
643 'css' => ':root{--gb-container-width:' . $container_width . ';}',
644 );
645
646 $editor_settings['styles'][] = array(
647 'css' => '.gb-shape{line-height:0;}',
648 );
649
650 return $editor_settings;
651 }
652
653 add_action( 'enqueue_block_editor_assets', 'generateblocks_set_editor_permissions', 0 );
654 /**
655 * Output permissions for use in the editor.
656 *
657 * @return void
658 */
659 function generateblocks_set_editor_permissions() {
660 $permissions = apply_filters(
661 'generateblocks_permissions',
662 [
663 'isAdminUser' => current_user_can( 'manage_options' ),
664 'canEditPosts' => current_user_can( 'edit_posts' ),
665 'isGbProActive' => is_plugin_active( 'generateblocks-pro/plugin.php' ),
666 'isGpPremiumActive' => is_plugin_active( 'gp-premium/gp-premium.php' ),
667 ]
668 );
669
670 $permission_object = wp_json_encode( $permissions );
671 wp_register_script( 'generateblocks-editor-permissions', '', [], '1.0', false );
672 wp_enqueue_script( 'generateblocks-editor-permissions' );
673 $script = sprintf(
674 'const gbPermissions = %s;
675 Object.freeze( gbPermissions );',
676 $permission_object
677 );
678 wp_add_inline_script( 'generateblocks-editor-permissions', $script );
679 }
680
681 add_filter( 'render_block', 'generateblocks_do_html_attributes_escaping', 20, 2 );
682 /**
683 * Filter the rendered block content and escape HTML attributes.
684 *
685 * @param string $content The block content about to be appended to the post content.
686 * @param array $block The full block, including name and attributes.
687 * @return string
688 */
689 function generateblocks_do_html_attributes_escaping( $content, $block ) {
690 $html_attributes = $block['attrs']['htmlAttributes'] ?? [];
691 $link_attributes = $block['attrs']['linkHtmlAttributes'] ?? [];
692
693 if ( empty( $html_attributes ) && empty( $link_attributes ) ) {
694 return $content;
695 }
696
697 $v1_block_names = generateblocks_get_v1_block_names();
698 $block_name = $block['blockName'] ?? '';
699
700 // Only do this for our non-v1 blocks.
701 if (
702 ! generateblocks_str_starts_with( $block_name, 'generateblocks' ) ||
703 in_array( $block_name, $v1_block_names, true )
704 ) {
705 return $content;
706 }
707
708 $content = generateblocks_with_escaped_attributes(
709 $content,
710 [
711 'block_html_attrs' => $html_attributes,
712 'link_html_attrs' => $link_attributes,
713 ]
714 );
715
716 return $content;
717 }
718
719 add_filter( 'generateblocks_allowed_option_keys_rest_api', 'generateblocks_allow_additional_option_keys_rest_api' );
720 /**
721 * Allow additional option keys to be accessible via the REST API.
722 *
723 * @param array $allowed_keys Existing allowed keys.
724 */
725 function generateblocks_allow_additional_option_keys_rest_api( $allowed_keys ) {
726 if ( ! is_array( $allowed_keys ) ) {
727 $allowed_keys = [];
728 }
729
730 $acf_option_keys = generateblocks_get_acf_option_field_keys();
731 $allowed_keys = array_merge( $allowed_keys, $acf_option_keys );
732
733 return $allowed_keys;
734 }
735