/presets/, expose ALL preset colors in Customizer. * - If no preset exists, expose only 3 minimal controls: * - Site Background, Header Background, Footer Background * - Keep everything inside ONE section: Customizing → General → Colors * - Hide/remove other color-related sections to avoid duplicates. */ if ( ! function_exists( 'bongoto_wc_find_demo_preset_file' ) ) { function bongoto_wc_find_demo_preset_file() : string { $demo_slug = 'bongoto-woocommerce'; $upload = wp_upload_dir(); $upload_dir = trailingslashit( $upload['basedir'] ) . 'bongoto-woocommerce/demo/' . $demo_slug . '/presets/'; $dir = $upload_dir; if ( ! is_dir( $dir ) ) { $dir = trailingslashit( get_template_directory() ) . 'template-parts/demos/' . $demo_slug . '/presets/'; } if ( ! is_dir( $dir ) ) { return ''; } // Prefer colors-default.json if present (matches your server naming). $preferred = $dir . 'colors-default.json'; if ( is_file( $preferred ) ) { return $preferred; } // Otherwise pick the first JSON file. $files = glob( $dir . '*.json' ); if ( is_array( $files ) && ! empty( $files ) ) { // Deterministic order sort( $files, SORT_STRING ); return (string) $files[0]; } return ''; } } if ( ! function_exists( 'bongoto_wc_read_preset_theme_mods' ) ) { function bongoto_wc_read_preset_theme_mods( string $file ) : array { if ( ! $file || ! is_file( $file ) ) { return []; } $raw = file_get_contents( $file ); if ( ! is_string( $raw ) || $raw === '' ) { return []; } $json = json_decode( $raw, true ); if ( ! is_array( $json ) ) { return []; } // Expect: {"theme_mods": { "bongoto_color_...": "#fff", ... }} if ( isset( $json['theme_mods'] ) && is_array( $json['theme_mods'] ) ) { return $json['theme_mods']; } // Fallback: allow flat key/value JSON. return $json; } } if ( ! function_exists( 'bongoto_wc_humanize_color_key' ) ) { function bongoto_wc_humanize_color_key( string $key ) : string { $k = $key; // Drop common prefixes. $k = preg_replace( '/^bongoto_color_/', '', $k ); $k = preg_replace( '/^bongoto_woocommerce_color_/', '', $k ); // Special nicer labels. $map = [ 'site_bg' => 'Site Background', 'header_bg' => 'Header Background', 'footer_bg' => 'Footer Background', 'header_top_bg' => 'Header Top Background', 'site_title' => 'Site Title', 'search_box' => 'Search Box', 'search_btn' => 'Search Button', 'cart_icon' => 'Cart Icon', 'cart_count_bg' => 'Cart Count Background', 'cart_count_text' => 'Cart Count Text', 'account_icon' => 'Account Icon', 'add_to_cart' => 'Add to Cart Button', 'buy_now' => 'Buy Now Button', 'cart_btn' => 'Cart Button', 'checkout_btn' => 'Checkout Button', 'free_download' => 'Free Download Button', 'download_now' => 'Download Now Button', 'related_product' => 'Related Product Button', 'footer_text' => 'Footer Text', 'copyright_bg' => 'Copyright Background', 'copyright_text' => 'Copyright Text', 'primary' => 'Primary', 'accent' => 'Accent', 'text' => 'Text', 'bg' => 'Background', 'checkout' => 'Checkout', ]; if ( isset( $map[ $k ] ) ) { return $map[ $k ]; } // Generic humanize. $k = str_replace( ['-', '_'], ' ', $k ); $k = trim( preg_replace( '/\s+/', ' ', $k ) ); return ucwords( $k ); } } add_action( 'customize_register', function( $wp_customize ) { // Single colors section under General panel. $section_id = 'bongoto_wc_colors'; if ( ! $wp_customize->get_section( $section_id ) ) { $wp_customize->add_section( $section_id, [ 'title' => __( 'Colors', 'bongoto-woocommerce' ), 'priority' => 30, 'panel' => 'bongoto_general_panel', 'description' => __( 'Theme colors. Demo color presets are available only after purchase code verification.', 'bongoto-woocommerce' ), ] ); } $add_color = function( string $id, string $label, string $default = '' ) use ( $wp_customize, $section_id ) { if ( $wp_customize->get_setting( $id ) ) { return; } $wp_customize->add_setting( $id, [ 'default' => $default, 'type' => 'theme_mod', 'transport' => 'refresh', 'sanitize_callback' => 'sanitize_hex_color', ] ); $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, $id, [ 'label' => $label, 'section' => $section_id, 'settings' => $id, ] ) ); }; // Conditional: if preset exists, expose ALL theme_mods from preset (Premium only). $is_pro = function_exists( 'bongoto_woocommerce_is_pro_active' ) ? bongoto_woocommerce_is_pro_active() : ( function_exists( 'bongoto_is_premium_active' ) ? bongoto_is_premium_active() : (bool) get_option( 'bongoto_premium_active', false ) ); $preset_file = $is_pro ? bongoto_wc_find_demo_preset_file() : ''; $preset_mods = $preset_file ? bongoto_wc_read_preset_theme_mods( $preset_file ) : []; if ( ! empty( $preset_mods ) ) { // Only show color-like keys (hex values) to avoid random JSON data. foreach ( $preset_mods as $key => $value ) { if ( ! is_string( $key ) ) { continue; } if ( ! is_string( $value ) ) { continue; } $val = trim( $value ); // Accept hex colors only. if ( ! preg_match( '/^#([a-fA-F0-9]{3}|[a-fA-F0-9]{6})$/', $val ) ) { continue; } // We only want theme mods that belong to this theme. if ( strpos( $key, 'bongoto_' ) !== 0 ) { continue; } $add_color( $key, bongoto_wc_humanize_color_key( $key ), $val ); } } else { // Minimal defaults (shown only when demo preset doesn't exist). $add_color( 'bongoto_color_site_bg', __( 'Site Background', 'bongoto-woocommerce' ), '#ffffff' ); $add_color( 'bongoto_color_header_bg', __( 'Header Background', 'bongoto-woocommerce' ), '#ffffff' ); $add_color( 'bongoto_color_footer_bg', __( 'Footer Background', 'bongoto-woocommerce' ), '#111111' ); } }, 30 ); /** * Remove other color sections to avoid duplicates. */ add_action( 'customize_register', function( $wp_customize ) { $keep = 'bongoto_wc_colors'; // Remove core Colors section. if ( $wp_customize->get_section( 'colors' ) ) { $wp_customize->remove_section( 'colors' ); } // Remove any section that looks like a color menu, except our keep section. foreach ( $wp_customize->sections() as $id => $section ) { if ( $id === $keep ) { continue; } $lid = strtolower( (string) $id ); if ( strpos( $lid, 'color' ) !== false || strpos( $lid, 'palette' ) !== false ) { $wp_customize->remove_section( $id ); } } }, 999 );