<?php
/**
 * Functions.php contains all the core functions for your theme to work properly.
 * Please do not edit this file!!
 *
 * @package Ultra WordPress Theme
 * @since 1.0
 * @author AJ Clarke : http://wpexplorer.com
 * @copyright Copyright (c) 2012, AJ Clarke
 * @link http://wpexplorer.com
 * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 */


/**
* Define Constants
* @Since 1.0
*/
define( 'WPEX_JS_DIR', get_template_directory_uri().'/js' );
define( 'WPEX_CSS_DIR', get_template_directory_uri().'/css' );
define( 'WPEX_PLUGINS', get_template_directory_uri().'/functions/plugins' );



/*
 * Theme options
 * @since 1.0
*/

// Post Type & Taxonomy helper functions
// Load before Options panel so we can use them in our theme options
require_once ( get_template_directory() .'/functions/post_types_taxonomies/post-type-helpers.php' );

//Typography Options
require_once( get_template_directory() .'/functions/typography.php' );

// Load core Options Framework Plugin
if ( !function_exists( 'optionsframework_init' ) ) {
	define( 'OPTIONS_FRAMEWORK_DIRECTORY', get_template_directory_uri() . '/admin/' );
	require_once ( get_template_directory() . '/admin/options-framework.php' );
} else {
	// Display message if the OF plugin is being used
	function disable_of_message() {
		global $pagenow;
		if ( current_user_can('install_plugins') && ( $pagenow == 'themes.php' || $pagenow == 'plugins.php' ) ) {
			echo '<div id="message" class="error"><p>'. __('Please DISABLE The Options Framework Plugin while using this theme to prevent issues, it\'s already built-in.','wpex') .'</p></div>';
		}
	}  
	add_action('admin_notices', 'disable_of_message');
}

// Set default values for Theme Options
if ( !function_exists( 'of_get_option' ) ) {
	function of_get_option($name, $default = false) {
		$optionsframework_settings = get_option('optionsframework');
		$option_name = $optionsframework_settings['id'];
		if ( get_option($option_name) ) {
			$options = get_option($option_name);
		}
		if ( isset($options[$name]) ) {
			return $options[$name];
		} else {
			return $default;
		}
	}
}



/**
* Include theme functions
* @since 1.0
*/

// TGM Plugin Activation => https://github.com/thomasgriffin/TGM-Plugin-Activation
require_once ( get_template_directory() .'/functions/recommend-plugins.php' );


// Register Custom Post Types & Taxonomies & Create Some Functions For Them
if ( of_get_option('enable_portfolio','1') == '1' ) {
	// Register portfolio post type and taxonomies
	require_once ( get_template_directory() .'/functions/post_types_taxonomies/register-portfolio.php' );
}
if ( of_get_option('enable_services','1') == '1' ) {
	// Register services post type and taxonomies
	require_once ( get_template_directory() .'/functions/post_types_taxonomies/register-services.php' );
}
if ( of_get_option('enable_staff','1') == '1' ) {
	// Register staff post type and taxonomies
	require_once ( get_template_directory() .'/functions/post_types_taxonomies/register-staff.php' );
}
if ( of_get_option('enable_faq','1') == '1' ) {
	// Register FAQ post type and taxonomies
	require_once ( get_template_directory() .'/functions/post_types_taxonomies/register-faq.php' );
}
if ( of_get_option('enable_testimonials','1') == '1' ) {
	// Register testimonials post type and taxonomies
	require_once ( get_template_directory() .'/functions/post_types_taxonomies/register-testimonials.php' );
}


// Set custom labels for post types and taxonomies based on theme options
require_once ( get_template_directory() .'/functions/post_types_taxonomies/post-type-labels.php' );
require_once ( get_template_directory() .'/functions/post_types_taxonomies/taxonomies-labels.php' );


// Load CSS and JS
require_once( get_template_directory() .'/functions/scripts.php' );


// Define widget areas and add custom widgets
require_once( get_template_directory() .'/functions/widgets/widget-areas.php' );
require_once( get_template_directory() .'/functions/widgets/widget-video.php' );
require_once( get_template_directory() .'/functions/widgets/widget-slider.php' );
require_once( get_template_directory() .'/functions/widgets/widget-flickr.php' );
require_once( get_template_directory() .'/functions/widgets/widget-recent-posts-thumb-grid.php' );
require_once( get_template_directory() .'/functions/widgets/widget-testimonials.php' );
require_once( get_template_directory() .'/functions/widgets/widget-posts-thumbnails.php' );


// Image resizing function ( Aqua Resizer )
require_once( get_template_directory() .'/functions/aqua-resizer.php' );

// Add retina support for featured images
if ( of_get_option('retina_support','0' ) == '1' ) {
	require_once( get_template_directory() .'/functions/aqua-resizer-ratio-check.php' ); 
}


// Load in Admin only
if( is_admin() ) {
	require_once( get_template_directory() .'/functions/gallery-metabox/gallery-metabox.php' );
	require_once( get_template_directory() .'/functions/meta/post-meta.php' );
	require_once( get_template_directory() .'/functions/meta/page-meta.php' );
	if ( class_exists( 'Symple_Portfolio_Post_Type' ) ) {
		require_once( get_template_directory() .'/functions/meta/portfolio-meta.php' );
	}
	if ( class_exists( 'Symple_Services_Post_Type' ) ) {
		require_once( get_template_directory() .'/functions/meta/services-meta.php' );
	}
	if ( class_exists( 'Symple_Staff_Post_Type' ) ) {
		require_once( get_template_directory() .'/functions/meta/staff-meta.php' );
	}
	if ( class_exists( 'Symple_FAQ_Post_Type' ) ) {
		require_once( get_template_directory() .'/functions/meta/faq-meta.php' );
	}
	if ( class_exists( 'Symple_Testimonials_Post_Type' ) ) {
		require_once( get_template_directory() .'/functions/meta/testimonials-meta.php' );
	}
}

// Load outside of admin only
else {
	require_once( get_template_directory() .'/functions/custom-css.php' ); // Output custom CSS in <head> based on OF options
	require_once( get_template_directory() .'/functions/pagination.php' ); // Pagination function
	require_once( get_template_directory() .'/functions/comments-callback.php' ); // Default comment output function
	require_once( get_template_directory() .'/functions/posts-per-page.php' ); // used to change posts per page on portfolio/services archives	
	require_once( get_template_directory() .'/functions/default-img-sizes.php' ); // Define default image cropping sizes
}

// Custom Theme-Specific Shortcodes
require_once( get_template_directory() .'/functions/shortcodes/blog-shortcode.php' );
if( of_get_option('custom_wp_gallery','1') == '1' ) {
	require_once( get_template_directory() .'/functions/shortcodes/wp-gallery.php' ); // Replaces default WP gallery
}
if ( class_exists( 'Symple_Portfolio_Post_Type' ) ) {
	require_once( get_template_directory() .'/functions/shortcodes/portfolio-shortcode.php' );
}
if ( class_exists( 'Symple_Services_Post_Type' ) ) {
	require_once( get_template_directory() .'/functions/shortcodes/services-shortcode.php' );
}
if ( class_exists( 'Symple_FAQ_Post_Type' ) ) {
	require_once( get_template_directory() .'/functions/shortcodes/faq-shortcode.php' );
}
if ( class_exists( 'Symple_Staff_Post_Type' ) ) {
	require_once( get_template_directory() .'/functions/shortcodes/staff-shortcode.php' );
}
if ( class_exists( 'Symple_Testimonials_Post_Type' ) ) {
	require_once( get_template_directory() .'/functions/shortcodes/testimonials-shortcode.php' );
}
require_once( get_template_directory() .'/functions/shortcodes/carousel-shortcode.php' );




/**
* Theme Setup
* @Since 1.0
*/

// Add Post Formats Support
add_theme_support( 'post-formats', array( 'video', 'gallery', 'image', 'link' ) );


//Localization support
load_theme_textdomain( 'wpex', get_template_directory() .'/lang' );


// Attach functions to the after_setup_theme hook
if ( !function_exists('wpex_theme_setup') ) {
	
	add_action( 'after_setup_theme', 'wpex_theme_setup' );
	function wpex_theme_setup() {
	
		//default width of primary content area
		$content_width = 1000;
		
		//theme support
		add_theme_support('automatic-feed-links');
		add_theme_support('custom-background');
		add_theme_support('post-thumbnails');
		
		//register navigation menus
		if ( ! function_exists ( 'wpex_register_nav_menus' ) ) {
			function wpex_register_nav_menus ( ) {
				$wpex_menus = array(
					'top_menu' => __( 'Top Right', 'wpex' ),
					'main_menu' => __( 'Main', 'wpex' ),
					'footer_menu' => __( 'Footer', 'wpex' )
				);
				$wpex_menus = apply_filters ( 'wpex_nav_menus', $wpex_menus );
				register_nav_menus ( $wpex_menus );
			}
		}
		wpex_register_nav_menus();
	
	}
}



/**
* Change post types for the gallery-metabox plugin
* @since 1.0
*/
if ( !function_exists( 'custom_be_gallery_metabox_post_types' ) ) :
	function custom_be_gallery_metabox_post_types( $classes ) {
			return array('portfolio', 'services');
	}
	add_filter( 'be_gallery_metabox_post_types', 'custom_be_gallery_metabox_post_types' );
endif;




/**
* Change default read more style
* @since 1.0
*/
if ( !function_exists( 'wpex_new_excerpt_more' ) ) :
	function wpex_new_excerpt_more($more) {
		global $post;
		return '...';
	}
	add_filter('excerpt_more', 'wpex_new_excerpt_more');
endif;



/**
* Add home page option to WordPress Menu
* @since 1.0
*/
if ( !function_exists( 'home_page_menu_args' ) ) :
	function home_page_menu_args( $args ) {
		$args['show_home'] = true;
		return $args;
	}
	add_filter( 'wp_page_menu_args', 'home_page_menu_args' );
endif;




/**
* Remove anchor from WordPress read more links
* @since 1.0
*/
if ( !function_exists('wpex_remove_more_anchor') ) {
	if ( !function_exists( 'wpex_remove_more_anchor' ) ) :
		function wpex_remove_more_anchor($link) {
			 $offset = strpos($link, '#more-');
			 if ($offset) {
				  $end = strpos($link, '"',$offset);
			 }
			 if ($end) {
				  $link = substr_replace($link, '', $offset, $end-$offset);
			 }
			 return $link;
		}
		add_filter('the_content_more_link', 'wpex_remove_more_anchor');
	endif;
}




/**
* Enable more post editor buttons
* @since 1.0
*/
if ( !function_exists('wpex_enable_mce_buttons') ) {
	add_filter("mce_buttons_2", "wpex_enable_mce_buttons");
	function wpex_enable_mce_buttons($buttons) {
		$buttons[] = 'fontsizeselect';
		return $buttons;
	}
}




/**
* Custom User profile settings
* Enables more fields (twitter,facebook,etc)
* @since 1.0
*/
if ( !function_exists('wpex_add_user_fields') ) {
	add_filter( 'user_contactmethods', 'wpex_add_user_fields', 10, 1 );
	function wpex_add_user_fields( $contactmethods ) {
		
	  // Add Facebook
	  if ( !isset( $contactmethods['facebook'] ) )
		$contactmethods['facebook'] = 'Facebook';
		
	// Add GoglePlus
	if ( !isset( $contactmethods['googleplus'] ) )
		$contactmethods['googleplus'] = 'Google+';
		
	// Add GoglePlus
	if ( !isset( $contactmethods['pinterest'] ) )
		$contactmethods['pinterest'] = 'Pinterest';
	
	  return $contactmethods;
	}
}



/**
* Return an array of social links
* @since 1.0
*/
if ( !function_exists('wpex_get_social') ) {
	function wpex_get_social() {
		$social_icons = array( 
				'twitter' => 'twitter',
				'facebook' => 'facebook',
				'google' => 'google',
				'dribbble' => 'dribbble', 
				'behance' => 'behance',
				'lastfm' => 'lastfm',
				'linkedin' => 'linkedin',
				'pinterest' => 'pinterest',
				'tumblr' => 'tumblr',
				'vimeo' => 'vimeo',
				'youtube' => 'youtube',
				'behance' => 'behance',
				'yahoo' => 'yahoo',
				'flickr' => 'flickr',
				'picasa' => 'picasa',
				'forrst' => 'forrst',
				'yelp' => 'yelp',
				'rss' => 'rss',
			);
		return apply_filters('wpex_get_social', $social_icons);
	}
}


/**
* Returns an array of font awesome icon
* @since 1.0
*/
function wpex_get_awesome_icons() {
	
		//define array of icons
		$awesome_icons = array('Select','beaker','bell','bolt','bookmark-empty','briefcase','bullhorn','caret-down','caret-left','caret-right','caret-up','certificate','check-empty','circle-arrow-down','circle-arrow-left','circle-arrow-right','circle-arrow-up','cloud','columns','comment-alt','comments-alt','copy','credit-card','cut','dashboard','envelope-alt','eye-open','facebook','filter','fullscreen','github','globe','google-plus-sign','group','hand-down','hand-left','hand-right','hand-up','hdd','legal','link','linkedin','list-ol','list-ul','magic','money','paper-clip','paste','phone-sign','phone','pinterest-sign','pinterest','reorder','rss','save','sign-blank','sitemap','sort-down','sort-up','sort','strikethough','table','tasks','truck','twitter','umbrella','underline','undo','unlock','user-md','wrench','music','search','envelope','heart', 'star','user','film','ok','remove','zoom-in','zoom-out','off','signal','trash', 'home','file','time', 'download','inbox', 'repeat','refresh','flag','headphones','qrcode','tag','tags','book','bookmark','print','camera','list','facetime-video','picture','pencil','map-marker', 'tint','edit', 'share','check','move','play','plus-sign', 'minus-sign','ok-sign','question-sign','info-sign', 'screenshot','remove-circle','ok-circle','ban-circle','plus','minus','asterisk','exclamation-sign','gift','leaf','fire','warning-sign','plane','calendar','random','comment','magnet','shopping-cart','folder-open','folder-close','bar-chart','cogs','external-link','pushpin','key','thumbs-up','comments','trophy','upload-alt','upload','fire','cloud-download','cloud-upload','lightbulb','exchange','belt-alt','beer','coffee','food','fighter-jet','user-md',' stethoscope','suitcase','building','hospital','ambulance','medkit','h-sign','plus-sign-alt','spinner','angle-left','angle-right','angle-up','angle-down','cicle-blank','circle','desktop','laptop','tablet','mobile-phone','quote-left','quote-right','reply','github-alt','close-alt','open-alt');
		
		//set keys equal to values
		$awesome_icons = array_combine($awesome_icons, $awesome_icons);

		//return array
		return apply_filters('wpex_get_awesome_icons', $awesome_icons);
}



/**
* Adds extra classes to #site-wrap div
* Used for header styles & sidebar locations
* @since 1.0
*/

if ( !function_exists('wpex_sitewrap_classes') ) {
	function wpex_sitewrap_classes() {
		global $post;
		
		$header_classes = 'header-'. of_get_option('header_style', 'one') .'';
		
		$header_classes .= ' '; // Add a space between items
		
		if ( $post && !is_author() ) {
			$post_layout = get_post_meta ( $post->ID, 'wpex_post_layout', true );
			if ( $post_layout ) {
				if ( $post_layout == 'Full Width' ) {
					$header_classes .= 'full-width-post';
				} elseif ( $post_layout == 'Left Sidebar' ) {
					$header_classes .= 'left-sidebar-post post-with-sidebar';
				} else {
					$header_classes .= 'right-sidebar-post post-with-sidebar';
				}
			} else {
				if (  is_singular('portfolio') ) {
					$header_classes .= 'full-width-post';
				} else {
					$header_classes .= 'right-sidebar-post post-with-sidebar';
				}
			}
		}
		
		return $header_classes;
	}
}


/**
* Featured Image Overlay Function
* @since 1.0
*/

if ( !function_exists('wpex_featured_image_overlay') ) {
	function wpex_featured_image_overlay() {
		global $post;
		$overlay = get_post_meta( $post->ID, 'wpex_page_featured_img_overlay', true );
		if ( $overlay !== '' ) {
			echo '<div id="page-featured-img-overlay">'. do_shortcode( wpautop( htmlspecialchars_decode( $overlay ) ) ) .'
			</div><!-- #page-featured-img-overlay -->';
		} 
	}
}

/**
* Responsive Oembeds
* @since 1.0
*/
add_filter( 'embed_oembed_html', 'wpex_embed_oembed_html', 99, 4);
if ( !function_exists('wpex_embed_oembed_html') ) {
	function wpex_embed_oembed_html($html, $url, $attr, $post_id) {
		return '<div class="responsive-video-wrap entry-video">' . $html . '</div>';
	}
}