<?php
/**
 * Brimstone functions and definitions
 *
 * @package WordPress
 * @subpackage Brimstone
 * @since 1.01
 * @author Chris Baldelomar <chris@webplantmedia.com>
 * @copyright Copyright (c) 2018, Chris Baldelomar
 * @link https://webplantmedia.com/product/brimstone-wordpress-theme/
 * @license http://www.gnu.org/licenses/gpl-2.0.html
 */

/**
 * The current version of the theme.
 */
define( 'BRIMSTONE_VERSION', '1.18' );

if ( ! function_exists( 'brimstone_setup' ) ) :
	/**
	 * Sets up theme defaults and registers support for various WordPress features.
	 *
	 * Note that this function is hooked into the after_setup_theme hook, which
	 * runs before the init hook. The init hook is too late for some features, such
	 * as indicating support for post thumbnails.
	 *
	 * @since Brimstone 1.01
	 *
	 * @return void
	 */
	function brimstone_setup() {
		/**
		 * Make theme available for translation.
		 * Translations can be filed in the /languages/ directory.
		 * If you're building a theme based on Brimstone, use a find and replace
		 * to change 'brimstone' to the name of your theme in all the template files.
		 */
		load_theme_textdomain( 'brimstone', get_template_directory() . '/languages' );

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );

		/**
		 * Let WordPress manage the document title.
		 * By adding theme support, we declare that this theme does not use a
		 * hard-coded <title> tag in the document head, and expect WordPress to
		 * provide it for us.
		 */
		add_theme_support( 'title-tag' );

		/**
		 * Enable support for Post Thumbnails on posts and pages.
		 *
		 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
		 */
		add_theme_support( 'post-thumbnails' );

		// This theme uses wp_nav_menu() in four location.
		register_nav_menus(
			array(
				'menu-1' => esc_html__( 'Primary', 'brimstone' ),
				'menu-2' => esc_html__( 'Top Header Left', 'brimstone' ),
				'menu-3' => esc_html__( 'Top Header Right', 'brimstone' ),
				'social' => esc_html__( 'Social Menu', 'brimstone' ),
			)
		);

		// This theme styles the visual editor with editor-style.css to match the theme style.
		add_editor_style( array( 'css/admin/editor-style.css', get_parent_theme_file_uri() . '/fonts/lato/stylesheet.css' ) );

		/**
		 * Switch default core markup for search form, comment form, and comments
		 * to output valid HTML5.
		 */
		add_theme_support(
			'html5', array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
			)
		);

		// Set up the WordPress core custom background feature.
		add_theme_support(
			'custom-background', apply_filters(
				'brimstone_custom_background_args', array(
					'default-color' => '#ffffff',
					'default-image' => '',
				)
			)
		);

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

		/**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		add_theme_support(
			'custom-logo', array(
				'height'      => 250,
				'width'       => 400,
				'flex-width'  => true,
				'flex-height' => true,
				// 'header-text' => array( 'site-title', 'site-description' ),
			)
		);

		/**
		 * Add support for up-sell fonts plugin
		 *
		 * @link https://webplantmedia.com/product/designer-fonts-wordpress-plugin/
		 */
		add_theme_support(
			'wpm-fonts', array(
				'logo'             => array(
					'font'      => 'PT Sans', /* This is a lookup value, and should not be translated. */
					'selectors' => '.site-title',
				),
				'body'             => array(
					'font'      => 'Lato', /* This is a lookup value, and should not be translated. */
					'selectors' => 'body, button, input, select, optgroup, textarea',
				),
				'heading'          => array(
					'font'      => 'PT Sans', /* This is a lookup value, and should not be translated. */
					'selectors' => '#master .h1, #master .h2, #master .h3, #master .h4, #master .h5, #master .h6, h1, h2, h3, h4, h5, h6',
				),
				'accent'           => array(
					'font'      => 'Allura', /* This is a lookup value, and should not be translated. */
					'selectors' => array(
						'.search .archive-page-header .page-title .archive-type',
						'.archive .archive-page-header .page-title .archive-type',
					),
				),
				'customizer_panel' => 'theme_options',
			)
		);
	}
endif;
add_action( 'after_setup_theme', 'brimstone_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @since Brimstone 1.01
 *
 * @global int $content_width
 * @return void
 */
function brimstone_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'brimstone_content_width', 660 );
}
add_action( 'after_setup_theme', 'brimstone_content_width', 0 );

/**
 * Change content width for full site width pages.
 *
 * @since Brimstone 1.01
 *
 * @global int $content_width
 * @return void
 */
function brimstone_content_width_check() {
	if ( brimstone_display_fullwidth() ) {
		$GLOBALS['content_width'] = apply_filters( 'brimstone_content_width', 1320 );
	}
}
add_action( 'template_redirect', 'brimstone_content_width_check' );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 *
 * @since Brimstone 1.01
 *
 * @return void
 */
function brimstone_widgets_init() {
	register_sidebar(
		array(
			'name'          => esc_html__( 'Sidebar', 'brimstone' ),
			'id'            => 'sidebar-1',
			'description'   => esc_html__( 'Add widgets here.', 'brimstone' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer 1', 'brimstone' ),
			'id'            => 'footer-1',
			'description'   => esc_html__( 'Add widgets here.', 'brimstone' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer 2', 'brimstone' ),
			'id'            => 'footer-2',
			'description'   => esc_html__( 'Add widgets here.', 'brimstone' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer 3', 'brimstone' ),
			'id'            => 'footer-3',
			'description'   => esc_html__( 'Add widgets here.', 'brimstone' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer Bottom', 'brimstone' ),
			'id'            => 'footer-bottom',
			'description'   => esc_html__( 'Add a text or HTML widget here with your site credit and copyright information. Doing so will override the default footer credit at the bottom of your pages.', 'brimstone' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Instagram Gallery', 'brimstone' ),
			'id'            => 'gallery-1',
			'description'   => esc_html__( 'Add Instagram widget here.', 'brimstone' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);

	register_sidebar(
		array(
			'name'          => esc_html__( 'Widgetized Page', 'brimstone' ),
			'id'            => 'widgetized-page',
			'description'   => esc_html__( 'Add content widgets here.', 'brimstone' ),
			'before_widget' => '<section id="%1$s" class="content-widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title content-widget-title">',
			'after_title'   => '</h2>',
		)
	);
}
add_action( 'widgets_init', 'brimstone_widgets_init' );

/**
 * Display customizer CSS.
 *
 * @since Brimstone 1.01
 *
 * @return void
 */
function brimstone_customizer_css_wrap() {
	/**
	 * Load CSS mixins
	 */
	require get_template_directory() . '/css/mixins.php';

	/**
	 * Load CSS functions
	 */
	require get_template_directory() . '/css/css-theme.php';

	$out  = '/* WP Customizer start */' . PHP_EOL;
	$out .= brimstone_custom_css();
	$out .= PHP_EOL . '/* WP Customizer end */';

	wp_add_inline_style( 'brimstone-style', $out );
}
add_action( 'wp_enqueue_scripts', 'brimstone_customizer_css_wrap', 20 );

/**
 * Enqueue scripts and styles.
 *
 * @since Brimstone 1.01
 *
 * @return void
 */
function brimstone_scripts() {
	global $brimstone;

	// Is up-sell designer font plugin installed and activated?
	if ( brimstone_is_wpm_fonts_deactivated() ) {
		if ( ! $brimstone['disable_body_font'] ) {
			// Add custom fonts, used in the main stylesheet.
			wp_enqueue_style( 'brimstone-body-font', get_parent_theme_file_uri() . '/fonts/lato/stylesheet.css', array(), BRIMSTONE_VERSION );
		}

		if ( ! $brimstone['disable_heading_font'] ) {
			// Add custom fonts, used in the main stylesheet.
			wp_enqueue_style( 'brimstone-heading-font', get_parent_theme_file_uri() . '/fonts/pt-sans/stylesheet.css', array(), BRIMSTONE_VERSION );
		}

		if ( ! $brimstone['disable_accent_font'] ) {
			// Add custom fonts, used in the main stylesheet.
			wp_enqueue_style( 'brimstone-accent-font', get_parent_theme_file_uri() . '/fonts/allura/stylesheet.css', array(), BRIMSTONE_VERSION );
		}
	}

	// Add genericons.
	wp_enqueue_style( 'genericons-neue', get_parent_theme_file_uri() . '/fonts/genericons-neue/genericons-neue.css', array(), BRIMSTONE_VERSION );

	// Add social logos.
	wp_deregister_style( 'social-logos' ); // remove any plugins that try and use social icons, and use the themes.
	wp_enqueue_style( 'social-logos', get_parent_theme_file_uri() . '/fonts/social-logos/social-logos.css', array(), BRIMSTONE_VERSION );

	// Add theme stylesheet.
	wp_enqueue_style( 'brimstone-style', get_stylesheet_uri(), array(), BRIMSTONE_VERSION );

	// Add bxslider style.
	wp_enqueue_style( 'bx2slider', get_parent_theme_file_uri() . '/inc/vendors/bx2slider/css/jquery.bx2slider.css', array(), BRIMSTONE_VERSION );

	// Register bxslider.
	wp_register_script( 'bx2slider', get_template_directory_uri() . '/inc/vendors/bx2slider/js/jquery.bx2slider.js', array( 'jquery' ), '4.2.14', true );

	// Register accordion.
	wp_enqueue_script( 'brimstone-accordion', get_template_directory_uri() . '/js/accordion.js', array( 'jquery' ), BRIMSTONE_VERSION, true );

	// Add menu script.
	wp_enqueue_script( 'brimstone-navigation', get_template_directory_uri() . '/js/navigation.js', array( 'jquery' ), BRIMSTONE_VERSION, true );

	// Add theme script.
	wp_enqueue_script( 'brimstone-theme', get_template_directory_uri() . '/js/theme.js', array( 'jquery' ), BRIMSTONE_VERSION, true );

	// Helps with accessibility for keyboard only users.
	wp_enqueue_script( 'brimstone-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), BRIMSTONE_VERSION, true );

	// Load comment script.
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}

	// Load if Affiliate Royale plugin is activated.
	if ( defined( 'WAFP_VERSION' ) ) {
		wp_enqueue_style( 'brimstone-affiliate-royale', get_parent_theme_file_uri() . '/css/affiliate-royale.css', array(), BRIMSTONE_VERSION );
	}

	// Load if Affiliate WP plugin is activated.
	if ( class_exists( 'Affiliate_WP' ) ) {
		wp_enqueue_style( 'brimstone-affiliate-wp', get_parent_theme_file_uri() . '/css/affiliate-wp.css', array(), BRIMSTONE_VERSION );
	}
}
add_action( 'wp_enqueue_scripts', 'brimstone_scripts' );

/**
 * Check if blog is on first page.
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_show_full_post() {
	global $paged;

	// should only show on first page.
	if ( $paged ) {
		return false;
	}

	return true;
}

/**
 * Check is h2 element exists so we can hide WooCommerce default heading element.
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_display_sub_header() {
	global $post;

	if ( preg_match( '/\<h2/', $post->post_content ) ) {
		return false;
	}

	return true;
}

/**
 * Check if h1 element exists so we can hide default page title on page.
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_display_header() {
	global $post;

	if ( preg_match( '/\<h1/', $post->post_content ) ) {
		return false;
	}

	return true;
}

/**
 * Check to load sidebar depending on page load
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_display_sidebar() {
	global $brimstone;

	if ( is_single() && 'post' === get_post_type() ) {
		if ( ! is_active_sidebar( 'sidebar-1' ) ) {
			return false;
		}

		return $brimstone['display_sidebar_post'];
	}

	if ( is_home() ) {
		if ( ! is_active_sidebar( 'sidebar-1' ) ) {
			return false;
		}

		return $brimstone['display_sidebar_blog'];
	}

	if ( brimstone_is_woocommerce_activated() ) {
		if ( ! is_active_sidebar( 'sidebar-1' ) ) {
			return false;
		}

		if ( is_shop() ) {
			return $brimstone['display_sidebar_shop'];
		} elseif ( is_product_taxonomy() ) {
			return $brimstone['display_sidebar_shop_archive'];
		}
	}

	if ( is_search() ) {
		if ( ! is_active_sidebar( 'sidebar-1' ) ) {
			return false;
		}

		return $brimstone['display_sidebar_search'];
	}

	if ( is_archive() ) {
		if ( ! is_active_sidebar( 'sidebar-1' ) ) {
			return false;
		}

		return $brimstone['display_sidebar_archive'];
	}

	if ( is_attachment() ) {
		if ( ! is_active_sidebar( 'sidebar-1' ) ) {
			return false;
		}

		return $brimstone['display_sidebar_attachment'];
	}

	return false;
}

/**
 * Check if any footer sidebars are loaded.
 *
 * @since Brimstone 1.01
 *
 * @return array 1:bool, 2:bool, 3:bool
 */
function brimstone_display_sidebar_footer() {
	$footer_1 = is_active_sidebar( 'footer-1' );
	$footer_2 = is_active_sidebar( 'footer-2' );
	$footer_3 = is_active_sidebar( 'footer-3' );

	if ( $footer_1 || $footer_2 || $footer_3 ) {
		return array(
			1 => $footer_1,
			2 => $footer_2,
			3 => $footer_3,
		);
	}

	return false;
}

/**
 * Check if fullwidth layout is activated
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_display_fullwidth() {
	global $brimstone;

	if ( brimstone_is_woocommerce_activated() ) {
		if ( is_woocommerce() || is_cart() || is_checkout() ) {
			return true;
		}
	}

	if ( is_page_template( 'templates/full-width-page.php' ) ) {
		return true;
	}

	return false;
}

/**
 * Check and load the correct blog template according to Customizer option.
 *
 * @since Brimstone 1.01
 *
 * @return void
 */
function brimstone_get_blog_part() {
	global $brimstone;

	if ( is_home() ) {
		get_template_part( 'template-parts/' . $brimstone['blog_display'] );
	} elseif ( is_archive() ) {
		get_template_part( 'template-parts/' . $brimstone['archive_display'] );
	} elseif ( is_search() ) {
		get_template_part( 'template-parts/' . $brimstone['search_display'] );
	}
}

/**
 * Additonal check to Show/Hide Featured Image outside of the loop.
 *
 * @link https://jetpack.com/support/content-options/
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_jetpack_featured_image_display() {
	if ( ! function_exists( 'jetpack_featured_images_remove_post_thumbnail' ) ) {
		return true;
	} else {
		$options         = get_theme_support( 'jetpack-content-options' );
		$featured_images = ( ! empty( $options[0]['featured-images'] ) ) ? $options[0]['featured-images'] : null;

		$settings = array(
			'post-default' => ( isset( $featured_images['post-default'] ) && false === $featured_images['post-default'] ) ? '' : 1,
			'page-default' => ( isset( $featured_images['page-default'] ) && false === $featured_images['page-default'] ) ? '' : 1,
		);

		$settings = array_merge(
			$settings, array(
				'post-option' => get_option( 'jetpack_content_featured_images_post', $settings['post-default'] ),
				'page-option' => get_option( 'jetpack_content_featured_images_page', $settings['page-default'] ),
			)
		);

		if ( ( ! $settings['post-option'] && is_single() )
			|| ( ! $settings['page-option'] && is_singular() && is_page() ) ) {
			return false;
		} else {
			return true;
		}
	}
}

/**
 * Check to see if page can display a header image using the posts
 * featured image.
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_display_header_image() {
	if ( ( is_page() || ( is_single() && 'post' === get_post_type() ) ) && has_post_thumbnail() && brimstone_jetpack_featured_image_display() ) {
		if ( ! is_page_template( 'templates/widgetized-page.php' ) ) {
			if ( ! is_page_template( 'templates/no-featured-image-page.php' ) ) {
				if ( ! is_page_template( 'templates/media-post.php' ) ) {
					return true;
				}
			}
		}
	}

	return false;
}

/**
 * Get page template when inside loop.
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_get_page_template_in_loop() {
	$page_template = get_page_template_slug( get_the_ID() );

	if ( 'templates/media-post.php' === $page_template ) {
		return 'media';
	}

	return '';
}

/**
 * Check if search form should be in main menu bar.
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_is_menu_search_activated() {
	global $brimstone;

	return $brimstone['show_menu_search'] ? true : false;
}

/**
 * Query WooCommerce activation
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_is_woocommerce_activated() {
	return class_exists( 'woocommerce' ) ? true : false;
}

/**
 * Check Jetpack activation
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_is_jetpack_activated() {
	if ( defined( 'JETPACK__VERSION' ) ) {
		return true;
	}

	return false;
}

/**
 * Check WPM Fonts is activated
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_is_wpm_fonts_activated() {
	if ( defined( 'WPM_FONTS_VERSION' ) ) {
		return true;
	}

	return false;
}

/**
 * Check WPM Fonts id deactivated
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_is_wpm_fonts_deactivated() {
	return ! brimstone_is_wpm_fonts_activated();
}

/**
 * Check One Click Demo Import (ocdi) activation
 *
 * @since Brimstone 1.01
 *
 * @return bool
 */
function brimstone_is_ocdi_activated() {
	return class_exists( 'OCDI_Plugin' ) ? true : false;
}

/**
 * Load Default option values.
 */
require get_template_directory() . '/inc/default-options.php';

/**
 * Load Gutenberg editor functions.
 */
require get_template_directory() . '/inc/gutenberg.php';

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';

/**
 * Widget Base
 */
require get_template_directory() . '/inc/class-widget.php';

/**
 * Load Each Individual Widget
 */
foreach ( glob( get_template_directory() . '/inc/widgets/*.php' ) as $filename ) {
	require_once $filename;
}

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Load Jetpack compatibility file.
 */
if ( brimstone_is_jetpack_activated() ) {
	require get_template_directory() . '/inc/jetpack.php';
}

/**
 * Load WooCommerce class
 */
if ( brimstone_is_woocommerce_activated() ) {
	require get_parent_theme_file_path() . '/inc/class-woocommerce.php';
}

/**
 * SVG icons functions and filters.
 */
require get_parent_theme_file_path() . '/inc/icon-functions.php';

/**
 * Check for recommended plugins.
 */
require get_parent_theme_file_path() . '/inc/recommended-plugins.php';

/**
 * Add dashboard widget and info page.
 */
require get_parent_theme_file_path() . '/inc/dashboard.php';

/**
 * One Click Demo Import
 */
if ( brimstone_is_ocdi_activated() ) {
	require get_parent_theme_file_path() . '/inc/one-click-demo-import.php';
}
