/** * Trash or delete an attachment. * * When an attachment is permanently deleted, the file will also be removed. * Deletion removes all post meta fields, taxonomy, comments, etc. associated * with the attachment (except the main post). * * The attachment is moved to the trash instead of permanently deleted unless trash * for media is disabled, item is already in the trash, or $force_delete is true. * * @since 0.0.1 * * @global hqdb $hqdb HiveQueen database abstraction object. * * @param int $post_id Attachment ID. * @param bool $force_delete Optional. Whether to bypass trash and force deletion. * Default false. * @return mixed False on failure. Post data on success. */ function hq_delete_attachment($post_id, $force_delete = false) { global $hqdb; if (!($post = $hqdb->get_row($hqdb->prepare("SELECT * FROM {$hqdb->posts} WHERE ID = %d", $post_id)))) { return $post; } if ('attachment' != $post->post_type) { return false; } if (!$force_delete && EMPTY_TRASH_DAYS && MEDIA_TRASH && 'trash' != $post->post_status) { return hq_trash_post($post_id); } delete_post_meta($post_id, '_hq_trash_meta_status'); delete_post_meta($post_id, '_hq_trash_meta_time'); $meta = hq_get_attachment_metadata($post_id); $backup_sizes = get_post_meta($post->ID, '_hq_attachment_backup_sizes', true); $file = get_attached_file($post_id); if (is_multisite()) { delete_transient('dirsize_cache'); } /** * Fires before an attachment is deleted, at the start of hq_delete_attachment(). * * @since 0.0.1 * * @param int $post_id Attachment ID. */ do_action('delete_attachment', $post_id); hq_delete_object_term_relationships($post_id, array('category', 'post_tag')); hq_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); // Delete all for any posts. delete_metadata('post', null, '_thumbnail_id', $post_id, true); $comment_ids = $hqdb->get_col($hqdb->prepare("SELECT comment_ID FROM {$hqdb->comments} WHERE comment_post_ID = %d", $post_id)); foreach ($comment_ids as $comment_id) { hq_delete_comment($comment_id, true); } $post_meta_ids = $hqdb->get_col($hqdb->prepare("SELECT meta_id FROM {$hqdb->postmeta} WHERE post_id = %d ", $post_id)); foreach ($post_meta_ids as $mid) { delete_metadata_by_mid('post', $mid); } /** This action is documented in hq-includes/post.php */ do_action('delete_post', $post_id); $result = $hqdb->delete($hqdb->posts, array('ID' => $post_id)); if (!$result) { return false; } /** This action is documented in hq-includes/post.php */ do_action('deleted_post', $post_id); $uploadpath = hq_upload_dir(); if (!empty($meta['thumb'])) { // Don't delete the thumb if another attachment uses it. if (!$hqdb->get_row($hqdb->prepare("SELECT meta_id FROM {$hqdb->postmeta} WHERE meta_key = '_hq_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $hqdb->esc_like($meta['thumb']) . '%', $post_id))) { $thumbfile = str_replace(basename($file), $meta['thumb'], $file); /** This filter is documented in hq-includes/functions.php */ $thumbfile = apply_filters('hq_delete_file', $thumbfile); @unlink(path_join($uploadpath['basedir'], $thumbfile)); } } // Remove intermediate and backup images if there are any. if (isset($meta['sizes']) && is_array($meta['sizes'])) { foreach ($meta['sizes'] as $size => $sizeinfo) { $intermediate_file = str_replace(basename($file), $sizeinfo['file'], $file); /** This filter is documented in hq-includes/functions.php */ $intermediate_file = apply_filters('hq_delete_file', $intermediate_file); @unlink(path_join($uploadpath['basedir'], $intermediate_file)); } } if (is_array($backup_sizes)) { foreach ($backup_sizes as $size) { $del_file = path_join(dirname($meta['file']), $size['file']); /** This filter is documented in hq-includes/functions.php */ $del_file = apply_filters('hq_delete_file', $del_file); @unlink(path_join($uploadpath['basedir'], $del_file)); } } hq_delete_file($file); clean_post_cache($post); return $post; }
/** * Display third step of custom header image page. * * @since 0.0.1 */ public function step_3() { check_admin_referer('custom-header-crop-image'); if (!current_theme_supports('custom-header', 'uploads')) { hq_die(__('Cheatin’ uh?'), 403); } if (!empty($_POST['skip-cropping']) && !(current_theme_supports('custom-header', 'flex-height') || current_theme_supports('custom-header', 'flex-width'))) { hq_die(__('Cheatin’ uh?'), 403); } if ($_POST['oitar'] > 1) { $_POST['x1'] = $_POST['x1'] * $_POST['oitar']; $_POST['y1'] = $_POST['y1'] * $_POST['oitar']; $_POST['width'] = $_POST['width'] * $_POST['oitar']; $_POST['height'] = $_POST['height'] * $_POST['oitar']; } $attachment_id = absint($_POST['attachment_id']); $original = get_attached_file($attachment_id); $dimensions = $this->get_header_dimensions(array('height' => $_POST['height'], 'width' => $_POST['width'])); $height = $dimensions['dst_height']; $width = $dimensions['dst_width']; if (empty($_POST['skip-cropping'])) { $cropped = hq_crop_image($attachment_id, (int) $_POST['x1'], (int) $_POST['y1'], (int) $_POST['width'], (int) $_POST['height'], $width, $height); } elseif (!empty($_POST['create-new-attachment'])) { $cropped = _copy_image_file($attachment_id); } else { $cropped = get_attached_file($attachment_id); } if (!$cropped || is_hq_error($cropped)) { hq_die(__('Image could not be processed. Please go back and try again.'), __('Image Processing Error')); } /** This filter is documented in hq-admin/custom-header.php */ $cropped = apply_filters('hq_create_file_in_uploads', $cropped, $attachment_id); // For replication $object = $this->create_attachment_object($cropped, $attachment_id); if (!empty($_POST['create-new-attachment'])) { unset($object['ID']); } // Update the attachment $attachment_id = $this->insert_attachment($object, $cropped); $url = $object['guid']; $this->set_header_image(compact('url', 'attachment_id', 'width', 'height')); // Cleanup. $medium = str_replace(basename($original), 'midsize-' . basename($original), $original); if (file_exists($medium)) { hq_delete_file($medium); } if (empty($_POST['create-new-attachment']) && empty($_POST['skip-cropping'])) { hq_delete_file($original); } return $this->finished(); }