hjack_setup(); } public function add_theme_supports() { add_theme_support( 'title-tag' ); add_theme_support( 'customize-selective-refresh-widgets' ); add_theme_support( 'responsive-embeds' ); add_theme_support( 'woocommerce', array( 'thumbnail_image_width' => 800, 'single_image_width' => 800, ) ); add_theme_support( 'wc-product-gallery-zoom' ); add_theme_support( 'wc-product-gallery-lightbox' ); add_theme_support( 'wc-product-gallery-slider' ); add_theme_support( 'title-tag' ); add_theme_support( 'automatic-feed-links' ); add_theme_support( 'align-wide' ); add_theme_support( 'custom-background', array() ); add_theme_support( 'custom-header', array() ); add_theme_support( 'custom-logo', array() ); add_theme_support( 'html5', array( 'comment-list', 'comment-form', 'search-form', 'gallery', 'caption', 'style', 'script' ) ); add_theme_support( 'wp-block-styles' ); add_theme_support( 'post-thumbnails' ); } public function register_menus() { register_nav_menus( MenuHelper::get_locations() ); } public function register_block_patterns_and_categories() { register_block_pattern_category( 'brandy', array( 'label' => __( 'Brandy', 'brandy' ) ) ); } public function unregister_wc_patterns_when_necessary() { if ( is_wc_installed() ) { return; } $themes = array(); $theme = wp_get_theme(); $themes[] = $theme; if ( $theme->parent() ) { $themes[] = $theme->parent(); } $patterns = array(); foreach ( $themes as $theme ) { $patterns = array_merge( $patterns, $theme->get_block_patterns() ); } foreach ( $patterns as $pattern_name => $pattern ) { if ( empty( $pattern['categories'] ) ) { continue; } if ( in_array( 'woocommerce', $pattern['categories'], true ) ) { unregister_block_pattern( $pattern['slug'] ); } } } public function unregister_brandy_blocks_patterns_when_necessary() { if ( is_brandy_blocks_installed() ) { return; } $themes = array(); $theme = wp_get_theme(); $themes[] = $theme; if ( $theme->parent() ) { $themes[] = $theme->parent(); } $patterns = array(); foreach ( $themes as $theme ) { $patterns = array_merge( $patterns, $theme->get_block_patterns() ); } foreach ( $patterns as $pattern_name => $pattern ) { if ( empty( $pattern['categories'] ) ) { continue; } if ( in_array( 'brandy-blocks', $pattern['categories'], true ) ) { unregister_block_pattern( $pattern['slug'] ); } if ( 'brandy/simple-contact-form' === $pattern['slug'] ) { unregister_block_pattern( $pattern['slug'] ); } } } public function remove_wc_templates_when_necessary( $query_result ) { if ( is_wc_installed() ) { return $query_result; } $removal_template_slugs = array( 'archive-product', 'order-confirmation', 'page-cart', 'page-checkout', 'page-my-account', 'product-search-results', 'single-product', ); $query_result = array_filter( $query_result, function( $template ) use ( $removal_template_slugs ) { if ( false === strpos( $template->theme, 'brandy' ) && false === strpos( $template->id, 'brandy' ) ) { return true; } return ! in_array( $template->slug, $removal_template_slugs, true ); } ); return $query_result; } public function hjack_setup() { // Trick to add outline style for button element add_action( 'init', function() { $parsed_block = array( 'attrs' => array( 'className' => 'wp-block-button is-style-outline', ), 'blockName' => 'core/button', ); if ( function_exists( '\wp_render_block_style_variation_support_styles' ) ) { \wp_render_block_style_variation_support_styles( $parsed_block ); } } ); add_filter( 'wp_prepare_themes_for_js', function( $themes ) { if ( isset( $themes['brandy']['actions']['customize'] ) ) { $themes['brandy']['actions']['customize'] = wp_customize_url( 'brandy' ); } return $themes; } ); add_filter( 'script_loader_tag', function( $tag, $handle ) { if ( strpos( $handle, 'brandy' ) !== false ) { return str_replace( ' src=', ' defer src=', $tag ); } return $tag; }, 10, 2 ); add_filter( 'theme_scandir_exclusions', function( $exclusions ) { if ( ! isset( $_POST['themename'] ) || 'brandy' !== $_POST['themename'] ) { return $exclusions; } if ( ! check_admin_referer( 'themecheck-nonce' ) ) { return $exclusions; } $exclusions[] = 'icons'; $exclusions[] = 'Elements'; return $exclusions; } ); } public function get_ref_color( $color ) { if ( str_starts_with( $color, 'var:preset|color' ) ) { $path = explode( '|', $color ); $color_slug = $path[2] ?? ''; if ( empty( $color_slug ) ) { return $color; } $global_settings = wp_get_global_settings(); $palette = array_merge( $global_settings['color']['palette']['default'], $global_settings['color']['palette']['theme'] ); $color_data = array_values( array_filter( $palette, function( $item ) use ( $color_slug ) { return $item['slug'] === $color_slug; } ) ); if ( ! empty( $color_data ) ) { $color = $color_data[0]['color']; } } return $color; } public function global_styles_save_function( $post_id, $post, $update ) { if ( 'wp_global_styles' !== $post->post_type ) { return; } $data = json_decode( $post->post_content, true ); if ( ! isset( $data['styles']['elements']['button']['color']['background'] ) ) { return; } $primary_color = $this->get_ref_color( $data['styles']['elements']['button']['color']['background'] ); $luminance = Helpers::get_luminance( $primary_color ); $hover_color = '#000000'; if ( '#000000' === $primary_color ) { $hover_color = '#2f2f2f'; } if ( $luminance < 0.2 ) { $hover_color = Helpers::lighten_color( $primary_color, 0.1 ); } else { $hover_color = Helpers::darken_color( $primary_color, 0.12 ); } $button_settings = get_theme_mod( 'button_settings', ButtonSettingsSection::default_settings() ); $button_settings['primaryColor'] = $primary_color; $button_settings['primaryHoverColor'] = $hover_color; $button_settings['type'] = 'custom'; set_theme_mod( 'button_settings', $button_settings ); // This can help to preview change colors in Editor // if ( ! isset( $data['styles']['elements']['button'][':hover'] ) ) { // $data['styles']['elements']['button'][':hover'] = array(); // } // if ( ! isset( $data['styles']['elements']['button'][':hover']['color'] ) ) { // $data['styles']['elements']['button'][':hover']['color'] = array(); // } // $data['styles']['elements']['button'][':hover']['color']['background'] = $primary_color; // wp_update_post( // array( // 'ID' => $post_id, // 'post_content' => json_encode( $data ), // ) // ); } } ThemeSetup::get_instance();