* @copyright Copyright (c) 2013, Nicolas GUILLAUME * @link http://themesandco.com/customizr * @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html */ if ( ! class_exists( 'TC_slider' ) ) : class TC_slider { static $instance; function __construct () { self::$instance =& $this; add_action( '__after_header' , array( $this , 'tc_slider_display' )); }//end of construct /** * Get slides from option or default * Returns and array of slides with data * * @package Customizr * @since Customizr 3.0.15 * */ function tc_get_slides( $slider_name_id, $img_size ) { //returns the default slider if requested if ( 'demo' == $slider_name_id ) return apply_filters( 'tc_default_slides', TC_init::$instance -> default_slides ); //if not demo, we get slides from options $all_sliders = tc__f('__get_option' , 'tc_sliders'); $saved_slides = ( isset($all_sliders[$slider_name_id]) ) ? $all_sliders[$slider_name_id] : false; //if the slider not longer exists or exists but is empty, return false if ( !isset($saved_slides) || !is_array($saved_slides) || empty($saved_slides) ) return; //inititalize the slides array $slides = array(); //init slide active state index $i = 0; foreach ( $saved_slides as $s) { $slide_object = get_post( $s); //next loop if attachment does not exist anymore (has been deleted for example) if (!isset( $slide_object)) continue; $id = $slide_object -> ID; //check if slider enabled for this attachment and go to next slide if not $slider_checked = esc_attr(get_post_meta( $id, $key = 'slider_check_key' , $single = true )); if ( !isset( $slider_checked) || $slider_checked != 1 ) continue; //title $title = esc_attr(get_post_meta( $id, $key = 'slide_title_key' , $single = true )); $default_title_length = apply_filters( 'tc_slide_title_length', 80 ); $title = ( strlen($title) > $default_title_length ) ? substr( $title,0,strpos( $title, ' ' , $default_title_length) ). ' ...' : $title; //lead text $text = get_post_meta( $id, $key = 'slide_text_key' , $single = true ); $default_text_length = apply_filters( 'tc_slide_text_length', 250 ); $text = ( strlen($text) > $default_text_length ) ? substr( $text,0,strpos( $text, ' ' ,$default_text_length) ). ' ...' : $text; //button text $button_text = esc_attr(get_post_meta( $id, $key = 'slide_button_key' , $single = true )); $default_button_length = apply_filters( 'tc_slide_button_length', 80 ); $button_text = ( strlen($button_text) > $default_button_length ) ? substr( $button_text,0,strpos( $button_text, ' ' ,$default_button_length)). ' ...' : $button_text; //link post id $link_id = esc_attr(get_post_meta( $id, $key = 'slide_link_key' , $single = true )); //button link $link_url = $link_id ? get_permalink( $link_id ) : 'javascript:void(0)'; //sets the first slide active $active = ( 0 == $i ) ? 'active' : ''; //checks if $text_color is set and create an html style attribute $text_color = esc_attr(get_post_meta( $id, $key = 'slide_color_key' , $single = true )); $color_style = ( $text_color != null) ? 'style="color:'.$text_color.'"' : ''; //attachment image $alt = apply_filters( 'tc_slide_background_alt' , trim(strip_tags(get_post_meta( $id, '_wp_attachment_image_alt' , true))) ); $slide_background = wp_get_attachment_image( $id, $img_size, false, array( 'class' => 'slide' , 'alt' => $alt ) ); //adds all values to the slide array only if the content exists (=> handle the case when an attachment has been deleted for example). Otherwise go to next slide. if ( !isset($slide_background) || empty($slide_background) ) continue; $slides[$id] = array( 'title' => $title, 'text' => $text, 'button_text' => $button_text, 'link_id' => $link_id, 'link_url' => $link_url, 'active' => $active, 'color_style' => $color_style, 'slide_background' => $slide_background, ); //increments active index $i++; }//end of slides loop //returns the slides or false if nothing return ( !empty($slides) ) ? $slides : false; } /** * Displays the slider based on the context : home, post/page. * * @package Customizr * @since Customizr 1.0 * */ function tc_slider_display() { global $wp_query; //gets the front slider if any $tc_front_slider = tc__f( '__get_option' , 'tc_front_slider' ); //when do we display a slider? By default only for home (if a slider is defined), pages and posts (including custom post types) if ( ! apply_filters( 'tc_show_slider' , !is_404() && !is_archive() && !is_search() || ( tc__f('__is_home') && $tc_front_slider ) ) ) return; //gets the actual page id if we are displaying the posts page $queried_id = get_queried_object_id(); $queried_id = ( !tc__f('__is_home') && $wp_query -> is_posts_page && !empty($queried_id) ) ? $queried_id : get_the_ID(); //gets the current slider id $slider_name_id = ( tc__f('__is_home') && $tc_front_slider ) ? $tc_front_slider : esc_attr( get_post_meta( $queried_id, $key = 'post_slider_key' , $single = true ) ); $slider_name_id = apply_filters( 'tc_slider_name_id', $slider_name_id , $queried_id); //is the slider set to on for the queried id? $slider_active = ( tc__f('__is_home') && $tc_front_slider ) ? true : esc_attr(get_post_meta( $queried_id, $key = 'post_slider_check_key' , $single = true )); $slider_active = apply_filters( 'tc_slider_active_status', $slider_active , $queried_id); if ( isset( $slider_active) && !$slider_active ) return; //gets slider options if any $layout_value = tc__f('__is_home') ? tc__f( '__get_option' , 'tc_slider_width' ) : esc_attr(get_post_meta( $queried_id, $key = 'slider_layout_key' , $single = true )); $layout_value = apply_filters( 'tc_slider_layout', $layout_value, $queried_id ); //declares the layout vars $layout_class = apply_filters( 'tc_slider_layout_class' , ( 0 == $layout_value ) ? 'container' : '' ); $img_size = apply_filters( 'tc_slider_img_size' , ( 0 == $layout_value ) ? 'slider' : 'slider-full'); //get slides $slides = $this-> tc_get_slides( $slider_name_id , $img_size ); //returns nothing if no slides to display if (!$slides) return; ob_start(); ?>
1 ) : ?>