add_ajax_calls(); } } /** * Wrapper to avoid future fuck-ups and repetitive works (aka DRY!) */ function get_slider_settings( $default = array() ) { return get_option( $this->optionName, $default ); } function update_slider_settings( $settings = array() ) { $return = false; if( !empty( $settings ) ) { $return = update_option( $this->optionName, $settings ); } return $return; } /** * Add all required ajax calls and processing stuff * * @author Fabian Wolf * @package cc2 * @since 2.0 */ function add_ajax_calls() { /** * TODO: We might want to add a user_can() check in here, to avoid possible security holes * @see http://codex.wordpress.org/AJAX_in_Plugins#Ajax_on_the_Viewer-Facing_Side */ // save settings add_action( 'wp_ajax_' . $this->classPrefix . 'query', array( $this, 'update_settings') ); //add_action( 'wp_ajax_nopriv_' . $this->classPrefix . 'query', array( $this, 'update_settings') ); // show list of slides add_action( 'wp_ajax_' . $this->classPrefix . 'display_slides_list', array( $this, 'display_slides_list') ); //add_action( 'wp_ajax_nopriv_' . $this->classPrefix . 'display_slides_list', array( $this, 'display_slides_list') ); // add new slideshow add_action( 'wp_ajax_' . $this->classPrefix . 'add_slideshow', array( $this, 'add_slideshow') ); //add_action( 'wp_ajax_nopriv_' . $this->classPrefix . 'add_slideshow', array( $this, 'add_slideshow') ); // add single slide add_action( 'wp_ajax_' . $this->classPrefix . 'add_slide', array( $this, 'add_slide' ) ); //add_action( 'wp_ajax_nopriv_' . $this->classPrefix . 'add_slide', array( $this, 'add_slide' ) ); // delete whole slideshow add_action( 'wp_ajax_' . $this->classPrefix . 'delete_slideshow', array( $this, 'delete_slideshow') ); //add_action( 'wp_ajax_nopriv_' . $this->classPrefix . 'delete_slideshow', array( $this,'delete_slideshow') ); // delete single slide add_action( 'wp_ajax_' . $this->classPrefix . 'delete_slide', array( $this, 'delete_slide' ) ); //add_action( 'wp_ajax_nopriv_' . $this->classPrefix . 'delete_slide', array( $this, 'delete_slide') ); // change order of slides add_action( 'wp_ajax_' . $this->classPrefix . 'slideshow_neworder', array( $this, 'slideshow_neworder' ) ); //add_action( 'wp_ajax_nopriv_' . $this->classPrefix . 'slideshow_neworder', array( $this, 'slideshow_neworder' ) ); // safety switch: reset all slideshows add_action( 'wp_ajax_' . $this->classPrefix . 'reset_slideshows', array( $this, 'reset_slideshows' ) ); //add_action( 'wp_ajax_nopriv_' . $this->classPrefix . 'reset_slideshows', array( $this, 'reset_slideshows' ) ); // update post meta fields (aka custom fields) add_action('wp_ajax_save-attachment-compat', array( $this, 'update_media_xtra_fields'), 0, 1); } /** * Processing methods for handling the ajax calls on the server side are all up next * * @author Fabian Wolf * @package cc2 * @since 2.0 */ /** * Update settings */ function update_settings() { // validate data /** * TODO: Validation and sanitization is missing */ if(isset($_POST['select_slides_list'])) { $select_slides_list = $_POST['select_slides_list']; } if(isset($_POST['slideshow_post_type'])) { $slideshow_post_type = $_POST['slideshow_post_type']; } if(isset($_POST['slideshow_taxonomy'])) { $slideshow_taxonomy = $_POST['slideshow_taxonomy']; } if(isset($_POST['slideshow_cat'])) { $slideshow_cat = $_POST['slideshow_cat']; } // update settings //$cc_slider_options = get_option('cc_slider_options', array() ); $cc_slider_options = $this->get_slider_settings(); if(isset($slideshow_post_type)) { $cc_slider_options[$select_slides_list]['query']['post_type'] = $slideshow_post_type; } if(isset($slideshow_taxonomy)) { $cc_slider_options[$select_slides_list]['query']['taxonomy'] = $slideshow_taxonomy; } if(isset($slideshow_cat)) { $cc_slider_options[$select_slides_list]['query']['cat'] = $slideshow_cat; } $this->update_slider_settings( $cc_slider_options ); //$this->update_slider_settings( $cc_slider_options ); die(); } /** * Safety switch, eg. if some of the slideshows got fucked up during tests etc. */ function reset_slideshows() { update_option( $this->optionName, array() ); } /** * Returns the slide list */ function display_slides_list() { $cc_slider_options = $this->get_slider_settings(); if(isset($_POST['slide_list'])) { $slide_list = $_POST['slide_list']; } if(!isset($slide_list)) { return; } if(!isset($cc_slider_options[$slide_list])) { die(); } if( isset($slide_list) && isset($cc_slider_options[$slide_list]) ) { ?>
All Saved! X Delete This Slideshow
true, '_builtin' => true ); $output = 'names'; // or objects $operator = 'and'; // 'and' or 'or' ?> get_slider_settings(); $slideshow_name = null; $arrDefaultParams = array( 'slideshow_name' => $slideshow_name, 'slideshow_type' => 'image', ); if(isset($_POST['new_slideshow_name'])) { $slideshow_name = $_POST['new_slideshow_name']; } if(isset($_POST['new_slideshow_type'])) { $slideshow_type = $_POST['new_slideshow_type']; } if( !empty( $slideshow_name ) ) { // old unsafe style /*$cc_slider_options[ sanitize_title($slideshow_name) ]['meta-data'] = array( 'slideshow_type' => $slideshow_type, );*/ // avoid overwriting existing slideshows $new_slug = self::sanitize_array_key( $slideshow_name ); if( !isset( $cc_slider_options[ $new_slug ] ) ) { // new style $cc_slider_options[ $new_slug ] = array( 'title' => sanitize_title( $slideshow_name ), 'meta-data' => array( 'slideshow_type' => $slideshow_type ), ); //$this->update_slider_settings( $cc_slider_options ); $this->update_slider_settings( $cc_slider_options ); } } die(); } public static function sanitize_array_key( $text = false ) { $return = $text; if( !empty( $return ) ) { /** * NOTE: Array key is either index or string. So, anything that goes in a string will work as an array key as well! */ //$return = str_replace( array(' ', '--'), '-', strtolower($return) ); $return = strip_tags( $text ); //$return = sanitize_key( $return ); } return $return; } /** * Remove whole slideshow */ function delete_slideshow() { $slideshow = false; if(isset($_POST['slideshow'])) { $slideshow = $_POST['slideshow']; } if( !empty( $slideshow ) ) { $cc_slider_options = $this->get_slider_settings(); unset($cc_slider_options[$slideshow]); $this->update_slider_settings( $cc_slider_options ); } die(); } /** * Remove just a single slide * TODO: Proper security check / sanitization */ function delete_slide() { if(isset($_POST['args'])) { $args = $_POST['args']; } if( !empty($args) ) { $args = explode('/',$args); $cc_slider_options = $this->get_slider_settings(); unset($cc_slider_options[$args[0]]['slides'][$args[1]]); $this->update_slider_settings( $cc_slider_options ); } die(); } /** * Add new slide */ function add_slide() { $id = null; $select_slides_list = null; if(isset($_POST['id'])) { $id = $_POST['id']; } if(isset($_POST['select_slides_list'])) { $select_slides_list = $_POST['select_slides_list']; } if( !empty( $id ) && !empty( $select_slides_list ) ) { if(isset($_POST['url'])) { $url = $_POST['url']; } $cc_slider_options = $this->get_slider_settings(); $cc_slider_options[$select_slides_list]['slides'][$id] = array( 'id' => $id, 'url' => $url, ); $this->update_slider_settings( $cc_slider_options ); } die(); } /** * Change slide ordering */ function slideshow_neworder() { if( isset($_POST['neworder'])) { $neworder = $_POST['neworder']; } if( isset($_POST['select_slides_list'])) { $select_slides_list = $_POST['select_slides_list']; } if( !empty( $neworder ) ) { $cc_slider_options = $this->get_slider_settings(); $neworder = explode(',',$neworder); $slide = array(); foreach ( $neworder as $key) { $slide[$key] = $cc_slider_options[$select_slides_list]['slides'][$key]; } unset($cc_slider_options[$select_slides_list]['slides']); // remove old slideshow (order) $cc_slider_options[$select_slides_list]['slides'] = $slide; // add new slideshow (order) $this->update_slider_settings( $cc_slider_options ); } die(); } /** * Update custom field via ajax */ function update_media_xtra_fields() { $post_id = $_POST['id']; update_post_meta($post_id , 'new_slider', $_POST['attachments'][$post_id ]['new_slider']); clean_post_cache($post_id); } }