get_settings(); if ( isset( $settings['color']['palette']['theme'] ) ) { $color_palette = $settings['color']['palette']['theme']; } // If empty color palette, try to get from theme supports. if ( empty( $color_palette ) ) { $color_palette = get_theme_support( 'editor-color-palette' ); if ( ! empty( $color_palette ) ) { $color_palette = $color_palette[0]; } } /** * Filter the color palette used by the plugin. * * @param array $color_palette { * @type string $name The color name. * @type string $slug The color slug. * @type string $color The color hex value. * } */ $color_palette = apply_filters( 'alerts_dlx_color_palette', $color_palette ); return $color_palette; } /** * Sanitize a CSS color without allowing declaration injection. * * Accepts hex (3/4/6/8), rgb/hsl functions, named colors, and CSS custom * properties with an optional one-level fallback so theme palette values * such as var(--theme-palette-color-2, #587863) survive the shortcode path. * * @since 2.4.1 * * @param mixed $value Untrusted CSS color. * @return string|false Sanitized color, empty string when blank, or false when invalid. */ public static function sanitize_css_color( $value ) { $color = trim( sanitize_text_field( (string) $value ) ); if ( '' === $color ) { return ''; } if ( strlen( $color ) > 200 ) { return false; } // Hex: #RGB, #RGBA, #RRGGBB, #RRGGBBAA. if ( preg_match( '/^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$/i', $color ) ) { return $color; } // Functional RGB/HSL colors. if ( preg_match( '/^(?:rgb|rgba|hsl|hsla)\([0-9.,%\s+\/-]+\)$/i', $color ) ) { return $color; } // CSS custom properties, including WordPress preset tokens and one fallback. $custom_property = '--[a-zA-Z0-9_-]+'; $hex_fallback = '#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{4}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})'; $fn_fallback = '(?:rgb|rgba|hsl|hsla)\([0-9.,%\s+\/-]+\)'; $named_fallback = '[a-zA-Z]+'; $nested_var = 'var\(\s*' . $custom_property . '\s*\)'; $fallback = '(?:' . $hex_fallback . '|' . $fn_fallback . '|' . $nested_var . '|' . $named_fallback . ')'; $var_pattern = '/^var\(\s*' . $custom_property . '(?:\s*,\s*' . $fallback . ')?\s*\)$/'; if ( preg_match( $var_pattern, $color ) ) { return $color; } // Named CSS colors and keywords already accepted by the builder. if ( preg_match( '/^[a-zA-Z]+$/', $color ) ) { return $color; } return false; } /** * Take a _ separated field and convert to camelcase. * * @param string $field Field to convert to camelcase. * * @return string camelCased field. */ public static function to_camelcase( string $field ) { return str_replace( '_', '', lcfirst( ucwords( $field, '_' ) ) ); } /** * Take a camelCase field and convert to underlines. * * @param string $field Field to convert to underlines. * * @return string underlined field. */ public static function to_underlines( string $field ) { $regex = '/([a-z])([A-Z])/'; if ( preg_match( $regex, $field ) ) { $field = strtolower( preg_replace( $regex, '$1_$2', $field ) ); } return $field; } /** * Take a camelcase key array and converts it to underline case. * * @param array $fields Array of fields to convert to underline case. * * @return array Array of fields in underline case. */ public static function to_underlines_recursive( array $fields ) { foreach ( $fields as $key => $value ) { if ( is_numeric( $key ) || is_bool( $key ) ) { continue; } $old_key = $key; $key = self::to_underlines( $key ); if ( is_array( $value ) ) { $value = self::to_underlines_recursive( $value ); } $fields[ $key ] = $value; if ( $key !== $old_key ) { unset( $fields[ $old_key ] ); } } return $fields; } /** * Get the current admin tab. * * @return null|string Current admin tab. */ public static function get_admin_tab() { $tab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'tab', FILTER_DEFAULT ) ) ); if ( $tab && is_string( $tab ) ) { return sanitize_key( $tab ); } return null; } /** * Get the current admin subtab. * * @return null|string Current admin subtab. */ public static function get_admin_subtab() { $subtab = sanitize_text_field( wp_unslash( filter_input( INPUT_GET, 'subtab', FILTER_DEFAULT ) ) ); if ( $subtab && is_string( $subtab ) ) { return sanitize_key( $subtab ); } return null; } /** * Get the library item ID from the admin query string. * * @return int */ public static function get_admin_library_item_id() { $item_id = filter_input( INPUT_GET, 'item', FILTER_VALIDATE_INT ); return $item_id ? (int) $item_id : 0; } /** * Get the library kind from the admin query string. * * @return string */ public static function get_admin_library_kind() { $kind = sanitize_key( (string) wp_unslash( filter_input( INPUT_GET, 'kind', FILTER_DEFAULT ) ) ); if ( in_array( $kind, AlertLibrary::get_allowed_kinds(), true ) ) { return $kind; } return AlertLibrary::KIND_GLOBAL_STYLE; } /** * Return the URL to the admin screen * * @param string $tab Tab path to load. * @param string $sub_tab Subtab path to load. * * @return string URL to admin screen. Output is not escaped. */ public static function get_settings_url( $tab = '', $sub_tab = '' ) { $options_url = admin_url( 'options-general.php?page=singular-alerts' ); if ( ! empty( $tab ) ) { $options_url = add_query_arg( array( 'tab' => sanitize_title( $tab ) ), $options_url ); if ( ! empty( $sub_tab ) ) { $options_url = add_query_arg( array( 'subtab' => sanitize_title( $sub_tab ) ), $options_url ); } } return $options_url; } /** * Return the plugin slug. * * @return string plugin slug. */ public static function get_plugin_slug() { return dirname( plugin_basename( ALERTS_DLX_FILE ) ); } /** * Return the basefile for the plugin. * * @return string base file for the plugin. */ public static function get_plugin_file() { return plugin_basename( ALERTS_DLX_FILE ); } /** * Return the version for the plugin. * * @return float version for the plugin. */ public static function get_plugin_version() { return ALERTS_DLX_VERSION; } /** * Get the plugin author name. */ public static function get_plugin_author() { /** * Filer the output of the plugin Author. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_author Plugin Author name. */ $plugin_author = apply_filters( 'alerts_dlx_plugin_author', 'MediaRon LLC' ); return $plugin_author; } /** * Return the Plugin author URI. */ public static function get_plugin_author_uri() { /** * Filer the output of the plugin Author URI. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_author_uri Plugin Author URI. */ $plugin_author = apply_filters( 'alerts_dlx_plugin_author_uri', 'https://mediaron.com' ); return $plugin_author; } /** * Return the plugin name for the plugin. * * @return string Plugin name. */ public static function get_plugin_name() { /** * Filer the output of the plugin name. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_name Plugin name. */ return apply_filters( 'alerts_dlx_plugin_name', __( 'Singular Alerts', 'alerts-dlx' ) ); } /** * Return the plugin description for the plugin. * * @return string plugin description. */ public static function get_plugin_description() { /** * Filer the output of the plugin name. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_description Plugin description. */ return apply_filters( 'alerts_dlx_plugin_description', __( 'An alert and notification block inspired by Bootstrap, Material UI, and Chakra UI.', 'alerts-dlx' ) ); } /** * Retrieve the plugin URI. */ public static function get_plugin_uri() { /** * Filer the output of the plugin URI. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_uri Plugin URI. */ return apply_filters( 'alerts_dlx_plugin_uri', 'https://github.com/ronalfy/alerts-dlx' ); } /** * Retrieve the plugin support URI. */ public static function get_plugin_support_uri() { /** * Filer the output of the plugin support URI. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_support_uri Plugin Support URI. */ return apply_filters( 'alerts_dlx_plugin_support_uri', 'https://github.com/ronalfy/alerts-dlx/issues' ); } /** * Retrieve the plugin documentation URI. */ public static function get_plugin_docs_uri() { /** * Filer the output of the plugin docs URI. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_docs_uri Plugin Docs URI. */ return apply_filters( 'alerts_dlx_plugin_docs_uri', 'https://github.com/MediaRon/alertsdlx-docs' ); } /** * Retrieve the plugin documentation URI. */ public static function get_plugin_ratings_uri() { /** * Filer the output of the plugin ratings URI. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_ratings_uri Plugin ratings URI. */ return apply_filters( 'alerts_dlx_plugin_docs_uri', 'https://wordpress.org/plugins/alerts-dlx/' ); } /** * Retrieve the plugin title. */ public static function get_plugin_title() { /** * Filer the output of the plugin title. * * Potentially change branding of the plugin. * * @since 1.0.0 * * @param string $plugin_menu_title Plugin Menu Name. */ return apply_filters( 'alerts_dlx_plugin_menu_title', self::get_plugin_name() ); } /** * Returns appropriate html for KSES. * * @param bool $svg Whether to add SVG data to KSES. */ public static function get_kses_allowed_html( $svg = true ) { $allowed_tags = wp_kses_allowed_html(); $allowed_tags['nav'] = array( 'class' => array(), ); $allowed_tags['a']['class'] = array(); $allowed_tags['div']['class'] = array(); if ( ! $svg ) { return $allowed_tags; } $allowed_tags['svg'] = array( 'xmlns' => array(), 'fill' => array(), 'viewbox' => array(), 'role' => array(), 'aria-hidden' => array(), 'focusable' => array(), 'class' => array(), ); $allowed_tags['path'] = array( 'd' => array(), 'fill' => array(), 'opacity' => array(), ); $allowed_tags['g'] = array( 'fill' => array(), 'fill-opacity' => array(), 'stroke' => array(), 'stroke-width' => array(), 'stroke-opacity' => array(), 'stroke-dasharray' => array(), 'stroke-dashoffset' => array(), 'stroke-linecap' => array(), 'stroke-linejoin' => array(), ); $allowed_tags['circle'] = array( 'cx' => array(), 'cy' => array(), 'r' => array(), 'fill' => array(), 'opacity' => array(), 'stroke' => array(), ); $allowed_tags['line'] = array( 'x1' => array(), 'x2' => array(), 'y1' => array(), 'y2' => array(), 'fill' => array(), 'opacity' => array(), 'stroke' => array(), ); $allowed_tags['use'] = array( 'xlink:href' => array(), ); $allowed_tags['symbol'] = array( 'aria-hidden' => array(), 'viewBox' => array(), 'id' => array(), 'xmls' => array(), ); return $allowed_tags; } /** * Sanitize a user-supplied icon to inline presentation SVG. * * Close-button sprites are hardcoded and must not pass through this helper. * * @param string $svg Raw icon markup. * @return string Clean SVG, or an empty string when the markup is rejected. */ public static function sanitize_icon_svg( string $svg ): string { return SvgSanitizer::sanitize( $svg ); } /** * Whether an icon value is inline SVG markup. * * @param string $icon Stored icon value. * @return bool */ public static function is_raw_svg_icon( string $icon ): bool { return IconCatalog::is_raw_svg( $icon ); } /** * Persist an icon as a catalog key or sanitized custom SVG. * * @param string $icon Raw icon value. * @param string $alert_group Alert design slug. * @return string Catalog key, sanitized SVG, or empty string. */ public static function sanitize_icon_value( string $icon, string $alert_group ): string { $icon = trim( $icon ); if ( '' === $icon ) { return ''; } if ( IconCatalog::is_key( $alert_group, $icon ) ) { return $icon; } if ( IconCatalog::is_raw_svg( $icon ) ) { return self::sanitize_icon_svg( $icon ); } return ''; } /** * Expand a stored icon to sanitized SVG for output. * * Catalog keys resolve from the shipped set. Legacy and custom markup * still pass through the SVG sanitizer. * * @param string $icon Stored icon value. * @param string $alert_group Alert design slug. * @return string Sanitized SVG or empty string. */ public static function resolve_icon_svg( string $icon, string $alert_group ): string { $icon = trim( $icon ); if ( '' === $icon ) { return ''; } if ( IconCatalog::is_key( $alert_group, $icon ) ) { return self::sanitize_icon_svg( IconCatalog::get_svg( $alert_group, $icon ) ); } return self::sanitize_icon_svg( $icon ); } /** * Sanitize URL parameter array from block or shortcode input. * * @param mixed $value Raw URL params. * @return array Sanitized array of {key, value} objects. */ public static function sanitize_url_params( $value ) { if ( is_object( $value ) ) { $value = (array) $value; } if ( ! is_array( $value ) ) { return array(); } $out = array(); foreach ( $value as $item ) { if ( is_object( $item ) ) { $item = (array) $item; } if ( ! is_array( $item ) ) { continue; } $key = isset( $item['key'] ) ? sanitize_text_field( (string) $item['key'] ) : ''; $val = isset( $item['value'] ) ? sanitize_text_field( (string) $item['value'] ) : ''; if ( '' === $key ) { continue; } $out[] = array( 'key' => $key, 'value' => $val, ); } return $out; } /** * Encode URL params for the shortcode attribute value. * * WordPress' shortcode parser treats a literal `]` inside an attribute * value as the end of the shortcode tag. Only the JSON brackets are * escaped so the generated shortcode stays readable while remaining * parseable. * * @param array $url_params Sanitized URL parameter list. * @return string Bracket-escaped JSON. */ public static function encode_url_params_for_shortcode( array $url_params ): string { $json = wp_json_encode( $url_params ); return str_replace( array( '[', ']' ), array( '%5B', '%5D' ), $json ); } /** * Decode a URL params shortcode attribute value back into an array. * * Supports the bracket-escaped JSON format, the previous full * URL-encoded JSON format, and the older plain JSON format. * * @param mixed $value Raw attribute value. * @return array Decoded URL parameter list. */ public static function decode_url_params_string( $value ): array { if ( is_array( $value ) ) { return $value; } if ( ! is_string( $value ) || '' === $value ) { return array(); } // Escape url encoded params. $decoded = str_replace( array( '%5B', '%5D', '%5b', '%5d' ), array( '[', ']', '[', ']' ), $value ); $array = json_decode( $decoded, true ); if ( is_array( $array ) ) { return $array; } // Legacy full URL-encoded JSON format. $decoded = rawurldecode( $value ); $array = json_decode( $decoded, true ); if ( is_array( $array ) ) { return $array; } // Older plain JSON fallback. $array = json_decode( $value, true ); return is_array( $array ) ? $array : array(); } /** * Array data that must be sanitized. * * @param array $data Data to be sanitized. * * @return array Sanitized data. */ public static function sanitize_array_recursive( array $data ) { $sanitized_data = array(); foreach ( $data as $key => $value ) { if ( '0' === $value ) { $value = 0; } if ( 'true' === $value ) { $value = true; } elseif ( 'false' === $value ) { $value = false; } if ( is_array( $value ) ) { $value = self::sanitize_array_recursive( $value ); $sanitized_data[ $key ] = $value; continue; } if ( is_bool( $value ) ) { $sanitized_data[ $key ] = (bool) $value; continue; } if ( is_int( $value ) ) { $sanitized_data[ $key ] = (int) $value; continue; } if ( is_string( $value ) ) { $sanitized_data[ $key ] = sanitize_text_field( $value ); continue; } } return $sanitized_data; } /** * Get the plugin directory for a path. * * @param string $path The path to the file. * * @return string The new path. */ public static function get_plugin_dir( $path = '' ) { $dir = rtrim( plugin_dir_path( ALERTS_DLX_FILE ), '/' ); if ( ! empty( $path ) && is_string( $path ) ) { $dir .= '/' . ltrim( $path, '/' ); } return $dir; } /** * Return a plugin URL path. * * @param string $path Path to the file. * * @return string URL to to the file. */ public static function get_plugin_url( $path = '' ) { $dir = rtrim( plugin_dir_url( ALERTS_DLX_FILE ), '/' ); if ( ! empty( $path ) && is_string( $path ) ) { $dir .= '/' . ltrim( $path, '/' ); } return $dir; } /** * Gets the highest priority for a filter. * * @param int $subtract The amount to subtract from the high priority. * * @return int priority. */ public static function get_highest_priority( $subtract = 0 ) { $highest_priority = PHP_INT_MAX; $subtract = absint( $subtract ); if ( 0 === $subtract ) { --$highest_priority; } else { $highest_priority = absint( $highest_priority - $subtract ); } return $highest_priority; } }