<?php
/**
 * Adventurous functions and definitions
 *
 * @package Catch Themes
 * @subpackage Adventurous
 * @since Adventurous 1.0
 */

if ( ! function_exists( 'adventurous_content_width' ) ) :
	/**
	 * Set the content width in pixels, based on the theme's design and stylesheet.
	 *
	 * Priority 0 to make it available to lower priority callbacks.
	 *
	 * @global int $content_width
	 */
	function adventurous_content_width() {
		$GLOBALS['content_width'] = apply_filters( 'adventurous_content_width', 800 );
	}
endif;
add_action( 'after_setup_theme', 'adventurous_content_width', 0 );


if ( ! function_exists( 'adventurous_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 post thumbnails.
 *
 * @since Adventurous 1.0
 */
function adventurous_setup() {
	/**
	 * Make theme available for translation
	 * Translations can be filed in the /languages/ directory
	 * If you're building a theme based on Adventurous, use a find and replace
	 * to change 'adventurous' to the name of your theme in all the template files
	 */
	load_theme_textdomain( 'adventurous', get_template_directory() . '/languages' );

	/**
	 * Add callback for custom TinyMCE editor stylesheets. (editor-style.css)
	 * @see http://codex.wordpress.org/Function_Reference/add_editor_style
	 */
	add_editor_style();

	/**
	 * 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' );

	// This theme allows users to set a custom background.
	add_theme_support( 'custom-background', array(
		// Let WordPress know what our default background color is.
		'default-color' => 'f9f9f9',
	) );

	/**
     * This feature enables custom-menus support for a theme.
     * @see http://codex.wordpress.org/Function_Reference/register_nav_menus
     */
	register_nav_menus(array(
		'primary' 	=> __( 'Header Right Menu', 'adventurous' ),
		'secondary'	=> __( 'Header Secondary Menu', 'adventurous' )
	) );

	/**
	 * Custom Menus Functions.
	 */
	require( get_template_directory() . '/inc/adventurous-menus.php' );

	/**
	 * Add support for the Aside Post Formats
	 */
	add_theme_support( 'post-formats', array( 'aside', 'gallery', 'link', 'image', 'quote', 'status', 'video', 'audio' ) );

	/**
     * This feature enables Jetpack plugin Infinite Scroll
     */
    add_theme_support( 'infinite-scroll', array(
		'type'           => 'click',
        'container'      => 'content',
        'footer_widgets' => array( 'sidebar-2', 'sidebar-3', 'sidebar-4' ),
        'footer'         => 'page'
    ) );

	/**
	 * Enable support for Post Thumbnails
	 */
	add_theme_support( 'post-thumbnails' );

	add_image_size( 'slider', 1600, 600, true); //Featured Post Slider Image
	add_image_size( 'featured', 800, 324, true); //Featured Image
	add_image_size( 'small-featured', 400, 267, true); //Small Featured Image

	//@remove Remove check when WordPress 4.8 is released
	if ( function_exists( 'has_custom_logo' ) ) {
		/**
		* Setup Custom Logo Support for theme
		* Supported from WordPress version 4.5 onwards
		* More Info: https://make.wordpress.org/core/2016/03/10/custom-logo/
		*/
		add_theme_support( 'custom-logo', array(
			'height'      => 41,
			'width'       => 245,
			'flex-height' => true,
			'flex-width'  => true,
		) );
	}

	// Add support for Block Styles.
	add_theme_support( 'wp-block-styles' );

	// Add support for editor styles.
	add_theme_support( 'editor-styles' );

	// Add support for full and wide align images.
	add_theme_support( 'align-wide' );

	// Add support for responsive embeds.
	add_theme_support( 'responsive-embeds' );

	// Add custom editor font sizes.
	add_theme_support(
		'editor-font-sizes',
		array(
			array(
				'name'      => esc_html__( 'Small', 'adventurous' ),
				'shortName' => esc_html__( 'S', 'adventurous' ),
				'size'      => 14,
				'slug'      => 'small',
			),
			array(
				'name'      => esc_html__( 'Normal', 'adventurous' ),
				'shortName' => esc_html__( 'M', 'adventurous' ),
				'size'      => 17,
				'slug'      => 'normal',
			),
			array(
				'name'      => esc_html__( 'Large', 'adventurous' ),
				'shortName' => esc_html__( 'L', 'adventurous' ),
				'size'      => 48,
				'slug'      => 'large',
			),
			array(
				'name'      => esc_html__( 'Huge', 'adventurous' ),
				'shortName' => esc_html__( 'XL', 'adventurous' ),
				'size'      => 64,
				'slug'      => 'huge',
			),
		)
	);

	// Add support for custom color scheme.
	add_theme_support( 'editor-color-palette', array(
		array(
			'name'  => esc_html__( 'White', 'adventurous' ),
			'slug'  => 'white',
			'color' => '#ffffff',
		),
		array(
			'name'  => esc_html__( 'Black', 'adventurous' ),
			'slug'  => 'black',
			'color' => '#111111',
		),
		array(
			'name'  => esc_html__( 'Gray', 'adventurous' ),
			'slug'  => 'gray',
			'color' => '#f4f4f4',
		),
		array(
			'name'  => esc_html__( 'Yellow', 'adventurous' ),
			'slug'  => 'yellow',
			'color' => '#e5ae4a',
		),
		array(
			'name'  => esc_html__( 'Blue', 'adventurous' ),
			'slug'  => 'blue',
			'color' => '#21759b',
		),
		array(
			'name'  => esc_html__( 'Green', 'adventurous' ),
			'slug'  => 'green',
			'color' => '#7c9b30',
		),
	) );
}
endif; // adventurous_setup
add_action( 'after_setup_theme', 'adventurous_setup' );


if ( ! function_exists( 'adventurous_get_theme_layout' ) ) :
	/**
	 * Returns Theme Layout prioritizing the meta box layouts
	 *
	 * @uses  get_options
	 *
	 * @action wp_head
	 *
	 * @since Adventurous 1.9.1
	 */
	function adventurous_get_theme_layout() {
		$id = '';

		global $post, $wp_query;

	    // Front page displays in Reading Settings
		$page_on_front  = get_option('page_on_front') ;
		$page_for_posts = get_option('page_for_posts');

		// Get Page ID outside Loop
		$page_id = $wp_query->get_queried_object_id();

		// Blog Page or Front Page setting in Reading Settings
		if ( $page_id == $page_for_posts || $page_id == $page_on_front ) {
	        $id = $page_id;
	    }
	    elseif ( is_singular() ) {
	 		if ( is_attachment() ) {
				$id = $post->post_parent;
			}
			else {
				$id = $post->ID;
			}
		}

		//Get appropriate metabox value of layout
		if ( '' != $id ) {
			$layout = get_post_meta( $id, 'adventurous-sidebarlayout', true );
		}
		else {
			$layout = 'default';
		}

		//Load options data
		$options = adventurous_get_options();

   		//check empty and load default
		if ( empty( $layout ) || 'default' == $layout ) {
			$layout = $options['sidebar_layout'];
		}

		return $layout;
	}
endif; //adventurous_get_theme_layout


/**
 * Migrate Logo to New WordPress core Custom Logo
 *
 *
 * Runs if version number saved in theme_mod "logo_version" doesn't match current theme version.
 */
function adventurous_logo_migrate() {
	$ver = get_theme_mod( 'logo_version', false );

	// Return if update has already been run
	if ( version_compare( $ver, '3.6' ) >= 0 ) {
		return;
	}

	/**
	 * Get Theme Options Values
	 */
	$options = adventurous_get_options();

   	// If a logo has been set previously, update to use logo feature introduced in WordPress 4.5
	if ( function_exists( 'the_custom_logo' ) ) {
		if ( isset( $options['featured_logo_header'] ) && '' != $options['featured_logo_header'] ) {
			// Since previous logo was stored a URL, convert it to an attachment ID
			$logo = attachment_url_to_postid( $options['featured_logo_header'] );

			if ( is_int( $logo ) ) {
				set_theme_mod( 'custom_logo', $logo );
			}
		}

  		// Update to match logo_version so that script is not executed continously
		set_theme_mod( 'logo_version', '3.6' );
	}
}
add_action( 'after_setup_theme', 'adventurous_logo_migrate' );


/**
 * Migrate Custom CSS to WordPress core Custom CSS
 *
 * Runs if version number saved in theme_mod "custom_css_version" doesn't match current theme version.
 */
function adventurous_custom_css_migrate(){
	$ver = get_theme_mod( 'custom_css_version', false );

	// Return if update has already been run
	if ( version_compare( $ver, '4.7' ) >= 0 ) {
		return;
	}

	if ( function_exists( 'wp_update_custom_css_post' ) ) {
	    // Migrate any existing theme CSS to the core option added in WordPress 4.7.

	    /**
		 * Get Theme Options Values
		 */
		$options = adventurous_get_options();	

	    if ( '' != $options['custom_css'] ) {
			$core_css = wp_get_custom_css(); // Preserve any CSS already added to the core option.
			$return   = wp_update_custom_css_post( $core_css . $options['custom_css'] );

	        if ( ! is_wp_error( $return ) ) {
	            // Remove the old theme_mod, so that the CSS is stored in only one place moving forward.
	            unset( $options['custom_css'] );
	            update_option( 'adventurous_options', $options );

	            // Update to match custom_css_version so that script is not executed continously
				set_theme_mod( 'custom_css_version', '4.7' );
	        }
	    }
	}
}
add_action( 'after_setup_theme', 'adventurous_custom_css_migrate' );


/**
 * Implement the Custom Header feature
 */
require( get_template_directory() . '/inc/adventurous-custom-header.php' );

/**
 * Theme Options Defaults
 */
require( get_template_directory() . '/inc/adventurous-default-options.php' );

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

/**
 * Custom functions that act independently of the theme templates
 */
require( get_template_directory() . '/inc/adventurous-functions.php' );

/**
 * Slider Function
 */
require( get_template_directory() . '/inc/adventurous-slider.php' );

/**
 * Headlines Function
 */
require( get_template_directory() . '/inc/adventurous-promotion-headlines.php' );

/**
 * Featured Content Function
 */
require( get_template_directory() . '/inc/adventurous-featured-content.php' );

/**
 * Metabox
 */
require( get_template_directory() . '/inc/adventurous-metabox.php' );

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

/**
 * Register Sidebar and Widget.
 */
require( get_template_directory() . '/inc/adventurous-widgets.php' );
