/** * Deletes all the copies of the original attachment (database data only, not files) */ function file_gallery_delete_all_attachment_copies($attachment_id) { $copies = get_post_meta($attachment_id, '_has_copies', true); if (is_array($copies) && !empty($copies)) { do_action('file_gallery_delete_all_attachment_copies', $attachment_id, array(&$copies)); foreach ($copies as $copy) { file_gallery_delete_attachment($copy); } return $copies; } // no copies return false; }
/** * Displays the main form for inserting shortcodes / single images. * also handles attachments edit/delete/detach response * and displays atachment thumbnails on post edit screen in admin */ function file_gallery_main($ajax = true) { global $wpdb; check_ajax_referer('file-gallery'); $post_id = isset($_POST['post_id']) ? $_POST['post_id'] : ''; $attachment_order = isset($_POST['attachment_order']) ? $_POST['attachment_order'] : ''; $attachment_orderby = isset($_POST['attachment_orderby']) ? $_POST['attachment_orderby'] : ''; $files_or_tags = isset($_POST['files_or_tags']) ? $_POST["files_or_tags"] : ''; $tags_from = isset($_POST['tags_from']) ? $_POST["tags_from"] : ''; $action = isset($_POST['action']) ? $_POST['action'] : ''; $attachment_ids = isset($_POST['attachment_ids']) ? $_POST['attachment_ids'] : ''; $attachment_data = isset($_POST['attachment_data']) ? $_POST['attachment_data'] : ''; $delete_what = isset($_POST['delete_what']) ? $_POST['delete_what'] : ''; $checked_attachments = isset($_POST['checked_attachments']) ? explode(',', $_POST['checked_attachments']) : array(); $normals = isset($_POST['normals']) ? $_POST['normals'] : ''; $copies = isset($_POST['copies']) ? $_POST['copies'] : ''; $originals = isset($_POST['originals']) ? $_POST['originals'] : ''; $fieldsets = isset($_POST['fieldsets']) ? $_POST['fieldsets'] : ''; $file_gallery_options = get_option('file_gallery'); $gallery_state = isset($file_gallery_options['insert_options_state']) && true == $file_gallery_options['insert_options_state'] ? true : false; $single_state = isset($file_gallery_options['insert_single_options_state']) && true == $file_gallery_options['insert_single_options_state'] ? true : false; $output = " "; $count_attachments = 0; $hide_form = ''; $sizes = file_gallery_get_intermediate_image_sizes(); $normals = explode(',', $normals); $copies = explode(',', $copies); $originals = explode(',', $originals); $attachment_ids = explode(',', $attachment_ids); if (empty_array($normals)) { $normals = array(); } if (empty_array($copies)) { $copies = array(); } if (empty_array($originals)) { $originals = array(); } if (empty_array($attachment_ids)) { $attachment_ids = array(); } if ('file_gallery_main_delete' == $action) { if (!empty($copies) && !empty($originals)) { $cpluso = array_merge($copies, $originals); $normals = array_xor((array) $attachment_ids, $cpluso); } elseif (!empty($copies)) { $normals = array_xor((array) $attachment_ids, $copies); } elseif (!empty($originals)) { $normals = array_xor((array) $attachment_ids, $originals); } else { $normals = $attachment_ids; } // cancel our own 'wp_delete_attachment' filter define("FILE_GALLERY_SKIP_DELETE_CANCEL", true); foreach ($normals as $normal) { if (current_user_can('delete_post', $normal)) { wp_delete_attachment($normal); $fully_deleted[] = $normal; } } foreach ($copies as $copy) { if (current_user_can('delete_post', $copy)) { file_gallery_delete_attachment($copy); $partially_deleted[] = $copy; } } foreach ($originals as $original) { if ("all" == $delete_what && current_user_can('delete_post', $original)) { file_gallery_delete_all_attachment_copies($original); wp_delete_attachment($original); $fully_deleted[] = $original; } elseif ("data_only" == $delete_what && current_user_can('delete_post', $original)) { file_gallery_promote_first_attachment_copy($original); file_gallery_delete_attachment($original); $partially_deleted[] = $original; } } if (empty($fully_deleted) && empty($partially_deleted)) { $output = __("No attachments were deleted (capabilities?)", "file-gallery"); } else { $output = __("Attachment(s) deleted", "file-gallery"); } } elseif ("file_gallery_main_detach" == $action) { foreach ($attachment_ids as $attachment_id) { if (false === $wpdb->query(sprintf("UPDATE {$wpdb->posts} SET `post_parent`='0' WHERE `ID`='%d'", $attachment_id))) { $detach_errors[] = $attachment_id; } } if (empty($detach_errors)) { $output = __("Attachment(s) detached", "file-gallery"); } else { $output = __("Error detaching attachment(s)", "file-gallery"); } } elseif ("file_gallery_main_update" == $action) { $attachment_id = (int) $_POST['attachment_id']; $attachment_data['ID'] = $attachment_id; $attachment_data['post_alt'] = $_POST['post_alt']; $attachment_data['post_title'] = $_POST['post_title']; $attachment_data['post_content'] = $_POST['post_content']; $attachment_data['post_excerpt'] = $_POST['post_excerpt']; $attachment_data['menu_order'] = $_POST['menu_order']; // attachment custom fields $custom = get_post_custom($attachment_id); $custom_fields = isset($_POST['custom_fields']) ? $_POST['custom_fields'] : ''; if (!empty($custom) && !empty($custom_fields)) { foreach ($custom_fields as $key => $val) { if (isset($custom[$key]) && $custom[$key][0] != $val) { update_post_meta($attachment_id, $key, $val); } } } // media_tag taxonomy - attachment tags $tax_input = ""; $old_media_tags = ""; $get_old_media_tags = wp_get_object_terms((int) $_POST['attachment_id'], FILE_GALLERY_MEDIA_TAG_NAME); if (!empty($get_old_media_tags)) { foreach ($get_old_media_tags as $mt) { $old_media_tags[] = $mt->name; } $old_media_tags = implode(", ", $old_media_tags); } if ("" != $_POST['tax_input'] && $old_media_tags != $_POST['tax_input']) { $tax_input = preg_replace("#\\s+#", " ", $_POST['tax_input']); $tax_input = preg_replace("#,+#", ",", $_POST['tax_input']); $tax_input = trim($tax_input, " "); $tax_input = trim($tax_input, ","); $tax_input = explode(", ", $tax_input); $media_tags_result = wp_set_object_terms($attachment_id, $tax_input, FILE_GALLERY_MEDIA_TAG_NAME); } elseif ("" == $_POST['tax_input']) { $media_tags_result = wp_set_object_terms($attachment_id, NULL, FILE_GALLERY_MEDIA_TAG_NAME); } // check if there were any changes $old_attachment_data = get_object_vars(get_post($attachment_id)); if (file_gallery_file_is_displayable_image(get_attached_file($attachment_id))) { $old_attachment_data['post_alt'] = get_post_meta($attachment_id, "_wp_attachment_image_alt", true); } if (isset($old_attachment_data['post_alt']) && $old_attachment_data['post_alt'] != $attachment_data['post_alt'] || $old_attachment_data['post_title'] != $attachment_data['post_title'] || $old_attachment_data['post_content'] != $attachment_data['post_content'] || $old_attachment_data['post_excerpt'] != $attachment_data['post_excerpt'] || $old_attachment_data['menu_order'] != $attachment_data['menu_order'] || is_array($tax_input)) { if (0 !== wp_update_post($attachment_data)) { update_post_meta($attachment_id, "_wp_attachment_image_alt", $attachment_data['post_alt']); $output = __("Attachment data updated", "file-gallery"); } else { $output = __("Error updating attachment data!", "file-gallery"); } } else { $output = __("No change.", "file-gallery"); } } elseif ("file_gallery_set_post_thumb" == $action) { update_post_meta($post_id, "_thumbnail_id", $attachment_ids[0]); exit(_wp_post_thumbnail_html($attachment_ids[0], $post_id)); } elseif ("file_gallery_unset_post_thumb" == $action) { exit; } include_once "main-form.php"; exit; }