Ejemplo n.º 1
0
 function avia_ajax_save_options_page()
 {
     //check if user is allowed to save and if its his intention with a nonce check
     if (function_exists('check_ajax_referer')) {
         check_ajax_referer('avia_nonce_save_backend');
     }
     //if we got no post data or no database key abort the script
     if (!isset($_POST['data']) || !isset($_POST['prefix']) || !isset($_POST['slug'])) {
         die;
     }
     $optionkey = $_POST['prefix'];
     $data_sets = explode("&", $_POST['data']);
     $store_me = avia_ajax_save_options_create_array($data_sets);
     $current_options = get_option($optionkey);
     $current_options[$_POST['slug']] = $store_me;
     //if a dynamic order was passed by javascript convert the string to an array and re order the items of the set controller to match the order array
     if (isset($_POST['dynamicOrder']) && $_POST['dynamicOrder'] != "") {
         global $avia;
         $current_elments = array();
         $options = get_option($optionkey . '_dynamic_elements');
         //split dynamic options into elements of this page and others
         foreach ($options as $key => $element) {
             if (in_array($element['slug'], $avia->subpages[$_POST['slug']])) {
                 $current_elments[$key] = $element;
                 unset($options[$key]);
             }
         }
         $sortedOptions = array();
         $neworder = explode('-__-', $_POST['dynamicOrder']);
         foreach ($neworder as $key) {
             if ($key != "" && array_key_exists($key, $current_elments)) {
                 $sortedOptions[$key] = $current_elments[$key];
             }
         }
         $options = array_merge($options, $sortedOptions);
         //save the resorted options
         update_option($optionkey . '_dynamic_elements', $options);
     }
     //hook in case we want to do somethin with the new options
     do_action('avia_ajax_save_options_page', $current_options);
     //remove old option set and save those key/value pairs in the database
     update_option($optionkey, $current_options);
     //flush rewrite rules for custom post types
     update_option('avia_rewrite_flush', 1);
     //hook in case we want to do somethin after saving
     do_action('avia_ajax_after_save_options_page', $current_options);
     die('avia_save');
 }
Ejemplo n.º 2
0
 /**
  * Meta box saving 
  * This function hooks into the native wordpress post saving. Once a user saves a post the function first checks if we got new cloned option sets, 
  * creates them and saves them to the post meta table. That way each post can have an individual set of options. Then we iterate over each array
  * entry and save, edit or delete the according post data
  */
 function save_post()
 {
     if (isset($_POST['post_ID'])) {
         $must_check = false;
         if (!is_array($this->default_boxes) || !isset($_POST['post_ID']) || !isset($_POST['post_type']) || $this->saved) {
             return;
         }
         //check if a metabox was attached to this post type
         foreach ($this->default_boxes as $default_box) {
             if (in_array($_POST['post_type'], $default_box['page'])) {
                 $must_check = true;
             }
         }
         if (!$must_check) {
             return;
         }
         if (function_exists('check_ajax_referer')) {
             check_ajax_referer('avia_nonce_save_metabox', 'avia-nonce');
         }
         //check if we got an options array and a post id or if it was already saved: if wordpress does an ajax save or creates a new page one of them might be unavailable
         //check which capability is needed to edit the current post/page
         $post_id = $_POST['post_ID'];
         $capability = "edit_post";
         if ('page' == $_POST['post_type']) {
             $capability = "edit_page";
         }
         //does the user have the capability?
         if (!current_user_can($capability, $post_id)) {
             return $post_id;
         }
         $this->saved = true;
         $meta_array = array();
         foreach ($this->box_elements as $box) {
             foreach ($_POST as $key => $value) {
                 if (strpos($key, $box['id']) !== false) {
                     if (strpos($key, 'on_save_') !== false) {
                         $function = str_replace('on_save_', "", $key);
                         $meta_array = apply_filters('avia_filter_save_meta_box_' . $function, $meta_array, $_POST);
                     }
                     $meta_array[$key] = $value;
                 }
             }
         }
         $result = avia_ajax_save_options_create_array($meta_array, true);
         update_post_meta($post_id, '_avia_elements_' . $this->superobject->option_prefix, $result);
         //also save the data to a neutral field that can be used for compatibility with other themes
         update_post_meta($post_id, '_avia_elements_theme_compatibility_mode', $result);
         //hook in case the value should be processed otherwise by an external function (example: slideshow first entry should be saved as post thumb)
         do_action('avia_meta_box_save_post', $post_id, $result);
     }
 }