array( $this, 'frontend' ),
'mediaron/alerts-dlx-bootstrap' => array( $this, 'frontend' ),
'mediaron/alerts-dlx-chakra' => array( $this, 'frontend' ),
'mediaron/alerts-dlx-material' => array( $this, 'frontend' ),
'mediaron/alerts-dlx-shoelace' => array( $this, 'frontend' ),
);
add_filter(
'block_type_metadata_settings',
function ( $settings, $metadata ) use ( $render_callbacks ) {
if ( isset( $render_callbacks[ $metadata['name'] ] ) ) {
$settings['render_callback'] = $render_callbacks[ $metadata['name'] ];
}
return $settings;
},
10,
2
);
if ( function_exists( 'wp_register_block_types_from_metadata_collection' ) ) {
\wp_register_block_types_from_metadata_collection( Functions::get_plugin_dir( 'build/js/blocks/' ), Functions::get_plugin_dir( 'build/blocks-manifest.php' ) );
} else {
if ( function_exists( 'wp_register_block_metadata_collection' ) ) {
\wp_register_block_metadata_collection( Functions::get_plugin_dir( 'build/js/blocks/' ), Functions::get_plugin_dir( 'build/blocks-manifest.php' ) );
}
$manifest_data = require Functions::get_plugin_dir( 'build/blocks-manifest.php' );
foreach ( array_keys( $manifest_data ) as $block_type ) {
register_block_type( __DIR__ . "/build/js/blocks/{$block_type}" );
}
}
// Register style handles before WordPress enqueues editorStyle at priority 10.
add_action( 'enqueue_block_assets', array( $this, 'register_editor_style_handles' ), 1 );
add_action( 'enqueue_block_editor_assets', array( $this, 'register_editor_style_handles' ), 1 );
add_action( 'enqueue_block_assets', array( $this, 'register_block_editor_scripts_iframe' ) );
add_action( 'enqueue_block_editor_assets', array( $this, 'register_block_editor_scripts' ) );
// Add alertsdlx block category.
add_filter(
'block_categories_all',
function ( $categories ) {
return array_merge(
$categories,
array(
array(
'slug' => 'singular-alerts',
'title' => __( 'Singular Alerts', 'alerts-dlx' ),
),
array(
'slug' => 'singular-alerts-global',
'title' => __( 'Singular Alerts (Global)', 'alerts-dlx' ),
),
array(
'slug' => 'alertsdlx-legacy',
'title' => __( 'AlertsDLX Legacy', 'alerts-dlx' ),
),
)
);
}
);
// Add shortcode to block.
add_shortcode( 'alertsdlx', array( $this, 'shortcode' ) );
}
/**
* Shortcode callback.
*
* @param array $atts Shortcode attributes.
* @param string $content Shortcode content.
* @return string
*/
public function shortcode( $atts = array(), $content = '' ) {
$defaults = AlertAttributes::get_shortcode_defaults();
$atts = shortcode_atts( $defaults, $atts, 'alertsdlx' );
$atts = AlertLibrary::apply_global_style_slug_to_shortcode_atts( $atts );
// Sanitize values used before the shared renderer. Inline CSS is emitted
// below, so waiting for renderer normalization would be too late.
$sanitized_id = sanitize_html_class( (string) $atts['unique_id'] );
$atts['unique_id'] = '' !== $sanitized_id ? $sanitized_id : $defaults['unique_id'];
// Values embedded into CSS syntax need strict allowlists; HTML escaping
// alone does not neutralize braces or declaration separators. Preserve
// every unit exposed by the existing builder and fall back to defaults.
$allowed_alert_groups = array( 'bootstrap', 'chakra', 'material', 'shoelace' );
if ( ! in_array( $atts['alert_group'], $allowed_alert_groups, true ) ) {
$atts['alert_group'] = $defaults['alert_group'];
}
$allowed_width_units = array( 'px', 'em', 'rem', '%', 'vw' );
if ( ! in_array( $atts['maximum_width_unit'], $allowed_width_units, true ) ) {
$atts['maximum_width_unit'] = $defaults['maximum_width_unit'];
}
$atts['margin_spacing'] = AlertAttributes::sanitize_margin_spacing( $atts['margin_spacing'] ?? '' );
$atts['margin_spacing_target'] = AlertAttributes::sanitize_margin_spacing_target( $atts['margin_spacing_target'] ?? '' );
$color_fields = array(
'color_primary',
'color_border',
'color_accent',
'color_alt',
'color_alt_hover',
'color_alt_text',
'color_alt_text_hover',
'color_secondary',
'color_secondary_hover',
'color_secondary_text',
'color_secondary_text_hover',
'color_bold',
'color_light',
);
foreach ( $color_fields as $color_field ) {
$color = Functions::sanitize_css_color( (string) $atts[ $color_field ] );
$atts[ $color_field ] = false === $color ? $defaults[ $color_field ] : $color;
}
// Catalog keys resolve to SVG. Custom and legacy markup stay sanitized.
$atts['icon'] = Functions::resolve_icon_svg( (string) $atts['icon'], (string) $atts['alert_group'] );
// Numeric shortcode attributes share the builder's documented bounds.
// This prevents extreme values from reaching CSS or cookie metadata.
$atts['maximum_width'] = min( 5000, max( 1, (int) $atts['maximum_width'] ) );
$atts['base_font_size'] = min( 96, max( 8, (int) $atts['base_font_size'] ) );
$atts['close_button_expiration'] = min( 31536000, max( 0, (int) $atts['close_button_expiration'] ) );
$atts['start_date'] = substr( sanitize_text_field( (string) $atts['start_date'] ), 0, 19 );
$atts['end_date'] = substr( sanitize_text_field( (string) $atts['end_date'] ), 0, 19 );
$atts['days_of_week'] = substr( sanitize_text_field( (string) $atts['days_of_week'] ), 0, 20 );
$atts['url_params'] = Functions::sanitize_url_params( Functions::decode_url_params_string( $atts['url_params'] ?? array() ) );
$atts['required_roles'] = substr( sanitize_text_field( (string) $atts['required_roles'] ), 0, 200 );
// If alert description is empty, use content.
if ( empty( $atts['alert_description'] ) && ! empty( $content ) ) {
// Bound content before filters and their result after filtering.
$content = substr( (string) $content, 0, 20000 );
$atts['alert_description'] = apply_filters( 'alerts_dlx_the_content', $content );
}
$atts['alert_description'] = substr( (string) $atts['alert_description'], 0, 20000 );
// Set the default variant.
if ( '' === $atts['variant'] ) {
switch ( $atts['alert_group'] ) {
case 'bootstrap':
$atts['variant'] = 'default';
break;
case 'chakra':
$atts['variant'] = 'subtle';
break;
case 'material':
$atts['variant'] = 'default';
break;
case 'shoelace':
$atts['variant'] = 'top-accent';
break;
}
}
// Fill in the rest of the attributes.
$atts['button_enabled'] = ! empty( $atts['button_text'] ) && ! empty( $atts['button_url'] );
$atts['secondary_button_enabled'] = ! empty( $atts['secondary_button_text'] ) && ! empty( $atts['secondary_button_url'] );
$atts['description_enabled'] = ! empty( $atts['alert_description'] );
$atts['title_enabled'] = ! empty( $atts['alert_title'] );
$atts['icon_enabled'] = ! empty( $atts['icon'] ) || ! empty( $atts['image_url'] );
// Register only the theme styles the public shortcode can print.
$this->register_frontend_styles();
$style_handles_to_print = array();
if ( ! wp_style_is( 'alerts-dlx-block-editor-styles-lato', 'done' ) ) {
$style_handles_to_print[] = 'alerts-dlx-block-editor-styles-lato';
}
switch ( $atts['alert_group'] ) {
case 'bootstrap':
if ( ! wp_style_is( 'alerts-dlx-bootstrap-styles', 'done' ) ) {
wp_enqueue_style( 'alerts-dlx-bootstrap-styles' );
$style_handles_to_print[] = 'alerts-dlx-bootstrap-styles';
}
break;
case 'chakra':
if ( ! wp_style_is( 'alerts-dlx-chakra-styles', 'done' ) ) {
wp_enqueue_style( 'alerts-dlx-chakra-styles' );
$style_handles_to_print[] = 'alerts-dlx-chakra-styles';
}
break;
case 'material':
if ( ! wp_style_is( 'alerts-dlx-material-styles', 'done' ) ) {
wp_enqueue_style( 'alerts-dlx-material-styles' );
$style_handles_to_print[] = 'alerts-dlx-material-styles';
}
break;
case 'shoelace':
if ( ! wp_style_is( 'alerts-dlx-shoelace-styles', 'done' ) ) {
$style_handles_to_print[] = 'alerts-dlx-shoelace-styles';
}
break;
}
ob_start();
if ( 'custom' === $atts['alert_type'] ) {
ob_start();
?>
$value ) {
$new_atts[ Functions::to_camelcase( $key ) ] = $value;
}
$return = ob_get_clean() . $this->frontend( $new_atts, $new_atts['alertDescription'] );
return $return;
}
/**
* Output the front-end structure.
*
* @param array $attributes Block editor attributes.
* @param string $content Current content.
* @param \WP_Block|null $block Block instance.
* @return string
*/
public function frontend( array $attributes, string $content, $block = null ) {
// Merge linked global style appearance when globalStyleId is valid.
$attributes = AlertLibrary::apply_global_style_to_block_attributes( $attributes );
$renderer = new AlertRenderer(
array( $this, 'register_frontend_styles' ),
array( $this, 'print_close_button_svgs' )
);
return $renderer->render( $attributes, $content, $block );
}
/**
* Render an alert from stored attributes (used by REST re-render).
*
* @param array $attributes Block editor attributes.
* @param string $content Current content.
* @return string
*/
public static function render_alert( array $attributes, string $content = '' ) {
$attributes['conditionsEnabled'] = false;
$blocks = new self();
return $blocks->frontend( $attributes, $content, null );
}
/**
* Register the block editor script for the iframe.
*/
public function register_block_editor_scripts_iframe(): void {
if ( ! is_admin() ) {
return;
}
$this->register_block_editor_scripts();
}
/**
* Register theme styles and the optional Lato font for front-end alerts.
*
* Public rendering uses this instead of the editor script so a page of
* alerts does not query the style library. Registration is checked on the
* current style registry so the editor iframe can collect the same handles.
*/
public function register_frontend_styles() {
$version = Functions::get_plugin_version();
$styles = array(
'alerts-dlx-common-styles' => 'dist/alerts-dlx-common.css',
'alerts-dlx-bootstrap-styles' => 'dist/alerts-dlx-bootstrap-styles.css',
'alerts-dlx-chakra-styles' => 'dist/alerts-dlx-chakra-styles.css',
'alerts-dlx-material-styles' => 'dist/alerts-dlx-material-styles.css',
'alerts-dlx-shoelace-styles' => 'dist/alerts-dlx-shoelace-styles.css',
);
foreach ( $styles as $handle => $path ) {
if ( wp_style_is( $handle, 'registered' ) ) {
continue;
}
wp_register_style(
$handle,
Functions::get_plugin_url( $path ),
array(),
$version,
'all'
);
}
if ( wp_style_is( 'alerts-dlx-block-editor-styles-lato', 'registered' ) ) {
return;
}
// Bundled Lato stays on unless Override fonts is enabled or alerts_dlx_load_fonts returns false.
if ( Options::should_load_fonts() ) {
wp_register_style(
'alerts-dlx-block-editor-styles-lato',
Functions::get_plugin_url( 'dist/alerts-dlx-gfont-lato.css' ),
array(),
$version,
'all'
);
wp_add_inline_style(
'alerts-dlx-block-editor-styles-lato',
':root {
--alerts-dlx-font-family: "Lato", "Helvetica", "Arial", sans-serif;
}'
);
return;
}
wp_register_style(
'alerts-dlx-block-editor-styles-lato',
false,
array(),
$version,
'all'
);
$default_font_family = apply_filters( 'alerts_dlx_default_font_family', Options::get_fallback_font_family() );
wp_add_inline_style(
'alerts-dlx-block-editor-styles-lato',
':root {
--alerts-dlx-font-family: ' . wp_strip_all_tags( $default_font_family ) . ';
}'
);
}
/**
* Register editor and theme style handles without loading the style library.
*
* Block metadata lists these handles in editorStyle. WordPress enqueues
* them while collecting iframe assets, so they must already be registered.
*/
public function register_editor_style_handles() {
$this->register_frontend_styles();
$version = Functions::get_plugin_version();
if ( ! wp_style_is( 'alerts-dlx-block-editor', 'registered' ) ) {
// Register styles here because array in block.json fails when using array of styles (enqueues wrong script).
wp_register_style(
'alerts-dlx-block-editor',
Functions::get_plugin_url( 'dist/alerts-dlx-block-editor.css' ),
array(),
$version,
'all'
);
}
if ( ! wp_style_is( 'alerts-dlx-block-editor-styles', 'registered' ) ) {
wp_register_style(
'alerts-dlx-block-editor-styles',
Functions::get_plugin_url( 'build/index.css' ),
array( 'alerts-dlx-block-editor' ),
$version,
'all'
);
}
}
/**
* Register the block editor script with localized vars.
*/
public function register_block_editor_scripts() {
$this->register_editor_style_handles();
$deps = require Functions::get_plugin_dir( 'build/index.asset.php' );
wp_register_script(
'alerts-dlx-block',
Functions::get_plugin_url( 'build/index.js' ),
$deps['dependencies'],
$deps['version'],
true
);
wp_localize_script(
'alerts-dlx-block',
'alertsDlxBlock',
array(
'font_stylesheet' => Functions::get_plugin_url( 'dist/alerts-dlx-gfont-lato.css' ),
'loadFonts' => Options::should_load_fonts(),
'isEditor' => current_user_can( 'edit_others_posts' ),
'isAuthor' => current_user_can( 'edit_posts' ),
'isAdmin' => current_user_can( 'manage_options' ),
'colorPalette' => Functions::get_theme_color_palette(),
'defaultImage' => Functions::get_plugin_url( 'assets/bell.png' ),
'headlineStyle' => Options::get_headline_tag(),
'headlineCustomClasses' => Options::get_headline_custom_classes(),
'headlineForceSize' => Options::is_headline_force_size(),
'marginSpacingDefault' => Options::get_margin_spacing(),
'marginSpacingTargetDefault' => Options::get_margin_spacing_target(),
'libraryItems' => AlertLibrary::get_items_for_editor(),
'canManageLibrary' => current_user_can( 'manage_options' ),
'debugMode' => Options::is_debug_mode(),
'roles' => wp_roles()->get_names(),
)
);
}
/**
* Print the close button SVGs.
*/
public function print_close_button_svgs() {
?>