<?php
/**
 * Meteorite functions and definitions.
 *
 * @package Meteorite
 */

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

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

	// Enable custom logo function since WP Version 4.5
	add_theme_support( 'custom-logo' );

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

	/*
	* Add Support for Custom Fields, Meteorite needs it for parallax header text.
	*/
	add_theme_support( 'custom-fields' );

	/*
	 * 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' );
	add_image_size('meteorite-large-thumb', 830);
	add_image_size('meteorite-medium-thumb', 550, 400, true);
	add_image_size('meteorite-small-thumb', 75, 75, true);
	add_image_size('meteorite-tiny-thumb', 50, 50, true);

	// This theme uses wp_nav_menu() in four location.
	register_nav_menus( array(
		'topbar' => esc_html__( 'Topbar', 'meteorite' ),
		'primary' => esc_html__( 'Primary', 'meteorite' ),
		'footer' => esc_html__( 'Footer', 'meteorite' ),
		'404_pages' => esc_html__( '404 Menu', 'meteorite' ),
	) );

	/*
	 * 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( 'meteorite_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );

	//Terra Themes Tools support
	add_theme_support( 'terra-themes-tools-post-types', array( 'clients', 'employees', 'projects', 'slides' , 'testimonials', 'timeline' ) );

}
endif;
add_action( 'after_setup_theme', 'meteorite_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.
 *
 * @global int $content_width
 */
function meteorite_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'meteorite_content_width', 1170 );
}
add_action( 'after_setup_theme', 'meteorite_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function meteorite_widgets_init() {
	register_sidebar( array(
		'name'          => esc_html__( 'Sidebar default', 'meteorite' ),
		'id'            => 'sidebar-1',
		'description'   => '',
		'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__( 'Sidebar left', 'meteorite' ),
		'id'            => 'sidebar-left',
		'description'   => '',
		'before_widget' => '<section id="%1$s" class="widget %2$s">',
		'after_widget'  => '</section>',
		'before_title'  => '<h2 class="widget-title">',
		'after_title'   => '</h2>',
	) );

	//Footer widget areas
	$widget_areas = get_theme_mod('footer_widget_areas', '3');
	for ($i=1; $i<=$widget_areas; $i++) {
		register_sidebar( array(
			'name'          => esc_html__( 'Footer ', 'meteorite' ) . $i,
			'id'            => 'footer-' . $i,
			'description'   => '',
			'before_widget' => '<aside id="%1$s" class="widget %2$s">',
			'after_widget'  => '</aside>',
			'before_title'  => '<h3 class="widget-title">',
			'after_title'   => '</h3>',
		) );
	}

	//Register widgets
	register_widget( 'Meteorite_List_With_Icons' );
	register_widget( 'Meteorite_Clients' );
	register_widget( 'Meteorite_Testimonials' );
	register_widget( 'Meteorite_Skills' );
	register_widget( 'Meteorite_Action' );
	register_widget( 'Meteorite_Video_Widget' );
	register_widget( 'Meteorite_Employees' );
	register_widget( 'Meteorite_Projects' );
	register_widget( 'Meteorite_Projects_Carousel' );
	register_widget( 'Meteorite_Text_With_Icon' );
	register_widget( 'Meteorite_Contact_Info' );
	register_widget( 'Meteorite_Latest_News' );
	register_widget( 'Meteorite_Latest_News_Carousel' );
	register_widget( 'Meteorite_Social_Media' );
	register_widget( 'Meteorite_Facts' );
	register_widget( 'Meteorite_Tabs' );
	register_widget( 'Meteorite_Image_Frame' );
	register_widget( 'Meteorite_Timeline' );
	register_widget( 'Meteorite_Pricing_Table' );
	register_widget( 'Meteorite_Skills_Circle' );

}
add_action( 'widgets_init', 'meteorite_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function meteorite_scripts() {
	if ( get_theme_mod('disable_google_fonts') != 1 ) :
		wp_enqueue_style( 'meteorite-fonts', esc_url( meteorite_fonts_url() ), array(), null );
	endif;

	wp_enqueue_style( 'meteorite-style', get_stylesheet_uri() );

	wp_enqueue_style( 'meteorite-font-awesome', get_template_directory_uri() . '/fonts/font-awesome.min.css' );	

	wp_enqueue_script( 'meteorite-scripts', get_template_directory_uri() . '/js/scripts.js', array('jquery'), '', true );

	wp_enqueue_script( 'meteorite-main', get_template_directory_uri() . '/js/main.min.js', array('jquery'), '', true );

	wp_enqueue_script( 'meteorite-skip-link-focus-fix', get_template_directory_uri() . '/js/skip-link-focus-fix.js', array(), '20151215', true );

	wp_enqueue_script( 'meteorite-html5shiv', get_template_directory_uri() . '/js/html5shiv.js', array(), '', true );
	wp_script_add_data( 'meteorite-html5shiv', 'conditional', 'lt IE 9' );	

	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'meteorite_scripts' );

/**
 * Google Fonts - adapted from TwentySixteen
 *
 * Lets you load Google Fonts by adding only the name
 */
if ( ! function_exists( 'meteorite_fonts_url' ) ) :
function meteorite_fonts_url() {

	$fonts_url 		= '';
	$subsets   		= 'latin,latin-ext,cyrillic'; //Fallback for browsers with no unicode-range support
	$weights 		= get_theme_mod('font_weights', array( '400', '400italic', '600', '600italic' ));
	$weights 		= implode(',', $weights);
	$body_font 		= get_theme_mod('body_font_family', 'Source Sans Pro');
	$headings_font 	= get_theme_mod('headings_font_family', 'Libre Franklin');
	$fonts     		= array();
	$fonts[] 		= $body_font . ':' . $weights;
	$fonts[] 		= $headings_font . ':' . $weights;

	if ( $fonts ) {
		$fonts_url = add_query_arg( array(
			'family' => urlencode( implode( '|', wp_kses_post($fonts) ) ),
			'subset' => urlencode( $subsets ),
		), 'https://fonts.googleapis.com/css' );
	}

	return $fonts_url;
}
endif;

/**
 * Enqueue Bootstrap
 */
function meteorite_enqueue_bootstrap() {
	wp_enqueue_style( 'meteorite-bootstrap', get_template_directory_uri() . '/assets/bootstrap/bootstrap.min.css', array(), true );
	wp_enqueue_script( 'meteorite-bootstrap-js', get_template_directory_uri() . '/assets/bootstrap/bootstrap.min.js', array('jquery'), '', true );
}
add_action( 'wp_enqueue_scripts', 'meteorite_enqueue_bootstrap', 9 );

/**
 * Enqueue Mediaelementplayer css
 */
function meteorite_enqueue_mediaelementplayer() {
	wp_enqueue_style( 'meteorite-mediaelementplayer', get_template_directory_uri() . '/assets/mediaelementplayer/mediaelementplayer.min.css', array(), true );
}
add_action( 'wp_enqueue_scripts', 'meteorite_enqueue_mediaelementplayer', 9 );

/**
 * Enqueue Owlcarousel transition css
 */
function meteorite_enqueue_owlcarouselcss() {
	wp_enqueue_style( 'meteorite-owlcarousel', get_template_directory_uri() . '/assets/owlcarousel/owl.transitions.css', array(), true );
}
add_action( 'wp_enqueue_scripts', 'meteorite_enqueue_owlcarouselcss' );

/**
 * Registers an editor stylesheet for the theme.
 */
function meteorite_add_editor_styles() {
    add_editor_style( 'assets/custom-editor-style.css' );
}
add_action( 'admin_init', 'meteorite_add_editor_styles' );

/**
 * Enqueue media upload scripts for widgets
 */
function meteorite_enqueue_widget_scripts() {
	wp_enqueue_script( 'meteorite-mediaupload', get_template_directory_uri() . '/inc/widgets/assets/meteorite-media-upload.js', array('jquery'), '', true );
	wp_enqueue_style( 'meteorite-meta-style', get_template_directory_uri() . '/inc/widgets/assets/meteorite-meta-style.css', array(), true );
}
add_action( 'admin_enqueue_scripts', 'meteorite_enqueue_widget_scripts' );

/**
 * Custom post types slugs
 */
function meteorite_custom_employees_slug() {
    $slug = get_theme_mod('cpts_slug_employees','employees');
    return $slug;
}
add_filter('terra_themes_employees_rewrite_slug', 'meteorite_custom_employees_slug');

function meteorite_custom_clients_slug() {
    $slug = get_theme_mod('cpts_slug_clients','clients');
    return $slug;
}
add_filter('terra_themes_clients_rewrite_slug', 'meteorite_custom_clients_slug');

function meteorite_custom_projects_slug() {
    $slug = get_theme_mod('cpts_slug_projects','projects');
    return $slug;
}
add_filter('terra_themes_projects_rewrite_slug', 'meteorite_custom_projects_slug');

function meteorite_custom_testimonials_slug() {
    $slug = get_theme_mod('cpts_slug_testimonials','testimonials');
    return $slug;
}
add_filter('terra_themes_testimonials_rewrite_slug', 'meteorite_custom_testimonials_slug');

function meteorite_custom_slides_slug() {
    $slug = get_theme_mod('cpts_slug_slides','slides');
    return $slug;
}
add_filter('terra_themes_slides_rewrite_slug', 'meteorite_custom_slides_slug');

function meteorite_custom_timeline_events_slug() {
    $slug = get_theme_mod('cpts_slug_timeline-events','timeline-events');
    return $slug;
}
add_filter('terra_themes_timeline_events_rewrite_slug', 'meteorite_custom_timeline_events_slug');

function meteorite_custom_woocommerce_delimiter() {
    $delimiter = get_theme_mod('shop_breadcrumb_delimiter','/');
    return $delimiter;
}
add_filter('meteorite_woocommerce_delimiter', 'meteorite_custom_woocommerce_delimiter');

/**
 * Menu fallback
 */
function meteorite_menu_fallback() {
	if ( current_user_can('edit_theme_options') ) {
		echo '<li class="li-placeholder"><a class="menu-fallback" href="' . admin_url('nav-menus.php') . '">' . __( 'Create your menu here', 'meteorite' ) . '</a></li>';
	}
}

/**
 * Needed for Image Frame Widget / Text With Icon Widget
 *
 */
if ( ! function_exists('meteorite_get_attachment_id_from_url') ) :
	/**
	 * Function that retrieves attachment id for passed attachment url
	 * @param $attachment_url
	 * @return null|string
	 */
	function meteorite_get_attachment_id_from_url( $attachment_url ) {
		global $wpdb;
		$attachment_id = '';

		//is attachment url set?
		if ( $attachment_url !== '' ) {
			//prepare query
			$query = "SELECT ID FROM {$wpdb->posts} WHERE guid='$attachment_url'";

			//get attachment id
			$attachment_id = $wpdb->get_var($query);
		}

		//return it
		return $attachment_id;
	}
endif;

/**
 * Needed for Image Frame Widget
 *
 */
if ( ! function_exists('meteorite_get_attachment_thumb_url') ) :
	function meteorite_get_attachment_thumb_url( $attachment_url ) {
		$attachment_id = meteorite_get_attachment_id_from_url( $attachment_url );

		if ( ! empty ( $attachment_id ) ) {
			return wp_get_attachment_thumb_url( $attachment_id );
		} else {
			return $attachment_url;
		}
	}
endif;

/**
 * Needed for project alternative image in widgets
 *
 */
if ( ! function_exists('meteorite_get_attachment_image') ) :
	function meteorite_get_attachment_image( $attachment_url, $thumbSize ) {
		$attachment_id = meteorite_get_attachment_id_from_url( $attachment_url );

		if ( ! empty ( $attachment_id ) ) {
			return wp_get_attachment_image($attachment_id, $thumbSize);
		} else {
			return $attachment_url;
		}
	}
endif;


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

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

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

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

/**
 * Functions
 */
require get_template_directory() . '/inc/functions/loader.php';

/**
 * Widgets
 */
require get_template_directory() . '/inc/widgets/loader.php';

/**
* Custom styles through customizer.
*/
require get_template_directory() . '/inc/styles.php';

/**
* SVG for text with icon widget.
*/
require get_template_directory() . '/inc/svg.php';

/**
* Post options for pages
*/
require get_template_directory() . '/inc/page-options.php';

/**
 * Page builder support
 */
require get_template_directory() . '/inc/page-builder.php';

/**
 * Woocommerce
 */
require get_template_directory() . '/inc/woocommerce.php';

/**
 *TGM Plugin activation.
 */
require_once dirname( __FILE__ ) . '/plugins/class-tgm-plugin-activation.php';
 
add_action( 'tgmpa_register', 'meteorite_recommend_plugin' );
function meteorite_recommend_plugin() {
 
    $plugins = array(
        array(
            'name'               => 'Page Builder by SiteOrigin',
            'slug'               => 'siteorigin-panels',
            'required'           => false,
        ),
        array(
            'name'               => 'Terra Themes Tools',
            'slug'               => 'terra-themes-tools',
            'required'           => false,
        )
    );
 
    tgmpa( $plugins);
 
}