function wp_delete_link($link_id) { global $wpdb; do_action('delete_link', $link_id); wp_delete_object_term_relationships($link_id, 'link_category'); $wpdb->query("DELETE FROM {$wpdb->links} WHERE link_id = '{$link_id}'"); do_action('deleted_link', $link_id); return true; }
/** * {@internal Missing Short Description}} * * @since unknown * * @param unknown_type $link_id * @return unknown */ function wp_delete_link($link_id) { global $wpdb; do_action('delete_link', $link_id); wp_delete_object_term_relationships($link_id, 'link_category'); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->links} WHERE link_id = %d", $link_id)); do_action('deleted_link', $link_id); clean_bookmark_cache($link_id); return true; }
/** * Delete link specified from database * * @since 2.0.0 * * @param int $link_id ID of the link to delete * @return bool True */ function wp_delete_link($link_id) { global $wpdb; do_action('delete_link', $link_id); wp_delete_object_term_relationships($link_id, 'link_category'); $wpdb->delete($wpdb->links, array('link_id' => $link_id)); do_action('deleted_link', $link_id); clean_bookmark_cache($link_id); return true; }
function toggle_case() { global $post; if (isset($_POST['closeCase'])) { $tag = get_term_by('id', $_POST['result_select'], 'results'); wp_set_post_terms($post->ID, $tag->slug, 'results'); } elseif (isset($_POST['openCase'])) { wp_delete_object_term_relationships($post->ID, 'results'); } }
public function test_delete_object_term_relationships() { $anther_term = wp_insert_term('mouse', $this->taxonomy); wp_set_post_terms($this->post_id, array($anther_term['term_id']), $this->taxonomy, false); $anther_term_2 = wp_insert_term('cat', $this->taxonomy); wp_set_post_terms($this->post_id, array($anther_term_2['term_id']), $this->taxonomy, true); wp_delete_object_term_relationships($this->post_id, array($this->taxonomy)); $this->client->do_sync(); $object_terms = get_the_terms($this->post_id, $this->taxonomy); $server_object_terms = $this->server_replica_storage->get_the_terms($this->post_id, $this->taxonomy); $this->assertEquals($object_terms, $server_object_terms); }
function delete_series_post_relationship($postid) { $id = (int) $postid; $series = get_the_series($id); if (!empty($series)) { //let's not waste any cycles foreach ($series as $ser) { wp_reset_series_order_meta_cache($id, $ser->term_id); } return wp_delete_object_term_relationships($id, array('series')); } return; }
/** * Save meta box data */ public static function save($post_id, $post) { global $wpdb; update_post_meta($post_id, '_on_market', isset($_POST['_on_market']) ? $_POST['_on_market'] : ''); update_post_meta($post_id, '_featured', isset($_POST['_featured']) ? $_POST['_featured'] : ''); if (!empty($_POST['_availability'])) { wp_set_post_terms($post_id, $_POST['_availability'], 'availability'); } else { // Setting to blank wp_delete_object_term_relationships($post_id, 'availability'); } wp_delete_object_term_relationships($post_id, 'marketing_flag'); if (!empty($_POST['_marketing_flags'])) { wp_set_post_terms($post_id, $_POST['_marketing_flags'], 'marketing_flag'); } do_action('propertyhive_save_property_marketing', $post_id); }
public function test_array_of_taxonomies() { register_taxonomy('wptests_tax1', 'post'); register_taxonomy('wptests_tax2', 'post'); register_taxonomy('wptests_tax3', 'post'); $t1 = $this->factory->term->create(array('taxonomy' => 'wptests_tax1')); $t2 = $this->factory->term->create(array('taxonomy' => 'wptests_tax2')); $t3 = $this->factory->term->create(array('taxonomy' => 'wptests_tax3')); $object_id = 567; wp_set_object_terms($object_id, array($t1), 'wptests_tax1'); wp_set_object_terms($object_id, array($t2), 'wptests_tax2'); wp_set_object_terms($object_id, array($t3), 'wptests_tax3'); // Confirm the setup. $terms = wp_get_object_terms($object_id, array('wptests_tax1', 'wptests_tax2', 'wptests_tax3'), array('fields' => 'ids')); $this->assertEqualSets(array($t1, $t2, $t3), $terms); // wp_delete_object_term_relationships() doesn't have a return value. wp_delete_object_term_relationships($object_id, array('wptests_tax1', 'wptests_tax3')); $terms = wp_get_object_terms($object_id, array('wptests_tax1', 'wptests_tax2', 'wptests_tax3'), array('fields' => 'ids')); $this->assertEqualSets(array($t2), $terms); }
function toggle_case_result($comment_ID) { $comment = get_comment($comment_ID); $post = get_post($comment->comment_post_ID); //Check case single if ('cases' != $post->post_type) { return; } if (isset($_POST['close_case_comment_cp'])) { if (isset($_POST['case_result_select'])) { $result = $_POST['case_result_select']; } $tag = get_term_by('id', $result, 'results'); $answer = wp_set_object_terms($post->ID, $tag->term_id, 'results'); return; } elseif (isset($_POST['case_reset_result_cp'])) { wp_delete_object_term_relationships($post->ID, 'results'); return; } }
function deleteAll($product_id = 0) { global $wpdb; if ($product_id > 0) { if ($attributes = $wpdb->get_results("SELECT attribute_name FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies")) { foreach ($attributes as $attribute) { wp_delete_object_term_relationships($product_id, 'pa_' . $attribute->attribute_name); } } $transient_name = 'wc_product_children_ids_' . $product_id; delete_transient($transient_name); } $ids = $wpdb->get_results("SELECT ID FROM " . $wpdb->posts . " WHERE post_type='product_variation' " . ($product_id > 0 ? "AND post_parent='" . (int) $product_id . "'" : ""), 'ARRAY_A'); if (!empty($ids)) { foreach ($ids as $id) { $x = (int) $id['ID']; $wpdb->query("DELETE FROM " . $wpdb->posts . " WHERE ID='" . $x . "'"); $wpdb->query("DELETE FROM " . $wpdb->postmeta . " WHERE post_id='" . $x . "'"); } } }
/** * Save taxonomy terms for a specific user * * @since 0.1.0 * * @param int $user_id * @param string $taxonomy * @param array $terms * @param boolean $bulk * * @return boolean */ function wp_set_terms_for_user($user_id, $taxonomy, $terms = array(), $bulk = false) { // Get the taxonomy $tax = get_taxonomy($taxonomy); // Make sure the current user can edit the user and assign terms before proceeding. if (!current_user_can('edit_user', $user_id) && current_user_can($tax->cap->assign_terms)) { return false; } if (empty($terms) && empty($bulk)) { $terms = isset($_POST[$taxonomy]) ? $_POST[$taxonomy] : null; } // Delete all user terms if (is_null($terms) || empty($terms)) { wp_delete_object_term_relationships($user_id, $taxonomy); // Set the terms } else { $_terms = array_map('sanitize_key', $terms); // Sets the terms for the user wp_set_object_terms($user_id, $_terms, $taxonomy, false); } // Clean the cache clean_object_term_cache($user_id, $taxonomy); }
public function delete_term_language($term_id) { wp_delete_object_term_relationships($term_id, 'term_language'); }
/** * Validate and save the meta box fields. * * @since 3.0.0 * * @param int $post_id * @param \WP_Post $post * * @return void */ public static function save($post_id, $post) { /* ====================== * * Calendar type and view * * ====================== */ // Unlink existing terms for feed type and calendar type. wp_delete_object_term_relationships($post_id, array('calendar_feed', 'calendar_type')); // Set the feed type as term. $feed_type = isset($_POST['_feed_type']) ? sanitize_title(stripslashes($_POST['_feed_type'])) : apply_filters('simcal_default_feed_type', 'google'); wp_set_object_terms($post_id, $feed_type, 'calendar_feed'); // Set the calendar type as a term. $calendar_type = isset($_POST['_calendar_type']) ? sanitize_title(stripslashes($_POST['_calendar_type'])) : apply_filters('simcal_default_calendar_type', 'default-calendar'); wp_set_object_terms($post_id, $calendar_type, 'calendar_type'); // Set the calendar type view as post meta. $calendar_view = isset($_POST['_calendar_view']) ? $_POST['_calendar_view'] : ''; if ($calendar_view && is_array($calendar_view)) { $views = array_map('sanitize_title', $calendar_view); update_post_meta($post_id, '_calendar_view', $views); } /* ===================== * * Events settings panel * * ===================== */ // Calendar opening. $calendar_begins = isset($_POST['_calendar_begins']) ? sanitize_key($_POST['_calendar_begins']) : 'this_month'; update_post_meta($post_id, '_calendar_begins', $calendar_begins); $calendar_begins_nth = isset($_POST['_calendar_begins_nth']) ? absint($_POST['_calendar_begins_nth']) : 2; update_post_meta($post_id, '_calendar_begins_nth', $calendar_begins_nth); $calendar_begins_custom_date = isset($_POST['_calendar_begins_custom_date']) ? sanitize_title($_POST['_calendar_begins_custom_date']) : ''; update_post_meta($post_id, '_calendar_begins_custom_date', $calendar_begins_custom_date); // Feed earliest events date. $earliest_events = isset($_POST['_feed_earliest_event_date']) ? sanitize_key($_POST['_feed_earliest_event_date']) : ''; update_post_meta($post_id, '_feed_earliest_event_date', $earliest_events); $earliest_events_range = isset($_POST['_feed_earliest_event_date_range']) ? max(absint($_POST['_feed_earliest_event_date_range']), 1) : 1; update_post_meta($post_id, '_feed_earliest_event_date_range', $earliest_events_range); // Feed latest events date. $latest_events = isset($_POST['_feed_latest_event_date']) ? sanitize_key($_POST['_feed_latest_event_date']) : ''; update_post_meta($post_id, '_feed_latest_event_date', $latest_events); $latest_events_range = isset($_POST['_feed_latest_event_date_range']) ? max(absint($_POST['_feed_latest_event_date_range']), 1) : 1; update_post_meta($post_id, '_feed_latest_event_date_range', $latest_events_range); /* ======================= * * Calendar settings panel * * ======================= */ // Static calendar. $static = isset($_POST['_calendar_is_static']) ? 'yes' : 'no'; update_post_meta($post_id, '_calendar_is_static', $static); // No events message. $message = isset($_POST['_no_events_message']) ? wp_kses_post($_POST['_no_events_message']) : ''; update_post_meta($post_id, '_no_events_message', $message); /* ======================= * * Advanced settings panel * * ======================= */ // Timezone. $feed_timezone_setting = isset($_POST['_feed_timezone_setting']) ? sanitize_key($_POST['_feed_timezone_setting']) : 'use_calendar'; update_post_meta($post_id, '_feed_timezone_setting', $feed_timezone_setting); $default_timezone = simcal_get_wp_timezone(); $feed_timezone = $default_timezone ? $default_timezone : 'UTC'; $feed_timezone = isset($_POST['_feed_timezone']) ? sanitize_text_field($_POST['_feed_timezone']) : $feed_timezone; update_post_meta($post_id, '_feed_timezone', $feed_timezone); // Date format. $date_format_setting = isset($_POST['_calendar_date_format_setting']) ? sanitize_key($_POST['_calendar_date_format_setting']) : 'use_site'; update_post_meta($post_id, '_calendar_date_format_setting', $date_format_setting); $date_format = isset($_POST['_calendar_date_format']) ? sanitize_text_field(trim($_POST['_calendar_date_format'])) : get_option('date_format'); update_post_meta($post_id, '_calendar_date_format', $date_format); $date_format_php = isset($_POST['_calendar_date_format_php']) ? sanitize_text_field(trim($_POST['_calendar_date_format_php'])) : get_option('date_format'); update_post_meta($post_id, '_calendar_date_format_php', $date_format_php); // Time format. $time_format_setting = isset($_POST['_calendar_time_format_setting']) ? sanitize_key($_POST['_calendar_time_format_setting']) : 'use_site'; update_post_meta($post_id, '_calendar_time_format_setting', $time_format_setting); $time_format = isset($_POST['_calendar_time_format']) ? sanitize_text_field(trim($_POST['_calendar_time_format'])) : get_option('time_format'); update_post_meta($post_id, '_calendar_time_format', $time_format); $time_format_php = isset($_POST['_calendar_time_format_php']) ? sanitize_text_field(trim($_POST['_calendar_time_format_php'])) : get_option('time_format'); update_post_meta($post_id, '_calendar_time_format_php', $time_format_php); // Date-time separator. $datetime_separator = isset($_POST['_calendar_datetime_separator']) ? sanitize_text_field($_POST['_calendar_datetime_separator']) : ' '; update_post_meta($post_id, '_calendar_datetime_separator', $datetime_separator); // Week start. $week_start_setting = isset($_POST['_calendar_week_starts_on_setting']) ? sanitize_key($_POST['_calendar_week_starts_on_setting']) : 'use_site'; update_post_meta($post_id, '_calendar_week_starts_on_setting', $week_start_setting); $week_start = isset($_POST['_calendar_week_starts_on']) ? intval($_POST['_calendar_week_starts_on']) : get_option('start_of_week'); update_post_meta($post_id, '_calendar_week_starts_on', $week_start); // Cache interval. $cache = 7200; if (isset($_POST['_feed_cache_user_amount']) && isset($_POST['_feed_cache_user_unit'])) { $amount = is_numeric($_POST['_feed_cache_user_amount']) ? absint($_POST['_feed_cache_user_amount']) : 1; $unit = is_numeric($_POST['_feed_cache_user_unit']) ? absint($_POST['_feed_cache_user_unit']) : 3600; update_post_meta($post_id, '_feed_cache_user_amount', $amount); update_post_meta($post_id, '_feed_cache_user_unit', $unit); $cache = $amount * $unit; } update_post_meta($post_id, '_feed_cache', $cache); /* ============= * * Miscellaneous * * ============= */ // Update version. update_post_meta($post_id, '_calendar_version', SIMPLE_CALENDAR_VERSION); // Action hook. do_action('simcal_process_settings_meta', $post_id); // Clear cache. simcal_delete_feed_transients($post_id); }
/** * Hooks the dynamic load-* action called when * this tools page loads. * * @return void **/ public function load_tools_page() { if (!($action = isset($_GET['btgt_action']) ? sanitize_text_field($_GET['btgt_action']) : false)) { return; } $obj_id = isset($_GET['obj_id']) ? absint($_GET['obj_id']) : false; $wp_nonce = isset($_GET['_wpnonce']) ? sanitize_text_field($_GET['_wpnonce']) : false; if (!$wp_nonce || !wp_verify_nonce($wp_nonce, "btgt_{$action}_{$obj_id}")) { $this->set_admin_error('Sorry, went wrong. Please try again.'); return; } switch ($action) { case 'delete_from_groups': if (!current_user_can('manage_options')) { $this->set_admin_error("You are not allowed to delete term relation ships for {$obj_id}."); return; } wp_delete_object_term_relationships($obj_id, 'post_translation'); $this->set_admin_notice("Deleted term relationships for {$obj_id}"); break; case 'delete_post': if (!current_user_can('delete_post', $obj_id)) { $this->set_admin_error('You are not allowed to move this post to the trash.'); return; } wp_delete_object_term_relationships($obj_id, 'post_translation'); wp_delete_post($obj_id, true); break; case 'trash_post': if (!current_user_can('delete_post', $obj_id)) { $this->set_admin_error('You are not allowed to move this post to the trash.'); return; } wp_delete_object_term_relationships($obj_id, 'post_translation'); wp_trash_post($obj_id); break; } $args = array('page' => 'btgt', 'lang' => bbl_get_default_lang_code()); $args = array_map('rawurlencode', $args); $url = add_query_arg($args, admin_url('tools.php')); $url .= '#' . rawurlencode($_GET['anchor']); wp_safe_redirect($url); }
/** * Saves the term selected on the edit user/profile page in the admin. This function is triggered when the page * is updated. We just grab the posted data and use wp_set_object_terms() to save it. * * @param int $user_id The ID of the user to save the terms for. */ public function wepn_save_user_city_group_category($user_id) { if (!WEPN_Helper::check_user_role('vendor', $user_id)) { return; } global $wpdb; $company_name = esc_attr($_POST['company_name']); $company_id = get_user_meta($user_id, 'company', true); if ($company_id && is_numeric($company_id)) { wp_update_post(array('ID' => $company_id, 'post_title' => $company_name, 'post_author' => $user_id, 'post_type' => 'vendor', 'post_status' => 'publish')); } else { $company_id = $wpdb->get_var("SELECT ID FROM wp_posts WHERE post_title = '" . $company_name . "'"); if (!empty($company_name) && !$company_id) { $company_id = wp_insert_post(array('post_title' => $company_name, 'post_author' => $user_id, 'post_type' => 'vendor', 'post_status' => 'publish')); update_user_meta($user_id, 'company', $company_id); } } // Update Post Meta update_post_meta($company_id, 'vendor', $user_id); //Only administrator can change this section if (current_user_can('manage_options')) { $city = $_POST['city']; $group = $_POST['group']; $category = $_POST['category']; $group_slug = sanitize_title($group); $category_slug = sanitize_title($category); $other_categories = (array) (!empty($_POST['categories']) ? $_POST['categories'] : array()); // Remove existing post and term relatinships $old_tax = get_post_meta($company_id, 'city', true); wp_delete_object_term_relationships($company_id, $old_tax); if (!in_array($category, $other_categories)) { $other_categories = array_merge($other_categories, array($category)); } if (count($other_categories) > 0) { $terms = array(); foreach ($other_categories as $term_title) { if (empty($term_title)) { continue; } $term_slug = sanitize_title($term_title); if (!($term = term_exists($term_title, $city))) { $term = wp_insert_term($term_title, $city, array('slug' => $term_slug)); } if (!is_wp_error($term)) { $terms[] = $term['term_id']; } } wp_set_post_terms($company_id, $terms, $city, false); } // Update custom permalink update_post_meta($company_id, 'custom_permalink', $city . '/' . $group_slug . '/' . $category_slug . '/' . sanitize_title($company_name)); update_post_meta($company_id, 'region', $group_slug); update_post_meta($company_id, 'city', $city); update_post_meta($company_id, 'category', $category_slug); // Update user meta update_user_meta($user_id, 'city', $city); update_user_meta($user_id, 'group', $group_slug); update_user_meta($user_id, 'category', $category_slug); } }
/** * 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 2.0.0 * * @global wpdb $wpdb WordPress 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 wp_delete_attachment($post_id, $force_delete = false) { global $wpdb; if (!($post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->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 wp_trash_post($post_id); } delete_post_meta($post_id, '_wp_trash_meta_status'); delete_post_meta($post_id, '_wp_trash_meta_time'); $meta = wp_get_attachment_metadata($post_id); $backup_sizes = get_post_meta($post->ID, '_wp_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 wp_delete_attachment(). * * @since 2.0.0 * * @param int $post_id Attachment ID. */ do_action('delete_attachment', $post_id); wp_delete_object_term_relationships($post_id, array('category', 'post_tag')); wp_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); wp_defer_comment_counting(true); $comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id)); foreach ($comment_ids as $comment_id) { wp_delete_comment($comment_id, true); } wp_defer_comment_counting(false); $post_meta_ids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d ", $post_id)); foreach ($post_meta_ids as $mid) { delete_metadata_by_mid('post', $mid); } /** This action is documented in wp-includes/post.php */ do_action('delete_post', $post_id); $result = $wpdb->delete($wpdb->posts, array('ID' => $post_id)); if (!$result) { return false; } /** This action is documented in wp-includes/post.php */ do_action('deleted_post', $post_id); $uploadpath = wp_upload_dir(); if (!empty($meta['thumb'])) { // Don't delete the thumb if another attachment uses it. if (!$wpdb->get_row($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $wpdb->esc_like($meta['thumb']) . '%', $post_id))) { $thumbfile = str_replace(basename($file), $meta['thumb'], $file); /** This filter is documented in wp-includes/functions.php */ $thumbfile = apply_filters('wp_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 wp-includes/functions.php */ $intermediate_file = apply_filters('wp_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 wp-includes/functions.php */ $del_file = apply_filters('wp_delete_file', $del_file); @unlink(path_join($uploadpath['basedir'], $del_file)); } } wp_delete_file($file); clean_post_cache($post); return $post; }
/** * Unified save child function. * * @param int $parent_id * @param int $child_id * @param array $save_fields * @return bool|WP_Error */ function save_child($parent_id, $child_id, $save_fields = array()) { $parent = get_post(intval($parent_id)); $child = get_post(intval($child_id)); $post_data = array(); if (empty($parent) || empty($child)) { return new WP_Error('wpcf-relationship-save-child', 'no parent/child post'); } // Save relationship update_post_meta($child->ID, '_wpcf_belongs_' . $parent->post_type . '_id', $parent->ID); // Check if added via AJAX $check = get_post_meta($child->ID, '_wpcf_relationship_new', true); $new = !empty($check); delete_post_meta($child->ID, '_wpcf_relationship_new'); // Set post data $post_data['ID'] = $child->ID; // Title needs to be checked if submitted at all if (!isset($save_fields['_wp_title'])) { // If not submitted that means it is not offered to be edited if (!empty($child->post_title)) { $post_title = $child->post_title; } else { // DO NOT LET IT BE EMPTY $post_title = $child->post_type . ' ' . $child->ID; } } else { $post_title = $save_fields['_wp_title']; } $post_data['post_title'] = $post_title; $post_data['post_content'] = isset($save_fields['_wp_body']) ? $save_fields['_wp_body'] : $child->post_content; $post_data['post_type'] = $child->post_type; // Check post status - if new, convert to 'publish' else keep remaining if ($new) { $post_data['post_status'] = 'publish'; } else { $post_data['post_status'] = get_post_status($child->ID); } /* * * * * * * * UPDATE POST */ $cf = new WPCF_Field(); if (isset($_POST['wpcf_post_relationship'][$parent_id]) && isset($_POST['wpcf_post_relationship'][$parent_id][$child_id])) { $_POST['wpcf'] = array(); foreach ($_POST['wpcf_post_relationship'][$parent_id][$child_id] as $slug => $value) { $_POST['wpcf'][$cf->__get_slug_no_prefix($slug)] = $value; $_POST['wpcf'][$slug] = $value; } } unset($cf); /** * avoid filters for children * / global $wp_filter; $save_post = $wp_filter['save_post']; $wp_filter['save_post'] = array(); */ $updated_id = wp_update_post($post_data); /* $wp_filter['save_post'] = $save_post; */ unset($save_post); if (empty($updated_id)) { return new WP_Error('relationship-update-post-failed', 'Updating post failed'); } // Save parents if (!empty($save_fields['parents'])) { foreach ($save_fields['parents'] as $parent_post_type => $parent_post_id) { update_post_meta($child->ID, '_wpcf_belongs_' . $parent_post_type . '_id', $parent_post_id); } } // Update taxonomies if (!empty($save_fields['taxonomies']) && is_array($save_fields['taxonomies'])) { $_save_data = array(); foreach ($save_fields['taxonomies'] as $taxonomy => $t) { if (!is_taxonomy_hierarchical($taxonomy)) { $_save_data[$taxonomy] = strval($t); continue; } foreach ($t as $term_id) { if ($term_id != '-1') { $term = get_term($term_id, $taxonomy); if (empty($term)) { continue; } $_save_data[$taxonomy][] = $term_id; } } } wp_delete_object_term_relationships($child->ID, array_keys($save_fields['taxonomies'])); foreach ($_save_data as $_taxonomy => $_terms) { wp_set_post_terms($child->ID, $_terms, $_taxonomy, $append = false); } } // Unset non-types unset($save_fields['_wp_title'], $save_fields['_wp_body'], $save_fields['parents'], $save_fields['taxonomies']); /* * * * * * * * UPDATE Loop over fields */ foreach ($save_fields as $slug => $value) { if (defined('WPTOOLSET_FORMS_VERSION')) { // Get field by slug $field = wpcf_fields_get_field_by_slug(str_replace(WPCF_META_PREFIX, '', $slug)); if (empty($field)) { continue; } // Set config $config = wptoolset_form_filter_types_field($field, $child->ID); // Check if valid $valid = wptoolset_form_validate_field('post', $config, $value); if (is_wp_error($valid)) { $errors = $valid->get_error_data(); $msg = sprintf(__('Child post "%s" field "%s" not updated:', 'wpcf'), $child->post_title, $field['name']); wpcf_admin_message_store($msg . ' ' . implode(', ', $errors), 'error'); continue; } } $this->cf->set($child, $field); $this->cf->context = 'post_relationship'; $this->cf->save($value); } do_action('wpcf_relationship_save_child', $child, $parent); clean_post_cache($parent->ID); clean_post_cache($child->ID); // Added because of caching meta 1.5.4 wp_cache_flush(); return true; }
/** * Merge topic handler * * Handles the front end merge topic submission * * @since bbPress (r2756) * * @param string $action The requested action to compare this function to * @uses bbp_add_error() To add an error message * @uses bbp_get_topic() To get the topics * @uses bbp_verify_nonce_request() To verify the nonce and check the request * @uses current_user_can() To check if the current user can edit the topics * @uses is_wp_error() To check if the value retrieved is a {@link WP_Error} * @uses do_action() Calls 'bbp_merge_topic' with the destination and source * topic ids * @uses bbp_get_topic_subscribers() To get the source topic subscribers * @uses bbp_add_user_subscription() To add the user subscription * @uses bbp_remove_user_subscription() To remove the user subscription * @uses bbp_get_topic_favoriters() To get the source topic favoriters * @uses bbp_add_user_favorite() To add the user favorite * @uses bbp_remove_user_favorite() To remove the user favorite * @uses wp_get_post_terms() To get the source topic tags * @uses wp_set_post_terms() To set the topic tags * @uses wp_delete_object_term_relationships() To delete the topic tags * @uses bbp_open_topic() To open the topic * @uses bbp_unstick_topic() To unstick the topic * @uses bbp_get_reply_post_type() To get the reply post type * @uses get_posts() To get the replies * @uses wp_update_post() To update the topic * @uses bbp_update_reply_topic_id() To update the reply topic id * @uses bbp_get_topic_forum_id() To get the topic forum id * @uses bbp_update_reply_forum_id() To update the reply forum id * @uses do_action() Calls 'bbp_merged_topic_reply' with the reply id and * destination topic id * @uses do_action() Calls 'bbp_merged_topic' with the destination and source * topic ids and source topic's forum id * @uses bbp_get_topic_permalink() To get the topic permalink * @uses wp_safe_redirect() To redirect to the topic link */ function bbp_merge_topic_handler($action = '') { // Bail if action is not bbp-merge-topic if ('bbp-merge-topic' !== $action) { return; } // Define local variable(s) $source_topic_id = $destination_topic_id = 0; $source_topic = $destination_topic = 0; $subscribers = $favoriters = $replies = array(); /** Source Topic **********************************************************/ // Topic id if (empty($_POST['bbp_topic_id'])) { bbp_add_error('bbp_merge_topic_source_id', __('<strong>ERROR</strong>: Topic ID not found.', 'bbpress')); } else { $source_topic_id = (int) $_POST['bbp_topic_id']; } // Nonce check if (!bbp_verify_nonce_request('bbp-merge-topic_' . $source_topic_id)) { bbp_add_error('bbp_merge_topic_nonce', __('<strong>ERROR</strong>: Are you sure you wanted to do that?', 'bbpress')); return; // Source topic not found } elseif (!($source_topic = bbp_get_topic($source_topic_id))) { bbp_add_error('bbp_merge_topic_source_not_found', __('<strong>ERROR</strong>: The topic you want to merge was not found.', 'bbpress')); return; } // Cannot edit source topic if (!current_user_can('edit_topic', $source_topic->ID)) { bbp_add_error('bbp_merge_topic_source_permission', __('<strong>ERROR</strong>: You do not have the permissions to edit the source topic.', 'bbpress')); return; } /** Destination Topic *****************************************************/ // Topic id if (empty($_POST['bbp_destination_topic'])) { bbp_add_error('bbp_merge_topic_destination_id', __('<strong>ERROR</strong>: Destination topic ID not found.', 'bbpress')); } else { $destination_topic_id = (int) $_POST['bbp_destination_topic']; } // Destination topic not found if (!($destination_topic = bbp_get_topic($destination_topic_id))) { bbp_add_error('bbp_merge_topic_destination_not_found', __('<strong>ERROR</strong>: The topic you want to merge to was not found.', 'bbpress')); } // Cannot edit destination topic if (!current_user_can('edit_topic', $destination_topic->ID)) { bbp_add_error('bbp_merge_topic_destination_permission', __('<strong>ERROR</strong>: You do not have the permissions to edit the destination topic.', 'bbpress')); } // Bail if errors if (bbp_has_errors()) { return; } /** No Errors *************************************************************/ // Update counts, etc... do_action('bbp_merge_topic', $destination_topic->ID, $source_topic->ID); /** Date Check ************************************************************/ // Check if the destination topic is older than the source topic if (strtotime($source_topic->post_date) < strtotime($destination_topic->post_date)) { // Set destination topic post_date to 1 second before source topic $destination_post_date = date('Y-m-d H:i:s', strtotime($source_topic->post_date) - 1); // Update destination topic wp_update_post(array('ID' => $destination_topic_id, 'post_date' => $destination_post_date, 'post_date_gmt' => get_gmt_from_date($destination_post_date))); } /** Subscriptions *********************************************************/ // Get subscribers from source topic $subscribers = bbp_get_topic_subscribers($source_topic->ID); // Remove the topic from everybody's subscriptions if (!empty($subscribers)) { // Loop through each user foreach ((array) $subscribers as $subscriber) { // Shift the subscriber if told to if (!empty($_POST['bbp_topic_subscribers']) && "1" === $_POST['bbp_topic_subscribers'] && bbp_is_subscriptions_active()) { bbp_add_user_subscription($subscriber, $destination_topic->ID); } // Remove old subscription bbp_remove_user_subscription($subscriber, $source_topic->ID); } } /** Favorites *************************************************************/ // Get favoriters from source topic $favoriters = bbp_get_topic_favoriters($source_topic->ID); // Remove the topic from everybody's favorites if (!empty($favoriters)) { // Loop through each user foreach ((array) $favoriters as $favoriter) { // Shift the favoriter if told to if (!empty($_POST['bbp_topic_favoriters']) && "1" === $_POST['bbp_topic_favoriters']) { bbp_add_user_favorite($favoriter, $destination_topic->ID); } // Remove old favorite bbp_remove_user_favorite($favoriter, $source_topic->ID); } } /** Tags ******************************************************************/ // Get the source topic tags $source_topic_tags = wp_get_post_terms($source_topic->ID, bbp_get_topic_tag_tax_id(), array('fields' => 'names')); // Tags to possibly merge if (!empty($source_topic_tags) && !is_wp_error($source_topic_tags)) { // Shift the tags if told to if (!empty($_POST['bbp_topic_tags']) && "1" === $_POST['bbp_topic_tags']) { wp_set_post_terms($destination_topic->ID, $source_topic_tags, bbp_get_topic_tag_tax_id(), true); } // Delete the tags from the source topic wp_delete_object_term_relationships($source_topic->ID, bbp_get_topic_tag_tax_id()); } /** Source Topic **********************************************************/ // Status bbp_open_topic($source_topic->ID); // Sticky bbp_unstick_topic($source_topic->ID); // Get the replies of the source topic $replies = (array) get_posts(array('post_parent' => $source_topic->ID, 'post_type' => bbp_get_reply_post_type(), 'posts_per_page' => -1, 'order' => 'ASC')); // Prepend the source topic to its replies array for processing array_unshift($replies, $source_topic); if (!empty($replies)) { /** Merge Replies *****************************************************/ // Change the post_parent of each reply to the destination topic id foreach ($replies as $reply) { // Update the reply wp_update_post(array('ID' => $reply->ID, 'post_title' => sprintf(__('Reply To: %s', 'bbpress'), $destination_topic->post_title), 'post_name' => false, 'post_type' => bbp_get_reply_post_type(), 'post_parent' => $destination_topic->ID, 'guid' => '')); // Adjust reply meta values bbp_update_reply_topic_id($reply->ID, $destination_topic->ID); bbp_update_reply_forum_id($reply->ID, bbp_get_topic_forum_id($destination_topic->ID)); // Adjust reply to values $reply_to = bbp_get_reply_to($reply->ID); if (empty($reply_to)) { bbp_update_reply_to($reply->ID, $source_topic->ID); } // Do additional actions per merged reply do_action('bbp_merged_topic_reply', $reply->ID, $destination_topic->ID); } } /** Successful Merge ******************************************************/ // Update topic's last meta data bbp_update_topic_last_reply_id($destination_topic->ID); bbp_update_topic_last_active_id($destination_topic->ID); bbp_update_topic_last_active_time($destination_topic->ID); // Send the post parent of the source topic as it has been shifted // (possibly to a new forum) so we need to update the counts of the // old forum as well as the new one do_action('bbp_merged_topic', $destination_topic->ID, $source_topic->ID, $source_topic->post_parent); // Redirect back to new topic wp_safe_redirect(bbp_get_topic_permalink($destination_topic->ID)); // For good measure exit; }
private function duplicate_taxonomies($master_post_id, $lang) { $post_type = get_post_field('post_type', $master_post_id); $taxonomies = get_object_taxonomies($post_type); $trid = $this->sitepress->get_element_trid($master_post_id, 'post_' . $post_type); if ($trid) { $translations = $this->sitepress->get_element_translations($trid, 'post_' . $post_type, false, false, true); if (isset($translations[$lang])) { $duplicate_post_id = $translations[$lang]->element_id; /* If we have an existing post, we first of all remove all terms currently attached to it. * The main reason behind is the removal of the potentially present default category on the post. */ wp_delete_object_term_relationships($duplicate_post_id, $taxonomies); } else { return false; // translation not found! } } $term_helper = wpml_get_term_translation_util(); $term_helper->duplicate_terms($master_post_id, $lang); return true; }
function save_case_data_control() { if (!isset($_REQUEST['update_case_control'])) { return; } /** * Сохраняем только если находимся на странице дела */ if (!is_singular('cases')) { return; } global $post; /** * Если пришло ID категории дела - сохраняем его */ if (isset($_REQUEST['case_category']) && is_numeric($_REQUEST['case_category']) && $_REQUEST['case_category'] > -1) { wp_set_post_terms($post->ID, $_REQUEST['case_category'], 'functions', false); } /** * Если пришло ID подразделения - сохраняем его */ if (isset($_REQUEST['cp_case_branche']) && $_REQUEST['cp_case_branche'] != '') { $terms = $_REQUEST['cp_case_branche']; $taxonomy = "t-branche"; $append = false; wp_set_post_terms($post_id, $terms, $taxonomy, $append); } /** * Если пришло ID основания дела - сохраняем его */ if (isset($_REQUEST['case_post_parent_cp'])) { if (isset($_REQUEST['case_post_parent_cp'])) { $post_parent = $_REQUEST['case_post_parent_cp']; } if (is_numeric($post_parent)) { $case_post_parent_cp = $post_parent; } if (empty($post_parent)) { $case_post_parent_cp = ''; } wp_update_post(array('ID' => $post->ID, 'post_parent' => $case_post_parent_cp)); } //Сохраняем мету Дата закрытия if (isset($_REQUEST['date_end'])) { //'cp_date_end' => isset($_REQUEST['date_end']) ? $_REQUEST['date_end'] : '' $date_end_new = $_REQUEST['date_end']; //$date_end_current = get_post_meta($post->ID, 'cp_date_end', true); update_post_meta($post->ID, 'cp_date_end', $date_end_new); } /** * Если пришло ID результата дела - сохраняем его */ if (isset($_REQUEST['case_result']) && is_numeric($_REQUEST['case_result'])) { $result = $_REQUEST['case_result']; if ($result < 0) { wp_delete_object_term_relationships($post->ID, 'results'); } else { wp_set_post_terms($post->ID, $_REQUEST['case_result'], 'results', false); } } /** * Сохраняем все меты из виджета */ $meta = array('deadline_cp' => isset($_REQUEST['deadline_cp']) ? $_REQUEST['deadline_cp'] : '', 'responsible-cp-posts-sql' => isset($_REQUEST['cp_responsible']) ? $_REQUEST['cp_responsible'] : ''); foreach ($meta as $meta_name => $meta_value) { update_post_meta($post->ID, $meta_name, $meta_value); } //print_r($_REQUEST); //print_r(get_post_meta($post->ID)); //die(); }
/** * Save function. * * Taxonomies are saved differently as they are * not stored as post metas but actual taxonomy terms. * * @since 3.2.0 * * @param int $value New value * @param int $post_id ID of the post being saved * * @return int Result of the update */ public function update($value, $post_id) { /* If this is a standard taxonomy we don't do anything and let WordPress take care of it. */ if (true === $this->field['args']['taxo_std']) { return 0; } /* If no value is submitted we delete the term relationship */ if (empty($value)) { $terms = wp_get_post_terms($post_id, $this->field_id); if (!empty($terms)) { wp_delete_object_term_relationships($post_id, $this->field_id); return 3; } } /* Get all the terms for this ticket / taxo (we should have only one term) */ $terms = get_the_terms($post_id, $this->field_id); /** * As the taxonomy is handled like a select, we should have only one value. At least * that's what we want. Hence, we loop through the possible multiple terms (which * shouldn't happen) and only keep the last one. */ $the_term = ''; if (is_array($terms)) { foreach ($terms as $term) { $the_term = $term->term_id; } } /* Finally we save the new terms if changed */ if ($the_term !== (int) $value) { $term = get_term_by('id', (int) $value, $this->field_id); /* If the term does not exist we can't do anything. */ if (false === $term) { return 0; } wp_set_object_terms($post_id, (int) $value, $this->field_id, false); return empty($the_term) ? 1 : 2; } return 0; }
function mediatags_delete_attachment_proc($postid = '') { if (!$postid) { return; } wp_delete_object_term_relationships($postid, array(MEDIA_TAGS_TAXONOMY)); }
protected function deleteRecords($is_delete_attachments, $is_deleted_images, $ids = array()) { foreach ($ids as $k => $id) { // Remove attachments if ($is_delete_attachments == 'yes' or $is_delete_attachments == 'auto' and empty($this->options['is_keep_attachments'])) { wp_delete_attachments($id, true, 'files'); } else { wp_delete_attachments($id, false, 'files'); } // Remove images if ($is_deleted_images == 'yes' or $is_deleted_images == 'auto' and empty($this->options['is_keep_imgs'])) { wp_delete_attachments($id, true, 'images'); } else { wp_delete_attachments($id, false, 'images'); } do_action('pmxi_delete_post', $id); if ($this->options['custom_type'] != 'import_users') { wp_delete_object_term_relationships($id, get_object_taxonomies('' != $this->options['custom_type'] ? $this->options['custom_type'] : 'post')); } } if ($this->options['custom_type'] == 'import_users') { $sql = "delete a,b\n\t\t\tFROM " . $this->wpdb->users . " a\n\t\t\tLEFT JOIN " . $this->wpdb->usermeta . " b ON ( a.ID = b.user_id )\t\t\t\t\t\n\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } else { $sql = "delete a,b,c\n\t\t\tFROM " . $this->wpdb->posts . " a\n\t\t\tLEFT JOIN " . $this->wpdb->term_relationships . " b ON ( a.ID = b.object_id )\n\t\t\tLEFT JOIN " . $this->wpdb->postmeta . " c ON ( a.ID = c.post_id )\n\t\t\tLEFT JOIN " . $this->wpdb->posts . " d ON ( a.ID = d.post_parent )\n\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } $this->wpdb->query($sql); }
/** * This function mimics the WordPress function wp_delete_object_term_relationships() * because we cannot hook into the function without receiving errors. * * As of 1.3.1, runs 'cpt_onomy_deleted_object_term_relationship' action to allow user * to run code when relationships are deleted. Action provides 4 arguments: the term id, * the taxonomy/CPT-onomy, the object id/post id and object post type. * * @since 1.0 * @uses $wpdb, $cpt_onomies_manager * @param int $object_id The term Object Id that refers to the term * @param string|array $taxonomies List of Taxonomy Names or single Taxonomy name. If not set, deletes ALL relationships * @return boolean|WP_Error - true if relationships are deleted, otherwise false * @filters 'cpt_onomy_deleted_object_term_relationship' - $term_id, $taxonomy, $object_id, $object_post_type */ public function wp_delete_object_term_relationships($object_id, $taxonomies = NULL) { global $wpdb, $cpt_onomies_manager; $object_id = (int) $object_id; // Delete ALL relationships if (empty($taxonomies)) { return delete_post_meta($object_id, CPT_ONOMIES_POSTMETA_KEY); } else { if (!is_array($taxonomies)) { $taxonomies = array($taxonomies); } // This function only filters registered CPT-onomies $cpt_taxonomies = array(); foreach ($taxonomies as $taxonomy) { if ($cpt_onomies_manager->is_registered_cpt_onomy($taxonomy)) { $cpt_taxonomies[] = $taxonomy; } } // This means there are no CPT-onomies so wrap things up if (empty($cpt_taxonomies)) { return wp_delete_object_term_relationships($object_id, $taxonomies); } else { $taxonomies = $cpt_taxonomies; } // Add "quotes" foreach ($taxonomies as $index => $taxonomy) { $taxonomies[$index] = "'" . $taxonomy . "'"; } $taxonomies = implode(',', $taxonomies); // Get IDs of terms being deleted to use for 'cpt_onomy_deleted_object_term_relationship' action if ($term_ids_being_deleted = $wpdb->get_col($wpdb->prepare("SELECT wpmeta.meta_value FROM {$wpdb->postmeta} wpmeta INNER JOIN {$wpdb->posts} wpp ON wpp.ID = wpmeta.meta_value WHERE wpp.post_type IN (" . $taxonomies . ") AND wpmeta.post_id = %d AND wpmeta.meta_key = %s", $object_id, CPT_ONOMIES_POSTMETA_KEY))) { // Delete object relationships with specific taxonomies if ($wpdb->query($wpdb->prepare("DELETE wpmeta.* FROM {$wpdb->postmeta} wpmeta INNER JOIN {$wpdb->posts} wpp ON wpp.ID = wpmeta.meta_value WHERE wpp.post_type IN (" . $taxonomies . ") AND wpmeta.post_id = %d AND wpmeta.meta_key = %s", $object_id, CPT_ONOMIES_POSTMETA_KEY))) { // Action allows user to run code when relationships are deleted foreach ($term_ids_being_deleted as $deleted_term_id) { do_action('cpt_onomy_deleted_object_term_relationship', $deleted_term_id, get_post_type($deleted_term_id), $object_id, get_post_type($object_id)); } return true; } } return false; } }
/** * Clear associations with posts * @param bool[optional] $keepPosts When set to false associated wordpress posts will be deleted as well * @return PMXI_Import_Record * @chainable */ public function deletePosts($keepPosts = TRUE, $is_deleted_images = 'auto', $is_delete_attachments = 'auto') { $post = new PMXI_Post_List(); if (!$keepPosts) { $ids = array(); foreach ($post->getBy('import_id', $this->id)->convertRecords() as $p) { // Remove attachments if ($is_delete_attachments == 'yes' or $is_delete_attachments == 'auto' and empty($this->options['is_keep_attachments'])) { wp_delete_attachments($p->post_id, true, 'files'); } else { wp_delete_attachments($p->post_id, false, 'files'); } // Remove images if ($is_deleted_images == 'yes' or $is_deleted_images == 'auto' and empty($this->options['is_keep_imgs'])) { wp_delete_attachments($p->post_id, true, 'images'); } else { wp_delete_attachments($p->post_id, false, 'images'); } $ids[] = $p->post_id; } if (!empty($ids)) { foreach ($ids as $id) { do_action('pmxi_delete_post', $id); if ($this->options['custom_type'] != 'import_users') { wp_delete_object_term_relationships($id, get_object_taxonomies('' != $this->options['custom_type'] ? $this->options['custom_type'] : 'post')); } } if ($this->options['custom_type'] == 'import_users') { $sql = "delete a,b\n\t\t\t\t\tFROM " . $this->wpdb->users . " a\n\t\t\t\t\tLEFT JOIN " . $this->wpdb->usermeta . " b ON ( a.ID = b.user_id )\t\t\t\t\t\n\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } else { $sql = "delete a,b,c\n\t\t\t\t\tFROM " . $this->wpdb->posts . " a\n\t\t\t\t\tLEFT JOIN " . $this->wpdb->term_relationships . " b ON ( a.ID = b.object_id )\n\t\t\t\t\tLEFT JOIN " . $this->wpdb->postmeta . " c ON ( a.ID = c.post_id )\n\t\t\t\t\tLEFT JOIN " . $this->wpdb->posts . " d ON ( a.ID = d.post_parent )\n\t\t\t\t\tWHERE a.ID IN (" . implode(',', $ids) . ");"; } $this->wpdb->query($this->wpdb->prepare($sql, '')); } } $this->wpdb->query($this->wpdb->prepare('DELETE FROM ' . $post->getTable() . ' WHERE import_id = %s', $this->id)); return $this; }
/** * wp_delete_attachment() - Delete an attachment * * {@internal Missing Long Description}} * * @package WordPress * @subpackage Post * @since 2.0 * @uses $wpdb * * @param int $postid attachment Id * @return mixed {@internal Missing Description}} */ function wp_delete_attachment($postid) { global $wpdb; if (!($post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $postid)))) { return $post; } if ('attachment' != $post->post_type) { return false; } $meta = wp_get_attachment_metadata($postid); $file = get_attached_file($postid); /** @todo Delete for pluggable post taxonomies too */ wp_delete_object_term_relationships($postid, array('category', 'post_tag')); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->posts} WHERE ID = %d", $postid)); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->comments} WHERE comment_post_ID = %d", $postid)); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d ", $postid)); if (!empty($meta['thumb'])) { // Don't delete the thumb if another attachment uses it if (!$wpdb->get_row($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $meta['thumb'] . '%', $postid))) { $thumbfile = str_replace(basename($file), $meta['thumb'], $file); $thumbfile = apply_filters('wp_delete_file', $thumbfile); @unlink($thumbfile); } } // remove intermediate images if there are any $sizes = apply_filters('intermediate_image_sizes', array('thumbnail', 'medium')); foreach ($sizes as $size) { if ($intermediate = image_get_intermediate_size($postid, $size)) { $intermediate_file = apply_filters('wp_delete_file', $intermediate['path']); @unlink($intermediate_file); } } $file = apply_filters('wp_delete_file', $file); if (!empty($file)) { @unlink($file); } clean_post_cache($postid); do_action('delete_attachment', $postid); return $post; }
/** * Delete's permanently people (or person). * * @since 1.0.0 * * @return void */ protected function _delete_permanently_people() { $PERM = EE_Registry::instance()->load_model('Person'); $PPST = EE_Registry::instance()->load_Model('Person_Post'); $total_deleted = 0; $total_not_deleted = 0; if (!empty($this->_req_data['checkbox']) && is_array($this->_req_data['checkbox'])) { $count = count($this->_req_data['checkbox']); while (list($PER_ID, $value) = each($this->_req_data['checkbox'])) { //first delete any relationships with other posts for this id. $PPST->delete(array(array('PER_ID' => $PER_ID))); //delete any term_taxonomy_relationships (gonna use wp core functions cause it's likely a bit faster) wp_delete_object_term_relationships($PER_ID, array('espresso_people_type', 'espresso_people_categories')); //now should be able to delete permanently with no issues. $deleted = $PERM->delete_permanently_by_ID($PER_ID, false); if ($deleted) { $total_deleted++; } else { $total_not_deleted++; } } } else { $PER_ID = isset($this->_req_data['PER_ID']) ? absint($this->_req_data['PER_ID']) : 0; if (empty($PER_ID)) { EE_Error::add_error(__('Unable to delete permanently the selected Person because no ID was given.', 'event_espresso'), __FILE__, __FUNCTION__, __LINE__); $total_not_deleted++; } //first delete any relationships with other posts for this id. $PPST->delete(array(array('PER_ID' => $PER_ID))); //delete any term_taxonomy_relationships (gonna use wp core functions cause it's likely a bit faster) wp_delete_object_term_relationships($PER_ID, array('espresso_people_type', 'espresso_people_categories')); $deleted = $PERM->delete_permanently_by_ID($PER_ID, false); if ($deleted) { $total_deleted++; } else { $total_not_deleted++; } } if ($total_deleted > 0) { EE_Error::add_success(sprintf(_n('1 Person successfully deleted.', '%s People successfully deleted.', $total_deleted, 'event_espresso'), $total_deleted)); } if ($total_not_deleted > 0) { EE_Error::add_error(sprintf(_n('1 Person not deleted.', '%d People not deleted', $total_not_deleted, 'event_espresso'), $total_not_deleted), __FILE__, __FUNCTION__, __LINE__); } $this->_redirect_after_action(FALSE, '', '', array('action' => 'default'), TRUE); }
/** Function and action for deleting single products */ function wpsc_delete_product() { global $wpdb; $deleted = 0; $product_id = absint($_GET['product']); check_admin_referer('delete_product_' . $product_id); if ($wpdb->query("UPDATE `" . WPSC_TABLE_PRODUCT_LIST . "` SET `active` = '0' WHERE `id`='{$product_id}' LIMIT 1")) { $wpdb->query("DELETE FROM `" . WPSC_TABLE_PRODUCTMETA . "` WHERE `product_id` = '{$product_id}' AND `meta_key` IN ('url_name')"); product_tag_init(); $term = wp_get_object_terms($product_id, 'product_tag'); if ($term->errors == '') { wp_delete_object_term_relationships($product_id, 'product_tag'); } $deleted = 1; do_action('wpsc_delete_product', $product_id); } $sendback = wp_get_referer(); if (isset($deleted)) { $sendback = add_query_arg('deleted', $deleted, $sendback); } wp_redirect($sendback); exit; }
/** * Trashes or deletes 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 2.0.0 * @uses $wpdb * @uses do_action() Calls 'delete_attachment' hook on Attachment ID. * * @param int $post_id Attachment ID. * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false. * @return mixed False on failure. Post data on success. */ function wp_delete_attachment($post_id, $force_delete = false) { global $wpdb; if (!($post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->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 wp_trash_post($post_id); } delete_post_meta($post_id, '_wp_trash_meta_status'); delete_post_meta($post_id, '_wp_trash_meta_time'); $meta = wp_get_attachment_metadata($post_id); $backup_sizes = get_post_meta($post->ID, '_wp_attachment_backup_sizes', true); $file = get_attached_file($post_id); if (is_multisite()) { delete_transient('dirsize_cache'); } do_action('delete_attachment', $post_id); wp_delete_object_term_relationships($post_id, array('category', 'post_tag')); wp_delete_object_term_relationships($post_id, get_object_taxonomies($post->post_type)); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_key = '_thumbnail_id' AND meta_value = %d", $post_id)); $comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d", $post_id)); if (!empty($comment_ids)) { do_action('delete_comment', $comment_ids); foreach ($comment_ids as $comment_id) { wp_delete_comment($comment_id, true); } do_action('deleted_comment', $comment_ids); } $post_meta_ids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d ", $post_id)); if (!empty($post_meta_ids)) { do_action('delete_postmeta', $post_meta_ids); $in_post_meta_ids = "'" . implode("', '", $post_meta_ids) . "'"; $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_id IN({$in_post_meta_ids})"); do_action('deleted_postmeta', $post_meta_ids); } do_action('delete_post', $post_id); $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->posts} WHERE ID = %d", $post_id)); do_action('deleted_post', $post_id); $uploadpath = wp_upload_dir(); if (!empty($meta['thumb'])) { // Don't delete the thumb if another attachment uses it if (!$wpdb->get_row($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE %s AND post_id <> %d", '%' . $meta['thumb'] . '%', $post_id))) { $thumbfile = str_replace(basename($file), $meta['thumb'], $file); $thumbfile = apply_filters('wp_delete_file', $thumbfile); @unlink(path_join($uploadpath['basedir'], $thumbfile)); } } // remove intermediate and backup images if there are any foreach (get_intermediate_image_sizes() as $size) { if ($intermediate = image_get_intermediate_size($post_id, $size)) { $intermediate_file = apply_filters('wp_delete_file', $intermediate['path']); @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']); $del_file = apply_filters('wp_delete_file', $del_file); @unlink(path_join($uploadpath['basedir'], $del_file)); } } $file = apply_filters('wp_delete_file', $file); if (!empty($file)) { @unlink($file); } clean_post_cache($post_id); return $post; }
/** * Delete an image entry from the database * @param integer $id is the Image ID */ static function delete_image($id) { global $wpdb; // Delete the image $result = $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->nggpictures} WHERE pid = %d", $id)); // Delete tag references wp_delete_object_term_relationships($id, 'ngg_tag'); // Remove from cache wp_cache_delete($id, 'ngg_image'); return $result; }