'slide_title_key' ,
'slide_text_field' => 'slide_text_key' ,
'slide_color_field' => 'slide_color_key' ,
'slide_button_field' => 'slide_button_key' ,
'slide_link_field' => 'slide_link_key'
);
//if saving in a custom table, get post_ID
if ( $post_id == null)
return;
//sanitize user input by looping on the fields
foreach ( $tc_slider_fields as $tcid => $tckey) {
if ( isset( $_POST[$tcid])) {
$mydata = sanitize_text_field( $_POST[$tcid] );
switch ( $tckey) {
//different sanitizations
case 'slide_text_key':
$default_text_length = apply_filters( 'tc_slide_text_length', 250 );
if (strlen( $mydata) > $default_text_length) {
$mydata = substr( $mydata,0,strpos( $mydata, ' ' ,$default_text_length));
$mydata = esc_html( $mydata) . ' ...';
}
else {
$mydata = esc_html( $mydata);
}
break;
default://for button, color, title and post link field (actually not a link but an id)
$default_title_length = apply_filters( 'tc_slide_title_length', 80 );
if (strlen( $mydata) > $default_title_length) {
$mydata = substr( $mydata,0,strpos( $mydata, ' ' ,80));
$mydata = esc_attr( $mydata) . ' ...';
}
else {
$mydata = esc_attr( $mydata);
}
break;
}//end switch
//write in DB
add_post_meta( $post_id, $tckey, $mydata, true) or
update_post_meta( $post_id, $tckey , $mydata);
}//end if isset $tckey
}//end foreach
}
/*
----------------------------------------------------------------
---------- DISPLAY SLIDES TABLE (post and attachment) ----------
----------------------------------------------------------------
*/
/**
* Display slides table dynamic content for the selected slider
* @package Customizr
* @since Customizr 2.0
*/
function tc_show_slides ( $current_post_slides,$current_attachement_id) {
//check if we have slides to show
?>
$slide) {
//get the attachment object
$tc_slide = get_post( $slide );
//check if $tc_slide object exists otherwise go to the next iteration
if (!isset( $tc_slide))
continue;
//check if slider is checked for this attachment => otherwise go to the next iteration
$slider_check_value = esc_attr(get_post_meta( $tc_slide -> ID, $key = 'slider_check_key' , $single = true ));
if ( $slider_check_value == false)
continue;
//set up variables
$id = $tc_slide -> ID;
$slide_src = wp_get_attachment_image_src( $id, 'thumbnail' );
$slide_url = $slide_src[0];
$title = esc_attr(get_post_meta( $id, $key = 'slide_title_key' , $single = true ));
$text = esc_html(get_post_meta( $id, $key = 'slide_text_key' , $single = true ));
$text_color = esc_attr(get_post_meta( $id, $key = 'slide_color_key' , $single = true ));
$button_text = esc_attr(get_post_meta( $id, $key = 'slide_button_key' , $single = true ));
$button_link = esc_attr(get_post_meta( $id, $key = 'slide_link_key' , $single = true ));
//check if $text_color is set and create an html style attribute
$color_style ='';
if( $text_color != null) {
$color_style = 'style="color:'.$text_color.'"';
}
?>
>
class="lead">
';
?>
$tcvalue) {
switch ( $tckey) {
//delete slider
case 'delete_slider':
//first we delete the meta fields related to the deleted slider
//which screen are we coming from?
if( $tc_post_type == 'attachment' ) {
query_posts( 'meta_key=post_slider_key&meta_value='.$current_post_slider);
//we loop the posts with the deleted slider meta key
if(have_posts()) {
while ( have_posts() ) : the_post();
//delete the post meta
delete_post_meta(get_the_ID(), $key = 'post_slider_key' );
endwhile;
}
wp_reset_query();
}
//we delete from the post/page screen
else {
$post_slider_meta = esc_attr(get_post_meta( $post_ID, $key = 'post_slider_key' , $single = true ));
if(!empty( $post_slider_meta)) {
delete_post_meta( $post_ID, $key = 'post_slider_key' );
}
}
//in all cases, delete DB option
unset( $tc_options['tc_sliders'][$current_post_slider]);
//update DB with new slider array
update_option( 'tc_theme_options' , $tc_options );
break;
//reorder slides
case 'newOrder':
//turn new order into array
if(!empty( $tcvalue))
$neworder = explode( ',' , esc_attr( $tcvalue ));
//initialize the newslider array
$newslider = array();
foreach ( $neworder as $new_key => $new_index) {
$newslider[$new_index] = $tc_options['tc_sliders'][$current_post_slider][$new_index];
}
$tc_options['tc_sliders'][$current_post_slider] = $newslider;
//update DB with new slider array
update_option( 'tc_theme_options' , $tc_options );
break;
//sliders are added in options
case 'new_slider_name':
//check if we have something to save
$new_slider_name = esc_attr( $tcvalue );
$delete_slider = false;
if ( isset( $_POST['delete_slider']))
$delete_slider = $_POST['delete_slider'];
//prevent saving if we delete
if (!empty( $new_slider_name) && $delete_slider != true) {
$new_slider_name = wp_filter_nohtml_kses( $tcvalue );
//remove spaces and special char
$new_slider_name = strtolower(preg_replace("![^a-z0-9]+!i", "-", $new_slider_name));
$tc_options['tc_sliders'][$new_slider_name] = array( $post_ID);
//adds the new slider name in DB options
update_option( 'tc_theme_options' , $tc_options );
//associate the current post with the new saved slider
//looks for a previous slider entry and delete it
foreach ( $tc_options['tc_sliders'] as $slider_name => $slider) {
foreach ( $slider as $key => $tc_post) {
//clean empty values if necessary
if ( is_null( $tc_options['tc_sliders'][$slider_name][$key]))
unset( $tc_options['tc_sliders'][$slider_name][$key]);
//delete previous slider entries for this post
if ( $tc_post == $post_ID )
unset( $tc_options['tc_sliders'][$slider_name][$key]);
}
}
//update DB with clean option table
update_option( 'tc_theme_options' , $tc_options );
//push new post value for the new slider and write in DB
array_push( $tc_options['tc_sliders'][$new_slider_name], $post_ID);
update_option( 'tc_theme_options' , $tc_options );
}
break;
//post slider value
case 'post_slider_name':
//check if we display the attachment screen
if (!isset( $_POST['slider_check_field'])) {
break;
}
//we are in the attachment screen and we uncheck slider options checkbox
elseif ( $_POST['slider_check_field'] == 0) {
break;
}
//if we are in the slider creation case, the selected slider has to be the new one!
if (!empty( $new_slider_name))
break;
//check if we have something to save
$post_slider_name = esc_attr( $tcvalue );
//check if we have an input and if we are not in the slider creation case
if (!empty( $post_slider_name)) {
$post_slider_name = wp_filter_nohtml_kses( $post_slider_name );
//looks for a previous slider entry and delete it.
//Important : we check if the slider has slides first!
foreach ( $tc_options['tc_sliders'] as $slider_name => $slider) {
foreach ( $slider as $key => $tc_post) {
//clean empty values if necessary
if ( is_null( $tc_options['tc_sliders'][$slider_name][$key])) {
unset( $tc_options['tc_sliders'][$slider_name][$key]);
}
//clean slides with no images
$slide_img = wp_get_attachment_image( $tc_options['tc_sliders'][$slider_name][$key]);
if (isset($slide_img) && empty($slide_img)) {
unset( $tc_options['tc_sliders'][$slider_name][$key]);
}
//delete previous slider entries for this post
if ( $tc_post == $post_ID ) {
unset( $tc_options['tc_sliders'][$slider_name][$key]);
}
}//end for each
}
//update DB with clean option table
update_option( 'tc_theme_options' , $tc_options );
//check if the selected slider is empty and set it as array
if( empty( $tc_options['tc_sliders'][$post_slider_name]) ) {
$tc_options['tc_sliders'][$post_slider_name] = array();
}
//push new post value for the slider and write in DB
array_push( $tc_options['tc_sliders'][$post_slider_name], $post_ID);
update_option( 'tc_theme_options' , $tc_options );
}//end if !empty( $post_slider_name)
//No slider selected
else {
//looks for a previous slider entry and delete it
foreach ( $tc_options['tc_sliders'] as $slider_name => $slider) {
foreach ( $slider as $key => $tc_post) {
//clean empty values if necessary
if ( is_null( $tc_options['tc_sliders'][$slider_name][$key]))
unset( $tc_options['tc_sliders'][$slider_name][$key]);
//delete previous slider entries for this post
if ( $tc_post == $post_ID )
unset( $tc_options['tc_sliders'][$slider_name][$key]);
}
}
//update DB with clean option table
update_option( 'tc_theme_options' , $tc_options );
}
break;
}//end switch
}//end foreach
//POST META FIELDS
//set up the fields array
$tc_slider_fields = array(
//posts & pages
'post_slider_name' => 'post_slider_key' ,
'post_slider_check_field' => 'post_slider_check_key' ,
//attachments
'slider_check_field' => 'slider_check_key' ,
'slide_title_field' => 'slide_title_key' ,
'slide_text_field' => 'slide_text_key' ,
'slide_color_field' => 'slide_color_key' ,
'slide_button_field' => 'slide_button_key' ,
'slide_link_field' => 'slide_link_key'
);
//sanitize user input by looping on the fields
foreach ( $tc_slider_fields as $tcid => $tckey) {
if ( isset( $_POST[$tcid])) {
switch ( $tckey) {
//different sanitizations
//the slider name custom field for a post/page
case 'post_slider_key' :
$mydata = esc_attr( $_POST[$tcid] );
//Does the selected slider still exists in options? (we first check if the selected slider is not empty)
if(!empty( $mydata) && !isset( $tc_options['tc_sliders'][$mydata]))
break;
//write in DB
add_post_meta( $post_ID, $tckey, $mydata, true) or
update_post_meta( $post_ID, $tckey , $mydata);
break;
//inserted/updated in all cases
case 'post_slider_check_key':
case 'slider_check_key':
$mydata = esc_attr( $_POST[$tcid] );
//write in DB
add_post_meta( $post_ID, $tckey, $mydata, true) or
update_post_meta( $post_ID, $tckey , $mydata);
//check if we are in the attachment screen AND slider unchecked
if( $tckey == 'slider_check_key' && esc_attr( $_POST[$tcid] ) == 0) {
//if we uncheck the attachement slider, looks for a previous entry and delete it.
//Important : we check if the slider has slides first!
if ( isset( $tc_options['tc_sliders'])) {
foreach ( $tc_options['tc_sliders'] as $slider_name => $slider) {
foreach ( $slider as $key => $tc_post) {
//clean empty values if necessary
if ( is_null( $tc_options['tc_sliders'][$slider_name][$key]))
unset( $tc_options['tc_sliders'][$slider_name][$key]);
//delete previous slider entries for this post
if ( $tc_post == $post_ID )
unset( $tc_options['tc_sliders'][$slider_name][$key]);
}
}
}
//update DB with clean option table
update_option( 'tc_theme_options' , $tc_options );
}//endif;
break;
case 'slide_text_key':
//check if we add this attachment to a slider for the first time : do we have custom fields defined in DB and are the input fields existing in the DOM (sent by Ajax)?
$mydata = sanitize_text_field( $_POST[$tcid] );
if (strlen( $mydata) > 250) {
$mydata = substr( $mydata,0,strpos( $mydata, ' ' ,250));
$mydata = esc_html( $mydata) . ' ...';
}
else {
$mydata = esc_html( $mydata);
}
//write in DB
add_post_meta( $post_ID, $tckey, $mydata, true) or
update_post_meta( $post_ID, $tckey , $mydata);
break;
default://for button, color, title and post link field (actually not a link but an id)
//check if we add this attachment to a slider for the first time : do we have custom fields defined in DB and are the input fields existing in the DOM (sent by Ajax)?
$mydata = sanitize_text_field( $_POST[$tcid] );
$default_button_length = apply_filters( 'tc_slide_button_length', 80 );
if (strlen( $mydata) > $default_button_length) {
$mydata = substr( $mydata,0,strpos( $mydata, ' ' ,$default_button_length));
$mydata = esc_attr( $mydata) . ' ...';
}
else {
$mydata = esc_attr( $mydata);
}
//write in DB
add_post_meta( $post_ID, $tckey, $mydata, true) or
update_post_meta( $post_ID, $tckey , $mydata);
break;
}//end switchendif;
}//end if ( isset( $_POST[$tcid])) {
}//end foreach
}//function
/*
----------------------------------------------------------------
-------- AJAX CALL BACK FUNCTION (post and attachment) ---------
----------------------------------------------------------------
*/
/**
* Global slider ajax call back function : 1-Saves options and fields, 2-Renders
* Used in post or attachment context => uses post_slider var to check the context
* Works along with tc_ajax_slider.js
* @package Customizr
* @since Customizr 2.0
*/
function tc_slider_cb() {
$nonce = $_POST['SliderCheckNonce'];
// check if the submitted nonce matches with the generated nonce we created earlier
if ( ! wp_verify_nonce( $nonce, 'tc-slider-check-nonce' ) ) {
die();
}
Try{
//get the post_id with the hidden input field
$tc_post_id = $_POST['tc_post_id'];
//save $_POST var in DB
$this -> tc_slider_ajax_save( $tc_post_id);
//check if we are in the post or attachment screen and select the appropriate rendering
//we use the post_slider var defined in tc_ajax_slider.js
if ( isset( $_POST['tc_post_type'])) {
if( $_POST['tc_post_type'] == 'post' ) {
$this -> tc_get_post_slider_infos( $tc_post_id);
}
else {
$this -> tc_get_attachment_slider_infos( $tc_post_id);
}
}
//echo $_POST['slider_id'];
} catch (Exception $e){
exit;
}
exit;
}
/**
* Loads the necessary scripts and stylesheets to display slider options
* @package Customizr
* @since Customizr 1.0
*/
function tc_slider_admin_scripts( $hook) {
global $post;
//load scripts only for creating and editing slides options in pages and posts
if( ( 'media.php' == $hook)) {
wp_enqueue_script( 'jquery-ui-sortable' );
}
if( ( 'post-new.php' == $hook || 'post.php' == $hook || 'media.php' == $hook) ) {
//ajax refresh for slider options
wp_enqueue_script( 'tc_ajax_slider' , TC_BASE_URL.'inc/admin/js/tc_ajax_slider.min.js' , array( 'jquery' ), true );
// Tips to declare javascript variables http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#bad-ways
wp_localize_script( 'tc_ajax_slider' , 'SliderAjax' , array(
// URL to wp-admin/admin-ajax.php to process the request
//'ajaxurl' => admin_url( 'admin-ajax.php' ),
// generate a nonce with a unique ID "myajax-post-comment-nonce"
// so that you can check it later when an AJAX request is sent
'SliderNonce' => wp_create_nonce( 'tc-slider-nonce' ),
//
'SliderCheckNonce' => wp_create_nonce( 'tc-slider-check-nonce' ),
)
);
//iphone like button style and script
wp_enqueue_style( 'iphonecheckcss' , TC_BASE_URL.'inc/admin/css/iphonecheck.css' );
wp_enqueue_script( 'iphonecheck' , TC_BASE_URL.'inc/admin/js/jquery.iphonecheck.js' );
//thickbox
wp_admin_css( 'thickbox' );
add_thickbox();
//sortable stuffs
wp_enqueue_style( 'sortablecss' , TC_BASE_URL.'inc/admin/css/tc_sortable.css' );
//wp built-in color picker style and script
//Access the global $wp_version variable to see which version of WordPress is installed.
global $wp_version;
//If the WordPress version is greater than or equal to 3.5, then load the new WordPress color picker.
if ( 3.5 <= $wp_version ){
//Both the necessary css and javascript have been registered already by WordPress, so all we have to do is load them with their handle.
wp_enqueue_style( 'wp-color-picker' );
wp_enqueue_script( 'wp-color-picker' );
// load the minified version of custom script
wp_enqueue_script( 'cp_demo-custom' , TC_BASE_URL.'inc/admin/js/color-picker.js' , array( 'jquery' , 'wp-color-picker' ), true );
}
//If the WordPress version is less than 3.5 load the older farbtasic color picker.
else {
//As with wp-color-picker the necessary css and javascript have been registered already by WordPress, so all we have to do is load them with their handle.
wp_enqueue_style( 'farbtastic' );
wp_enqueue_script( 'farbtastic' );
// load the minified version of custom script
wp_enqueue_script( 'cp_demo-custom' , TC_BASE_URL.'inc/admin/js/color-picker.js' , array( 'jquery' , 'farbtastic' ), true );
}
}//end post type hook check
}
/*
----------------------------------------------------------------
------------- ATTACHMENT FIELDS FILTER IF WP < 3.5 -------------
----------------------------------------------------------------
*/
function tc_attachment_filter( $form_fields, $post = null) {
$this -> tc_attachment_slider_box ( $post);
return $form_fields;
}
function tc_attachment_save_filter( $post, $attachment ) {
if ( isset( $_POST['tc_post_id']))
$postid = $_POST['tc_post_id'];
$this -> tc_slide_save( $postid );
return $post;
}
}//end of class
endif;