Example #1
0
function ninja_forms_attach_files_to_post($post_id)
{
    global $ninja_forms_processing;
    if ($ninja_forms_processing->get_extra_value('uploads')) {
        foreach ($ninja_forms_processing->get_extra_value('uploads') as $field_id) {
            $field_row = $ninja_forms_processing->get_field_settings($field_id);
            $user_value = $ninja_forms_processing->get_field_value($field_id);
            if (is_array($user_value) and !empty($user_value)) {
                $tmp_array = array();
                $args = array('post_parent' => $post_id, 'post_status' => 'null', 'post_type' => 'attachment');
                $attachments = get_posts($args);
                if (!empty($attachments)) {
                    $x = 0;
                    foreach ($attachments as $attachment) {
                        $attach_field = get_post_meta($attachment->ID, 'ninja_forms_field_id', true);
                        $file_key = get_post_meta($attachment->ID, 'ninja_forms_file_key', true);
                        $upload_id = get_post_meta($attachment->ID, 'ninja_forms_upload_id', true);
                        if ($attach_field == $field_id) {
                            if (!array_key_exists($file_key, $user_value)) {
                                if ($upload_id != '') {
                                    ninja_forms_delete_upload($upload_id);
                                }
                                wp_delete_attachment($attachment->ID);
                            } else {
                                $tmp_array[$x]['id'] = $attachment->ID;
                                $tmp_array[$x]['field_id'] = $attach_field;
                                $tmp_array[$x]['file_key'] = $file_key;
                            }
                        } else {
                            if ($attach_field == '') {
                                //wp_update_post( array( 'ID' => $attachment->ID, 'post_parent' => 0 ) );
                            }
                        }
                        $x++;
                    }
                }
                $attachments = $tmp_array;
                foreach ($user_value as $key => $file) {
                    if (!isset($file['changed']) or $file['changed'] == 1) {
                        foreach ($attachments as $attachment) {
                            if ($attachment['file_key'] == $key) {
                                $upload_id = get_post_meta($attachment['id'], 'ninja_forms_upload_id', true);
                                if ($upload_id != '') {
                                    ninja_forms_delete_upload($upload_id);
                                }
                                wp_delete_attachment($attachment['id']);
                            }
                        }
                        if (isset($file['complete']) and $file['complete'] == 1) {
                            $filename = $file['file_path'] . $file['file_name'];
                            $attach_array = ninja_forms_generate_metadata($post_id, $filename);
                            $attach_id = $attach_array['attach_id'];
                            $attach_data = $attach_array['attach_data'];
                            if (!empty($attach_array) and isset($field_row['data']['featured_image']) and $field_row['data']['featured_image'] == 1) {
                                ninja_forms_set_featured_image($post_id, $attach_id);
                            }
                            update_post_meta($attach_id, 'ninja_forms_field_id', $field_id);
                            update_post_meta($attach_id, 'ninja_forms_file_key', $key);
                            update_post_meta($attach_id, 'ninja_forms_upload_id', $file['upload_id']);
                            $file['attachment_id'] = $attach_id;
                            $ninja_forms_processing->update_field_value($field_id, array($key => $file));
                        }
                    }
                }
            }
        }
    }
}
Example #2
0
function ninja_forms_save_browse_uploads($data)
{
    if (isset($data['bulk_action']) and $data['bulk_action'] == 'delete') {
        if (isset($data['ninja_forms_uploads'])) {
            if (is_array($data['ninja_forms_uploads']) and !empty($data['ninja_forms_uploads'])) {
                foreach ($data['ninja_forms_uploads'] as $upload) {
                    ninja_forms_delete_upload($upload);
                }
                $update_msg = count($_POST['ninja_forms_uploads']) . ' ';
                if (count($_POST['ninja_forms_uploads']) > 1) {
                    $update_msg .= __('Uploads Deleted', 'ninja-forms');
                } else {
                    $update_msg .= __('Upload Deleted', 'ninja-forms');
                }
            }
            return $update_msg;
        }
    }
}
function ninja_forms_attach_files_to_post($post_id)
{
    global $ninja_forms_processing;
    if ($ninja_forms_processing->get_extra_value('uploads')) {
        foreach ($ninja_forms_processing->get_extra_value('uploads') as $field_id) {
            $field_row = $ninja_forms_processing->get_field_settings($field_id);
            $user_value = $ninja_forms_processing->get_field_value($field_id);
            if (is_array($user_value) and !empty($user_value)) {
                $tmp_array = array();
                $args = array('post_parent' => $post_id, 'post_status' => 'null', 'post_type' => 'attachment', 'posts_per_page' => -1);
                $attachments = get_posts($args);
                if (!empty($attachments)) {
                    $x = 0;
                    foreach ($attachments as $attachment) {
                        $attach_field = get_post_meta($attachment->ID, 'ninja_forms_field_id', true);
                        $file_key = get_post_meta($attachment->ID, 'ninja_forms_file_key', true);
                        $upload_id = get_post_meta($attachment->ID, 'ninja_forms_upload_id', true);
                        if ($attach_field == $field_id) {
                            if (!array_key_exists($file_key, $user_value)) {
                                if ($upload_id != '') {
                                    ninja_forms_delete_upload($upload_id);
                                }
                                wp_delete_attachment($attachment->ID);
                            } else {
                                $tmp_array[$x]['id'] = $attachment->ID;
                                $tmp_array[$x]['field_id'] = $attach_field;
                                $tmp_array[$x]['file_key'] = $file_key;
                            }
                        } else {
                            if ($attach_field == '') {
                                wp_update_post(array('ID' => $attachment->ID, 'post_parent' => 0));
                            }
                        }
                        $x++;
                    }
                }
                $attachments = $tmp_array;
                foreach ($user_value as $key => $file) {
                    // Check to see if we are changing files that already exist.
                    if (!isset($file['changed']) or $file['changed'] == 1) {
                        foreach ($attachments as $attachment) {
                            if ($attachment['file_key'] == $key) {
                                $upload_id = get_post_meta($attachment['id'], 'ninja_forms_upload_id', true);
                                if ($upload_id != '') {
                                    ninja_forms_delete_upload($upload_id);
                                }
                                wp_delete_attachment($attachment['id']);
                            }
                        }
                    }
                    if (isset($file['complete']) and $file['complete'] == 1 && (!isset($file['changed']) || $file['changed'] == 1)) {
                        $filename = $file['file_path'] . $file['file_name'];
                        $attach_array = ninja_forms_generate_metadata($post_id, $filename);
                        $attach_id = $attach_array['attach_id'];
                        $attach_data = $attach_array['attach_data'];
                        if (!empty($attach_array) and isset($field_row['data']['featured_image']) and $field_row['data']['featured_image'] == 1) {
                            ninja_forms_set_featured_image($post_id, $attach_id);
                        }
                        update_post_meta($attach_id, 'ninja_forms_field_id', $field_id);
                        update_post_meta($attach_id, 'ninja_forms_file_key', $key);
                        update_post_meta($attach_id, 'ninja_forms_upload_id', $file['upload_id']);
                        $file['attachment_id'] = $attach_id;
                        $user_value[$key] = $file;
                    }
                }
                $ninja_forms_processing->update_field_value($field_id, $user_value);
            } else {
                $args = array('post_parent' => $post_id, 'post_status' => 'null', 'post_type' => 'attachment', 'posts_per_page' => -1);
                $attachments = get_posts($args);
                // Loop through our attachments and make sure that we don't have any empty fields.
                foreach ($ninja_forms_processing->get_all_fields() as $field_id => $user_value) {
                    if ($ninja_forms_processing->get_field_setting($field_id, 'type') == '_upload') {
                        if (!empty($attachments)) {
                            foreach ($attachments as $attachment) {
                                $attach_field = get_post_meta($attachment->ID, 'ninja_forms_field_id', true);
                                if ($attach_field == $field_id && empty($user_value)) {
                                    wp_delete_attachment($attachment->ID);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}