<?php
/**
 * Core functions.
 *
 * @package clothing_brand_store
 */

if ( ! function_exists( 'clothing_brand_store_get_option' ) ) :

	/**
	 * Get theme option
	 *
	 * @since 1.0.0
	 *
	 * @param string $key Option key.
	 * @return mixed Option value.
	 */
	function clothing_brand_store_get_option( $key ) {

		$clothing_brand_store_default_options = clothing_brand_store_get_default_theme_options();

		if ( empty( $key ) ) {
			return;
		}

		$clothing_brand_store_theme_options = (array)get_theme_mod( 'theme_options' );
		$clothing_brand_store_theme_options = wp_parse_args( $clothing_brand_store_theme_options, $clothing_brand_store_default_options );


		$clothing_brand_store_value = null;

		if ( isset( $clothing_brand_store_theme_options[ $key ] ) ) {
			$clothing_brand_store_value = $clothing_brand_store_theme_options[ $key ];
		}

		return $clothing_brand_store_value;

	}

endif;

if ( ! function_exists( 'clothing_brand_store_get_options' ) ) :

	/**
	 * Get all theme options.
	 *
	 * @since 1.0.0
	 *
	 * @return array Theme options.
	 */
  function clothing_brand_store_get_options() {

    $clothing_brand_store_default_options = clothing_brand_store_get_default_theme_options();
    $clothing_brand_store_theme_options = (array)get_theme_mod( 'theme_options' );
    $clothing_brand_store_theme_options = wp_parse_args( $clothing_brand_store_theme_options, $clothing_brand_store_default_options );
    return $clothing_brand_store_theme_options;

  }

endif;

if( ! function_exists( 'clothing_brand_store_exclude_category_in_blog_page' ) ) :

  /**
   * Exclude category in blog page.
   *
   * @since 1.0
   */
  function clothing_brand_store_exclude_category_in_blog_page( $query ) {

    if( $query->is_home && $query->is_main_query()   ) {
      $clothing_brand_store_exclude_categories = clothing_brand_store_get_option( 'exclude_categories' );
      if ( ! empty( $clothing_brand_store_exclude_categories ) ) {
        $cats = explode( ',', $clothing_brand_store_exclude_categories );
        $cats = array_filter( $cats, 'is_numeric' );
        $clothing_brand_store_string_exclude = '';
        if ( ! empty( $cats ) ) {
          $clothing_brand_store_string_exclude = '-' . implode( ',-', $cats);
          $query->set( 'cat', $clothing_brand_store_string_exclude );
        }
      }
    }
    return $query;
  }

endif;

add_filter( 'pre_get_posts', 'clothing_brand_store_exclude_category_in_blog_page' );
