50, 'width' => 150, 'flex-height' => true, 'flex-width' => true, ]); // Add support for automatic feed links add_theme_support('automatic-feed-links'); // Add support for custom header add_theme_support('custom-header', [ 'default-image' => '', 'width' => 1200, 'height' => 300, 'flex-height' => true, 'flex-width' => true, 'header-text' => false, 'uploads' => true, ]); // Add support for custom background add_theme_support('custom-background', [ 'default-color' => '#f9fafb', 'default-image' => '', 'wp-head-callback' => '_custom_background_cb', 'admin-head-callback' => '', 'admin-preview-callback' => '', ]); // Register navigation menus register_nav_menus([ 'primary' => __('Primary Menu', 'blockenix'), ]); // Set content width $GLOBALS['content_width'] = apply_filters('blnx_content_width', 1200); } add_action('after_setup_theme', 'blnx_theme_setup'); // Include required theme files require_once get_template_directory() . '/inc/enqueue-scripts.php'; require_once get_template_directory() . '/inc/class-mega-menu-walker.php'; require_once get_template_directory() . '/inc/class-footer.php'; require_once get_template_directory() . '/inc/customizer.php'; // Load responsive control class only when Customizer is available if (class_exists('WP_Customize_Control')) { require_once get_template_directory() . '/inc/class-responsive-control.php'; } /** * Add preconnect hints for Google Fonts (performance optimization) */ function blnx_google_fonts_preconnect() { $text_font = get_theme_mod('body_font_family', 'Arial, sans-serif'); $heading_font = get_theme_mod('heading_font_family', 'system-ui, sans-serif'); $button_font = get_theme_mod('button_font_family', 'Arial, sans-serif'); $web_safe_fonts = ['Arial', 'Helvetica', 'Georgia', 'Times New Roman', 'system-ui']; $needs_google_fonts = false; // Check if any font needs Google Fonts $fonts_to_check = [$text_font, $heading_font, $button_font]; foreach ($fonts_to_check as $font) { $font_name = trim(explode(',', $font)[0]); $is_web_safe = false; foreach ($web_safe_fonts as $safe_font) { if (stripos($font_name, $safe_font) !== false) { $is_web_safe = true; break; } } if (!$is_web_safe && !empty($font_name)) { $needs_google_fonts = true; break; } } if ($needs_google_fonts) { echo '' . "\n"; echo '' . "\n"; } } add_action('wp_head', 'blnx_google_fonts_preconnect', 1); /** * Enqueue Google Fonts from theme customizer settings * Loads fonts in header for better performance */ function blnx_enqueue_google_fonts() { $text_font = get_theme_mod('body_font_family', 'Arial, sans-serif'); $heading_font = get_theme_mod('heading_font_family', 'system-ui, sans-serif'); $button_font = get_theme_mod('button_font_family', 'Arial, sans-serif'); $web_safe_fonts = ['Arial', 'Helvetica', 'Georgia', 'Times New Roman', 'system-ui']; $fonts_to_load = []; // Check and add text font $text_font_name = trim(explode(',', $text_font)[0]); $text_needs_load = true; foreach ($web_safe_fonts as $safe_font) { if (stripos($text_font_name, $safe_font) !== false) { $text_needs_load = false; break; } } if ($text_needs_load && !empty($text_font_name)) { $fonts_to_load[] = $text_font_name; } // Check and add heading font $heading_font_name = trim(explode(',', $heading_font)[0]); $heading_needs_load = true; foreach ($web_safe_fonts as $safe_font) { if (stripos($heading_font_name, $safe_font) !== false) { $heading_needs_load = false; break; } } if ($heading_needs_load && !empty($heading_font_name) && $heading_font_name !== $text_font_name) { $fonts_to_load[] = $heading_font_name; } // Check and add button font $button_font_name = trim(explode(',', $button_font)[0]); $button_needs_load = true; foreach ($web_safe_fonts as $safe_font) { if (stripos($button_font_name, $safe_font) !== false) { $button_needs_load = false; break; } } if ($button_needs_load && !empty($button_font_name) && $button_font_name !== $text_font_name && $button_font_name !== $heading_font_name) { $fonts_to_load[] = $button_font_name; } // Load Google Fonts if needed if (!empty($fonts_to_load)) { // Build Google Fonts URL with all fonts $font_families = []; foreach ($fonts_to_load as $font) { $font_encoded = str_replace(' ', '+', $font); $font_families[] = $font_encoded . ':wght@300;400;500;600;700;800'; } // Combine all fonts into one request for better performance $google_fonts_url = 'https://fonts.googleapis.com/css2?family=' . implode('&family=', $font_families) . '&display=swap'; wp_enqueue_style('blnx-google-fonts', $google_fonts_url, [], null); } } add_action('wp_enqueue_scripts', 'blnx_enqueue_google_fonts', 5); /** * Enqueue dynamic styles */ function blnx_enqueue_dynamic_styles() { $version = is_customize_preview() ? time() : BLOCKENIX_VERSION; wp_enqueue_style( 'dynamic-styles', add_query_arg('ver', $version, get_template_directory_uri() . '/inc/dynamic-styles.php'), [], $version ); } add_action('wp_enqueue_scripts', 'blnx_enqueue_dynamic_styles'); /** * Fallback menu if no menu is assigned */ function blnx_fallback_menu() { echo ''; } /** * Fix theme.json fontSizes to ensure it's always an array * Prevents foreach warning when fontSizes is false */ function blnx_fix_theme_json_font_sizes($theme_json_data, $origin = 'theme') { if (class_exists('WP_Theme_JSON_Data') && $theme_json_data instanceof WP_Theme_JSON_Data) { $data = $theme_json_data->get_data(); if (!isset($data['settings']) || !is_array($data['settings'])) { $data['settings'] = []; } if (!isset($data['settings']['typography']) || !is_array($data['settings']['typography'])) { $data['settings']['typography'] = []; } if (!isset($data['settings']['typography']['fontSizes']) || !is_array($data['settings']['typography']['fontSizes']) || is_bool($data['settings']['typography']['fontSizes']) || $data['settings']['typography']['fontSizes'] === null) { $data['settings']['typography']['fontSizes'] = []; } $fix_font_sizes = function(&$array) use (&$fix_font_sizes) { if (!is_array($array)) { return; } foreach ($array as $key => &$value) { if ($key === 'fontSizes') { if (!is_array($value) || is_bool($value) || $value === null) { $value = []; } } elseif (is_array($value)) { $fix_font_sizes($value); } } }; $fix_font_sizes($data); return new WP_Theme_JSON_Data($data, $origin); } return $theme_json_data; } /** * Fix editor settings fontSizes */ function blnx_fix_editor_settings_font_sizes($settings) { if (!is_array($settings)) { return $settings; } if (isset($settings['fontSizes']) && (!is_array($settings['fontSizes']) || is_bool($settings['fontSizes']) || $settings['fontSizes'] === null)) { $settings['fontSizes'] = []; } $fix_recursive = function(&$arr) use (&$fix_recursive) { if (!is_array($arr)) { return; } foreach ($arr as $key => &$val) { if ($key === 'fontSizes' && (!is_array($val) || is_bool($val) || $val === null)) { $val = []; } elseif (is_array($val)) { $fix_recursive($val); } } }; $fix_recursive($settings); return $settings; } /** * Fix global settings fontSizes */ function blnx_fix_global_settings_font_sizes($settings) { if (!is_array($settings)) { return $settings; } if (isset($settings['typography']['fontSizes']) && (!is_array($settings['typography']['fontSizes']) || is_bool($settings['typography']['fontSizes']) || $settings['typography']['fontSizes'] === null)) { $settings['typography']['fontSizes'] = []; } $fix_recursive = function(&$arr) use (&$fix_recursive) { if (!is_array($arr)) { return; } foreach ($arr as $key => &$val) { if ($key === 'fontSizes' && (!is_array($val) || is_bool($val) || $val === null)) { $val = []; } elseif (is_array($val)) { $fix_recursive($val); } } }; $fix_recursive($settings); return $settings; } /** * Remove invalid editor-font-sizes theme support */ add_action('plugins_loaded', function() { $font_sizes = get_theme_support('editor-font-sizes'); if ($font_sizes !== false) { $font_sizes_array = current((array) $font_sizes); if (!is_array($font_sizes_array) || is_bool($font_sizes_array)) { remove_theme_support('editor-font-sizes'); } } }, 1); add_action('after_setup_theme', function() { $font_sizes = get_theme_support('editor-font-sizes'); if ($font_sizes !== false) { $font_sizes_array = current((array) $font_sizes); if (!is_array($font_sizes_array) || empty($font_sizes_array) || is_bool($font_sizes_array)) { remove_theme_support('editor-font-sizes'); } } }, 999); /** * Error handler to catch foreach warning as last resort */ add_action('muplugins_loaded', function() { $previous_handler = set_error_handler(function($errno, $errstr, $errfile, $errline) use (&$previous_handler) { if ($errno === E_WARNING && strpos($errstr, 'foreach() argument must be of type array|object') !== false && strpos($errfile, 'class-wp-theme-json.php') !== false && $errline === 3870) { return true; } if ($previous_handler) { return call_user_func($previous_handler, $errno, $errstr, $errfile, $errline); } return false; }, E_WARNING); }, 0); // Apply fontSizes fixes to theme.json data add_filter('wp_theme_json_data_theme', 'blnx_fix_theme_json_font_sizes', 0, 2); add_filter('wp_theme_json_data_user', 'blnx_fix_theme_json_font_sizes', 0, 2); add_filter('wp_theme_json_data_merged', 'blnx_fix_theme_json_font_sizes', 0, 2); add_filter('wp_theme_json_data_base', 'blnx_fix_theme_json_font_sizes', 0, 2); // Apply fontSizes fixes to editor settings add_filter('block_editor_settings_all', 'blnx_fix_editor_settings_font_sizes', 1); add_filter('block_editor_settings', 'blnx_fix_editor_settings_font_sizes', 1); add_filter('editor_stylesheets', 'blnx_fix_editor_settings_font_sizes', 1); // Apply fontSizes fixes to global settings add_filter('wp_get_global_settings', 'blnx_fix_global_settings_font_sizes', 1); /** * Add editor styles */ function blnx_add_editor_styles() { add_editor_style([ 'assets/css/main.css', add_query_arg('ver', BLOCKENIX_VERSION, get_template_directory_uri() . '/inc/dynamic-styles.php'), ]); } add_action('after_setup_theme', 'blnx_add_editor_styles'); /** * Register block styles */ function blnx_register_block_styles() { // Register a block style for buttons if (function_exists('register_block_style')) { register_block_style('core/button', [ 'name' => 'outline', 'label' => __('Outline', 'blockenix'), ]); } } add_action('after_setup_theme', 'blnx_register_block_styles'); /** * Register block patterns */ function blnx_register_block_patterns() { if (function_exists('register_block_pattern')) { register_block_pattern( 'blockenix/hero-section', [ 'title' => __('Hero Section', 'blockenix'), 'description' => __('A hero section with heading and call-to-action button', 'blockenix'), 'content' => '

' . esc_html__('Welcome to Our Website', 'blockenix') . '

' . esc_html__('This is a hero section pattern.', 'blockenix') . '

', 'categories' => ['featured'], ] ); } } add_action('after_setup_theme', 'blnx_register_block_patterns');