예제 #1
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;
 }
예제 #2
0
/**
 * Important save_post hook.
 *
 * Core function. Works and stable. Do not move or change.
 * If required, add hooks only.
 *
 * @internal breakpoint
 * @param type $post_ID
 * @param type $post
 */
function wpcf_admin_post_save_post_hook($post_ID, $post)
{
    if (defined('WPTOOLSET_FORMS_VERSION')) {
        global $wpcf;
        $errors = false;
        // Do not save post if is type of
        if (in_array($post->post_type, array('revision', 'view', 'view-template', 'cred-form', 'nav_menu_item', 'mediapage'))) {
            return false;
        }
        $_post_wpcf = array();
        if (!empty($_POST['wpcf'])) {
            $_post_wpcf = $_POST['wpcf'];
        }
        /**
         * handle checkbox
         */
        if (array_key_exists('_wptoolset_checkbox', $_POST) && is_array($_POST['_wptoolset_checkbox'])) {
            foreach ($_POST['_wptoolset_checkbox'] as $key => $field_value) {
                $field_slug = preg_replace('/^wpcf\\-/', '', $key);
                if (array_key_exists($field_slug, $_post_wpcf)) {
                    continue;
                }
                $_post_wpcf[$field_slug] = false;
            }
        }
        if (count($_post_wpcf)) {
            /**
             * check some attachment to delete
             */
            $delete_attachments = apply_filters('wpcf_delete_attachments', true);
            $images_to_delete = array();
            if ($delete_attachments && isset($_POST['wpcf']) && array_key_exists('delete-image', $_POST['wpcf']) && is_array($_POST['wpcf']['delete-image'])) {
                foreach ($_POST['wpcf']['delete-image'] as $image) {
                    $images_to_delete[$image] = 1;
                }
            }
            foreach ($_post_wpcf as $field_slug => $field_value) {
                // Get field by slug
                $field = wpcf_fields_get_field_by_slug($field_slug);
                if (empty($field)) {
                    continue;
                }
                // Skip copied fields
                if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                    continue;
                }
                $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value;
                // Set config
                $config = wptoolset_form_filter_types_field($field, $post_ID, $_post_wpcf);
                /**
                 * remove from images_to_delete if user add again
                 */
                if ($delete_attachments && 'image' == $config['type']) {
                    $images = $_field_value;
                    if (!is_array($images)) {
                        $images = array($images);
                    }
                    foreach ($images as $image) {
                        if (array_key_exists($image, $images_to_delete)) {
                            unset($images_to_delete[$image]);
                        }
                    }
                }
                foreach ($_field_value as $_k => $_val) {
                    // Check if valid
                    $validation = wptoolset_form_validate_field('post', $config, $_val);
                    $conditional = wptoolset_form_conditional_check($config);
                    $not_valid = is_wp_error($validation) || !$conditional;
                    if (is_wp_error($validation)) {
                        $errors = true;
                        $_errors = $validation->get_error_data();
                        $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']);
                        wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error');
                    }
                    if ($not_valid) {
                        if (types_is_repetitive($field)) {
                            unset($field_value[$_k]);
                        } else {
                            break;
                        }
                    }
                }
                // Save field
                if (types_is_repetitive($field)) {
                    $wpcf->repeater->set($post_ID, $field);
                    $wpcf->repeater->save($field_value);
                } else {
                    if (!$not_valid) {
                        $wpcf->field->set($post_ID, $field);
                        $wpcf->field->save($field_value);
                    }
                }
                do_action('wpcf_post_field_saved', $post_ID, $field);
                // TODO Move to checkboxes
                if ($field['type'] == 'checkboxes') {
                    if (!empty($field['data']['options'])) {
                        $update_data = array();
                        foreach ($field['data']['options'] as $option_id => $option_data) {
                            if (!isset($_POST['wpcf'][$field['id']][$option_id])) {
                                if (isset($field['data']['save_empty']) && $field['data']['save_empty'] == 'yes') {
                                    $update_data[$option_id] = 0;
                                }
                            } else {
                                $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id];
                            }
                        }
                        update_post_meta($post_ID, $field['meta_key'], $update_data);
                    }
                }
            }
            /**
             * delete images
             */
            if ($delete_attachments && !empty($images_to_delete)) {
                $args = array('post_parent' => $post_ID, 'posts_per_page' => -1);
                $children_array = get_children($args);
                foreach ($children_array as $child) {
                    if (!array_key_exists($child->guid, $images_to_delete)) {
                        continue;
                    }
                    wp_delete_attachment($child->ID);
                }
            }
        }
        if ($errors) {
            update_post_meta($post_ID, '__wpcf-invalid-fields', true);
        }
        do_action('wpcf_post_saved', $post_ID);
        return;
    }
    // OLD
    global $wpcf;
    // Basic cheks
    /*
     * Allow this hook to be triggered only if Types form is submitted
     */
    //    if ( !isset( $_POST['_wpcf_post_wpnonce'] ) || !wp_verify_nonce( $_POST['_wpcf_post_wpnonce'],
    //                    'update-' . $post->post_type . '_' . $post_ID ) ) {
    //        return false;
    //    }
    /*
     * Do not save post if is type of:
     * revision
     * attachment
     * wp-types-group
     * view
     * view-template
     * cred-form
     */
    if (in_array($post->post_type, $wpcf->excluded_post_types)) {
        return false;
    }
    /*
     *
     *
     * Get all groups connected to this $post
     */
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    if (empty($groups)) {
        return false;
    }
    $all_fields = array();
    $_not_valid = array();
    $_error = false;
    /*
     *
     *
     * Loop over each group
     *
     * TODO Document this
     * Connect 'wpcf-invalid-fields' with all fields
     */
    foreach ($groups as $group) {
        if (isset($group['fields'])) {
            // Process fields
            $fields = wpcf_admin_post_process_fields($post, $group['fields'], true, false, 'validation');
            // Validate fields
            $form = wpcf_form_simple_validate($fields);
            $all_fields = $all_fields + $fields;
            // Collect all not valid fields
            if ($form->isError()) {
                $_error = true;
                // Set error only to true
                $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid());
            }
        }
    }
    // Set fields
    foreach ($all_fields as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Field();
        $_temp->set($wpcf->post, $v['wpcf-id']);
        $all_fields[$k]['_field'] = $_temp;
    }
    foreach ($_not_valid as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Field();
        $_temp->set($wpcf->post, $v['wpcf-id']);
        $_not_valid[$k]['_field'] = $_temp;
    }
    /*
     *
     * Allow interaction here.
     * Conditional will set $error to false if field is conditional
     * and not submitted.
     */
    $error = apply_filters('wpcf_post_form_error', $_error, $_not_valid, $all_fields);
    $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields);
    // Notify user about error
    if ($error) {
        wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error');
    }
    /*
     * Save invalid elements so user can be informed after redirect.
     */
    if (!empty($not_valid)) {
        update_post_meta($post_ID, 'wpcf-invalid-fields', $not_valid);
    }
    /*
     *
     *
     *
     *
     * Save meta fields
     */
    if (!empty($_POST['wpcf'])) {
        foreach ($_POST['wpcf'] as $field_slug => $field_value) {
            // Get field by slug
            $field = wpcf_fields_get_field_by_slug($field_slug);
            if (empty($field)) {
                continue;
            }
            // Set field
            $wpcf->field->set($post_ID, $field);
            // Skip copied fields
            // CHECKPOINT
            if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                continue;
            }
            // Don't save invalid
            // CHECKPOINT
            if (isset($not_valid[$field['slug']])) {
                continue;
            }
            /*
             *
             *
             * Saving fields
             * @since 1.2
             *
             * We changed way repetitive fields are saved.
             * On each save fields are rewritten and order is saved in
             * '_$slug-sort-order' meta field.
             */
            /*
             *
             * We marked fields as repetitive in POST['__wpcf_repetitive']
             * Without this check we won't save any.
             * @see WPCF_Repeater::get_fields_form()
             */
            if (isset($_POST['__wpcf_repetitive'][$wpcf->field->slug])) {
                /*
                 * Use here WPCF_Repeater class.
                 * WPCF_Repeater::set() - switches to current post
                 * WPCF_Repeater::save() - saves repetitive field
                 */
                $wpcf->repeater->set($post_ID, $field);
                $wpcf->repeater->save();
            } else {
                /*
                 * Use WPCF_Field::save()
                 */
                $wpcf->field->save();
            }
            do_action('wpcf_post_field_saved', $post_ID, $field);
        }
    }
    /*
     * Process checkboxes
     *
     * TODO Revise and remove
     * Since Types 1.1.5 we moved this check to embedded/includes/checkbox.php
     * checkbox.php added permanently to bootstrap.
     */
    foreach ($all_fields as $field) {
        if (!isset($field['#type'])) {
            continue;
        }
        //        if ( $field['#type'] == 'checkbox'
        //                && !isset( $_POST['wpcf'][$field['wpcf-slug']] ) ) {
        //            $field_data = wpcf_admin_fields_get_field( $field['wpcf-id'] );
        //            if ( isset( $field_data['data']['save_empty'] )
        //                    && $field_data['data']['save_empty'] == 'yes' ) {
        //                update_post_meta( $post_ID,
        //                        wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'],
        //                        0 );
        //            } else {
        //                delete_post_meta( $post_ID,
        //                        wpcf_types_get_meta_prefix( $field ) . $field['wpcf-slug'] );
        //            }
        //        }
        if ($field['#type'] == 'checkboxes') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id']);
            if (!empty($field_data['data']['options'])) {
                $update_data = array();
                foreach ($field_data['data']['options'] as $option_id => $option_data) {
                    if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) {
                        if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                            $update_data[$option_id] = 0;
                        }
                    } else {
                        $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id];
                    }
                }
                update_post_meta($post_ID, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data);
            }
        }
    }
    do_action('wpcf_post_saved', $post_ID);
}
예제 #3
0
function wpcf_admin_userprofilesave_init($user_id)
{
    if (defined('WPTOOLSET_FORMS_VERSION')) {
        global $wpcf;
        $errors = false;
        /**
         * check checkbox type fields to delete or save empty if needed
         */
        $groups = wpcf_admin_usermeta_get_groups_fields();
        foreach ($groups as $group) {
            if (!array_key_exists('fields', $group) || empty($group['fields'])) {
                continue;
            }
            foreach ($group['fields'] as $field) {
                switch ($field['type']) {
                    case 'checkboxes':
                        if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) {
                            delete_user_meta($user_id, $field['meta_key']);
                        }
                        break;
                    case 'checkbox':
                        if (!array_key_exists('wpcf', $_POST) || !array_key_exists($field['slug'], $_POST['wpcf'])) {
                            if ('yes' == $field['data']['save_empty']) {
                                $_POST['wpcf'][$field['slug']] = 0;
                            } else {
                                delete_user_meta($user_id, $field['meta_key']);
                            }
                        }
                        break;
                }
            }
        }
        // Save meta fields
        if (!empty($_POST['wpcf'])) {
            foreach ($_POST['wpcf'] as $field_slug => $field_value) {
                // Get field by slug
                $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta');
                if (empty($field)) {
                    continue;
                }
                // Skip copied fields
                if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                    continue;
                }
                $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value;
                // Set config
                $config = wptoolset_form_filter_types_field($field, $user_id);
                foreach ($_field_value as $_k => $_val) {
                    // Check if valid
                    $valid = wptoolset_form_validate_field('your-profile', $config, $_val);
                    if (is_wp_error($valid)) {
                        $errors = true;
                        $_errors = $valid->get_error_data();
                        $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']);
                        wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error');
                        if (types_is_repetitive($field)) {
                            unset($field_value[$_k]);
                        } else {
                            break;
                        }
                    }
                }
                // Save field
                if (types_is_repetitive($field)) {
                    $wpcf->usermeta_repeater->set($user_id, $field);
                    $wpcf->usermeta_repeater->save($field_value);
                } else {
                    $wpcf->usermeta_field->set($user_id, $field);
                    $wpcf->usermeta_field->usermeta_save($field_value);
                }
                do_action('wpcf_user_field_saved', $user_id, $field);
                // TODO Move to checkboxes
                if ($field['type'] == 'checkboxes') {
                    $field_data = wpcf_admin_fields_get_field($field['id'], false, false, false, 'wpcf-usermeta');
                    if (!empty($field_data['data']['options'])) {
                        $update_data = array();
                        foreach ($field_data['data']['options'] as $option_id => $option_data) {
                            if (!isset($_POST['wpcf'][$field['id']][$option_id])) {
                                if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                                    $update_data[$option_id] = 0;
                                }
                            } else {
                                $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id];
                            }
                        }
                        update_user_meta($user_id, $field['meta_key'], $update_data);
                    }
                }
            }
        }
        if ($errors) {
            update_post_meta($user_id, '__wpcf-invalid-fields', true);
        }
        do_action('wpcf_user_saved', $user_id);
        return;
    }
    global $wpcf;
    $all_fields = array();
    $_not_valid = array();
    $_error = false;
    $error = '';
    $groups = $groups = wpcf_admin_usermeta_get_groups_fields();
    if (empty($groups)) {
        return false;
    }
    foreach ($groups as $group) {
        // Process fields
        $fields = wpcf_admin_usermeta_process_fields($user_id, $group['fields'], true, false, 'validation');
        // Validate fields
        $form = wpcf_form_simple_validate($fields);
        $all_fields = $all_fields + $fields;
        // Collect all not valid fields
        if ($form->isError()) {
            $_error = true;
            // Set error only to true
            $_not_valid = array_merge($_not_valid, (array) $form->get_not_valid());
        }
    }
    // Set fields
    foreach ($all_fields as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Usermeta_Field();
        $_temp->set($user_id, $v['wpcf-id']);
        $all_fields[$k]['_field'] = $_temp;
    }
    foreach ($_not_valid as $k => $v) {
        // only Types field
        if (empty($v['wpcf-id'])) {
            continue;
        }
        $_temp = new WPCF_Usermeta_Field();
        $_temp->set($user_id, $v['wpcf-id']);
        $_not_valid[$k]['_field'] = $_temp;
    }
    $not_valid = apply_filters('wpcf_post_form_not_valid', $_not_valid, $_error, $all_fields);
    // Notify user about error
    if ($error) {
        wpcf_admin_message_store(__('Please check your input data', 'wpcf'), 'error');
    }
    /*
     * Save invalid elements so user can be informed after redirect.
     */
    if (!empty($not_valid)) {
        update_user_meta($user_id, 'wpcf-invalid-fields', $not_valid);
    }
    if (!empty($_POST['wpcf'])) {
        foreach ($_POST['wpcf'] as $field_slug => $field_value) {
            $field = wpcf_fields_get_field_by_slug($field_slug, 'wpcf-usermeta');
            if (empty($field)) {
                continue;
            }
            $wpcf->usermeta_field->set($user_id, $field);
            if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                continue;
            }
            if (isset($_POST['__wpcf_repetitive'][$wpcf->usermeta_field->slug])) {
                $wpcf->usermeta_repeater->set($user_id, $field);
                $wpcf->usermeta_repeater->save();
            } else {
                $wpcf->usermeta_field->usermeta_save();
            }
            do_action('wpcf_post_field_saved', '', $field);
        }
        //end foreach
    }
    //end if
    foreach ($all_fields as $field) {
        if (!isset($field['#type'])) {
            continue;
        }
        if ($field['#type'] == 'checkbox') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta');
            if (!isset($_POST['wpcf'][$field['wpcf-slug']])) {
                if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                    update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], 0);
                } else {
                    delete_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug']);
                }
            }
        }
        if ($field['#type'] == 'checkboxes') {
            $field_data = wpcf_admin_fields_get_field($field['wpcf-id'], false, false, false, 'wpcf-usermeta');
            if (!empty($field_data['data']['options'])) {
                $update_data = array();
                foreach ($field_data['data']['options'] as $option_id => $option_data) {
                    if (!isset($_POST['wpcf'][$field['wpcf-slug']][$option_id])) {
                        if (isset($field_data['data']['save_empty']) && $field_data['data']['save_empty'] == 'yes') {
                            $update_data[$option_id] = 0;
                        }
                    } else {
                        $update_data[$option_id] = $_POST['wpcf'][$field['wpcf-slug']][$option_id];
                    }
                }
                update_user_meta($user_id, wpcf_types_get_meta_prefix($field) . $field['wpcf-slug'], $update_data);
            }
        }
    }
}
예제 #4
0
 /**
  * @param $field_config
  * @param $value
  *
  * @return true|WP_Error
  */
 private function validate_single_field_value($field_config, $value)
 {
     return wptoolset_form_validate_field($this->form_id, $field_config, $value);
 }
예제 #5
0
/**
 * The save_post hook.
 *
 * @param int $post_ID
 * @param WP_Post $post
 * @since unknown
 */
function wpcf_admin_post_save_post_hook($post_ID, $post)
{
    // Apparently, some things are *slightly* different when saving a child post in the Post relationships metabox.
    // Ugly hack that will go away with m2m.
    //
    // Note: The types_updating_child_post filter is needed for a situation when user clicks on the Update button
    // for the parent post. In that case we don't get enough information to determine if a child post is being updated.
    $is_child_post_update = 'wpcf_ajax' == wpcf_getget('action') && 'pr_save_child_post' == wpcf_getget('wpcf_action') || apply_filters('types_updating_child_post', false);
    global $wpcf;
    $errors = false;
    // Do not save post if is type of
    if (in_array($post->post_type, array('revision', 'view', 'view-template', 'cred-form', 'cred-user-form', 'nav_menu_item', 'mediapage'))) {
        return;
    }
    $_post_wpcf = array();
    if (!empty($_POST['wpcf'])) {
        $_post_wpcf = $_POST['wpcf'];
    }
    // handle checkbox
    if (array_key_exists('_wptoolset_checkbox', $_POST) && is_array($_POST['_wptoolset_checkbox'])) {
        foreach ($_POST['_wptoolset_checkbox'] as $key => $field_value) {
            $field_slug = preg_replace('/^wpcf\\-/', '', $key);
            if (array_key_exists($field_slug, $_post_wpcf)) {
                continue;
            }
            $_post_wpcf[$field_slug] = false;
        }
    }
    // handle radios
    if (array_key_exists('_wptoolset_radios', $_POST) && is_array($_POST['_wptoolset_radios'])) {
        foreach ($_POST['_wptoolset_radios'] as $key => $field_value) {
            $field_slug = preg_replace('/^wpcf\\-/', '', $key);
            if (array_key_exists($field_slug, $_post_wpcf)) {
                continue;
            }
            $_post_wpcf[$field_slug] = false;
        }
    }
    if (count($_post_wpcf)) {
        $add_error_message = true;
        if (isset($_POST['post_id']) && $_POST['post_id'] != $post_ID) {
            $add_error_message = false;
        }
        // check some attachment to delete
        $delete_attachments = apply_filters('wpcf_delete_attachments', true);
        $images_to_delete = array();
        if ($delete_attachments && isset($_POST['wpcf']) && array_key_exists('delete-image', $_POST['wpcf']) && is_array($_POST['wpcf']['delete-image'])) {
            foreach ($_POST['wpcf']['delete-image'] as $image) {
                $images_to_delete[$image] = 1;
            }
        }
        foreach ($_post_wpcf as $field_slug => $field_value) {
            // Get field by slug
            $field = wpcf_fields_get_field_by_slug($field_slug);
            if (empty($field)) {
                continue;
            }
            // Skip copied fields
            if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                continue;
            }
            // This is (apparently) expected for repetitive fields...
            if ($is_child_post_update && types_is_repetitive($field)) {
                $field_value = array($field_value);
            }
            $_field_value = !types_is_repetitive($field) ? array($field_value) : $field_value;
            // Set config
            $config = wptoolset_form_filter_types_field($field, $post_ID, $_post_wpcf);
            // remove from images_to_delete if user add again
            if ($delete_attachments && 'image' == $config['type']) {
                $images = $_field_value;
                if (!is_array($images)) {
                    $images = array($images);
                }
                foreach ($images as $image) {
                    if (array_key_exists($image, $images_to_delete)) {
                        unset($images_to_delete[$image]);
                    }
                }
            }
            // add filter to remove field name from error message
            // This filter is toolset-common/toolset-forms/classes/class.validation.php
            add_filter('toolset_common_validation_add_field_name_to_error', '__return_false', 1234, 1);
            foreach ($_field_value as $_k => $_val) {
                // Check if valid
                $validation = wptoolset_form_validate_field('post', $config, $_val);
                $conditional = wptoolset_form_conditional_check($config);
                $not_valid = is_wp_error($validation) || !$conditional;
                if ($add_error_message && is_wp_error($validation)) {
                    $errors = true;
                    $_errors = $validation->get_error_data();
                    $_msg = sprintf(__('Field "%s" not updated:', 'wpcf'), $field['name']);
                    wpcf_admin_message_store($_msg . ' ' . implode(', ', $_errors), 'error');
                }
                if ($not_valid) {
                    if (types_is_repetitive($field)) {
                        unset($field_value[$_k]);
                    } else {
                        break;
                    }
                }
            }
            remove_filter('toolset_common_validation_add_field_name_to_error', '__return_false', 1234, 1);
            // Save field
            if (types_is_repetitive($field)) {
                $wpcf->repeater->set($post_ID, $field);
                $wpcf->repeater->save($field_value);
            } else {
                if (!$not_valid) {
                    $wpcf->field->set($post_ID, $field);
                    $wpcf->field->save($field_value);
                }
            }
            do_action('wpcf_post_field_saved', $post_ID, $field);
            // TODO Move to checkboxes
            if ($field['type'] == 'checkboxes') {
                if (!empty($field['data']['options'])) {
                    $update_data = array();
                    foreach ($field['data']['options'] as $option_id => $option_data) {
                        if (!isset($_POST['wpcf'][$field['id']][$option_id])) {
                            if (isset($field['data']['save_empty']) && $field['data']['save_empty'] == 'yes') {
                                $update_data[$option_id] = 0;
                            }
                        } else {
                            $update_data[$option_id] = $_POST['wpcf'][$field['id']][$option_id];
                        }
                    }
                    update_post_meta($post_ID, $field['meta_key'], $update_data);
                }
            }
        }
        // delete images
        if ($delete_attachments && !empty($images_to_delete)) {
            $args = array('post_parent' => $post_ID, 'posts_per_page' => -1);
            $children_array = get_children($args);
            foreach ($children_array as $child) {
                if (!array_key_exists($child->guid, $images_to_delete)) {
                    continue;
                }
                wp_delete_attachment($child->ID);
            }
        }
    }
    if ($errors) {
        update_post_meta($post_ID, '__wpcf-invalid-fields', true);
    }
    do_action('wpcf_post_saved', $post_ID);
    return;
}
예제 #6
0
 /**
  * New validation using API calls from toolset-forms.
  *
  * Uses API cals
  * @uses wptoolset_form_validate_field()
  * @uses wptoolset_form_conditional_check()
  *
  * @todo Make it work with other fields (generic)
  *
  * @param type $post_id
  * @param type $values
  * @return boolean
  */
 function validate($post_id, $values)
 {
     $form_id = $this->form_id;
     $valid = true;
     // Loop over fields
     $form_source_data = $this->_formData->getForm()->post_content;
     preg_match_all("/\\[cred_show_group.*cred_show_group\\]/Uism", $form_source_data, $res);
     $conditional_group_fields = array();
     if (count($res[0]) > 0) {
         for ($i = 0, $res_limit = count($res[0]); $i < $res_limit; $i++) {
             preg_match_all("/field=\"([^\"]+)\"/Uism", $res[0][$i], $parsed_fields);
             if (count($parsed_fields[1]) > 0) {
                 for ($j = 0, $count_parsed_fields = count($parsed_fields); $j < $count_parsed_fields; $j++) {
                     if (!empty($parsed_fields[1][$j])) {
                         $conditional_group_fields[] = trim($parsed_fields[1][$j]);
                     }
                 }
             }
         }
     }
     foreach ($this->form_properties['fields'] as $field) {
         if (in_array(str_replace('wpcf-', '', $field['name']), $conditional_group_fields)) {
             continue;
         }
         // If Types field
         if (isset($field['plugin_type']) && $field['plugin_type'] == 'types') {
             $field_name = $field['name'];
             if (!isset($_POST[$field_name])) {
                 continue;
             }
             /* 	
                               // Adjust field ID
                               $field['id'] = strpos( $field['name'], 'wpcf-' ) === 0 ? substr( $field['name'], 5 ) : $field['name'];
                               // CRED have synonym 'text' for textfield
                               if ( $field['type'] == 'text' ) {
                               $field['type'] = 'textfield';
                               } */
             $field = wpcf_fields_get_field_by_slug(str_replace('wpcf-', '', $field['name']));
             if (empty($field)) {
                 continue;
             }
             // Skip copied fields
             if (isset($_POST['wpcf_repetitive_copy'][$field['slug']])) {
                 continue;
             }
             // Set field config
             $config = wptoolset_form_filter_types_field($field, $post_id);
             //Add custom colorpicer hexadecimal backend validator
             //Fix https://icanlocalize.basecamphq.com/projects/7393061-toolset/todo_items/193712665/comments
             //Todo in future add hexadecimal check in types
             if ($field['type'] == 'colorpicker') {
                 $config['validation']['hexadecimal'] = array('args' => array('$value', '1'), 'message' => 'Enter valid hexadecimal value');
             }
             if (isset($config['conditional']['values'])) {
                 foreach ($config['conditional']['values'] as $post_key => $post_value) {
                     if (isset($this->form_properties['fields'][$post_key])) {
                         $config['conditional']['values'][$post_key] = $this->form_properties['fields'][$post_key]['value'];
                     }
                 }
             }
             // Set values to loop over
             $_values = !empty($values[$field_name]) ? $values[$field_name]['value'] : null;
             if (empty($config['repetitive'])) {
                 $_values = array($_values);
             }
             // Loop over each value
             if (is_array($_values)) {
                 foreach ($_values as $value) {
                     $validation = wptoolset_form_validate_field($form_id, $config, $value);
                     $conditional = wptoolset_form_conditional_check($config);
                     /**
                      * add form_errors messages
                      */
                     if (is_wp_error($validation) && $conditional) {
                         $error_data = $validation->get_error_data();
                         if (isset($error_data[0])) {
                             $this->add_top_message($error_data[0], $config['id']);
                         } else {
                             $this->add_top_message($validation->get_error_message(), $config['id']);
                         }
                         $valid = false;
                         if (empty($ret_validation)) {
                             continue;
                         }
                         //                            foreach( $errors as $error ) {
                         //                                $error = explode( ' ', $error );
                         //                                $key = array_shift($error);
                         //                                $error = implode( ' ', $error );
                         //                                $this->form_errors[$key] = $error;
                         //                                $this->form_messages[$key] = $validation->get_error_message();
                         //                            }
                     }
                 }
             }
         } elseif (!isset($field['plugin_type']) && isset($field['validation'])) {
             if (!isset($_POST[$field['name']])) {
                 continue;
             }
             $config = array('id' => $field['name'], 'type' => $field['type'], 'slug' => $field['name'], 'title' => $field['name'], 'description' => '', 'name' => $field['name'], 'repetitive' => $field['repetitive'], 'validation' => $field['validation'], 'conditional' => array());
             $value = $field['value'];
             require_once WPTOOLSET_FORMS_ABSPATH . '/classes/class.types.php';
             $validation = array();
             foreach ($field['validation'] as $rule => $settings) {
                 if ($settings['active']) {
                     $id = $config['slug'];
                     $validation[$rule] = array('args' => isset($settings['args']) ? array_unshift($value, $settings['args']) : array($value, true), 'message' => WPToolset_Types::translate('field ' . $id . ' validation message ' . $rule, $settings['message']));
                 }
             }
             $config['validation'] = $validation;
             $validation = wptoolset_form_validate_field($form_id, $config, $value);
             if (is_wp_error($validation)) {
                 $error_data = $validation->get_error_data();
                 //TODO: replace id with name
                 if (isset($error_data[0])) {
                     $this->add_top_message($error_data[0], $config['id']);
                 } else {
                     $this->add_top_message($validation->get_error_message(), $config['id']);
                 }
                 $valid = false;
                 if (empty($ret_validation)) {
                     continue;
                 }
                 //                    foreach( $errors as $error ) {
                 //                        $error = explode( ' ', $error );
                 //                        $key = array_shift($error);
                 //                        $error = implode( ' ', $error );
                 //                        $this->form_errors[$key] = $error;
                 //                        $this->form_messages[$key] = $validation->get_error_message();
                 //                    }
             }
         }
     }
     return $valid;
 }