Example #1
0
 public function SaveEditor($event = 'save-editor')
 {
     if ($event != $this->save_event || empty($_POST) || count($this->fields) == 0) {
         return false;
     }
     global $post, $cb_post;
     // Options to update will be stored here
     $post = $cb_post;
     //set the wp global post to the clearbase global post
     $this->update_fields = array();
     // Loop options and get values to save
     foreach ($this->fields as $field) {
         if (!isset($field['id'])) {
             continue;
         }
         $convertedID = str_replace('.', '-', $field['id']);
         $type = isset($field['type']) ? sanitize_title($field['type']) : '';
         // Get the option name
         $value = null;
         switch ($type) {
             // Standard types
             case "text":
             case 'email':
             case 'number':
             case "select":
             case "color":
             case 'password':
             case 'radio':
                 if (isset($_POST[$convertedID])) {
                     $value = sanitize_text_field(stripslashes($_POST[$convertedID]));
                 } else {
                     $value = isset($field['default']) ? $field['default'] : '';
                 }
                 break;
             case "textarea":
                 if (isset($_POST[$convertedID])) {
                     $value = wp_kses_post(trim(stripslashes($_POST[$convertedID])));
                 } else {
                     $value = '';
                 }
                 break;
             case "checkbox":
                 if (isset($_POST[$convertedID])) {
                     $value = 'yes';
                 } else {
                     $value = 'no';
                 }
                 break;
             case 'sectionstart':
             case 'sectionend':
                 continue;
                 // Custom handling
             // Custom handling
             case 'post_title':
                 $value = array();
                 if (isset($_POST['post-post_title'])) {
                     $value['post_title'] = sanitize_text_field(stripslashes($_POST['post-post_title']));
                 }
                 if (isset($_POST['post-post_name'])) {
                     $value['post_name'] = sanitize_text_field(stripslashes($_POST['post-post_name']));
                 }
                 break;
             default:
                 if (is_callable($field['save'])) {
                     call_user_func($field['save'], $field);
                 }
                 do_action("clearbase_editor_field_{$type}_save", $field);
                 break;
         }
         if (is_null($value)) {
             continue;
         }
         //Split the $field into tokens that we can proccess
         $tokens = explode('.', $field['id']);
         if ('post' === $tokens[0]) {
             if (!is_array($this->update_fields['post'])) {
                 $this->update_fields['post'] = array();
             }
             if (is_array($value)) {
                 $this->update_fields['post'] = array_merge($this->update_fields['post'], $value);
             } else {
                 $this->update_fields['post'][$tokens[1]] = $value;
             }
         } else {
             if ('postmeta' === $tokens[0] && count($tokens) > 2) {
                 /*  
                 token[0] is keyword postmeta, 
                 token[1] is the postmeta key
                 token[2+] references a nested array 
                 
                 if the field id contains more than token[0] and token[1],
                 then the user is referencing a nested array
                 example: postmeta.house.doors.back.color
                 */
                 //ensure that the root meta reference is an array
                 if (!is_array($this->update_fields['postmeta.' . $tokens[1]])) {
                     $this->update_fields['postmeta.' . $tokens[1]] = array();
                 }
                 $context =& _clearbase_array_traverse(array_slice($tokens, 2, count($tokens) - 3), $this->update_fields['postmeta.' . $tokens[1]], true);
                 //now that we're at the correct nested array position, assign the specified value
                 $context[$tokens[count($tokens) - 1]] = $value;
             } else {
                 $this->update_fields[$field['id']] = $value;
             }
         }
     }
     // Now save the update fields
     foreach ($this->update_fields as $key => $value) {
         clearbase_set_value($key, $value);
     }
     return true;
 }
Example #2
0
function clearbase_set_value($key, $value, $data = null)
{
    //split the key into tokens that we can proccess
    $tokens = explode('.', $key);
    $post = $result = null;
    if (in_array($tokens[0], array('post', 'postmeta'))) {
        if (!($post = get_post($data))) {
            return new WP_Error('invalid_post', 'Could not find a valid post for the specified key', $key);
        }
    }
    switch ($tokens[0]) {
        case 'post':
            if (count($tokens) === 1 && !is_array($value)) {
                return new WP_Error('clearbase_invalid_key_value', 'For a root post key, you must specify the $value parameter as an array of post field key/values', $value);
            } else {
                if (count($tokens) > 2) {
                    return new WP_Error('clearbase_invalid_key', 'A post key cannot contain nested references', $key);
                }
            }
            //store the post value
            $args = array('ID' => $post->ID, $key => $value);
            if (is_array($value)) {
                $args = array_merge($args, $value);
            }
            $result = wp_update_post(apply_filters('clearbase_set_value', $args, $key));
            break;
        case 'postmeta':
        case 'option':
            /*  
            token[0] is keyword, 
            token[1] is the key
            token[2+] references a nested array 
            */
            //get a copy of the current value
            $current_value = 'postmeta' == $tokens[0] ? get_post_meta($post->ID, $tokens[1], true) : get_option($tokens[1], array());
            //if the field id contains more than token[0] and token[1],
            //then the user is referencing a nested array
            //example: meta.house.doors.back.color
            if (count($tokens) > 2) {
                if (!is_array($current_value)) {
                    $current_value = array();
                }
                $context =& _clearbase_array_traverse(array_slice($tokens, 2, count($tokens) - 3), $current_value, true);
                //now that we're at the correct nested array position, assign the specified value
                $context[$tokens[count($tokens) - 1]] = $value;
                //now we need to set $value's reference to the root array
                $value = $current_value;
            } else {
                //since the user may not have supplied all of the values in an array
                //we merge the two arrays to ensure that the prev db values are not lost
                if (is_array($value) && is_array($meta_value)) {
                    $value = array_merge($meta_value, $value);
                }
            }
            $value = apply_filters('clearbase_set_value', $value, $key);
            //store the meta value
            $result = 'postmeta' == $tokens[0] ? update_post_meta($post->ID, $tokens[1], $value) : update_option($tokens[1], $value);
            break;
        default:
            //TODO check function
            //try to insert a value into a runtime array
            if (!is_array($data)) {
                return false;
            }
            $context =& _clearbase_array_traverse(array_slice($tokens, 0, count($tokens) - 2), $data, true);
            //now that we're at the correct nested array position, assign the specified value
            $context[$tokens[count($tokens) - 1]] = $value;
            $result = true;
            break;
    }
    return $result;
}