0 ), $atts, self::SHORTCODE ); return self::render( (int) $atts['id'] ); } /** * HTML of a published Custom block or pattern, '' when there is none. * * @param int $id Post id. * @return string */ public static function render( $id ) { $post = $id ? get_post( $id ) : null; if ( ! $post || ! in_array( $post->post_type, self::post_types(), true ) || 'publish' !== $post->post_status || post_password_required( $post ) ) { return ''; } if ( in_array( $post->ID, self::$stack, true ) ) { return ''; } self::$stack[] = $post->ID; $before = self::style_rules(); $content = (string) $post->post_content; if ( has_blocks( $content ) ) { $html = do_blocks( $content ); } else { $html = wpautop( wptexturize( $content ) ); // Saved with the classic editor. } $html = do_shortcode( shortcode_unautop( $html ) ); if ( function_exists( 'wp_filter_content_tags' ) ) { $html = wp_filter_content_tags( $html, 'cpod_block' ); } array_pop( self::$stack ); if ( '' === trim( $html ) ) { return ''; } self::late_styles( $before ); $html = '
' . $html . '
'; /** * HTML of a Custom block or pattern. * * @param string $html HTML. * @param \WP_Post $post Block post. */ return cpod_strip_code_tags( (string) apply_filters( 'cpod_block_html', $html, $post ) ); } /** Selectors of the block-support styles WordPress has collected so far. */ private static function style_rules() { if ( ! class_exists( '\WP_Style_Engine' ) || ! method_exists( '\WP_Style_Engine', 'get_store' ) ) { return null; } return array_keys( \WP_Style_Engine::get_store( 'block-supports' )->get_all_rules() ); } /** * Block-support styles added while the block rendered, once WordPress has * printed its own (at the start of wp_footer), go through the style loader. * Earlier they are left to WordPress. * * @param string[]|null $before Selectors before rendering. */ private static function late_styles( $before ) { if ( null === $before || ! did_action( 'wp_footer' ) || ! function_exists( 'wp_style_engine_get_stylesheet_from_css_rules' ) ) { return; } $rules = \WP_Style_Engine::get_store( 'block-supports' )->get_all_rules(); $css = array(); foreach ( array_diff_key( $rules, array_flip( $before ) ) as $rule ) { $declarations = $rule->get_declarations()->get_declarations(); if ( $declarations ) { $item = array( 'selector' => $rule->get_selector(), 'declarations' => $declarations, ); // Media query of the rule (WordPress 6.6+). if ( method_exists( $rule, 'get_rules_group' ) && $rule->get_rules_group() ) { $item['rules_group'] = $rule->get_rules_group(); } $css[] = $item; } } $css = $css ? wp_style_engine_get_stylesheet_from_css_rules( $css, array( 'prettify' => false ) ) : ''; if ( '' === $css ) { return; } if ( ! wp_style_is( 'cpod-block-styles', 'registered' ) ) { wp_register_style( 'cpod-block-styles', false, array(), CPOD_VERSION ); } wp_enqueue_style( 'cpod-block-styles' ); wp_add_inline_style( 'cpod-block-styles', wp_strip_all_tags( $css ) ); // Footer styles already printed: print this one through the style loader now. if ( did_action( 'wp_print_footer_scripts' ) ) { wp_print_styles( 'cpod-block-styles' ); } } }