← All changes
|
modules/gutenberg/classes/class-spec-init-blocks.php
+44
-8
0.0.10
→
2.12.8
View file →
| @@ -61,9 +61,9 @@ | ||
| 61 | 61 | add_action( 'enqueue_block_assets', [ $this, 'block_assets' ] ); |
| 62 | 62 | |
| 63 | 63 | // Hook: Editor assets. |
| 64 | 64 | add_action( 'enqueue_block_editor_assets', [ $this, 'editor_assets' ] ); |
| 65 | - | |
| 65 | + add_action( 'enqueue_block_assets', [ $this, 'editor_assets' ] ); | |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | 68 | /** |
| 69 | 69 | * Enqueue Gutenberg block assets for both frontend + backend. |
| @@ -73,15 +73,51 @@ | ||
| 73 | 73 | public function block_assets() { |
| 74 | 74 | |
| 75 | 75 | global $post; |
| 76 | 76 | |
| 77 | - // Register block styles for both frontend + backend. | |
| 78 | - wp_enqueue_style( | |
| 79 | - 'srfm-block-style-css', // Handle. | |
| 80 | - SRFM_URL . 'modules/gutenberg/build/style-blocks.css', | |
| 81 | - is_admin() ? [ 'wp-editor' ] : null, // Dependency to include the CSS after it. | |
| 82 | - SRFM_VER // filemtime( plugin_dir_path( __DIR__ ) . 'build/style-blocks.css' ) // Version: File modification time. | |
| 83 | - ); | |
| 77 | + if ( empty( $post->post_content ) ) { | |
| 78 | + return; | |
| 79 | + } | |
| 80 | + | |
| 81 | + // find blocks in post content. | |
| 82 | + $blocks = parse_blocks( $post->post_content ); | |
| 83 | + | |
| 84 | + if ( empty( $blocks ) && ! is_array( $blocks ) ) { | |
| 85 | + return; | |
| 86 | + } | |
| 87 | + | |
| 88 | + $allowed_blocks = [ | |
| 89 | + 'srfm/form', | |
| 90 | + 'srfm/advanced-heading', | |
| 91 | + 'srfm/separator', | |
| 92 | + 'srfm/image', | |
| 93 | + 'srfm/icon', | |
| 94 | + ]; | |
| 95 | + | |
| 96 | + $has_required_block = false; | |
| 97 | + | |
| 98 | + foreach ( $blocks as $block ) { | |
| 99 | + | |
| 100 | + if ( $has_required_block ) { | |
| 101 | + break; | |
| 102 | + } | |
| 103 | + | |
| 104 | + if ( in_array( $block['blockName'], $allowed_blocks, true ) ) { | |
| 105 | + $has_required_block = true; | |
| 106 | + } | |
| 107 | + } | |
| 108 | + | |
| 109 | + if ( ! $has_required_block ) { | |
| 110 | + return; | |
| 111 | + } | |
| 112 | + | |
| 113 | + // Register block styles for both frontend + backend. | |
| 114 | + wp_enqueue_style( | |
| 115 | + 'srfm-block-style-css', // Handle. | |
| 116 | + SRFM_URL . 'modules/gutenberg/build/style-blocks.css', | |
| 117 | + is_admin() ? [ 'wp-editor' ] : null, // Dependency to include the CSS after it. | |
| 118 | + SRFM_VER // filemtime( plugin_dir_path( __DIR__ ) . 'build/style-blocks.css' ) // Version: File modification time. | |
| 119 | + ); | |
| 84 | 120 | |
| 85 | 121 | } |
| 86 | 122 | |
| 87 | 123 | /** |