<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package wpstream-theme
 */

if ( ! function_exists( 'wpstream_theme_show_page_title' ) ) {
	/**
	 * Show page title.
	 * @param $post_id
	 * @return bool|null
	 */
	function wpstream_theme_show_page_title( $post_id = null ) {
		$post_type = get_post_type( $post_id );

		if ( $post_type === 'page' ) {
			if(function_exists('rwmb_meta')){
				$show_title_page = rwmb_meta( 'wpstream_theme_show_page_title' );

				if ( '' !== $show_title_page ) {
					return '1' === $show_title_page;
				} else {
					return get_theme_mod( 'wpstream_page_show_page_title', true );
				}
			}else{
				return 1;
			}
		}

		return null;
	}
}

if ( ! function_exists( 'wpstream_theme_is_show_sidebar' ) ) {
	/**
	 * Returns true if the sidebar should be display for post.
	 *
	 * @param $post_id
	 * @return bool
	 */
	function wpstream_theme_is_show_sidebar( $post_id = null ) {
		$is_show_sidebar=null;
		if(function_exists('rwmb_meta')){
			$is_show_sidebar = rwmb_meta( 'wpstream_theme_show_sidebar_on_post', array(), $post_id );
		}
		if ( '' !== $is_show_sidebar ) {
			return '1' === $is_show_sidebar;
		} else {
			$post_type = get_post_type( $post_id );

			switch ( $post_type ) {
				case 'post':
					$wpstream_show_sidebar = get_theme_mod( 'wpstream_blog_post_sidebar', true );
					break;
				default:
					$wpstream_show_sidebar = apply_filters( 'wpstream_show_sidebar_for_post_type', false, $post_type );
					break;
			}

			return $wpstream_show_sidebar;
		}
	}
}

if ( ! function_exists( 'wpstream_theme_is_transparent_header' ) ) {
	/**
	 * Returns true if the post header should be set to transparent.
	 *
	 * @param $post_id
	 * @return bool
	 */
	function wpstream_theme_is_transparent_header( $post_id = null ) {
		if(function_exists('rwmb_meta')){
			$is_transparent_header = rwmb_meta( 'wpstream_theme_use_transparent_on_post', array(), $post_id );

			if ( '' !== $is_transparent_header ) {
				return '1' === $is_transparent_header;
			} else {
				// Get the value of the 'wpstream_theme_transparent_header' theme modification option.
				$transparent_header = get_theme_mod( 'wpstream_theme_transparent_header' );

				// Use a ternary operator to conditionally assign the class based on the value.
				return $transparent_header;
			}
		}
	}
}

if ( ! function_exists( 'wpstream_is_type_1_button_background_gradient' ) ) {
	/**
	 * Returns true if the button background should be a gradient.
	 *
	 * @return bool
	 */
	function wpstream_is_type_1_button_background_gradient() {
		$wpstream_type_1_button_background_option = get_theme_mod( 'wpstream_type_1_button_background_option', 'simple' );

		return 'gradient' === $wpstream_type_1_button_background_option;
	}
}

if ( ! function_exists( 'wpstream_is_type_1_button_hover_background_gradient' ) ) {
	/**
	 * Returns true if the button background should be a gradient.
	 *
	 * @return bool
	 */
	function wpstream_is_type_1_button_hover_background_gradient() {
		$wpstream_type_1_button_background_option = get_theme_mod( 'wpstream_type_1_button_hover_background_option', 'simple' );

		return 'gradient' === $wpstream_type_1_button_background_option;
	}
}

if ( ! function_exists( 'wpstream_is_type_2_button_background_gradient' ) ) {
	/**
	 * Returns true if the button background should be a gradient.
	 *
	 * @return bool
	 */
	function wpstream_is_type_2_button_background_gradient() {
		$wpstream_type_2_button_background_option = get_theme_mod( 'wpstream_type_2_button_background_option', 'simple' );

		return 'gradient' === $wpstream_type_2_button_background_option;
	}
}

if ( ! function_exists( 'wpstream_is_type_2_button_hover_background_gradient' ) ) {
	/**
	 * Returns true if the button background should be a gradient.
	 *
	 * @return bool
	 */
	function wpstream_is_type_2_button_hover_background_gradient() {
		$wpstream_type_2_button_background_option = get_theme_mod( 'wpstream_type_2_button_hover_background_option', 'simple' );

		return 'gradient' === $wpstream_type_2_button_background_option;
	}
}


/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function wpstream_theme_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'sidebar-1' ) ) {
		$classes[] = 'no-sidebar';
	}

	return $classes;
}

add_filter( 'body_class', 'wpstream_theme_body_classes' );


if ( ! function_exists( 'wpstream_theme_pingback_header' ) ) {
	/**
	 * Add a ping-back url auto-discovery header for single posts, pages, or attachments.
	 */
	function wpstream_theme_pingback_header() {
		if ( is_singular() && pings_open() ) {
			printf( '<link rel="pingback" href="%s">', esc_url( get_bloginfo( 'pingback_url' ) ) );
		}
	}
}


add_action( 'wp_head', 'wpstream_theme_pingback_header' );

if ( ! function_exists( 'wpstream_theme_container_class' ) ) :
	/**
	 * Container class
	 */
	function wpstream_theme_container_class() {

		if ( wpstreamtheme_is_built_with_elementor() ) {
			return 'container container-fluid p-0 ' . (wpstream_theme_is_show_sidebar() ? 'elementor-container' : '');
		}

		return 'container';
	}
endif;

if ( ! function_exists( 'wpstream_video_cards_column_class' ) ) {
	/**
	 * Determine the column classes for video cards based on the number of columns per row.
	 *
	 * @param int $per_row The number of columns per row.
	 * @return string The string of column classes.
	 */
	function wpstream_video_cards_column_class( $per_row ) {
		$per_row = intval( $per_row );
		$return = 'col-12 col-sm-6 col-md-4 col-lg-4';

		if ( 4 === $per_row ) {
			$return = 'col-12 col-sm-6 col-md-4 col-lg-3';
		}else if ( 2 === $per_row ) {
			$return = 'col-12 col-sm-6 col-md-6 col-lg-6';
		}else if ( 6 === $per_row ) {
			$return = 'col-12 col-sm-6 col-md-4 col-lg-3 col-xl-2';
		}else if ( 5 === $per_row ) {
			$return = 'col-12 col-sm-6 col-md-4 col-2-4';
		}
		return $return;
	}
}



/**
 * Elementor functions
 *
 * @package wpstream-theme
 */

if ( ! function_exists( 'wpstreamtheme_is_built_with_elementor' ) ) {
	/**
	 * Check whether the post was built with Elementor.
	 *
	 * @param int $post_id Post ID.
	 * @return bool
	 */
	function wpstreamtheme_is_built_with_elementor( $post_id = 0 ) {
		$is_built_with_elementor = false;

		if ( class_exists( 'Elementor\Plugin' ) ) {
			if ( 0 === $post_id ) {
				$post_id = get_queried_object_id();
			}

			$document = Elementor\Plugin::$instance->documents->get( $post_id );

			if ( $document ) {
				$is_built_with_elementor = $document->is_built_with_elementor();
			}
		}

		return $is_built_with_elementor;
	}
}



if ( ! function_exists( 'wpstream_theme_header_container_class' ) ) {
	/**
	 * Allow modifying the Header Container Class
	 *
	 * @return string
	 */
	function wpstream_theme_header_container_class() {
		$wide_header = get_theme_mod( 'wpstream_theme_wide_header', true );

		// Use a ternary operator to conditionally assign the container class.
		return ( $wide_header ) ? 'wpstream-wide-container container' : 'container';
	}
}

if ( ! function_exists( 'wpstream_theme_footer_container_class' ) ) {
	/**
	 * Allow modifying the Footer Container Class
	 *
	 * @return string
	 */
	function wpstream_theme_footer_container_class() {
		$wide_footer = get_theme_mod( 'wpstream_theme_wide_footer' );

		// Use a ternary operator to conditionally assign the container class.
		return ( $wide_footer ) ? 'container-fluid' : 'container';
	}
}

if ( ! function_exists( 'wpstream_theme_transparent_header_class' ) ) {
	/**
	 * Get the transparent header CSS class based on the theme modification option.
	 *
	 * @return string CSS class to apply to the header element.
	 */
	function wpstream_theme_transparent_header_class() {
		// Use a ternary operator to conditionally assign the class based on the value.
		return wpstream_theme_is_transparent_header() ? 'wpstream_transparent_header' : 'bg-dark';
	}
}


if ( ! function_exists( 'wpstream_get_current_post_type' ) ) {
	/**
	 * Get current post type
	 */
	function wpstream_get_current_post_type() {
		global $post;

		if ( is_object( $post ) && isset( $post->post_type ) ) {
			return $post->post_type;
		} else {
			return null;
		}
	}
}