*/ private static $footer_style_groups = array(); /** * Whether the close-button sprite callback is already scheduled. * * @var bool */ private static $close_button_svgs_scheduled = false; /** * Store the asset and close-button callbacks. * * @param callable $register_scripts_callback Asset registration callback. * @param callable $print_close_button_svgs_callback Close-icon callback. */ public function __construct( $register_scripts_callback, $print_close_button_svgs_callback ) { $this->register_scripts_callback = $register_scripts_callback; $this->print_close_button_svgs_callback = $print_close_button_svgs_callback; } /** * Output the historical frontend structure. * * @param array $attributes Block editor attributes. * @param string $content Current content. * @param \WP_Block|null $block Block instance. * @return string */ public function render( array $attributes, string $content, $block = null ) { $normalized = AlertAttributes::normalize( $attributes, $block ); $unique_id = $normalized['unique_id']; $alert_group = $normalized['alert_group']; $alert_type = $normalized['alert_type']; $align = $normalized['align']; $alert_title = $normalized['alert_title']; $alert_description = $normalized['alert_description']; $button_enabled = $normalized['button_enabled']; $maximum_width_unit = $normalized['maximum_width_unit']; $maximum_width = $normalized['maximum_width']; $margin_spacing = $normalized['margin_spacing']; $margin_spacing_target = $normalized['margin_spacing_target']; $icon = $normalized['icon']; $description_enabled = $normalized['description_enabled']; $title_enabled = $normalized['title_enabled']; $icon_enabled = $normalized['icon_enabled']; $base_font_size = $normalized['base_font_size']; $icon_vertical_alignment = $normalized['icon_vertical_alignment']; $variant = $normalized['variant']; $mode = $normalized['mode']; $button_text = $normalized['button_text']; $button_url = $normalized['button_url']; $button_target = $normalized['button_target']; $button_rel_no_follow = $normalized['button_rel_no_follow']; $button_rel_sponsored = $normalized['button_rel_sponsored']; $icon_appearance = $normalized['icon_appearance']; $color_primary = $normalized['color_primary']; $color_border = $normalized['color_border']; $color_accent = $normalized['color_accent']; $color_alt = $normalized['color_alt']; $color_alt_hover = $normalized['color_alt_hover']; $color_alt_text = $normalized['color_alt_text']; $color_alt_text_hover = $normalized['color_alt_text_hover']; $color_bold = $normalized['color_bold']; $color_light = $normalized['color_light']; $close_button_enabled = $normalized['close_button_enabled']; $close_button_expiration = $normalized['close_button_expiration']; $is_block_editorial_only = $normalized['is_block_editorial_only']; $icon_source = $normalized['icon_source']; $image_url = $normalized['image_url']; $image_id = $normalized['image_id']; // If block is editorial only, return early. if ( $is_block_editorial_only ) { return ''; } // Check to see if expiration cookie is set. // Prefer uniqueId as cookie name; also accept legacy double-prefixed name. $cookie_name = $unique_id; $legacy_cookie_name = 'alerts-dlx-' . $unique_id; if ( $close_button_enabled && ( isset( $_COOKIE[ $cookie_name ] ) || isset( $_COOKIE[ $legacy_cookie_name ] ) ) ) { return ''; } ob_start(); // Add base classes to figure element. $figure_classes = array( 'alerts-dlx-alert', 'alerts-dlx-' . $alert_group, ); if ( $icon_enabled ) { $figure_classes[] = 'alerts-dlx-has-icon'; } if ( $description_enabled ) { $figure_classes[] = 'alerts-dlx-has-description'; } if ( $button_enabled ) { $figure_classes[] = 'alerts-dlx-has-button'; } // Register scripts. call_user_func( $this->register_scripts_callback ); // Print each theme's styles once per request. if ( ! isset( self::$footer_style_groups[ $alert_group ] ) ) { self::$footer_style_groups[ $alert_group ] = true; switch ( $alert_group ) { case 'bootstrap': add_action( 'wp_footer', function () { wp_print_styles( array( 'alerts-dlx-bootstrap-styles', 'alerts-dlx-block-editor-styles-lato', ) ); } ); break; case 'chakra': add_action( 'wp_footer', function () { wp_print_styles( array( 'alerts-dlx-chakra-styles', 'alerts-dlx-block-editor-styles-lato', ) ); } ); break; case 'material': add_action( 'wp_footer', function () { wp_print_styles( array( 'alerts-dlx-material-styles', 'alerts-dlx-block-editor-styles-lato', ) ); } ); break; case 'shoelace': add_action( 'wp_footer', function () { wp_print_styles( array( 'alerts-dlx-shoelace-styles', 'alerts-dlx-block-editor-styles-lato', ) ); } ); break; } } // Close button footer and scripts. if ( $close_button_enabled ) { // Add footer SVGs for close button once, even with several dismissible alerts. if ( ! self::$close_button_svgs_scheduled ) { self::$close_button_svgs_scheduled = true; add_action( 'wp_footer', $this->print_close_button_svgs_callback ); } // Add close button script. wp_enqueue_script( 'alerts-dlx-close-button', Functions::get_plugin_url( 'dist/alerts-dlx-dismiss.js' ), array(), Functions::get_plugin_version(), true ); } if ( 'custom' === $alert_type ) { ob_start(); ?> # { --alerts-dlx--color-primary: ; --alerts-dlx--color-border: ; --alerts-dlx--color-accent: ; --alerts-dlx--color-alt: ; --alerts-dlx--color-bold: ; --alerts-dlx--color-light: ; --alerts-dlx--color-alt-hover: ; --alerts-dlx--color-alt-text: ; --alerts-dlx--color-alt-text-hover: ; }