Ejemplo n.º 1
0
/**
 * save_post hook.
 * 
 * @param type $post_ID
 * @param type $post 
 */
function wpcf_admin_post_save_post_hook($post_ID, $post)
{
    if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-' . $post->post_type . '_' . $post_ID)) {
        return false;
    }
    if (!in_array($post->post_type, array('revision', 'attachment', 'wp-types-group', 'view', 'view-template'))) {
        // Get groups
        $groups = wpcf_admin_post_get_post_groups_fields($post);
        if (empty($groups)) {
            return false;
        }
        $all_fields = array();
        foreach ($groups as $group) {
            // Process fields
            // Fix duplicates for repetitive fields
            foreach ($group['fields'] as $temp_field_id => $temp_field_data) {
                if (isset($temp_field_data['data']['repetitive']) && isset($_REQUEST['wpcf'][$temp_field_id])) {
                    if (count($_POST['wpcf'][$temp_field_id]) < 2) {
                        continue;
                    }
                    $temp_check_duplicates_old_value = array();
                    $temp_check_duplicates_new_value = array();
                    foreach ($_POST['wpcf'][$temp_field_id] as $posted_field_key => $posted_field_value) {
                        $temp_old_value = base64_decode($posted_field_value['old_value']);
                        $temp_new_value = isset($posted_field_value['new_value']) ? $posted_field_value['new_value'] : serialize($posted_field_value);
                        $temp_field_key = wpcf_types_get_meta_prefix($temp_field_data) . $temp_field_id;
                        // Check new values
                        if (in_array(md5($temp_new_value), $temp_check_duplicates_new_value)) {
                            unset($_POST['wpcf'][$temp_field_id][$posted_field_key]);
                            if ($temp_old_value != '__wpcf_repetitive_new_field') {
                                // Rebuild
                                delete_post_meta($post_ID, $temp_field_key, $temp_old_value);
                                update_post_meta($post_ID, $temp_field_key, $temp_old_value);
                            }
                        } else {
                            if ($temp_old_value == '__wpcf_repetitive_new_field') {
                                $_POST['wpcf'][$temp_field_id][intval($posted_field_key) * 1000] = $_POST['wpcf'][$temp_field_id][$posted_field_key];
                                unset($_POST['wpcf'][$temp_field_id][$posted_field_key]);
                                continue;
                            }
                            $temp_check_duplicates_new_value[] = md5($temp_new_value);
                        }
                        // Check old values
                        //                        if ($temp_old_value != '__wpcf_repetitive_new_field') {
                        //                            if (in_array(md5($temp_old_value),
                        //                                            $temp_check_duplicates_old_value)) {
                        //                                unset($_POST['wpcf'][$temp_field_id][$posted_field_key]);
                        //                                if ($temp_old_value != '__wpcf_repetitive_new_field') {
                        //                                    // Rebuild
                        //                                    delete_post_meta($post_ID, $temp_field_key,
                        //                                            $temp_old_value);
                        //                                    update_post_meta($post_ID, $temp_field_key,
                        //                                            $temp_old_value);
                        //                                }
                        //                            } else {
                        //                                if ($temp_old_value == '__wpcf_repetitive_new_field') {
                        //                                    $_POST['wpcf'][$temp_field_id][intval($posted_field_key) * 1000] = $_POST['wpcf'][$temp_field_id][$posted_field_key];
                        //                                    unset($_POST['wpcf'][$temp_field_id][$posted_field_key]);
                        //                                    continue;
                        //                                }
                        //                                $temp_check_duplicates_old_value[] = md5($temp_old_value);
                        //                            }
                        //                        }
                    }
                }
            }
            $_REQUEST['wpcf'] = $_POST['wpcf'];
            // Fix indexes for repetitive
            foreach ($group['fields'] as $temp_field_id => $temp_field_data) {
                if (isset($temp_field_data['data']['repetitive']) && isset($_REQUEST['wpcf'][$temp_field_id])) {
                    $temp_count = 1;
                    $temp_data = $_REQUEST['wpcf'][$temp_field_id];
                    unset($_REQUEST['wpcf'][$temp_field_id]);
                    $_REQUEST['wpcf'][$temp_field_id] = array();
                    foreach ($temp_data as $temp_post_key => $temp_post_data) {
                        $_REQUEST['wpcf'][$temp_field_id][$temp_count] = $temp_post_data;
                        $temp_count += 1;
                    }
                }
            }
            $_POST['wpcf'] = $_REQUEST['wpcf'];
            $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;
            $error = $form->isError();
            // Trigger form 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($all_fields)) {
            update_post_meta($post_ID, 'wpcf-invalid-fields', $all_fields);
        }
        // Save meta fields
        if (!empty($_POST['wpcf'])) {
            foreach ($_POST['wpcf'] as $field_slug => $field_value) {
                // Repetitive fields
                if (isset($_POST['wpcf_repetitive'][$field_slug])) {
                    foreach ($field_value as $temp_id => $repetitive_data) {
                        $field = wpcf_fields_get_field_by_slug($field_slug);
                        // Skype specific
                        if ($field['type'] == 'skype') {
                            wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $repetitive_data, base64_decode($repetitive_data['old_value']));
                        } else {
                            wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $repetitive_data['new_value'], base64_decode($repetitive_data['old_value']));
                        }
                    }
                } else {
                    wpcf_admin_post_save_field($post_ID, $all_fields, $field_slug, $field_value);
                }
            }
        }
    }
}
Ejemplo n.º 2
0
/**
 * save_post hook.
 * 
 * @param type $post_ID
 * @param type $post 
 */
function wpcf_admin_post_save_post_hook($post_ID, $post)
{
    if (!isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'update-' . $post->post_type . '_' . $post_ID)) {
        return false;
    }
    if (in_array($post->post_type, array('revision', 'attachment', 'wp-types-group', 'view', 'view-template'))) {
        return false;
    }
    // Get groups
    $groups = wpcf_admin_post_get_post_groups_fields($post);
    if (empty($groups)) {
        return false;
    }
    $all_fields = array();
    foreach ($groups as $group) {
        // 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;
        $error = $form->isError();
        // Trigger form 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($all_fields)) {
        update_post_meta($post_ID, 'wpcf-invalid-fields', $all_fields);
    }
    // Save meta fields
    if (!empty($_POST['wpcf'])) {
        foreach ($_POST['wpcf'] as $field_slug => $field_value) {
            // Skip copied fields
            if (isset($_POST['wpcf_repetitive_copy'][$field_slug])) {
                continue;
            }
            $field = wpcf_fields_get_field_by_slug($field_slug);
            if (empty($field)) {
                continue;
            }
            $meta_key = wpcf_types_get_meta_prefix($field) . $field_slug;
            // Don't save invalid
            if (isset($all_fields[$meta_key]['#error'])) {
                continue;
            }
            // Repetitive fields
            if (isset($_POST['wpcf_repetitive'][$field_slug])) {
                delete_post_meta($post_ID, $meta_key);
                foreach ($field_value as $temp_id => $repetitive_data) {
                    // Skype specific
                    if ($field['type'] == 'skype') {
                        unset($repetitive_data['old_value']);
                        wpcf_admin_post_save_field($post_ID, $meta_key, $field, $repetitive_data, true);
                    } else {
                        wpcf_admin_post_save_field($post_ID, $meta_key, $field, $repetitive_data['new_value'], true);
                    }
                }
            } else {
                wpcf_admin_post_save_field($post_ID, $meta_key, $field, $field_value);
            }
        }
    }
    // Process checkboxes
    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);
            }
        }
    }
}