Exemplo n.º 1
0
function wpcf_fields_checkbox_update_one($post_id, $slug, $array_to_check)
{
    $cf = new WPCF_Field();
    $cf->set($post_id, $cf->__get_slug_no_prefix($slug));
    /**
     * return if field do not exists
     */
    if (!array_key_exists('data', $cf->cf)) {
        return;
    }
    if ('checkbox' == $cf->cf['type']) {
        if (isset($array_to_check[$cf->__get_slug_no_prefix($slug)]) || isset($array_to_check[$slug])) {
            update_post_meta($post_id, $slug, $cf->cf['data']['set_value']);
            return;
        }
        $cf->set($post_id, $cf->__get_slug_no_prefix($slug));
        if ($cf->cf['data']['save_empty'] != 'no') {
            update_post_meta($post_id, $cf->slug, 0);
        } else {
            delete_post_meta($post_id, $cf->slug);
        }
    } else {
        if ('checkboxes' == $cf->cf['type']) {
            $value = array();
            if (isset($array_to_check[$cf->__get_slug_no_prefix($slug)])) {
                foreach ($array_to_check[$cf->__get_slug_no_prefix($slug)] as $key => $val) {
                    if (isset($cf->cf['data']['options'])) {
                        $value[$key] = $val;
                    }
                }
            }
            update_post_meta($post_id, $cf->slug, $value);
        }
    }
}
Exemplo n.º 2
0
 /**
  * Unified save child function.
  *
  * @param int $parent_id
  * @param int $child_id
  * @param array $save_fields
  * @return bool|WP_Error
  */
 function save_child($parent_id, $child_id, $save_fields = array())
 {
     $parent = get_post(intval($parent_id));
     $child = get_post(intval($child_id));
     $post_data = array();
     if (empty($parent) || empty($child)) {
         return new WP_Error('wpcf-relationship-save-child', 'no parent/child post');
     }
     // Save relationship
     update_post_meta($child->ID, '_wpcf_belongs_' . $parent->post_type . '_id', $parent->ID);
     // Check if added via AJAX
     $check = get_post_meta($child->ID, '_wpcf_relationship_new', true);
     $new = !empty($check);
     delete_post_meta($child->ID, '_wpcf_relationship_new');
     // Set post data
     $post_data['ID'] = $child->ID;
     // Title needs to be checked if submitted at all
     if (!isset($save_fields['_wp_title'])) {
         // If not submitted that means it is not offered to be edited
         if (!empty($child->post_title)) {
             $post_title = $child->post_title;
         } else {
             // DO NOT LET IT BE EMPTY
             $post_title = $child->post_type . ' ' . $child->ID;
         }
     } else {
         $post_title = $save_fields['_wp_title'];
     }
     $post_data['post_title'] = $post_title;
     $post_data['post_content'] = isset($save_fields['_wp_body']) ? $save_fields['_wp_body'] : $child->post_content;
     $post_data['post_type'] = $child->post_type;
     // Check post status - if new, convert to 'publish' else keep remaining
     if ($new) {
         $post_data['post_status'] = 'publish';
     } else {
         $post_data['post_status'] = get_post_status($child->ID);
     }
     /*
      *
      *
      *
      *
      *
      *
      * UPDATE POST
      */
     $cf = new WPCF_Field();
     if (isset($_POST['wpcf_post_relationship'][$parent_id]) && isset($_POST['wpcf_post_relationship'][$parent_id][$child_id])) {
         $_POST['wpcf'] = array();
         foreach ($_POST['wpcf_post_relationship'][$parent_id][$child_id] as $slug => $value) {
             $_POST['wpcf'][$cf->__get_slug_no_prefix($slug)] = $value;
             $_POST['wpcf'][$slug] = $value;
         }
     }
     unset($cf);
     /**
     * avoid filters for children
     * /
             global $wp_filter;
             $save_post = $wp_filter['save_post'];
             $wp_filter['save_post'] = array();
     */
     $updated_id = wp_update_post($post_data);
     /*
        $wp_filter['save_post'] = $save_post;
     */
     unset($save_post);
     if (empty($updated_id)) {
         return new WP_Error('relationship-update-post-failed', 'Updating post failed');
     }
     // Save parents
     if (!empty($save_fields['parents'])) {
         foreach ($save_fields['parents'] as $parent_post_type => $parent_post_id) {
             update_post_meta($child->ID, '_wpcf_belongs_' . $parent_post_type . '_id', $parent_post_id);
         }
     }
     // Update taxonomies
     if (!empty($save_fields['taxonomies']) && is_array($save_fields['taxonomies'])) {
         $_save_data = array();
         foreach ($save_fields['taxonomies'] as $taxonomy => $t) {
             if (!is_taxonomy_hierarchical($taxonomy)) {
                 $_save_data[$taxonomy] = strval($t);
                 continue;
             }
             foreach ($t as $term_id) {
                 if ($term_id != '-1') {
                     $term = get_term($term_id, $taxonomy);
                     if (empty($term)) {
                         continue;
                     }
                     $_save_data[$taxonomy][] = $term_id;
                 }
             }
         }
         wp_delete_object_term_relationships($child->ID, array_keys($save_fields['taxonomies']));
         foreach ($_save_data as $_taxonomy => $_terms) {
             wp_set_post_terms($child->ID, $_terms, $_taxonomy, $append = false);
         }
     }
     // Unset non-types
     unset($save_fields['_wp_title'], $save_fields['_wp_body'], $save_fields['parents'], $save_fields['taxonomies']);
     /*
      *
      *
      *
      *
      *
      *
      * UPDATE Loop over fields
      */
     foreach ($save_fields as $slug => $value) {
         if (defined('WPTOOLSET_FORMS_VERSION')) {
             // Get field by slug
             $field = wpcf_fields_get_field_by_slug(str_replace(WPCF_META_PREFIX, '', $slug));
             if (empty($field)) {
                 continue;
             }
             // Set config
             $config = wptoolset_form_filter_types_field($field, $child->ID);
             // Check if valid
             $valid = wptoolset_form_validate_field('post', $config, $value);
             if (is_wp_error($valid)) {
                 $errors = $valid->get_error_data();
                 $msg = sprintf(__('Child post "%s" field "%s" not updated:', 'wpcf'), $child->post_title, $field['name']);
                 wpcf_admin_message_store($msg . ' ' . implode(', ', $errors), 'error');
                 continue;
             }
         }
         $this->cf->set($child, $field);
         $this->cf->context = 'post_relationship';
         $this->cf->save($value);
     }
     do_action('wpcf_relationship_save_child', $child, $parent);
     clean_post_cache($parent->ID);
     clean_post_cache($child->ID);
     // Added because of caching meta 1.5.4
     wp_cache_flush();
     return true;
 }
Exemplo n.º 3
0
/**
 * Check if checkbox is submitted.
 * 
 * Currently used on Relationship saving. May be expanded to general code.
 * 
 * @param type $value
 * @param type $field
 * @param type $cf
 */
function wpcf_fields_checkbox_save_check()
{
    $meta_to_unset = array();
    $cf = new WPCF_Field();
    /*
     * 
     * We hve several calls on this:
     * 1. Saving post with Update
     * 2. Saving all children
     * 3. Saving child
     */
    $mode = 'save_main';
    if (defined('DOING_AJAX')) {
        if (isset($_GET['wpcf_action']) && $_GET['wpcf_action'] == 'pr_save_all') {
            $mode = 'save_all';
        } else {
            if (isset($_GET['wpcf_action']) && $_GET['wpcf_action'] == 'pr_save_child_post') {
                $mode = 'save_child';
            }
        }
    }
    // See if any marked for checking
    if (isset($_POST['_wpcf_check_checkbox'])) {
        // Loop and search in $_POST
        foreach ($_POST['_wpcf_check_checkbox'] as $child_id => $slugs) {
            foreach ($slugs as $slug => $true) {
                $cf->set($child_id, $cf->__get_slug_no_prefix($slug));
                // First check main post
                if ($mode == 'save_main' && intval($child_id) == wpcf_get_post_id()) {
                    if (!isset($_POST['wpcf'][$cf->cf['slug']])) {
                        $meta_to_unset[intval($child_id)][$cf->slug] = true;
                    }
                    continue;
                }
                /*
                 * 
                 * Relationship check
                 */
                if (!isset($_POST['wpcf_post_relationship'])) {
                    $meta_to_unset[$child_id][$cf->slug] = true;
                } else {
                    foreach ($_POST['wpcf_post_relationship'] as $_parent => $_children) {
                        foreach ($_children as $_child_id => $_slugs) {
                            if (!isset($_slugs[$slug])) {
                                $meta_to_unset[$_child_id][$cf->slug] = true;
                            }
                        }
                    }
                }
            }
        }
    }
    // After collected - delete them
    foreach ($meta_to_unset as $child_id => $slugs) {
        foreach ($slugs as $slug => $true) {
            $cf->set($child_id, $cf->__get_slug_no_prefix($slug));
            if ($cf->cf['data']['save_empty'] != 'no') {
                update_post_meta($child_id, $slug, 0);
            } else {
                delete_post_meta($child_id, $slug);
            }
        }
    }
}