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

if ( ! function_exists( 'cultural_festival_get_option' ) ) :

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

		$cultural_festival_default_options = cultural_festival_get_default_theme_options();

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

		$cultural_festival_theme_options = (array)get_theme_mod( 'theme_options' );
		$cultural_festival_theme_options = wp_parse_args( $cultural_festival_theme_options, $cultural_festival_default_options );

		$cultural_festival_value = null;

		if ( isset( $cultural_festival_theme_options[ $key ] ) ) {
			$cultural_festival_value = $cultural_festival_theme_options[ $key ];
		}

		return $cultural_festival_value;

	}

endif;

if ( ! function_exists( 'cultural_festival_get_options' ) ) :

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

    $cultural_festival_default_options = cultural_festival_get_default_theme_options();
    $cultural_festival_theme_options = (array)get_theme_mod( 'theme_options' );
    $cultural_festival_theme_options = wp_parse_args( $cultural_festival_theme_options, $cultural_festival_default_options );
    return $cultural_festival_theme_options;

  }

endif;

if( ! function_exists( 'cultural_festival_exclude_category_in_blog_page' ) ) :

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

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

endif;

add_filter( 'pre_get_posts', 'cultural_festival_exclude_category_in_blog_page' );
