<?php
/*
===================================================================================================
WARNING! DO NOT EDIT THIS FILE OR ANY TEMPLATE FILES IN THIS THEME!

To make it easy to update your theme, you should not edit this file. Instead, you should create a
Child Theme first. This will ensure your template changes are not lost when updating the theme.

You can learn more about creating Child Themes here: http://codex.wordpress.org/Child_Themes

You have been warned! :)
===================================================================================================
*/
?>
<?php

// Define theme location for bundled plugins
if(!defined('WP_THEME_URL')) {
	define( 'WP_THEME_URL', get_template_directory_uri());
}
if(!defined('WP_THEME_DIR')) {
	define( 'WP_THEME_DIR', get_template_directory_uri());
}


// Attempt to modify php.ini settings to handle larger media uploads
@ini_set( 'upload_max_size' , '64M' );
@ini_set( 'post_max_size', '64M' );
@ini_set( 'max_execution_time', '300' );


// Add support for various awesome things
if(function_exists('add_theme_support')){
	add_theme_support('menus');
	add_theme_support('automatic-feed-links');
	add_theme_support('post-thumbnails');
}

if(function_exists('add_image_size')){
	add_image_size('slide', 924, 345, true);
	add_image_size('single', 608, 9999, false);
	add_image_size('archive', 250, 9999, false);
	add_image_size('admin', 120, 9999, false);
	add_image_size('thumb', 75, 75, true);
}


// Set content width
if ( !isset( $content_width ) ) {
	$content_width = 940;
}


// Register the scripts
function churchthemes_register() {
	wp_register_style( 'churchthemes-main', get_stylesheet_uri(), array(), CHURCHTHEMES_CURRENT_VERSION );
	wp_register_style( 'churchthemes-scheme', WP_THEME_DIR . '/lib/scripts/scheme.php', array( 'churchthemes-main' ), false );
	wp_register_style( 'churchthemes-admin', WP_THEME_DIR . '/lib/admin/css/admin.css', array(), false );
	wp_register_script( 'churchthemes-slider', WP_THEME_DIR . '/lib/scripts/jquery.flexslider.min.js', array( 'jquery' ), '2.1', true );
	wp_register_script( 'churchthemes-placeholder', WP_THEME_DIR . '/lib/scripts/jquery.placeholder.min.js', array( 'jquery' ), false, true );
	wp_register_script( 'churchthemes-main', WP_THEME_DIR . '/lib/scripts/main.min.js', array( 'jquery' ), CHURCHTHEMES_CURRENT_VERSION, true );
}
add_action( 'init', 'churchthemes_register' );


// Load the scripts
function churchthemes_enqueue_scripts() {
	global $is_ie;

	$theme_options = get_option( 'ct_theme_options' );
	$external_target = isset( $theme_options['external_target'] ) ? $theme_options['external_target'] : '_blank';

	$slide_settings = get_option( 'ct_slide_settings' );
	$visibility = isset( $slide_settings['visibility'] ) ? $slide_settings['visibility'] : null;
	$animation = isset( $slide_settings['animation'] ) ? $slide_settings['animation'] : 'slide';
	$direction = isset( $slide_settings['direction'] ) ? $slide_settings['direction'] : 'horizontal';
	$speed = ( isset( $slide_settings['speed'] ) && is_numeric( $slide_settings['speed'] ) ) ? $slide_settings['speed'] : 5;

	if ( is_singular() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}

	if ( is_front_page() && $visibility !== 'off' ) {
		wp_enqueue_script( 'churchthemes-slider' );
		wp_localize_script( 'churchthemes-main', 'churchthemes_slide_vars', array(
			'animation' => esc_attr( $animation ),
			'direction' => esc_attr( $direction ),
			'speed' => intval( $speed ) . '000',
		));
	}

	if ( !is_admin() && $is_ie ) {
		wp_enqueue_script( 'churchthemes-placeholder' );
	}

	if ( !is_admin() ) {
		wp_enqueue_style( 'churchthemes-main' );
		wp_enqueue_style( 'churchthemes-scheme' );
		wp_localize_script( 'churchthemes-main', 'churchthemes_global_vars', array(
			'external_target' => esc_attr( $external_target ),
		));
		wp_enqueue_script( 'churchthemes-main' );
	}
}
add_action( 'wp_enqueue_scripts', 'churchthemes_enqueue_scripts' );


// Load up styles in WP Admin area
function churchthemes_admin_enqueue_scripts() {
	if ( is_admin() ) {
		wp_enqueue_style('churchthemes-admin');
	}
}
add_action('admin_enqueue_scripts', 'churchthemes_admin_enqueue_scripts');


/* Hide WP version meta tag from header and generator tag from feeds
 * @return null
 * @filter the_generator
 */
function churchthemes_remove_wp_version_tag() {
	return null;
}
add_filter( 'the_generator', 'churchthemes_remove_wp_version_tag' );


/* Hide WP version strings from scripts and styles
 * @return {string} $src
 * @filter script_loader_src
 * @filter style_loader_src
 */
function churchthemes_remove_wp_version_strings( $src ) {
	global $wp_version;
	parse_str( parse_url( $src, PHP_URL_QUERY ), $query );
	if ( !empty( $query['ver'] ) && $query['ver'] === $wp_version ) {
		$src = remove_query_arg( 'ver', $src );
	}
	return $src;
}
add_filter( 'script_loader_src', 'churchthemes_remove_wp_version_strings' );
add_filter( 'style_loader_src', 'churchthemes_remove_wp_version_strings' );


// Only allow Reorder functionality on specific post types
function churchthemes_exclude_from_reorder() {
	$post_types = get_post_types();
	$allowed = array( 'post', 'page', 'ct_slide' );
	$excluded = array_diff( $post_types, $allowed );
	return $excluded;
}
add_filter( 'reorder_exclude', 'churchthemes_exclude_from_reorder' );


// Register theme menus
if(!function_exists('ct_register_menus')) {
	function ct_register_menus() {
		register_nav_menus(
			array(
				'primary' => __('Primary Nav Menu'),
				'footer' => __('Footer Nav Menu')
			)
		);
	}
	add_action( 'init', 'ct_register_menus' );
}


// Primary nav menu with fallback
if(!function_exists('ct_primary_nav_menu')) {
	function ct_primary_nav_menu() {
	    if(function_exists('wp_nav_menu'))
	    	wp_nav_menu(array(
				'theme_location' => 'primary',
				'container' => 'div',
				'container_class' => 'navbar',
				'menu_class' => '',
				'menu_id' => false,
				'depth' => '3',
				'fallback_cb' => 'ct_primary_nav_fallback'
			));
	    else
	        ct_primary_nav_fallback();
	}
	function ct_primary_nav_fallback() {
	    wp_page_menu('menu_class=navbar');
	}
}


// Footer nav menu
if(!function_exists('ct_footer_nav_menu')) {
	function ct_footer_nav_menu() {
	    if(function_exists('wp_nav_menu'))
	    	wp_nav_menu(array(
				'theme_location' => 'footer',
				'container' => false,
				'menu_class' => 'footer_nav',
				'menu_id' => false,
				'depth' => '1'
			));
	}
}


/**
 * Display a the excerpt with unique length and more text at each instance.
 * Choose whether or not length refers to word count or character count, for
 * more fine-grained control over the size of the rendered excerpt.
 * @param {mixed} $args A WordPress-style query string or array consisting of 'length' (integer), 'more' (str), and 'unit' ('word' || 'char')
 * @return {string} The excerpt
 */
function churchthemes_get_the_excerpt( $args ){
	static $cache = array(); // function cache to improve create_function eval performance
	$all_units = array('word', 'char',);
	$defaults = array(
		'unit' => 'word',
		'wpautop' => true,
	);
	extract(wp_parse_args($args, $defaults), EXTR_SKIP); // => $length, $more, $unit

	// Filter the excerpt length, either in terms of word count or char count
	if( isset($length) ){
		assert( is_numeric($length) );
		assert( in_array( $unit, $all_units ) );
		if( $unit === 'word' ){
			$func = sprintf('return %d;', var_export($length, true));
			if( isset($cache[$func]) ){
				$length_filter = $cache[$func];
			}
			else {
				$cache[$func] = $length_filter = create_function('', $func);
			}
			add_filter( 'excerpt_length', $length_filter );
		}
		else if( $unit === 'char' ) {
			$params = '$text, $num_words, $more, $original_text';
			$func = sprintf('return substr(wp_strip_all_tags($original_text), 0, %d) . $more;', $length);
			if( isset($cache[$func]) ){
				$wp_trim_words_filter = $cache[$func];
			}
			else {
				$cache[$func] = $wp_trim_words_filter = create_function($params, $func);
			}
			add_filter( 'wp_trim_words', $wp_trim_words_filter, 10, 4 ); // @todo This does wpautop??? Thus requiring force_balance_tags
		}
	}

	// Customize the excerpt_more if specified
	if( isset($more) ){
		$func = sprintf('return %s;', var_export($more, true));
		if( isset($cache[$func]) ){
			$more_filter = $cache[$func];
		}
		else {
			$cache[$func] = $more_filter = create_function('', $func);
		}
		add_filter('excerpt_more', $more_filter);
	}

	$excerpt = get_the_excerpt();

	// Remove filters that were added
	if( isset($length_filter) ){
		remove_filter( 'excerpt_length', $length_filter );
	}
	if( isset($more_filter) ){
		remove_filter( 'excerpt_more', $more_filter );
	}
	if( isset($wp_trim_words_filter) ){
		remove_filter( 'wp_trim_words', $wp_trim_words_filter );
	}

	if( $wpautop ){ // @todo Should this apply the_content filters? Should it even wpautop by default?
		$excerpt = wpautop($excerpt);
	}
	return $excerpt;
}


/**
 * Shortcut for: echo churchthemes_get_the_excerpt($args)
 * @see churchthemes_get_the_excerpt()
 */
function churchthemes_the_excerpt( $args ){
	echo churchthemes_get_the_excerpt( $args );
}


if(!function_exists('churchthemes_comment')) {
	/**
	 * Template for comments and pingbacks.
	 */
	function churchthemes_comment( $comment, $args, $depth ) {
		$GLOBALS['comment'] = $comment;
		switch ( $comment->comment_type ) :
			case '' :
		?>
		<li <?php comment_class(); ?> id="li-comment-<?php comment_ID(); ?>">
			<div id="comment-<?php comment_ID(); ?>" class="single-comment">
			<div class="comment-author vcard">
				<?php echo get_avatar( $comment, 40 ); ?>
				<?php printf( __( '%s <span class="says">says:</span>', 'churchthemes' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
			</div><!-- .comment-author .vcard -->
			<?php if ( $comment->comment_approved == '0' ) : ?>
				<em class="comment-awaiting-moderation"><?php _e( 'Your comment is awaiting moderation.', 'churchthemes' ); ?></em>
				<br />
			<?php endif; ?>

			<div class="comment-meta commentmetadata"><a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
				<?php
					/* translators: 1: date, 2: time */
					printf( __( '%1$s at %2$s', 'churchthemes' ), get_comment_date(),  get_comment_time() ); ?></a><?php edit_comment_link( __( '(Edit)', 'churchthemes' ), ' ' );
				?>
			</div><!-- .comment-meta .commentmetadata -->

			<div class="comment-body"><?php comment_text(); ?></div>

			<div class="reply">
				<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
			</div><!-- .reply -->
		</div><!-- #comment-##  -->

		<?php
				break;
			case 'pingback'  :
			case 'trackback' :
		?>
		<li class="post pingback">
			<p><?php _e( 'Pingback:', 'churchthemes' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( __( '(Edit)', 'churchthemes' ), ' ' ); ?></p>
		<?php
				break;
		endswitch;
	}
}


if(!function_exists('ct_posted_by')) {
	/**
	 * Prints HTML with meta information for the current post date/time and author.
	 */
	function ct_posted_by() {
		printf( __( '<span class="%1$s">Posted by %2$s</span><br /><br />', 'churchthemes' ),
			'meta-prep-author',
			sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
				get_author_posts_url( get_the_author_meta( 'ID' ) ),
				sprintf( esc_attr__( 'View all posts by %s', 'churchthemes' ), get_the_author() ),
				get_the_author()
			)
		);
	}
}


// Assign unique classes to first and last menu items
if(!function_exists('ct_first_last_class')) {
	function ct_first_last_class($output) {
		$output = preg_replace('/class="menu-item/', 'class="first menu-item', $output, 1);
		$output = substr_replace($output, 'class="last menu-item', strripos($output, 'class="menu-item'), strlen('class="menu-item'));
		return $output;
	}
	add_filter( 'wp_nav_menu', 'ct_first_last_class' );
}


// Returns the URL of the current page
if(!function_exists('ct_get_current_url')) {
	function ct_get_current_url() {
		$url  = @( $_SERVER["HTTPS"] != 'on' ) ? 'http://'.$_SERVER["SERVER_NAME"] :  'https://'.$_SERVER["SERVER_NAME"];
		$url .= ( $_SERVER["SERVER_PORT"] !== '80' ) ? ":".$_SERVER["SERVER_PORT"] : null;
		$url .= $_SERVER["REQUEST_URI"];
		return esc_url_raw( $url );
	}
}


// Flush rewrite rules
if(!function_exists('ct_flush_rules')) {
	function ct_flush_rules() {
		global $wp_rewrite;
		$wp_rewrite->flush_rules();
	}
	add_filter('register_activation_hook', 'ct_flush_rules');
}


// Awesome Pagination
if(!function_exists('pagination')) {
	function pagination($pages = '', $range = 3) {
		$showitems = ($range * 2)+1;

		global $paged;
		if(empty($paged)) $paged = 1;

		if($pages == '') {
			global $wp_query;
			$pages = $wp_query->max_num_pages;
			if(!$pages) {
				$pages = 1;
			}
		}

		if(1 != $pages) {
			echo "<ul class=\"pagination\">";
			if($paged > 2 && $paged > $range+1 && $showitems < $pages) echo "<li><a href=\"".get_pagenum_link(1)."\">&lt;&lt; First</a></li>";
			if($paged > 1 && $showitems < $pages) echo "<li class=\"previous\"><a href=\"".get_pagenum_link($paged - 1)."\">&lt; Prev</a></li>";

			for ($i=1; $i <= $pages; $i++) {
				if (1 != $pages &&( !($i >= $paged+$range+1 || $i <= $paged-$range-1) || $pages <= $showitems )) {
					echo ($paged == $i)? "<li class=\"active\"><a href=\"javascript:void(0)\">".$i."</a></li>":"<li><a href=\"".get_pagenum_link($i)."\">".$i."</a></li>";
				}
			}

			if ($paged < $pages && $showitems < $pages) echo "<li class=\"next\"><a href=\"".get_pagenum_link($paged + 1)."\">Next &gt;</a></li>";
			if ($paged < $pages-1 &&  $paged+$range-1 < $pages && $showitems < $pages) echo "<li><a href=\"".get_pagenum_link($pages)."\">Last &gt;&gt;</a></li>";
			echo "</ul>\n";
			echo "<p class=\"pagecount\">Page ".$paged." of ".$pages."</p>\n";
		}
	}
}


// Find the real image path for TimThumb on multisite
function get_realpath($img_src) {
    global $blog_id;
    if (isset($blog_id) && $blog_id > 0) {
        $imageParts = explode('/files/', $img_src);
        if (isset($imageParts[1])) {
            $img_src = '/blogs.dir/'.$blog_id.'/files/'.$imageParts[1];
        }
    }
    return $img_src;
}


// Add support for TimThumb
if(!function_exists('get_thumbnail')) {
	function get_thumbnail($id, $w, $h) {
		global $post;
		$src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 300,300 ), false, '');
		$path = get_realpath($src[0]);
		$dir = WP_THEME_DIR;
		if(empty($w)) $w = '80';
		if(empty($h)) $h = '80';
		if(!empty($src)):
			$image = $dir.'/lib/scripts/timthumb.php?src='.$path.'&w='.$w.'&h='.$h;
			return $image;
		else:
			return null;
		endif;
	}
}


// Convert HEX colors to RGB
function churchthemes_hex_to_rgb( $color ) {
	if ( $color[0] == '#' ) {
		$color = substr( $color, 1 );
	}
	if ( strlen( $color ) == 6 ) {
		list( $r, $g, $b ) = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
	} elseif ( strlen( $color ) == 3 ) {
		list( $r, $g, $b ) = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
	} else {
		return false;
	}
	$r = hexdec( $r );
	$g = hexdec( $g );
	$b = hexdec( $b );
	return array( 'red' => $r, 'green' => $g, 'blue' => $b );
}


// Add support for entry views
if(!function_exists('ct_add_entry_views')) {
	function ct_add_entry_views() {
		add_post_type_support('post', array('entry-views'));
		add_post_type_support('page', array('entry-views'));
		add_post_type_support('ct_slide', array('entry-views'));
	}
	add_action( 'init', 'ct_add_entry_views' );
}


// Register custom post column for views and allow sorting
if(!function_exists('ct_post_edit_columns')) {
	function ct_post_edit_columns( $columns ) {
		$columns['post_views'] = __( 'Views', 'churchthemes' );
		return $columns;
	}
	add_filter( 'manage_edit-post_columns', 'ct_post_edit_columns' );
}

if(!function_exists('ct_post_custom_columns')) {
	function ct_post_custom_columns( $column_name, $post_id ) {
		if ( 'post_views' != $column_name )
			return;
		$views = get_post_meta($post_id, 'Views', true);
		if ( !$views )
			$views = __( 'none', 'churchthemes' );
		echo $views;
	}
	add_action( 'manage_posts_custom_column', 'ct_post_custom_columns', 10, 2 );
}

if(!function_exists('ct_post_views_column_register_sortable')) {
	function ct_post_views_column_register_sortable($columns) {
		$columns['post_views'] = 'Views';
		return $columns;
	}
	add_filter('manage_edit-post_sortable_columns', 'ct_post_views_column_register_sortable');
}

if(!function_exists('ct_post_column_orderby_views')) {
	function ct_post_column_orderby_views($vars) {
		if (isset( $vars['orderby']) && 'Views' == $vars['orderby']) {
			$vars = array_merge( $vars, array(
				'meta_key' => 'Views',
				'orderby' => 'meta_value_num'
			));
		}
		return $vars;
	}
	add_filter( 'request', 'ct_post_column_orderby_views' );
}


// Create custom Page Options
$page_options_meta = new WPAlchemy_MetaBox(
	array(
		'id' => '_page_options',
		'types' => array('page'),
		'autosave' => TRUE,
		'title' => 'Page Options',
		'template' => TEMPLATEPATH . '/lib/admin/page-options.php',
	)
);


// Allow shortcodes in text widgets
add_filter('widget_text', 'do_shortcode');


// Add the shortcode handler for YouTube videos
if(!function_exists('ct_youtube_shortcode')) {
	function ct_youtube_shortcode($atts, $content = null) {

		extract( shortcode_atts(
			array(
				"id" => '',
				"width" => 608,
				"height" => 375,
				"hd" => 'false',
				"autoplay" => 'false',
				"related" => 'true',
				"showinfo" => 'true',
				"controls" => 'true',
				"https" => 'false'
			),
			$atts
		));

		$autoplay = ( $autoplay === 'true' ) ? '&autoplay=1' : null;
		$related = ( $related === 'false' ) ? '&rel=0' : null;
		$showinfo = ( $showinfo === 'false' ) ? '&showinfo=0' : null;
		$controls = ( $controls === 'false' ) ? '&controls=0' : null;
		$https = ( $https === 'true' ) ? 's' : null;

		if ( $hd === 'true' ) {
			return '<object class="ct-video-object" width="'.$width.'" height="'.$height.'"><param name="movie" value="http'.$https.'://www.youtube.com/v/'.$id.'&wmode=opaque&hd=1'.$autoplay.$related.$showinfo.$controls.'"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="wmode" value="transparent"><embed src="http'.$https.'://www.youtube.com/v/'.$id.'&wmode=opaque&hd=1'.$autoplay.$related.$showinfo.$controls.'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" width="'.$width.'" height="'.$height.'"></embed></object>';
		}
		else {
			return '<iframe width="'.$width.'" height="'.$height.'" src="http'.$https.'://www.youtube.com/embed/'.$id.'?wmode=opaque'.$autoplay.$related.$showinfo.$controls.'" frameborder="0" allowfullscreen></iframe>';
		}
	}
	add_shortcode('youtube', 'ct_youtube_shortcode');

	function ct_youtube_shortcode_button() {
	   // Don't bother doing this stuff if the current user lacks permissions
	   if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
	     return;

	   // Add only in Rich Editor mode
	   if ( get_user_option('rich_editing') == 'true') {
	     add_filter('mce_external_plugins', 'ct_add_youtube_tinymce_plugin');
	     add_filter('mce_buttons', 'ct_register_youtube_button');
	   }
	}

	function ct_register_youtube_button($buttons) {
	   array_push($buttons, "|", "ct_youtube_button");
	   return $buttons;
	}

	// Load the TinyMCE plugin : editor_plugin.js (wp2.5)
	function ct_add_youtube_tinymce_plugin($plugin_array) {
	   $plugin_array['ct_youtube_button'] = WP_THEME_DIR . '/lib/admin/scripts/editor_plugin.js';
	   return $plugin_array;
	}

	function ct_youtube_refresh_mce($ver) {
	  $ver += 3;
	  return $ver;
	}

	add_filter('tiny_mce_version', 'ct_youtube_refresh_mce');
	add_action('init', 'ct_youtube_shortcode_button');
}


// Add the shortcode handler for Vimeo videos
if(!function_exists('ct_vimeo_shortcode')) {
	function ct_vimeo_shortcode($atts, $content = null) {

		$theme_options = get_option('ct_theme_options');
		$ct_main_color = $theme_options['main_color'];
		$ct_main_color = str_replace('#', '', $ct_main_color);
		if(empty($ct_main_color)): $ct_main_color = '00adef'; endif;

		extract( shortcode_atts(
			array(
				"id" => '',
				"width" => '608',
				"height" => '342',
				"title" => 'false',
				"byline" => 'false',
				"portrait" => 'false',
				"color" => $ct_main_color
			),
			$atts
		));

		$title = ( $title == 'true' ) ? 1 : 0;
		$byline = ( $byline == 'true' ) ? 1 : 0;
		$portrait = ( $portrait == 'true' ) ? 1 : 0;

		return '<iframe src="http://player.vimeo.com/video/'.esc_attr($id).'?title='.esc_attr($title).'&amp;byline='.esc_attr($byline).'&amp;portrait='.esc_attr($portrait).'&amp;color='.esc_attr($color).'" width="'.intval($width).'" height="'.intval($height).'" frameborder="0"></iframe>';
	}
	add_shortcode('vimeo', 'ct_vimeo_shortcode');

	function ct_add_vimeo_shortcode_button() {
		// Don't bother doing this stuff if the current user lacks permissions
		if ( ! current_user_can('edit_posts') && ! current_user_can('edit_pages') )
			return;

		// Add only in Rich Editor mode
		if ( get_user_option('rich_editing') == 'true') {
			add_filter('mce_external_plugins', 'ct_add_vimeo_tinymce_plugin');
			add_filter('mce_buttons', 'ct_register_vimeo_button');
		}
	}

	function ct_register_vimeo_button($buttons) {
		array_push($buttons, "|", "ct_vimeo_button");
		return $buttons;
	}

	// Load the TinyMCE plugin : editor_plugin.js (wp2.5)
	function ct_add_vimeo_tinymce_plugin($plugin_array) {
		$plugin_array['ct_vimeo_button'] = WP_THEME_DIR . '/lib/admin/scripts/editor_plugin.js';
		return $plugin_array;
	}

	function ct_vimeo_refresh_mce($ver) {
		$ver += 3;
		return $ver;
	}

	// init process for button control
	add_filter('tiny_mce_version', 'ct_vimeo_refresh_mce');
	add_action('init', 'ct_add_vimeo_shortcode_button');
}


// CSS3 Button shortcode
if(!function_exists('ct_css3_button_shortcode')) {
	function ct_css3_button_shortcode($atts, $content = null) {
		extract(shortcode_atts(
			array(
				'text' => 'menu_order',
				'url' => '',
				'target' => '_self',
				'title' => '',
				'rel' => '',
			), $atts));
		return "<p><a href=\"$url\" class=\"button\" target=\"$target\">$text</a></p>";
	}
	add_shortcode('button', 'ct_css3_button_shortcode');
}


// Build an array of meta values from all posts in a specified post type
function churchthemes_get_meta_values( $key = null, $type = null, $status = null ) {
	global $wpdb;
	if ( !$key ) {
		return;
	}
	$meta_values = $wpdb->get_col( $wpdb->prepare( "
		SELECT DISTINCT pm.meta_value FROM {$wpdb->postmeta} pm
		LEFT JOIN {$wpdb->posts} p ON p.ID = pm.post_id
		WHERE pm.meta_key = '%s'
		AND p.post_type = '%s'
		AND p.post_status = '%s'
	", $key, $type, $status ) );
	return $meta_values;
}


// Add more Appearance options to the Admin Bar
function churchthemes_extend_admin_bar() {
	global $wp_admin_bar, $wpdb;

	if (!is_super_admin() || !is_admin_bar_showing())
		return;

	$wp_admin_bar->add_menu(
		array(
			'parent' => 'site-name',
			'title' => __('Theme Options', 'churchthemes'),
			'id' => 'theme-options',
			'href' => home_url('/wp-admin/themes.php?page=theme-options')
		)
	);
	$wp_admin_bar->add_menu(
		array(
			'parent' => 'site-name',
			'title' => __('Social Footer', 'churchthemes'),
			'id' => 'social-footer',
			'href' => home_url('/wp-admin/themes.php?page=social-footer')
		)
	);
	$wp_admin_bar->add_menu(
		array(
			'parent' => 'site-name',
			'title' => __('Sidebars', 'churchthemes'),
			'id' => 'sidebars',
			'href' => home_url('/wp-admin/themes.php?page=sidebars')
		)
	);
}
add_action('admin_bar_menu', 'churchthemes_extend_admin_bar', 1000);
