Settings) and the website's own branding. * * @package Basketwise */ defined( 'ABSPATH' ) || exit; /** * All general settings with their defaults. * * @return array */ function brai_settings() { $d = array( 'abandon_mins' => 60, 'keep_days' => 90, 'staff_skip' => 1, 'consent' => 'auto', 'consent_cookie' => '', 'consent_value' => '', 'track_device' => 1, 'track_search' => 1, 'track_time' => 1, 'geo' => 0, 'block_links' => 0, 'campaigns' => '', 'delete_on_uninstall' => 0, ); $s = get_option( 'brai_settings', array() ); return wp_parse_args( is_array( $s ) ? $s : array(), $d ); } /** * One general setting. * * @param string $key Setting name. * @return mixed */ function brai_setting( $key ) { $s = brai_settings(); return isset( $s[ $key ] ) ? $s[ $key ] : null; } /** Minutes of inactivity before a basket counts as abandoned. */ function brai_abandon_mins() { return max( 5, (int) brai_setting( 'abandon_mins' ) ); } /** Days records are kept. */ function brai_keep_days() { return max( 7, (int) brai_setting( 'keep_days' ) ); } /** * Google Ads campaign names, from lines like "12345678 = Summer sale". * * @return array ID => name. */ function brai_campaign_names() { $out = array(); foreach ( preg_split( '/\r\n|\r|\n/', (string) brai_setting( 'campaigns' ) ) as $line ) { if ( preg_match( '/^\s*(\d+)\s*[=:,]\s*(.+?)\s*$/', $line, $m ) ) { $out[ $m[1] ] = $m[2]; } } return $out; } /** * Cleans the settings form. * * @param array $in Raw form values. * @return array */ function brai_settings_sanitize( $in ) { $in = is_array( $in ) ? $in : array(); $o = array( 'abandon_mins' => max( 5, min( 1440, (int) ( $in['abandon_mins'] ?? 60 ) ) ), 'keep_days' => max( 7, min( 730, (int) ( $in['keep_days'] ?? 90 ) ) ), 'consent' => in_array( $in['consent'] ?? '', array( 'auto', 'cookie', 'none' ), true ) ? $in['consent'] : 'auto', 'consent_cookie' => preg_replace( '/[^A-Za-z0-9_\-.]/', '', (string) ( $in['consent_cookie'] ?? '' ) ), 'consent_value' => sanitize_text_field( (string) ( $in['consent_value'] ?? '' ) ), 'campaigns' => sanitize_textarea_field( (string) ( $in['campaigns'] ?? '' ) ), ); foreach ( array( 'staff_skip', 'track_device', 'track_search', 'track_time', 'geo', 'block_links', 'delete_on_uninstall' ) as $k ) { $o[ $k ] = empty( $in[ $k ] ) ? 0 : 1; } return $o; } /** * The website's colour palette. * * The default look of the emails uses GeneratePress's global colours, or the theme's own palette (theme.json). * The Branding screen asks for everything ($wide): it also looks at classic theme palettes, Elementor, Astra, * Kadence, the Customiser and, last of all, WooCommerce's own email colours, so there is always something to match. * * @param bool $wide Look in every known place. * @return array slug => array( name, color, from ). */ function brai_site_palette( $wide = false ) { $hex6 = function ( $c ) { $c = trim( (string) $c ); if ( '' !== $c && '#' !== $c[0] && preg_match( '/^[0-9a-f]{3}([0-9a-f]{3})?$/i', $c ) ) { $c = '#' . $c; } $c = sanitize_hex_color( $c ); if ( $c && 4 === strlen( $c ) ) { $c = '#' . $c[1] . $c[1] . $c[2] . $c[2] . $c[3] . $c[3]; } return $c ? strtolower( $c ) : ''; }; $out = array(); $add = function ( $slug, $label, $color, $from ) use ( &$out, $hex6 ) { $h = $hex6( $color ); $k = sanitize_key( (string) $slug ); if ( $h && $k && ! isset( $out[ $k ] ) ) { $out[ $k ] = array( 'name' => '' !== (string) $label ? (string) $label : $k, 'color' => $h, 'from' => $from, ); } }; /* GeneratePress global colours. */ $gp = get_option( 'generate_settings', array() ); $global = is_array( $gp ) && isset( $gp['global_colors'] ) ? $gp['global_colors'] : array(); if ( $wide && ! $global && function_exists( 'generate_get_option' ) ) { $global = generate_get_option( 'global_colors' ); // Includes the theme's defaults when they have not been changed. } foreach ( (array) $global as $c ) { if ( is_array( $c ) ) { $add( $c['slug'] ?? ( $c['name'] ?? $hex6( $c['color'] ?? '' ) ), $c['name'] ?? '', $c['color'] ?? '', 'GeneratePress' ); } } /* The theme's palette (theme.json and Site Editor styles). */ if ( ( $wide || ! $out ) && function_exists( 'wp_get_global_settings' ) ) { $p = wp_get_global_settings( array( 'color', 'palette' ) ); foreach ( array( 'custom', 'theme' ) as $set ) { foreach ( (array) ( is_array( $p ) && isset( $p[ $set ] ) ? $p[ $set ] : array() ) as $c ) { if ( is_array( $c ) ) { $add( $c['slug'] ?? '', $c['name'] ?? '', $c['color'] ?? '', __( 'your theme', 'basketwise' ) ); } } } } if ( ! $wide ) { return $out; } /* Classic themes that register an editor palette. */ $support = get_theme_support( 'editor-color-palette' ); foreach ( (array) ( is_array( $support ) && isset( $support[0] ) ? $support[0] : array() ) as $c ) { if ( is_array( $c ) ) { $add( $c['slug'] ?? '', $c['name'] ?? '', $c['color'] ?? '', __( 'your theme', 'basketwise' ) ); } } /* Elementor global colours (kept in the active kit). */ $kit = (int) get_option( 'elementor_active_kit' ); $el = $kit ? get_post_meta( $kit, '_elementor_page_settings', true ) : array(); foreach ( array( 'system_colors', 'custom_colors' ) as $set ) { foreach ( (array) ( is_array( $el ) && isset( $el[ $set ] ) ? $el[ $set ] : array() ) as $c ) { if ( is_array( $c ) ) { $add( $c['_id'] ?? '', $c['title'] ?? '', $c['color'] ?? '', 'Elementor' ); } } } /* Astra: a numbered palette. The first colour is the brand colour, the fourth the body text. */ $astra = get_option( 'astra-settings', array() ); $ap = is_array( $astra ) && isset( $astra['global-color-palette']['palette'] ) ? (array) $astra['global-color-palette']['palette'] : array(); foreach ( array_values( $ap ) as $i => $c ) { if ( is_string( $c ) ) { $add( 0 === $i ? 'accent' : ( 3 === $i ? 'text' : 'astra-' . $i ), 'Astra ' . ( $i + 1 ), $c, 'Astra' ); } } /* Kadence global palette. */ $kad = json_decode( (string) get_option( 'kadence_global_palette', '' ), true ); foreach ( (array) ( is_array( $kad ) && isset( $kad['palette'] ) ? $kad['palette'] : array() ) as $i => $c ) { if ( is_array( $c ) ) { $add( 0 === $i ? 'accent' : ( 3 === $i ? 'text' : ( $c['slug'] ?? 'kadence-' . $i ) ), $c['name'] ?? '', $c['color'] ?? '', 'Kadence' ); } } /* Colours set in the Customiser by classic themes. */ foreach ( array( 'accent_color' => array( 'accent', __( 'Accent', 'basketwise' ) ), 'primary_color' => array( 'primary', __( 'Primary', 'basketwise' ) ), 'link_color' => array( 'link', __( 'Links', 'basketwise' ) ), 'background_color' => array( 'background', __( 'Background', 'basketwise' ) ), ) as $mod => $as ) { $v = get_theme_mod( $mod ); if ( is_string( $v ) && '' !== $v ) { $add( $as[0], $as[1], $v, __( 'your theme', 'basketwise' ) ); } } /* WooCommerce > Settings > Emails. Every shop has these, so there is always something to match. */ $base = (string) get_option( 'woocommerce_email_base_color', '' ); $text = (string) get_option( 'woocommerce_email_text_color', '' ); $add( 'woo-base', __( 'WooCommerce email colour', 'basketwise' ), $base, __( 'your WooCommerce email settings', 'basketwise' ) ); $add( 'woo-text', __( 'WooCommerce email text', 'basketwise' ), $text, __( 'your WooCommerce email settings', 'basketwise' ) ); return $out; } /** * Suggested email colours from the website palette. * * @param bool $wide Look in every known place (see brai_site_palette()). * @return array Empty if no colours were found. */ function brai_site_map( $wide = false ) { $p = brai_site_palette( $wide ); if ( ! $p ) { return array(); } /* WooCommerce's email colours are only the fallback: when the theme has colours of its own, those decide. */ $own = array_diff_key( $p, array_flip( array( 'woo-base', 'woo-text' ) ) ); if ( $own ) { $p = $own; } else { $p = array_filter( array( 'accent' => $p['woo-base'] ?? null, 'text' => $p['woo-text'] ?? null, ) ); } $pick = function ( $slugs ) use ( $p ) { foreach ( $slugs as $s ) { if ( isset( $p[ $s ] ) ) { return $p[ $s ]['color']; } } return ''; }; $rgb = function ( $h ) { return array( hexdec( substr( $h, 1, 2 ) ), hexdec( substr( $h, 3, 2 ) ), hexdec( substr( $h, 5, 2 ) ) ); }; $accent = $pick( array( 'accent', 'primary', 'accent-1', 'brand', 'secondary' ) ); if ( ! $accent ) { $best = -1; foreach ( $p as $c ) { $x = $rgb( $c['color'] ); $mx = max( $x ); $s = $mx ? ( $mx - min( $x ) ) / $mx : 0; if ( $s > $best ) { $best = $s; $accent = $c['color']; } } } $dark = $pick( array( 'contrast', 'foreground', 'dark', 'contrast-2', 'text' ) ); if ( ! $dark ) { $low = 999; foreach ( $p as $c ) { $x = $rgb( $c['color'] ); $l = ( $x[0] * 299 + $x[1] * 587 + $x[2] * 114 ) / 1000; if ( $l < $low ) { $low = $l; $dark = $c['color']; } } } /* Text has to be readable on a light email: if the darkest colour found is still light, use the standard dark. */ if ( $wide && brai_is_light( $dark ) ) { $dark = '#1d2327'; } $light = $pick( array( 'base-3', 'background', 'white', 'base' ) ); return array( 'accent' => $accent, 'button_bg' => $accent, 'text' => $dark, 'link' => $dark, 'footer_bg' => $dark, 'footer_text' => brai_is_light( $dark ) ? '#1d2327' : '#ffffff', 'bg' => ( $light && brai_is_light( $light ) ) ? $light : '#ffffff', ); } /** * Where the colours offered on the Branding screen came from, for the note beside them. * * @return string Such as "GeneratePress" or "your theme and your WooCommerce email settings". */ function brai_site_palette_from() { $from = array_values( array_unique( wp_list_pluck( brai_site_palette( true ), 'from' ) ) ); return implode( ', ', $from ); } /** * The website's logo, wherever it is set: the Customiser or Site Editor, GeneratePress, or WooCommerce's email header image. * * @return array url (the address, or ''), ok (true if it is a PNG, JPG or GIF, which every email app can show), type (file extension). */ function brai_site_logo_info() { $u = ''; $id = (int) get_theme_mod( 'custom_logo' ); if ( ! $id ) { $id = (int) get_option( 'site_logo' ); } if ( $id ) { $u = (string) wp_get_attachment_image_url( $id, 'full' ); } if ( ! $u ) { $gp = get_option( 'generate_settings', array() ); $u = is_array( $gp ) && ! empty( $gp['logo'] ) ? (string) $gp['logo'] : ''; } if ( ! $u ) { $u = (string) get_option( 'woocommerce_email_header_image', '' ); } $u = esc_url_raw( $u ); $type = strtolower( (string) pathinfo( (string) wp_parse_url( $u, PHP_URL_PATH ), PATHINFO_EXTENSION ) ); return array( 'url' => $u, 'ok' => '' !== $u && in_array( $type, array( 'png', 'jpg', 'jpeg', 'gif' ), true ), 'type' => $type, ); } /** * The website logo, if it is an image type that works in email (PNG, JPG or GIF). * * @return string URL or empty. */ function brai_site_logo() { $logo = brai_site_logo_info(); return $logo['ok'] ? $logo['url'] : ''; } /** * Is this colour light (so dark text should go on it)? * * @param string $hex Colour such as #ffcc00. * @return bool */ function brai_is_light( $hex ) { $hex = ltrim( (string) $hex, '#' ); return 6 === strlen( $hex ) && ( hexdec( substr( $hex, 0, 2 ) ) * 299 + hexdec( substr( $hex, 2, 2 ) ) * 587 + hexdec( substr( $hex, 4, 2 ) ) * 114 ) / 1000 > 150; }