*/ private static $footer_style_groups = array(); /** * Whether the common styles footer callback is already scheduled. * * @var bool */ private static $common_styles_scheduled = false; /** * Whether the close-button sprite callback is already scheduled. * * @var bool */ private static $close_button_svgs_scheduled = false; /** * Unique IDs already emitted during this request. * * @var array */ private static $rendered_unique_ids = array(); /** * 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']; // Generate a deterministic ID when empty. if ( empty( $unique_id ) ) { $unique_id = 'alerts-dlx-' . substr( md5( $normalized['alert_title'] . wp_strip_all_tags( $normalized['alert_description'] ) . $normalized['button_text'] . $normalized['secondary_button_text'] ), 0, 12 ); } // Deduplicate within this request. $original_id = $unique_id; $suffix = 0; while ( isset( self::$rendered_unique_ids[ $unique_id ] ) ) { ++$suffix; $unique_id = $original_id . '-' . $suffix; } self::$rendered_unique_ids[ $unique_id ] = true; $normalized['unique_id'] = $unique_id; $attributes['uniqueId'] = $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']; $icon_size = $normalized['icon_size']; $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']; $secondary_button_enabled = $normalized['secondary_button_enabled']; $secondary_button_text = $normalized['secondary_button_text']; $secondary_button_url = $normalized['secondary_button_url']; $secondary_button_target = $normalized['secondary_button_target']; $secondary_button_rel_no_follow = $normalized['secondary_button_rel_no_follow']; $secondary_button_rel_sponsored = $normalized['secondary_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_secondary = $normalized['color_secondary']; $color_secondary_hover = $normalized['color_secondary_hover']; $color_secondary_text = $normalized['color_secondary_text']; $color_secondary_text_hover = $normalized['color_secondary_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']; $conditions_enabled = $normalized['conditions_enabled']; $start_date = $normalized['start_date']; $end_date = $normalized['end_date']; $days_of_week = $normalized['days_of_week']; $url_params = $normalized['url_params']; $require_login = $normalized['require_login']; $required_roles = $normalized['required_roles']; // If block is editorial only, return early. if ( $is_block_editorial_only ) { return ''; } /** * Filters whether to restart the dismissal countdown when content changes. * * @param bool $restart_dismissal_countdown Whether to restart the dismissal countdown. * @param string $unique_id The unique ID of the alert. * @param array $normalized The normalized alert attributes. * @return bool */ $reset_dismissal_countdown = apply_filters( 'alerts_dlx_allow_dismissal_reset', true, $unique_id, $normalized ); // Create hash from title, displayed description, and button text. // This detects if content has changed and restarts the dismissal countdown. $is_in_block_editor = is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ); $cookie_name = $unique_id; $legacy_cookie_name = 'alerts-dlx-' . $unique_id; if ( $close_button_enabled && $reset_dismissal_countdown && ! $is_in_block_editor ) { $display_content = ! empty( $alert_description ) ? $alert_description : $content; $hash = md5( $alert_title . wp_strip_all_tags( $display_content ) . $button_text . $secondary_button_text ); $option_name = 'adlx-' . $unique_id; $hash_option = get_option( sanitize_key( $option_name ) ); if ( $hash_option !== $hash || ( false === $hash_option && isset( $_COOKIE[ $cookie_name ] ) ) ) { delete_option( sanitize_key( $option_name ) ); add_option( sanitize_key( $option_name ), $hash, '', 'no' ); // Remove cookie. setcookie( sanitize_key( $unique_id ), '', time() - 3600, '/' ); if ( isset( $_COOKIE[ $cookie_name ] ) ) { unset( $_COOKIE[ $cookie_name ] ); } } } // Check to see if expiration cookie is set. // Prefer uniqueId as cookie name; also accept legacy double-prefixed name. 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 || $secondary_button_enabled ) { $figure_classes[] = 'alerts-dlx-has-button'; } // Register scripts. call_user_func( $this->register_scripts_callback ); // Print common styles once per request. if ( ! self::$common_styles_scheduled ) { self::$common_styles_scheduled = true; add_action( 'wp_footer', function () { wp_print_styles( 'alerts-dlx-common-styles' ); } ); } // 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 ); } // Add conditions script when any condition is enabled. if ( $conditions_enabled ) { wp_enqueue_script( 'alerts-dlx-conditions', Functions::get_plugin_url( 'dist/alerts-dlx-conditions.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: ; --alerts-dlx--color-secondary: ; --alerts-dlx--color-secondary-hover: ; --alerts-dlx--color-secondary-text: ; --alerts-dlx--color-secondary-text-hover: ; }
data-conditions-enabled="true" data-start-date="" data-end-date="" data-days-of-week="" data-url-params="" data-require-login="true" data-required-roles="" >
$attributes, 'content' => $content, ) ), DAY_IN_SECONDS ); return sprintf( '', esc_attr( $unique_id ), esc_attr( $start_date ), esc_attr( $end_date ), esc_attr( $days_of_week ), esc_attr( wp_json_encode( $url_params ) ), esc_attr( $require_login ? 'true' : 'false' ), esc_attr( $required_roles ) ); } return $html; } /** * Print one alert action button. * * @param string $class_name Button class names. * @param string $url Button URL. * @param string $text Button label. * @param bool $target Open in a new tab. * @param bool $nofollow Add rel=nofollow. * @param bool $sponsored Add rel=sponsored. */ private function print_action_button( $class_name, $url, $text, $target, $nofollow, $sponsored ) { ?>
target="_blank" rel="" >