/** * Delete attachments linked to a specified post * @param int $parent_id Parent id of post to delete attachments for */ function wp_delete_attachments($parent_id, $unlink = true, $type = 'images') { if ($type == 'images' and has_post_thumbnail($parent_id)) { delete_post_thumbnail($parent_id); } $ids = array(); $attachments = get_posts(array('post_parent' => $parent_id, 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null)); foreach ($attachments as $attach) { if ($type == 'files' and !wp_attachment_is_image($attach->ID)) { if ($unlink) { wp_delete_attachment($attach->ID, true); } else { $ids[] = $attach->ID; } } elseif ($type == 'images' and wp_attachment_is_image($attach->ID)) { if ($unlink) { wp_delete_attachment($attach->ID, true); } else { $ids[] = $attach->ID; } } } global $wpdb; if (!empty($ids)) { $ids_string = implode(',', $ids); // unattach $result = $wpdb->query("UPDATE {$wpdb->posts} SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( {$ids_string} )"); foreach ($ids as $att_id) { clean_attachment_cache($att_id); } } return $ids; }
/** * Encapsulate logic for Attach/Detach actions * * @since 4.2.0 * * @global wpdb $wpdb WordPress database abstraction object. * * @param int $parent_id Attachment parent ID. * @param string $action Optional. Attach/detach action. Accepts 'attach' or 'detach'. * Default 'attach'. */ function wp_media_attach_action($parent_id, $action = 'attach') { global $wpdb; if (!$parent_id) { return; } if (!current_user_can('edit_post', $parent_id)) { wp_die(__('You are not allowed to edit this post.')); } $ids = array(); foreach ((array) $_REQUEST['media'] as $att_id) { $att_id = (int) $att_id; if (!current_user_can('edit_post', $att_id)) { continue; } $ids[] = $att_id; } if (!empty($ids)) { $ids_string = implode(',', $ids); if ('attach' === $action) { $result = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( {$ids_string} )", $parent_id)); } else { $result = $wpdb->query("UPDATE {$wpdb->posts} SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( {$ids_string} )"); } foreach ($ids as $att_id) { clean_attachment_cache($att_id); } } if (isset($result)) { $location = 'upload.php'; if ($referer = wp_get_referer()) { if (false !== strpos($referer, 'upload.php')) { $location = remove_query_arg(array('attached', 'detach'), $referer); } } $key = 'attach' === $action ? 'attached' : 'detach'; $location = add_query_arg(array($key => $result), $location); wp_redirect($location); exit; } }
if (!current_user_can('edit_post', $parent_id)) { wp_die(__('You are not allowed to edit this post.')); } $attach = array(); foreach ((array) $_REQUEST['media'] as $att_id) { $att_id = (int) $att_id; if (!current_user_can('edit_post', $att_id)) { continue; } $attach[] = $att_id; } if (!empty($attach)) { $attach_string = implode(',', $attach); $attached = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( {$attach_string} )", $parent_id)); foreach ($attach as $att_id) { clean_attachment_cache($att_id); } } if (isset($attached)) { $location = 'upload.php'; if ($referer = wp_get_referer()) { if (false !== strpos($referer, 'upload.php')) { $location = $referer; } } $location = add_query_arg(array('attached' => $attached), $location); wp_redirect($location); exit; } break; case 'trash':
function insert_post($entry_id, $post, $editing = false, $form_id = false) { $field_ids = $new_post = array(); foreach ($_POST['frm_wp_post'] as $post_data => $value) { $post_data = explode('=', $post_data); $field_ids[] = (int) $post_data[0]; if (isset($new_post[$post_data[1]])) { $value = array_merge((array) $value, (array) $new_post[$post_data[1]]); } $post[$post_data[1]] = $new_post[$post_data[1]] = $value; //delete the entry meta below so it won't be stored twice } //if empty post content and auto display, then save compiled post content $display_id = $editing ? get_post_meta($post['ID'], 'frm_display_id', true) : (isset($_POST['frm_wp_post_custom']['=frm_display_id']) ? $_POST['frm_wp_post_custom']['=frm_display_id'] : 0); if (!isset($post['post_content']) and $display_id) { $dyn_content = get_post_meta($display_id, 'frm_dyncontent', true); $post['post_content'] = apply_filters('frm_content', $dyn_content, $form_id, $entry_id); } if (isset($post['post_date']) && !empty($post['post_date']) && (!isset($post['post_date_gmt']) || $post['post_date_gmt'] == '0000-00-00 00:00:00')) { // set post date gmt if post date is set $post['post_date_gmt'] = get_gmt_from_date($post['post_date']); } $post_ID = wp_insert_post($post); if (is_wp_error($post_ID) or empty($post_ID)) { return; } // Add taxonomies after save in case user doesn't have permissions if (isset($_POST['frm_tax_input'])) { foreach ($_POST['frm_tax_input'] as $taxonomy => $tags) { if (is_taxonomy_hierarchical($taxonomy)) { $tags = array_keys($tags); } wp_set_post_terms($post_ID, $tags, $taxonomy); unset($taxonomy); unset($tags); } } global $frm_entry_meta, $user_ID, $frm_vars, $wpdb; $exclude_attached = array(); if (isset($frm_vars['media_id']) and !empty($frm_vars['media_id'])) { global $wpdb; //link the uploads to the post foreach ((array) $frm_vars['media_id'] as $media_id) { $exclude_attached = array_merge($exclude_attached, (array) $media_id); if (is_array($media_id)) { $attach_string = implode(',', array_filter($media_id)); if (!empty($attach_string)) { $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE post_type = %s AND ID IN ( {$attach_string} )", $post_ID, 'attachment')); foreach ($media_id as $m) { clean_attachment_cache($m); unset($m); } } } else { $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $media_id, 'post_type' => 'attachment')); clean_attachment_cache($media_id); } } } if ($editing and count($_FILES) > 0) { global $wpdb; $args = array('post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null, 'post_parent' => $post_ID, 'exclude' => $exclude_attached); //unattach files from this post $attachments = get_posts($args); foreach ($attachments as $attachment) { $wpdb->update($wpdb->posts, array('post_parent' => null), array('ID' => $attachment->ID)); } } if (isset($_POST['frm_wp_post_custom'])) { foreach ($_POST['frm_wp_post_custom'] as $post_data => $value) { $post_data = explode('=', $post_data); $field_id = $post_data[0]; if ($value == '') { delete_post_meta($post_ID, $post_data[1]); } else { update_post_meta($post_ID, $post_data[1], $value); } $frm_entry_meta->delete_entry_meta($entry_id, $field_id); unset($post_data); unset($value); } } if (!$editing) { //save post_id with the entry if ($wpdb->update($wpdb->prefix . 'frm_items', array('post_id' => $post_ID), array('id' => $entry_id))) { wp_cache_delete($entry_id, 'frm_entry'); } } if (isset($dyn_content)) { $new_content = apply_filters('frm_content', $dyn_content, $form_id, $entry_id); if ($new_content != $post['post_content']) { global $wpdb; $wpdb->update($wpdb->posts, array('post_content' => $new_content), array('ID' => $post_ID)); } } // delete entry meta so it won't be duplicated $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->prefix}frm_item_metas WHERE item_id=%d AND field_id", $entry_id) . " IN (" . implode(',', $field_ids) . ")"); update_post_meta($post_ID, '_edit_last', $user_ID); return $post_ID; }
function do_attach($per_page) { global $wpdb; if (isset($_GET['attached']) && (int) $_GET['attached']) { $attached = (int) $_GET['attached']; $message = sprintf(_n('Changed %d attachment.', 'Attached %d attachments.', $attached, 'external-videos'), $attached); $_SERVER['REQUEST_URI'] = remove_query_arg(array('attached'), $_SERVER['REQUEST_URI']); ?> <div id="message" class="updated"><p><strong><?php echo $message; ?> </strong></p></div> <?php unset($_GET['attached']); } elseif (isset($_GET['found_post_id']) && isset($_GET['media'])) { if (!($parent_id = (int) $_GET['found_post_id'])) { return; } $parent =& get_post($parent_id); if (!current_user_can('edit_post', $parent_id)) { wp_die(__('You are not allowed to edit this video.', 'external-videos')); } $attach = array(); foreach ((array) $_GET['media'] as $att_id) { $att_id = (int) $att_id; if (!current_user_can('edit_post', $att_id)) { continue; } $attach[] = $att_id; clean_attachment_cache($att_id); } if (!empty($attach)) { $attach = implode(',', $attach); $attached = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE post_type = 'external-videos' AND ID IN ({$attach})", $parent_id)); } if (isset($attached)) { $location = 'edit.php'; if ($referer = wp_get_referer()) { if (false !== strpos($referer, 'edit.php')) { $location = $referer; } } $location = add_query_arg(array('attached' => $attached), $location); wp_redirect($location); exit; } } return $per_page; }
function rttchr_load_post() { global $wpdb; if (isset($_REQUEST['_ajax_nonce']) && isset($_REQUEST['found_post_id']) && isset($_REQUEST['post_type']) && 'attachment' == $_REQUEST['post_type']) { wp_verify_nonce($_REQUEST['_ajax_nonce'], 'find-posts'); /* generated in find posts div requester*/ $parent_id = intval($_REQUEST['found_post_id']); /* found so > 0 */ $att_id = $_REQUEST['post_ID']; if ($parent_id > 0 && !current_user_can('edit_post', $parent_id)) { wp_die(__('You are not allowed to edit this post.', 're_attacher')); } $attached = $wpdb->update($wpdb->posts, array('post_parent' => $parent_id), array('ID' => intval($att_id), 'post_type' => 'attachment')); clean_attachment_cache($att_id); $_GET['message'] = 1; } }
private static function link_post_attachments($post_ID, $editing) { global $frm_vars, $wpdb; $exclude_attached = array(); if (isset($frm_vars['media_id']) && !empty($frm_vars['media_id'])) { foreach ((array) $frm_vars['media_id'] as $media_id) { $exclude_attached = array_merge($exclude_attached, (array) $media_id); if (is_array($media_id)) { $attach_string = array_filter($media_id); if (!empty($attach_string)) { $where = array('post_type' => 'attachment', 'ID' => $attach_string); FrmDb::get_where_clause_and_values($where); array_unshift($where['values'], $post_ID); $wpdb->query($wpdb->prepare('UPDATE ' . $wpdb->posts . ' SET post_parent = %d' . $where['where'], $where['values'])); foreach ($media_id as $m) { clean_attachment_cache($m); unset($m); } } } else { $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $media_id, 'post_type' => 'attachment')); clean_attachment_cache($media_id); } } } self::unlink_post_attachments($post_ID, $editing, $exclude_attached); }
/** * Attach * * @since 1.0.0 * * @param string[] $args { * @type integer $parent_id Required Post ID to add the attachment to * @type array $media Attachment IDs * } * * @return boolean */ public function attach($args = array()) { extract($args); global $wpdb; if (!$parent_id) { return; } $parent = get_post($parent_id); $attach = array(); foreach ((array) $media as $att_id) { $att_id = (int) $att_id; $attach[] = $att_id; } // if() if (!empty($attach)) { $attach_string = implode(',', $attach); $attached = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_parent = %d WHERE post_type = 'attachment' AND ID IN ( {$attach_string} )", $parent_id)); foreach ($attach as $att_id) { clean_attachment_cache($att_id); } // foreach() } // if() if (isset($attached)) { return true; } return false; }