/**
  * Save or update custom values
  *
  * @param array $values - an associative array of values
  * @param string $theme optional - the theme name, defaults to the active the theme
  */
 static function update_custom_values($values, $theme = null)
 {
     if (empty($theme)) {
         $theme_data = wp_get_theme();
         $theme = $theme_data->get_stylesheet();
     }
     // Need the current version of the settings
     if ($post = self::get_post()) {
         $post_id = $post->ID;
     } else {
         $post_id = false;
     }
     if (!is_array($values)) {
         $values = array();
     } else {
         foreach ($values as $field => $value) {
             //fix the pixels ones
             if (strpos($field, "-pixels")) {
                 $values[str_replace("-pixels", "", $field)] = $value . "px";
                 unset($values[$field]);
             }
         }
     }
     $post_data = array('post_content' => json_encode($values), 'post_name' => $theme, 'post_type' => self::POST_TYPE, 'post_status' => 'publish');
     if (empty($post_id)) {
         $post_id = wp_insert_post($post_data);
     } else {
         $post_data['ID'] = $post_id;
         wp_update_post($post_data);
         // Clear out meta data
         $meta_keys = get_post_custom_keys($post_id);
         if (count($meta_keys)) {
             foreach ($meta_keys as $meta_key) {
                 delete_post_meta($post_id, $meta_key);
             }
         }
         // Delete revisions past the five most recent
         $revisions = wp_get_post_revisions($post_id);
         $revisions = array_slice($revisions, 5);
         foreach ($revisions as $revision) {
             wp_delete_post_revision($revision->ID);
         }
     }
     // clear cache
     $cache_key = 'customlessvars_' . $theme . '_current';
     delete_transient($cache_key);
     // Regenerate and cache
     foreach (self::$less_files as $less_file) {
         if ($compiled = self::compile_less($less_file, $values)) {
             update_post_meta($post_id, $less_file, addslashes($compiled));
         }
     }
 }
 /**
  * Does the sweeping/cleaning up
  *
  * @since 1.0.0
  *
  * @access public
  * @param string $name
  * @return string Processed message
  */
 public function sweep($name)
 {
     global $wpdb;
     $message = '';
     switch ($name) {
         case 'revisions':
             $query = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_type = %s", 'revision'));
             if ($query) {
                 foreach ($query as $id) {
                     wp_delete_post_revision(intval($id));
                 }
                 $message = sprintf(__('%s Revisions Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'auto_drafts':
             $query = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_status = %s", 'auto-draft'));
             if ($query) {
                 foreach ($query as $id) {
                     wp_delete_post(intval($id), true);
                 }
                 $message = sprintf(__('%s Auto Drafts Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'deleted_posts':
             $query = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_status = %s", 'trash'));
             if ($query) {
                 foreach ($query as $id) {
                     wp_delete_post($id, true);
                 }
                 $message = sprintf(__('%s Deleted Posts Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'unapproved_comments':
             $query = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved = %s", '0'));
             if ($query) {
                 foreach ($query as $id) {
                     wp_delete_comment(intval($id), true);
                 }
                 $message = sprintf(__('%s Unapproved Comments Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'spam_comments':
             $query = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_approved = %s", 'spam'));
             if ($query) {
                 foreach ($query as $id) {
                     wp_delete_comment(intval($id), true);
                 }
                 $message = sprintf(__('%s Spam Comments Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'deleted_comments':
             $query = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE (comment_approved = %s OR comment_approved = %s)", 'trash', 'post-trashed'));
             if ($query) {
                 foreach ($query as $id) {
                     wp_delete_comment(intval($id), true);
                 }
                 $message = sprintf(__('%s Trash Comments Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'transient_options':
             $query = $wpdb->get_col($wpdb->prepare("SELECT option_name FROM {$wpdb->options} WHERE option_name LIKE(%s)", '%_transient_%'));
             if ($query) {
                 foreach ($query as $option_name) {
                     if (strpos($option_name, '_site_transient_') !== false) {
                         delete_site_transient(str_replace('_site_transient_', '', $option_name));
                     } else {
                         delete_transient(str_replace('_transient_', '', $option_name));
                     }
                 }
                 $message = sprintf(__('%s Transient Options Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'orphan_postmeta':
             $query = $wpdb->get_results("SELECT post_id, meta_key FROM {$wpdb->postmeta} WHERE post_id NOT IN (SELECT ID FROM {$wpdb->posts})");
             if ($query) {
                 foreach ($query as $meta) {
                     $post_id = intval($meta->post_id);
                     if ($post_id === 0) {
                         $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s", $post_id, $meta->meta_key));
                     } else {
                         delete_post_meta($post_id, $meta->meta_key);
                     }
                 }
                 $message = sprintf(__('%s Orphaned Post Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'orphan_commentmeta':
             $query = $wpdb->get_results("SELECT comment_id, meta_key FROM {$wpdb->commentmeta} WHERE comment_id NOT IN (SELECT comment_ID FROM {$wpdb->comments})");
             if ($query) {
                 foreach ($query as $meta) {
                     $comment_id = intval($meta->comment_id);
                     if ($comment_id === 0) {
                         $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->commentmeta} WHERE comment_id = %d AND meta_key = %s", $comment_id, $meta->meta_key));
                     } else {
                         delete_comment_meta($comment_id, $meta->meta_key);
                     }
                 }
                 $message = sprintf(__('%s Orphaned Comment Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'orphan_usermeta':
             $query = $wpdb->get_results("SELECT user_id, meta_key FROM {$wpdb->usermeta} WHERE user_id NOT IN (SELECT ID FROM {$wpdb->users})");
             if ($query) {
                 foreach ($query as $meta) {
                     $user_id = intval($meta->user_id);
                     if ($user_id === 0) {
                         $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key = %s", $user_id, $meta->meta_key));
                     } else {
                         delete_user_meta($user_id, $meta->meta_key);
                     }
                 }
                 $message = sprintf(__('%s Orphaned User Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'orphan_termmeta':
             $query = $wpdb->get_results("SELECT term_id, meta_key FROM {$wpdb->termmeta} WHERE term_id NOT IN (SELECT term_id FROM {$wpdb->terms})");
             if ($query) {
                 foreach ($query as $meta) {
                     $term_id = intval($meta->term_id);
                     if ($term_id === 0) {
                         $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->termmeta} WHERE term_id = %d AND meta_key = %s", $term_id, $meta->meta_key));
                     } else {
                         delete_term_meta($term_id, $meta->meta_key);
                     }
                 }
                 $message = sprintf(__('%s Orphaned Term Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'orphan_term_relationships':
             $query = $wpdb->get_results("SELECT tr.object_id, tr.term_taxonomy_id, tt.term_id, tt.taxonomy FROM {$wpdb->term_relationships} AS tr INNER JOIN {$wpdb->term_taxonomy} AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy NOT IN ('" . implode('\',\'', $this->get_excluded_taxonomies()) . "') AND tr.object_id NOT IN (SELECT ID FROM {$wpdb->posts})");
             if ($query) {
                 foreach ($query as $tax) {
                     $wp_remove_object_terms = wp_remove_object_terms(intval($tax->object_id), intval($tax->term_id), $tax->taxonomy);
                     if ($wp_remove_object_terms !== true) {
                         $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->term_relationships} WHERE object_id = %d AND term_taxonomy_id = %d", $tax->object_id, $tax->term_taxonomy_id));
                     }
                 }
                 $message = sprintf(__('%s Orphaned Term Relationships Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'unused_terms':
             $query = $wpdb->get_results($wpdb->prepare("SELECT tt.term_taxonomy_id, t.term_id, tt.taxonomy FROM {$wpdb->terms} AS t INNER JOIN {$wpdb->term_taxonomy} AS tt ON t.term_id = tt.term_id WHERE tt.count = %d AND t.term_id NOT IN (" . implode(',', $this->get_excluded_termids()) . ")", 0));
             if ($query) {
                 $check_wp_terms = false;
                 foreach ($query as $tax) {
                     if (taxonomy_exists($tax->taxonomy)) {
                         wp_delete_term(intval($tax->term_id), $tax->taxonomy);
                     } else {
                         $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->term_taxonomy} WHERE term_taxonomy_id = %d", intval($tax->term_taxonomy_id)));
                         $check_wp_terms = true;
                     }
                 }
                 // We need this for invalid taxonomies
                 if ($check_wp_terms) {
                     $wpdb->get_results("DELETE FROM {$wpdb->terms} WHERE term_id NOT IN (SELECT term_id FROM {$wpdb->term_taxonomy})");
                 }
                 $message = sprintf(__('%s Unused Terms Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'duplicated_postmeta':
             $query = $wpdb->get_results($wpdb->prepare("SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, post_id, COUNT(*) AS count FROM {$wpdb->postmeta} GROUP BY post_id, meta_key, meta_value HAVING count > %d", 1));
             if ($query) {
                 foreach ($query as $meta) {
                     $ids = array_map('intval', explode(',', $meta->ids));
                     array_pop($ids);
                     $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE meta_id IN (" . implode(',', $ids) . ") AND post_id = %d", intval($meta->post_id)));
                 }
                 $message = sprintf(__('%s Duplicated Post Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'duplicated_commentmeta':
             $query = $wpdb->get_results($wpdb->prepare("SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, comment_id, COUNT(*) AS count FROM {$wpdb->commentmeta} GROUP BY comment_id, meta_key, meta_value HAVING count > %d", 1));
             if ($query) {
                 foreach ($query as $meta) {
                     $ids = array_map('intval', explode(',', $meta->ids));
                     array_pop($ids);
                     $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->commentmeta} WHERE meta_id IN (" . implode(',', $ids) . ") AND comment_id = %d", intval($meta->comment_id)));
                 }
                 $message = sprintf(__('%s Duplicated Comment Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'duplicated_usermeta':
             $query = $wpdb->get_results($wpdb->prepare("SELECT GROUP_CONCAT(umeta_id ORDER BY umeta_id DESC) AS ids, user_id, COUNT(*) AS count FROM {$wpdb->usermeta} GROUP BY user_id, meta_key, meta_value HAVING count > %d", 1));
             if ($query) {
                 foreach ($query as $meta) {
                     $ids = array_map('intval', explode(',', $meta->ids));
                     array_pop($ids);
                     $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->usermeta} WHERE umeta_id IN (" . implode(',', $ids) . ") AND user_id = %d", intval($meta->user_id)));
                 }
                 $message = sprintf(__('%s Duplicated User Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'duplicated_termmeta':
             $query = $wpdb->get_results($wpdb->prepare("SELECT GROUP_CONCAT(meta_id ORDER BY meta_id DESC) AS ids, term_id, COUNT(*) AS count FROM {$wpdb->termmeta} GROUP BY term_id, meta_key, meta_value HAVING count > %d", 1));
             if ($query) {
                 foreach ($query as $meta) {
                     $ids = array_map('intval', explode(',', $meta->ids));
                     array_pop($ids);
                     $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->termmeta} WHERE meta_id IN (" . implode(',', $ids) . ") AND term_id = %d", intval($meta->term_id)));
                 }
                 $message = sprintf(__('%s Duplicated Term Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'optimize_database':
             $query = $wpdb->get_col('SHOW TABLES');
             if ($query) {
                 $tables = implode(',', $query);
                 $wpdb->query("OPTIMIZE TABLE {$tables}");
                 $message = sprintf(__('%s Tables Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
         case 'oembed_postmeta':
             $query = $wpdb->get_results($wpdb->prepare("SELECT post_id, meta_key FROM {$wpdb->postmeta} WHERE meta_key LIKE(%s)", '%_oembed_%'));
             if ($query) {
                 foreach ($query as $meta) {
                     $post_id = intval($meta->post_id);
                     if ($post_id === 0) {
                         $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->postmeta} WHERE post_id = %d AND meta_key = %s", $post_id, $meta->meta_key));
                     } else {
                         delete_post_meta($post_id, $meta->meta_key);
                     }
                 }
                 $message = sprintf(__('%s oEmbed Caches In Post Meta Processed', 'wp-sweep'), number_format_i18n(sizeof($query)));
             }
             break;
     }
     return apply_filters('wp_sweep_sweep', $message, $name);
 }
/**
 * Trashes or deletes a post or page.
 *
 * When the post and page is permanently deleted, everything that is tied to it is deleted also.
 * This includes comments, post meta fields, and terms associated with the post.
 *
 * The post or page is moved to trash instead of permanently deleted unless trash is
 * disabled, item is already in the trash, or $force_delete is true.
 *
 * @since 1.0.0
 * @uses do_action() on 'delete_post' before deletion unless post type is 'attachment'.
 * @uses do_action() on 'deleted_post' after deletion unless post type is 'attachment'.
 * @uses wp_delete_attachment() if post type is 'attachment'.
 * @uses wp_trash_post() if item should be trashed.
 *
 * @param int $postid Post ID.
 * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false.
 * @return mixed False on failure
 */
function wp_delete_post($postid = 0, $force_delete = false)
{
    global $wpdb;
    if (!($post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $postid)))) {
        return $post;
    }
    if (!$force_delete && ($post->post_type == 'post' || $post->post_type == 'page') && get_post_status($postid) != 'trash' && EMPTY_TRASH_DAYS) {
        return wp_trash_post($postid);
    }
    if ($post->post_type == 'attachment') {
        return wp_delete_attachment($postid, $force_delete);
    }
    do_action('before_delete_post', $postid);
    delete_post_meta($postid, '_wp_trash_meta_status');
    delete_post_meta($postid, '_wp_trash_meta_time');
    wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type));
    $parent_data = array('post_parent' => $post->post_parent);
    $parent_where = array('post_parent' => $postid);
    if (is_post_type_hierarchical($post->post_type)) {
        // Point children of this page to its parent, also clean the cache of affected children
        $children_query = $wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type);
        $children = $wpdb->get_results($children_query);
        $wpdb->update($wpdb->posts, $parent_data, $parent_where + array('post_type' => $post->post_type));
    }
    // Do raw query. wp_get_post_revisions() is filtered
    $revision_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = 'revision'", $postid));
    // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
    foreach ($revision_ids as $revision_id) {
        wp_delete_post_revision($revision_id);
    }
    // Point all attachments to this post up one level
    $wpdb->update($wpdb->posts, $parent_data, $parent_where + array('post_type' => 'attachment'));
    $comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d", $postid));
    foreach ($comment_ids as $comment_id) {
        wp_delete_comment($comment_id, true);
    }
    $post_meta_ids = $wpdb->get_col($wpdb->prepare("SELECT meta_id FROM {$wpdb->postmeta} WHERE post_id = %d ", $postid));
    foreach ($post_meta_ids as $mid) {
        delete_metadata_by_mid('post', $mid);
    }
    do_action('delete_post', $postid);
    $wpdb->delete($wpdb->posts, array('ID' => $postid));
    do_action('deleted_post', $postid);
    clean_post_cache($post);
    if (is_post_type_hierarchical($post->post_type) && $children) {
        foreach ($children as $child) {
            clean_post_cache($child);
        }
    }
    wp_clear_scheduled_hook('publish_future_post', array($postid));
    do_action('after_delete_post', $postid);
    return $post;
}
 /**
  * Ajax callback to handle deleting the revision, then redirecting
  * back to the post edit page with a confirmation message.
  */
 public function revision_delete_action()
 {
     /**
      * Bail if required values unset.
      */
     if (!isset($_GET['revision'])) {
         return;
     }
     $revision_id = sanitize_key($_GET['revision']);
     /**
      * Verify revision ID valud.
      */
     if (!($revision = wp_get_post_revision($revision_id))) {
         break;
     }
     /**
      * Verify parent post valid.
      */
     if (!($post = get_post($revision->post_parent))) {
         break;
     }
     /**
      * Verify current user can edit parent post.
      */
     if (!current_user_can('edit_post', $post)) {
         break;
     }
     /**
      * Verify revisions not disabled and we're not looking at an autosave.
      */
     if (!constant('WP_POST_REVISIONS') && !wp_is_post_autosave($revision)) {
         break;
     }
     /**
      * Check the nonce.
      */
     check_admin_referer("delete-revision_{$post->ID}|{$revision->ID}");
     /**
      * Every checks out, delete the revision.
      */
     wp_delete_post_revision($revision->ID);
     wp_redirect(add_query_arg(array('message' => 99, 'revision' => $revision->ID), get_edit_post_link($post->ID, 'url')));
     exit;
 }
Exemple #5
0
/**
 * Trash or delete a post or page.
 *
 * When the post and page is permanently deleted, everything that is tied to
 * it is deleted also. This includes comments, post meta fields, and terms
 * associated with the post.
 *
 * The post or page is moved to trash instead of permanently deleted unless
 * trash is disabled, item is already in the trash, or $force_delete is true.
 *
 * @since 1.0.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 * @see wp_delete_attachment()
 * @see wp_trash_post()
 *
 * @param int  $postid       Optional. Post ID. Default 0.
 * @param bool $force_delete Optional. Whether to bypass trash and force deletion.
 *                           Default false.
 * @return array|false|WP_Post False on failure.
 */
function wp_delete_post($postid = 0, $force_delete = false)
{
    global $wpdb;
    if (!($post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $postid)))) {
        return $post;
    }
    if (!$force_delete && ($post->post_type == 'post' || $post->post_type == 'page') && get_post_status($postid) != 'trash' && EMPTY_TRASH_DAYS) {
        return wp_trash_post($postid);
    }
    if ($post->post_type == 'attachment') {
        return wp_delete_attachment($postid, $force_delete);
    }
    /**
     * Filter whether a post deletion should take place.
     *
     * @since 4.4.0
     *
     * @param bool    $delete       Whether to go forward with deletion.
     * @param WP_Post $post         Post object.
     * @param bool    $force_delete Whether to bypass the trash.
     */
    $check = apply_filters('pre_delete_post', null, $post, $force_delete);
    if (null !== $check) {
        return $check;
    }
    /**
     * Fires before a post is deleted, at the start of wp_delete_post().
     *
     * @since 3.2.0
     *
     * @see wp_delete_post()
     *
     * @param int $postid Post ID.
     */
    do_action('before_delete_post', $postid);
    delete_post_meta($postid, '_wp_trash_meta_status');
    delete_post_meta($postid, '_wp_trash_meta_time');
    wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type));
    $parent_data = array('post_parent' => $post->post_parent);
    $parent_where = array('post_parent' => $postid);
    if (is_post_type_hierarchical($post->post_type)) {
        // Point children of this page to its parent, also clean the cache of affected children.
        $children_query = $wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = %s", $postid, $post->post_type);
        $children = $wpdb->get_results($children_query);
        $wpdb->update($wpdb->posts, $parent_data, $parent_where + array('post_type' => $post->post_type));
    }
    // Do raw query. wp_get_post_revisions() is filtered.
    $revision_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = 'revision'", $postid));
    // Use wp_delete_post (via wp_delete_post_revision) again. Ensures any meta/misplaced data gets cleaned up.
    foreach ($revision_ids as $revision_id) {
        wp_delete_post_revision($revision_id);
    }
    // Point all attachments to this post up one level.
    $wpdb->update($wpdb->posts, $parent_data, $parent_where + array('post_type' => 'attachment'));
    wp_defer_comment_counting(true);
    $comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d", $postid));
    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 ", $postid));
    foreach ($post_meta_ids as $mid) {
        delete_metadata_by_mid('post', $mid);
    }
    /**
     * Fires immediately before a post is deleted from the database.
     *
     * @since 1.2.0
     *
     * @param int $postid Post ID.
     */
    do_action('delete_post', $postid);
    $result = $wpdb->delete($wpdb->posts, array('ID' => $postid));
    if (!$result) {
        return false;
    }
    /**
     * Fires immediately after a post is deleted from the database.
     *
     * @since 2.2.0
     *
     * @param int $postid Post ID.
     */
    do_action('deleted_post', $postid);
    clean_post_cache($post);
    if (is_post_type_hierarchical($post->post_type) && $children) {
        foreach ($children as $child) {
            clean_post_cache($child);
        }
    }
    wp_clear_scheduled_hook('publish_future_post', array($postid));
    /**
     * Fires after a post is deleted, at the conclusion of wp_delete_post().
     *
     * @since 3.2.0
     *
     * @see wp_delete_post()
     *
     * @param int $postid Post ID.
     */
    do_action('after_delete_post', $postid);
    return $post;
}
Exemple #6
0
/**
 * Trashes or deletes a post or page.
 *
 * When the post and page is permanently deleted, everything that is tied to it is deleted also.
 * This includes comments, post meta fields, and terms associated with the post.
 *
 * The post or page is moved to trash instead of permanently deleted unless trash is
 * disabled, item is already in the trash, or $force_delete is true.
 *
 * @since 1.0.0
 * @uses do_action() on 'delete_post' before deletion unless post type is 'attachment'.
 * @uses do_action() on 'deleted_post' after deletion unless post type is 'attachment'.
 * @uses wp_delete_attachment() if post type is 'attachment'.
 * @uses wp_trash_post() if item should be trashed.
 *
 * @param int $postid Post ID.
 * @param bool $force_delete Whether to bypass trash and force deletion. Defaults to false.
 * @return mixed False on failure
 */
function wp_delete_post($postid = 0, $force_delete = false)
{
    global $wpdb, $wp_rewrite;
    if (!($post = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE ID = %d", $postid)))) {
        return $post;
    }
    if (!$force_delete && ($post->post_type == 'post' || $post->post_type == 'page') && get_post_status($postid) != 'trash' && EMPTY_TRASH_DAYS) {
        return wp_trash_post($postid);
    }
    if ($post->post_type == 'attachment') {
        return wp_delete_attachment($postid, $force_delete);
    }
    do_action('before_delete_post', $postid);
    delete_post_meta($postid, '_wp_trash_meta_status');
    delete_post_meta($postid, '_wp_trash_meta_time');
    wp_delete_object_term_relationships($postid, get_object_taxonomies($post->post_type));
    $parent_data = array('post_parent' => $post->post_parent);
    $parent_where = array('post_parent' => $postid);
    if ('page' == $post->post_type) {
        // if the page is defined in option page_on_front or post_for_posts,
        // adjust the corresponding options
        if (get_option('page_on_front') == $postid) {
            update_option('show_on_front', 'posts');
            delete_option('page_on_front');
        }
        if (get_option('page_for_posts') == $postid) {
            delete_option('page_for_posts');
        }
        // Point children of this page to its parent, also clean the cache of affected children
        $children_query = $wpdb->prepare("SELECT * FROM {$wpdb->posts} WHERE post_parent = %d AND post_type='page'", $postid);
        $children = $wpdb->get_results($children_query);
        $wpdb->update($wpdb->posts, $parent_data, $parent_where + array('post_type' => 'page'));
    } else {
        unstick_post($postid);
    }
    // Do raw query.  wp_get_post_revisions() is filtered
    $revision_ids = $wpdb->get_col($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = 'revision'", $postid));
    // Use wp_delete_post (via wp_delete_post_revision) again.  Ensures any meta/misplaced data gets cleaned up.
    foreach ($revision_ids as $revision_id) {
        wp_delete_post_revision($revision_id);
    }
    // Point all attachments to this post up one level
    $wpdb->update($wpdb->posts, $parent_data, $parent_where + array('post_type' => 'attachment'));
    $comment_ids = $wpdb->get_col($wpdb->prepare("SELECT comment_ID FROM {$wpdb->comments} WHERE comment_post_ID = %d", $postid));
    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 ", $postid));
    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', $postid);
    $wpdb->query($wpdb->prepare("DELETE FROM {$wpdb->posts} WHERE ID = %d", $postid));
    do_action('deleted_post', $postid);
    if ('page' == $post->post_type) {
        clean_page_cache($postid);
        foreach ((array) $children as $child) {
            clean_page_cache($child->ID);
        }
        $wp_rewrite->flush_rules(false);
    } else {
        clean_post_cache($postid);
    }
    wp_clear_scheduled_hook('publish_future_post', array($postid));
    do_action('after_delete_post', $postid);
    return $post;
}
function tdomf_moderation_handler()
{
    $message .= "";
    # this means a post was deleted
    #
    if (isset($_REQUEST['deleted'])) {
        $message .= __("Submissions deleted. ", "tdomf");
    }
    // bulk actions
    if (isset($_REQUEST['doaction']) && isset($_REQUEST['action']) && isset($_REQUEST['post'])) {
        $posts = $_REQUEST['post'];
        $action = $_REQUEST['action'];
        if ($action != -1 && is_array($posts) && !empty($posts)) {
            check_admin_referer('tdomf-moderate-bulk');
            switch ($action) {
                case 'spam_recheck':
                    $spam_list = array();
                    $ham_list = array();
                    foreach ($posts as $post) {
                        if (tdomf_check_submissions_spam($post)) {
                            $ham_list[] = $post;
                        } else {
                            $spam_list[] = $post;
                        }
                    }
                    tdomf_log_message('Akismet thinks these submissions are spam: ' . implode(", ", $spam_list));
                    $message .= sprintf(__("Marked these submissions as spam: %s.", "tdomf"), implode(", ", $spam_list));
                    tdomf_log_message('Akismet thinks these posts are not spam: ' . implode(", ", $ham_list));
                    $message .= " ";
                    $message .= sprintf(__("Marked these submissions as not spam: %s.", "tdomf"), implode(", ", $ham_list));
                    break;
                case 'delete':
                    foreach ($posts as $p) {
                        wp_delete_post($p);
                    }
                    tdomf_log_message('Deleted ' . implode(", ", $posts) . ' posts');
                    $message .= sprintf(__("Deleted submissions: %s", "tdomf"), implode(", ", $posts));
                    break;
                case 'publish_now':
                    $list = "";
                    foreach ($posts as $p) {
                        if (!get_post_meta($p, TDOMF_KEY_SPAM)) {
                            // if we're going to publish the post, then it's not spam!
                            tdomf_ham_post($p);
                        }
                        tdomf_publish_post($p, false);
                        $list .= "<a href=\"" . get_permalink($p) . "\">" . $p . "</a>, ";
                    }
                    tdomf_log_message("Published {$list} posts");
                    $message .= sprintf(__("Attempted to published these submissions immediately: %s", "tdomf"), $list);
                    break;
                case 'publish':
                    $list = "";
                    foreach ($posts as $p) {
                        if (!get_post_meta($p, TDOMF_KEY_SPAM)) {
                            // if we're going to publish the post, then it's not spam!
                            tdomf_ham_post($p);
                        }
                        tdomf_publish_post($p);
                        $list .= "<a href=\"" . get_permalink($p) . "\">" . $p . "</a>, ";
                    }
                    tdomf_log_message("Published or queued {$list} posts");
                    $message .= sprintf(__("Attempted to publish or queue these submissions: %s", "tdomf"), $list);
                    break;
                case 'unpublish':
                    foreach ($posts as $p) {
                        tdomf_unpublish_post($p);
                    }
                    tdomf_log_message("Un-published " . implode(", ", $posts) . " posts");
                    $message .= sprintf(__("Attempted to un-publish theses submissions: %s", "tdomf"), implode(", ", $posts));
                    break;
                case 'spamit':
                    $spams = array();
                    foreach ($posts as $p) {
                        if (!get_post_meta($p, TDOMF_KEY_SPAM)) {
                            tdomf_spam_post($p);
                            $spams[] = $p;
                        }
                    }
                    tdomf_log_message("Marked as spam " . implode(", ", $spams) . " posts");
                    $message .= sprintf(__("Marked these submissions as spam: %s", "tdomf"), implode(", ", $spams));
                    break;
                case 'hamit':
                    $hams = array();
                    foreach ($posts as $p) {
                        if (get_post_meta($p, TDOMF_KEY_SPAM)) {
                            tdomf_spam_post($p);
                            $hams[] = $p;
                        }
                    }
                    if (!empty($hams)) {
                        tdomf_log_message("Marked as ham " . implode(", ", $hams) . " posts");
                        $message .= sprintf(__("Marked these submissions as not spam: %s", "tdomf"), implode(", ", $hams));
                    }
                    break;
                case 'lock':
                    $locks = array();
                    foreach ($posts as $p) {
                        if (!get_post_meta($p, TDOMF_KEY_LOCK)) {
                            add_post_meta($p, TDOMF_KEY_LOCK, true, true);
                            $locks[] = $p;
                        }
                    }
                    if (!empty($locks)) {
                        tdomf_log_message("Locked " . implode(", ", $locks) . " posts");
                        $message .= sprintf(__("Locked these posts/pages from editing: %s", "tdomf"), implode(", ", $locks));
                    }
                    break;
                case 'unlock':
                    $locks = array();
                    foreach ($posts as $p) {
                        if (get_post_meta($p, TDOMF_KEY_LOCK)) {
                            delete_post_meta($p, TDOMF_KEY_LOCK);
                            $locks[] = $p;
                        }
                    }
                    if (!empty($locks)) {
                        tdomf_log_message("Unlocked " . implode(", ", $locks) . " posts");
                        $message .= sprintf(__("Unlocked these posts/pages: %s", "tdomf"), implode(", ", $locks));
                    }
                    break;
                case 'edit_spam_recheck':
                    $spam_list = array();
                    $ham_list = array();
                    $edit_spam_list = array();
                    $edit_ham_list = array();
                    foreach ($posts as $post) {
                        $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1));
                        if ($last_edit != false && !empty($last_edit)) {
                            if (tdomf_check_edit_spam($last_edit[0]->edit_id, false)) {
                                $ham_list[] = $post;
                                $edit_ham_list[] = $last_edit[0]->edit_id;
                            } else {
                                $spam_list[] = $post;
                                $edit_spam_list[] = $last_edit[0]->edit_id;
                            }
                        }
                    }
                    tdomf_log_message('Akismet thinks these edits are spam: ' . implode(", ", $edit_spam_list));
                    $message .= sprintf(__("Marked last contribution on these submissions as spam: %s.", "tdomf"), implode(", ", $spam_list));
                    tdomf_log_message('Akismet thinks these edits are not spam: ' . implode(", ", $edit_ham_list));
                    $message .= " ";
                    $message .= sprintf(__("Marked last contribution on these submissions as not spam: %s.", "tdomf"), implode(", ", $ham_list));
                    break;
                case 'edit_approve':
                    $edit_list = array();
                    $post_list = array();
                    foreach ($posts as $post) {
                        $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1));
                        if (!empty($last_edit) && $last_edit[0]->state != 'approved') {
                            $edit_list[] = $last_edit[0]->edit_id;
                            $post_list[] = $post;
                            $user_id = $last_edit[0]->user_id;
                            if ($last_edit[0]->state == 'spam') {
                                tdomf_hamit_edit($last_edit[0]);
                            }
                            wp_restore_post_revision($edit->revision_id);
                            tdomf_set_state_edit('approved', $last_edit[0]->edit_id);
                            if ($user_id > 0) {
                                tdomf_trust_user($user_id);
                            }
                        }
                    }
                    tdomf_log_message('These edits have been approved: ' . implode(", ", $edit_list));
                    $message .= sprintf(__("Approved contributions on these submissions: %s.", "tdomf"), implode(", ", $post_list));
                    break;
                case 'edit_revert':
                    $edit_list = array();
                    $post_list = array();
                    foreach ($posts as $post) {
                        $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1));
                        if (!empty($last_edit) && $last_edit[0]->state == 'approved' && $last_edit[0]->revision_id != 0 && $last_edit[0]->current_revision_id != 0) {
                            $edit_list[] = $last_edit[0]->edit_id;
                            $post_list[] = $post;
                            wp_restore_post_revision($last_edit[0]->current_revision_id);
                            tdomf_set_state_edit('unapproved', $last_edit[0]->edit_id);
                        }
                    }
                    tdomf_log_message('These edits have been reverted: ' . implode(", ", $edit_list));
                    $message .= sprintf(__("Latest contribution on these submissions have been reverted: %s.", "tdomf"), implode(", ", $post_list));
                    break;
                case 'edit_delete':
                    $edit_list = array();
                    $post_list = array();
                    foreach ($posts as $post) {
                        $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1));
                        if (!empty($last_edit) && $last_edit[0]->state != 'approved') {
                            $edit_list[] = $last_edit[0]->edit_id;
                            $post_list[] = $post;
                            if ($last_edit[0]->revision_id != 0) {
                                wp_delete_post_revision($edit->revision_id);
                                tdomf_log_message("Deleting revision " . $last_edit[0]->revision_id . " on post " . $post);
                            }
                            if ($last_edit[0]->current_revision_id != 0) {
                                wp_delete_post_revision($last_edit[0]->current_revision_id);
                                tdomf_log_message("Deleting revision " . $last_edit[0]->current_revision_id . " on post " . $post);
                            }
                        }
                        tdomf_delete_edits($edit_list);
                    }
                    tdomf_log_message('These edits have been deleted: ' . implode(", ", $edit_list));
                    $message .= sprintf(__("Latest contribution on these submissions have been deleted: %s.", "tdomf"), implode(", ", $post_list));
                    break;
                case 'edit_spamit':
                    $edit_list = array();
                    $post_list = array();
                    foreach ($posts as $post) {
                        $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1));
                        if (!empty($last_edit) && $last_edit[0]->state != 'spam') {
                            $edit_list[] = $last_edit[0]->edit_id;
                            $post_list[] = $post;
                            tdomf_spamit_edit($last_edit[0]);
                        }
                    }
                    tdomf_log_message('These edits have been marked as spam: ' . implode(", ", $edit_list));
                    $message .= sprintf(__("Latest contribution on these submissions have been marked as spam: %s.", "tdomf"), implode(", ", $post_list));
                    break;
                case 'edit_hamit':
                    $edit_list = array();
                    $post_list = array();
                    foreach ($posts as $post) {
                        $last_edit = tdomf_get_edits(array('post_id' => $post, 'limit' => 1));
                        if (!empty($last_edit) && $last_edit[0]->state == 'soam') {
                            $edit_list[] = $last_edit[0]->edit_id;
                            $post_list[] = $post;
                            tdomf_hamit_edit($last_edit[0]);
                        }
                    }
                    tdomf_log_message('These edits have been marked as not spam: ' . implode(", ", $edit_list));
                    $message .= sprintf(__("Latest contribution on these submissions have been marked as not being spam: %s.", "tdomf"), implode(", ", $post_list));
                    break;
                default:
                    tdomf_log_message('Unexpected bulk action ' . $action . ' in moderation screen!', TDOMF_LOG_BAD);
                    $message .= sprintf(__("Unrecognised bulk action %s,", "tdomf"), $action);
                    break;
            }
        }
        // else no posts selected or bulk actions
        // individual actions
        // operations on posts/pages (submissions)
    } else {
        if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'publish_now') {
            $post_id = $_REQUEST['post'];
            check_admin_referer('tdomf-publish_' . $post_id);
            // if we're going to publish the post, then it's not spam!
            tdomf_ham_post($post_id);
            tdomf_publish_post($post_id, false);
            tdomf_log_message("Published post {$post_id}");
            $message .= sprintf(__("Published post <a href=\"%s\">%d</a>.", "tdomf"), get_permalink($post_id), $post_id);
        } else {
            if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'publish') {
                $post_id = $_REQUEST['post'];
                check_admin_referer('tdomf-publish_' . $post_id);
                // if we're going to publish the post, then it's not spam!
                tdomf_ham_post($post_id);
                tdomf_publish_post($post_id);
                tdomf_log_message("Published post {$post_id}");
                $message .= sprintf(__("Published post <a href=\"%s\">%d</a>.", "tdomf"), get_permalink($post_id), $post_id);
            } else {
                if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'unpublish') {
                    $post_id = $_REQUEST['post'];
                    check_admin_referer('tdomf-unpublish_' . $post_id);
                    tdomf_unpublish_post($post_id);
                    tdomf_log_message("Unpublished post {$post_id}");
                    $message .= sprintf(__("Unpublished post %d.", "tdomf"), $post_id);
                } else {
                    if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'spamit') {
                        $post_id = $_REQUEST['post'];
                        check_admin_referer('tdomf-spamit_' . $post_id);
                        if (!get_post_meta($post_id, TDOMF_KEY_SPAM)) {
                            tdomf_spam_post($post_id);
                            tdomf_log_message("Post {$post_id} submitted as spam");
                            $message .= sprintf(__("Post %d flagged as spam", "tdomf"), $post_id);
                        } else {
                            $message .= sprintf(__("Did not flag post %d as being spam as it is already flagged appropriately.", "tdomf"), $post_id);
                        }
                    } else {
                        if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'hamit') {
                            $post_id = $_REQUEST['post'];
                            check_admin_referer('tdomf-hamit_' . $post_id);
                            if (get_post_meta($post_id, TDOMF_KEY_SPAM)) {
                                tdomf_ham_post($post_id);
                                tdomf_log_message("Post {$post_id} submitted as ham");
                                $message .= sprintf(__("Post %d flagged as not being spam", "tdomf"), $post_id);
                            } else {
                                $message .= sprintf(__("Did not flag post %d as not being spam as it is already flagged appropriately.", "tdomf"), $post_id);
                            }
                        } else {
                            if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'lock') {
                                $post_id = $_REQUEST['post'];
                                check_admin_referer('tdomf-lock_' . $post_id);
                                if (!get_post_meta($post_id, TDOMF_KEY_LOCK)) {
                                    add_post_meta($post_id, TDOMF_KEY_LOCK, true, true);
                                    tdomf_log_message("Post {$post_id} locked");
                                    $message .= sprintf(__("Post %d is now locked from editing", "tdomf"), $post_id);
                                } else {
                                    $message .= sprintf(__("Post %d is already locked from editing.", "tdomf"), $post_id);
                                }
                            } else {
                                if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'unlock') {
                                    $post_id = $_REQUEST['post'];
                                    check_admin_referer('tdomf-unlock_' . $post_id);
                                    if (get_post_meta($post_id, TDOMF_KEY_LOCK)) {
                                        delete_post_meta($post_id, TDOMF_KEY_LOCK);
                                        tdomf_log_message("Post {$post_id} unlocked");
                                        $message .= sprintf(__("Post %d is now unlocked.", "tdomf"), $post_id);
                                    } else {
                                        $message .= sprintf(__("Post %d is already unlocked.", "tdomf"), $post_id);
                                    }
                                } else {
                                    if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'approve_edit') {
                                        $edit_id = $_REQUEST['edit'];
                                        check_admin_referer('tdomf-approve_edit_' . $edit_id);
                                        $edit = tdomf_get_edit($edit_id);
                                        if ($edit && ($edit->state == 'spam' || $edit->state == 'unapproved')) {
                                            if ($edit->state == 'spam') {
                                                tdomf_hamit_edit($edit);
                                            }
                                            wp_restore_post_revision($edit->revision_id);
                                            tdomf_set_state_edit('approved', $edit_id);
                                            if ($edit->user_id > 0) {
                                                tdomf_trust_user($edit->user_id);
                                            }
                                            tdomf_log_message("Edit {$edit_id} has been approved on post " . $edit->post_id);
                                            $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has been approved and published', "tdomf"), get_permalink($edit->post_id), $edit->post_id);
                                        } else {
                                            tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD);
                                            $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id);
                                        }
                                    } else {
                                        if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'revert_edit') {
                                            $edit_id = $_REQUEST['edit'];
                                            check_admin_referer('tdomf-revert_edit_' . $edit_id);
                                            $edit = tdomf_get_edit($edit_id);
                                            if ($edit && $edit->state == 'approved' && $edit->revision_id != 0 && $edit->current_revision_id != 0) {
                                                wp_restore_post_revision($edit->current_revision_id);
                                                tdomf_set_state_edit('unapproved', $edit_id);
                                                tdomf_log_message("Edit {$edit_id} on post " . $edit->post_id . " has been reverted");
                                                $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has reverted to previous revision', "tdomf"), get_permalink($edit->post_id), $edit->post_id);
                                            } else {
                                                tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD);
                                                $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id);
                                            }
                                        } else {
                                            if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'delete_edit') {
                                                $edit_id = $_REQUEST['edit'];
                                                check_admin_referer('tdomf-delete_edit_' . $edit_id);
                                                $edit = tdomf_get_edit($edit_id);
                                                if ($edit && $edit->state != 'approved') {
                                                    $post_id = $edit->post_id;
                                                    if ($edit->revision_id != 0) {
                                                        wp_delete_post_revision($edit->revision_id);
                                                        tdomf_log_message("Deleting revision {$revision_id} on post " . $post_id);
                                                    }
                                                    if ($edit->current_revision_id != 0) {
                                                        wp_delete_post_revision($edit->current_revision_id);
                                                        tdomf_log_message("Deleting revision {$current_revision_id} on post " . $post_id);
                                                    }
                                                    tdomf_delete_edits(array($edit_id));
                                                    tdomf_log_message("Edit {$edit_id} on post " . $post_id . " has been deleted");
                                                    $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has deleted', "tdomf"), get_permalink($edit->post_id), $edit->post_id);
                                                } else {
                                                    tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD);
                                                    $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id);
                                                }
                                            } else {
                                                if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'spamit_edit') {
                                                    $edit_id = $_REQUEST['edit'];
                                                    check_admin_referer('tdomf-spamit_edit_' . $edit_id);
                                                    $edit = tdomf_get_edit($edit_id);
                                                    if ($edit && $edit->state != 'spam') {
                                                        tdomf_spamit_edit($edit);
                                                        tdomf_log_message("Marking edit {$edit_id} as spam!");
                                                        $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has been flagged as spam', "tdomf"), get_permalink($edit->post_id), $edit->post_id);
                                                    } else {
                                                        tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD);
                                                        $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id);
                                                    }
                                                } else {
                                                    if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'hamit_edit') {
                                                        $edit_id = $_REQUEST['edit'];
                                                        check_admin_referer('tdomf-hamit_edit_' . $edit_id);
                                                        $edit = tdomf_get_edit($edit_id);
                                                        if ($edit && $edit->state == 'spam') {
                                                            tdomf_spamit_edit($edit);
                                                            tdomf_log_message("Marking edit {$edit_id} as not spam!");
                                                            $message .= sprintf(__('Contribution to <a href="%s">Post %d</a> has been flagged as not being spam', "tdomf"), get_permalink($edit->post_id), $edit->post_id);
                                                        } else {
                                                            tdomf_log_message("Invalid {$action} performed on edit {$edit_id}", TDOMF_LOG_BAD);
                                                            $message .= sprintf(__('Invalid action %s or invalid edit identifier %d!', 'tdomf'), $_REQUEST['action'], $edit_id);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    if (!empty($message)) {
        ?>
      <div id="message" class="updated fade"><p><?php 
        echo $message;
        ?>
</p></div>
   <?php 
    }
}
Exemple #8
0
/**
 * Creates autosave data for the specified post from $_POST data.
 *
 * @package WordPress
 * @subpackage Post_Revisions
 * @since 2.6.0
 *
 * @param mixed $post_data Associative array containing the post data or int post ID.
 * @return mixed The autosave revision ID. WP_Error or 0 on error.
 */
function wp_create_post_autosave($post_data)
{
    if (is_numeric($post_data)) {
        $post_id = $post_data;
        $post_data = $_POST;
    } else {
        $post_id = (int) $post_data['post_ID'];
    }
    $post_data = _wp_translate_postdata(true, $post_data);
    if (is_wp_error($post_data)) {
        return $post_data;
    }
    $post_author = get_current_user_id();
    // Store one autosave per author. If there is already an autosave, overwrite it.
    if ($old_autosave = wp_get_post_autosave($post_id, $post_author)) {
        $new_autosave = _wp_post_revision_data($post_data, true);
        $new_autosave['ID'] = $old_autosave->ID;
        $new_autosave['post_author'] = $post_author;
        // If the new autosave has the same content as the post, delete the autosave.
        $post = get_post($post_id);
        $autosave_is_different = false;
        foreach (array_intersect(array_keys($new_autosave), array_keys(_wp_post_revision_fields($post))) as $field) {
            if (normalize_whitespace($new_autosave[$field]) != normalize_whitespace($post->{$field})) {
                $autosave_is_different = true;
                break;
            }
        }
        if (!$autosave_is_different) {
            wp_delete_post_revision($old_autosave->ID);
            return 0;
        }
        /**
         * Fires before an autosave is stored.
         *
         * @since 4.1.0
         *
         * @param array $new_autosave Post array - the autosave that is about to be saved.
         */
        do_action('wp_creating_autosave', $new_autosave);
        return wp_update_post($new_autosave);
    }
    // _wp_put_post_revision() expects unescaped.
    $post_data = wp_unslash($post_data);
    // Otherwise create the new autosave as a special post revision
    return _wp_put_post_revision($post_data, true);
}
 public function meta_modal()
 {
     global $post, $post_type, $post_type_object, $current_screen, $wp_meta_modal_sections;
     $post_ID = $post->ID;
     set_current_screen($post_type);
     if (!wp_check_post_lock($post->ID)) {
         $active_post_lock = wp_set_post_lock($post->ID);
     }
     $messages = array();
     $messages['post'] = array(0 => '', 1 => sprintf(__('Post updated. <a href="%s">View post</a>', 'wplms-front-end'), esc_url(get_permalink($post_ID))), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Post updated.'), 5 => isset($_GET['revision']) ? sprintf(__('Post restored to revision from %s', 'wplms-front-end'), wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Post published. <a href="%s">View post</a>', 'wplms-front-end'), esc_url(get_permalink($post_ID))), 7 => __('Post saved.'), 8 => sprintf(__('Post submitted. <a target="_blank" href="%s">Preview post</a>', 'wplms-front-end'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))), 9 => sprintf(__('Post scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview post</a>', 'wplms-front-end'), date_i18n(__('M j, Y @ G:i'), strtotime($post->post_date)), esc_url(get_permalink($post_ID))), 10 => sprintf(__('Post draft updated. <a target="_blank" href="%s">Preview post</a>', 'wplms-front-end'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))));
     $messages['page'] = array(0 => '', 1 => sprintf(__('Page updated. <a href="%s">View page</a>', 'wplms-front-end'), esc_url(get_permalink($post_ID))), 2 => __('Custom field updated.'), 3 => __('Custom field deleted.'), 4 => __('Page updated.'), 5 => isset($_GET['revision']) ? sprintf(__('Page restored to revision from %s'), 'wplms-front-end', wp_post_revision_title((int) $_GET['revision'], false)) : false, 6 => sprintf(__('Page published. <a href="%s">View page</a>', 'wplms-front-end'), esc_url(get_permalink($post_ID))), 7 => __('Page saved.'), 8 => sprintf(__('Page submitted. <a target="_blank" href="%s">Preview page</a>', 'wplms-front-end'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))), 9 => sprintf(__('Page scheduled for: <strong>%1$s</strong>. <a target="_blank" href="%2$s">Preview page</a>', 'wplms-front-end'), date_i18n(__('M j, Y @ G:i'), strtotime($post->post_date)), esc_url(get_permalink($post_ID))), 10 => sprintf(__('Page draft updated. <a target="_blank" href="%s">Preview page</a>', 'wplms-front-end'), esc_url(add_query_arg('preview', 'true', get_permalink($post_ID)))));
     $messages['attachment'] = array_fill(1, 10, __('Media attachment updated.', 'wplms-front-end'));
     // Hack, for now.
     $messages = apply_filters('post_updated_messages', $messages);
     $message = false;
     if (isset($_GET['message'])) {
         $_GET['message'] = absint($_GET['message']);
         if (isset($messages[$post_type][$_GET['message']])) {
             $message = $messages[$post_type][$_GET['message']];
         } elseif (!isset($messages[$post_type]) && isset($messages['post'][$_GET['message']])) {
             $message = $messages['post'][$_GET['message']];
         }
     }
     $notice = false;
     $form_extra = '';
     if ('auto-draft' == $post->post_status) {
         if ('edit' == $action) {
             $post->post_title = '';
         }
         $autosave = false;
         $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
     } else {
         $autosave = wp_get_post_autosave($post_ID);
     }
     $form_action = 'editpost';
     $nonce_action = 'update-post_' . $post_ID;
     // Detect if there exists an autosave newer than the post and if that autosave is different than the post
     if ($autosave && mysql2date('U', $autosave->post_modified_gmt, false) > mysql2date('U', $post->post_modified_gmt, false)) {
         foreach (_wp_post_revision_fields() as $autosave_field => $_autosave_field) {
             if (normalize_whitespace($autosave->{$autosave_field}) != normalize_whitespace($post->{$autosave_field})) {
                 $notice = sprintf(__('There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>', 'wplms-front-end'), get_edit_post_link($autosave->ID));
                 break;
             }
         }
         // If this autosave isn't different from the current post, begone.
         if (!$notice) {
             wp_delete_post_revision($autosave->ID);
         }
         unset($autosave_field, $_autosave_field);
     }
     $post_type_object = get_post_type_object($post_type);
     $this->add_meta_modal_section('submitdiv', __('Publish'), array($this, 'meta_section_publish'), 10, 10);
     if (post_type_supports($post_type, 'revisions') && 'auto-draft' !== $post->post_status) {
         $revisions = wp_get_post_revisions($post->ID);
         $count = count($revisions);
         if ($count > 1) {
             $this->add_meta_modal_section('revisionsdiv', __('Revisions', 'wplms-front-end') . ' (' . $count . ')', 'post_revisions_meta_box', 30, 50);
         }
     }
     if (current_theme_supports('post-formats') && post_type_supports($post_type, 'post-formats')) {
         $this->add_meta_modal_section('formatdiv', _x('Format', 'post format', 'wplms-front-end'), 'post_format_meta_box', 20, 10);
     }
     foreach (get_object_taxonomies($post) as $tax_name) {
         $taxonomy = get_taxonomy($tax_name);
         if (!$taxonomy->show_ui || false === $taxonomy->meta_box_cb) {
             continue;
         }
         $label = $taxonomy->labels->name;
         if (!is_taxonomy_hierarchical($tax_name)) {
             $tax_meta_box_id = 'tagsdiv-' . $tax_name;
         } else {
             $tax_meta_box_id = $tax_name . 'div';
         }
         $this->add_meta_modal_section($tax_meta_box_id, $label, $taxonomy->meta_box_cb, 20, 20, array('taxonomy' => $tax_name));
     }
     if (post_type_supports($post_type, 'page-attributes')) {
         $this->add_meta_modal_section('pageparentdiv', 'page' == $post_type ? __('Page Attributes', 'wplms-front-end') : __('Attributes', 'wplms-front-end'), 'page_attributes_meta_box', 10, 10);
     }
     if (post_type_supports($post_type, 'excerpt')) {
         $this->add_meta_modal_section('postexcerpt', __('Excerpt', 'wplms-front-end'), 'post_excerpt_meta_box', 30, 10);
     }
     if (post_type_supports($post_type, 'trackbacks')) {
         $this->add_meta_modal_section('trackbacksdiv', __('Send Trackbacks', 'wplms-front-end'), 'post_trackback_meta_box', 30, 20);
     }
     if (post_type_supports($post_type, 'custom-fields')) {
         $this->add_meta_modal_section('postcustom', __('Custom Fields', 'wplms-front-end'), 'post_custom_meta_box', 30, 30);
     }
     if (post_type_supports($post_type, 'comments')) {
         $this->add_meta_modal_section('commentstatusdiv', __('Discussion', 'wplms-front-end'), 'post_comment_status_meta_box', 30, 40);
     }
     require_once 'meta-modal-template.php';
     unset($GLOBALS['current_screen']);
 }
Exemple #10
0
function tdomf_cleanup_spam()
{
    global $wpdb;
    if (!get_option(TDOMF_OPTION_SPAM_AUTO_DELETE)) {
        return;
    }
    // delete edit spam older than a month
    $edit_list = '';
    $time_diff = tdomf_timestamp_wp_sql(time() - 2592000);
    // 1 month in seconds
    $edits = tdomf_get_edits(array('state' => 'spam', 'older_than' => $time_diff));
    if (count($edits) > 0) {
        foreach ($edits as $edit) {
            $edit_list[] = $edit->edit_id;
            if ($edit->revision_id != 0) {
                wp_delete_post_revision($edit->revision_id);
            }
            if ($edit->current_revision_id != 0) {
                wp_delete_post_revision($last_edit[0]->current_revision_id);
            }
        }
        tdomf_delete_edits($edit_list);
        tdomf_log_message("Deleting spam edits older than a month: " . implode(",", $edit_list));
    }
    // delete spam more than a month old
    $query = "SELECT ID, post_modified_gmt\n             FROM {$wpdb->posts}\n             LEFT JOIN {$wpdb->postmeta} ON ({$wpdb->posts}.ID = {$wpdb->postmeta}.post_id)\n             WHERE meta_key = '" . TDOMF_KEY_SPAM . "'";
    $spam_posts = $wpdb->get_results($query);
    if (count($spam_posts) > 0) {
        $list = "";
        foreach ($spam_posts as $post) {
            // we use to use post_modified_gmt but since 2.6 or 2.7 this is
            // no longer set when the post is initially created in draft
            //
            $post_date_gmt = get_post_meta($post->ID, TDOMF_KEY_SUBMISSION_DATE, true);
            if ($post_date_gmt != false) {
                $post_date_ts = mysql2date('U', $post_date_gmt);
                $diff = time() - $post_date_ts;
                if ($diff >= 2952000) {
                    // 1 month (30 days)
                    $list .= $post->ID . ", ";
                    wp_delete_post($post->ID);
                }
                tdomf_log_message($post->ID . ' ' . $post_data_ts . ' ' . $diff);
            } else {
                // old way
                //
                $last_updated = strtotime($post->post_modified_gmt);
                $diff = time() - $last_updated;
                if ($diff >= 2952000) {
                    // 1 month (30 days)
                    $list .= $post->ID . ", ";
                    wp_delete_post($post->ID);
                }
            }
        }
        if ($list != "") {
            tdomf_log_message("Deleting spam posts older than a month: {$list}");
        }
    } else {
        #tdomf_log_message("No spam submissions to clean up!",TDOMF_LOG_GOOD);
    }
}
/**
 * Saves an already existing post as a post revision.
 *
 * Typically used immediately after post updates.
 * Adds a copy of the current post as a revision, so latest revision always matches current post
 *
 * @since 2.6.0
 *
 * @uses _wp_put_post_revision()
 *
 * @param int $post_id The ID of the post to save as a revision.
 * @return mixed Null or 0 if error, new revision ID, if success.
 */
function wp_save_post_revision($post_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (!($post = get_post($post_id))) {
        return;
    }
    if (!post_type_supports($post->post_type, 'revisions')) {
        return;
    }
    if ('auto-draft' == $post->post_status) {
        return;
    }
    if (!wp_revisions_enabled($post)) {
        return;
    }
    // Compare the proposed update with the last stored revision verifying that
    // they are different, unless a plugin tells us to always save regardless.
    // If no previous revisions, save one
    if ($revisions = wp_get_post_revisions($post_id)) {
        // grab the last revision, but not an autosave
        foreach ($revisions as $revision) {
            if (false !== strpos($revision->post_name, "{$revision->post_parent}-revision")) {
                $last_revision = $revision;
                break;
            }
        }
        if (isset($last_revision) && apply_filters('wp_save_post_revision_check_for_changes', true, $last_revision, $post)) {
            $post_has_changed = false;
            foreach (array_keys(_wp_post_revision_fields()) as $field) {
                if (normalize_whitespace($post->{$field}) != normalize_whitespace($last_revision->{$field})) {
                    $post_has_changed = true;
                    break;
                }
            }
            // Check whether revisioned meta fields have changed.
            foreach (_wp_post_revision_meta_keys() as $meta_key) {
                if (get_post_meta($post->ID, $meta_key, true) != get_post_meta($last_revision->ID, $meta_key, true)) {
                    $post_has_changed = true;
                    break;
                }
            }
            // Check whether the post format has changed
            if (get_post_format($post->ID) != get_post_meta($last_revision->ID, '_revision_post_format', true)) {
                $post_has_changed = true;
            }
            //don't save revision if post unchanged
            if (!$post_has_changed) {
                return;
            }
        }
    }
    $return = _wp_put_post_revision($post);
    $revisions_to_keep = wp_revisions_to_keep($post);
    if ($revisions_to_keep < 0) {
        return $return;
    }
    // all revisions and autosaves
    $revisions = wp_get_post_revisions($post_id, array('order' => 'ASC'));
    $delete = count($revisions) - $revisions_to_keep;
    if ($delete < 1) {
        return $return;
    }
    $revisions = array_slice($revisions, 0, $delete);
    for ($i = 0; isset($revisions[$i]); $i++) {
        if (false !== strpos($revisions[$i]->post_name, 'autosave')) {
            continue;
        }
        wp_delete_post_revision($revisions[$i]->ID);
    }
    return $return;
}
Exemple #12
0
 static function delete_revisions()
 {
     //Add nonce check
     check_admin_referer('revision-control-delete');
     if (empty($_POST['revisions'])) {
         $x = new WP_AJAX_Response();
         $x->add(array('data' => -1));
         $x->send();
         return;
     }
     $revisions = stripslashes($_POST['revisions']);
     $revisions = explode(',', $revisions);
     $revisions = array_map('intval', $revisions);
     $deleted = array();
     foreach ($revisions as $revision_id) {
         $revision = get_post($revision_id);
         if (wp_is_post_revision($revision) && !wp_is_post_autosave($revision) && current_user_can('delete_post', $revision->post_parent)) {
             if (wp_delete_post_revision($revision_id)) {
                 $deleted[] = $revision_id;
             }
         }
     }
     $x = new WP_AJAX_Response();
     $x->add(array('data' => 1, 'supplemental' => array('revisions' => implode(',', $deleted))));
     $x->send();
 }
 public function front_end_editor_shortcodes($attr)
 {
     global $wp, $current_screen, $wp_meta_boxes, $post;
     $is_bac = $this->is_bac();
     $output = '';
     /**
      * Start Checking the Conditional needed to render editor
      * Define Variable needed for use in whole function
      *  
      *
      */
     if (!is_user_logged_in()) {
         if ($is_bac === true) {
             wp_safe_redirect(bon_accounts()->my_account_url());
         } else {
             if (is_woocommerce_activated()) {
                 wp_safe_redirect(get_permalink(wc_get_page_id('myaccount')));
             }
         }
     } else {
         if (!$this->is_edit()) {
             return;
         }
         $object_id = $this->get_post_to_edit();
         if (!$object_id) {
             bon_error_notice()->add('invalid_post', __('You attempted to edit an item that doesn&#8217;t exist. Perhaps it was deleted?'), 'error');
             return;
         }
         $post_object = get_post($this->get_post_to_edit());
         setup_postdata($GLOBALS['post'] =& $post_object);
         $current_post_type = get_post_type($object_id);
         if (!$post_object) {
             bon_error_notice()->add('invalid_post', __('You attempted to edit an item that doesn&#8217;t exist. Perhaps it was deleted?'), 'error');
             return;
         }
         if (!current_user_can('edit_post', $object_id)) {
             bon_error_notice()->add('permission_denied', __('You are not allowed to edit this item.'), 'error');
             return;
         }
         if (!post_type_supports($post_object->post_type, 'front-end-editor')) {
             bon_error_notice()->add('unsupported_posttype', __('The post type assigned is not supporting front end post', 'bon'), 'error');
         }
         $form_extra = '';
         $notice = false;
         if ($post_object->post_status === 'auto-draft') {
             $post_object->post_title = '';
             $post_object->comment_status = get_option('default_comment_status');
             $post_object->ping_status = get_option('default_ping_status');
             $autosave = false;
             $form_extra .= "<input type='hidden' id='auto_draft' name='auto_draft' value='1' />";
         } else {
             $autosave = wp_get_post_autosave($object_id);
         }
         $form_action = 'editpost';
         $nonce_action = 'update-post_' . $object_id;
         $form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr($object_id) . "' />";
         $content_css = array(trailingslashit(get_stylesheet_directory_uri()) . 'assets/css/editor-styles.css', trailingslashit(includes_url()) . 'css/dashicons.min.css', trailingslashit(includes_url()) . 'js/mediaelement/mediaelementplayer.min.css', trailingslashit(includes_url()) . 'js/mediaelement/wp-mediaelement.css', trailingslashit(includes_url()) . 'js/tinymce/skins/wordpress/wp-content.css', trailingslashit(includes_url()) . 'css/editor.min.css');
         $content_css = join(',', array_map('esc_url', array_unique($content_css)));
         $args = array('post_ID' => $object_id, 'post_type' => $current_post_type, 'user_ID' => get_current_user_id(), 'post' => $post_object, 'post_type_object' => get_post_type_object($current_post_type), 'autosave' => $autosave, 'form_extra' => $form_extra, 'form_action' => $form_action, 'nonce_action' => $nonce_action, 'editor_settings' => array('dfw' => true, 'drag_drop_upload' => true, 'tabfocus_elements' => 'insert-media-button, save-post', 'editor_height' => 360, 'tinymce' => array('resize' => false, 'add_unload_trigger' => false, 'content_css' => $content_css)));
         ob_start();
         bon_get_template('posts/editor.php', $args);
         $args['editor'] = ob_get_clean();
         unset($args['editor_settings']);
         set_current_screen($current_post_type);
         $current_screen->set_parentage('edit.php?post_type=' . $current_post_type);
         if (!wp_check_post_lock($object_id)) {
             $args['active_post_lock'] = wp_set_post_lock($object_id);
         }
         $messages = $this->get_wp_messages($post_object);
         $message = false;
         if (isset($_GET['message'])) {
             $_GET['message'] = absint($_GET['message']);
             if (isset($messages[$current_post_type][$_GET['message']])) {
                 $message = $messages[$current_post_type][$_GET['message']];
             } elseif (!isset($messages[$current_post_type]) && isset($messages['post'][$_GET['message']])) {
                 $message = $messages['post'][$_GET['message']];
             }
         }
         // Detect if there exists an autosave newer than the post and if that autosave is different than the post
         if ($autosave && mysql2date('U', $autosave->post_modified_gmt, false) > mysql2date('U', $post_object->post_modified_gmt, false)) {
             foreach (_wp_post_revision_fields() as $autosave_field => $_autosave_field) {
                 if (normalize_whitespace($autosave->{$autosave_field}) != normalize_whitespace($post_object->{$autosave_field})) {
                     bon_error_notice()->add('autosave_exists', sprintf(__('There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>'), get_edit_post_link($autosave->ID)), 'notice');
                     break;
                 }
             }
             // If this autosave isn't different from the current post, begone.
             if (!$notice) {
                 wp_delete_post_revision($autosave->ID);
             }
             unset($autosave_field, $_autosave_field);
         }
         bon_get_template('posts/post.php', $args);
         unset($GLOBALS['current_screen']);
         wp_reset_postdata();
     }
 }
    $autosave = wp_get_post_autosave($post_ID);
}
$form_action = 'editpost';
$nonce_action = 'update-post_' . $post_ID;
$form_extra .= "<input type='hidden' id='post_ID' name='post_ID' value='" . esc_attr($post_ID) . "' />";
// Detect if there exists an autosave newer than the post and if that autosave is different than the post
if ($autosave && mysql2date('U', $autosave->post_modified_gmt, false) > mysql2date('U', $post->post_modified_gmt, false)) {
    foreach (_wp_post_revision_fields($post) as $autosave_field => $_autosave_field) {
        if (normalize_whitespace($autosave->{$autosave_field}) != normalize_whitespace($post->{$autosave_field})) {
            $notice = sprintf(__('There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>'), get_edit_post_link($autosave->ID));
            break;
        }
    }
    // If this autosave isn't different from the current post, begone.
    if (!$notice) {
        wp_delete_post_revision($autosave->ID);
    }
    unset($autosave_field, $_autosave_field);
}
$post_type_object = get_post_type_object($post_type);
// All meta boxes should be defined and added before the first do_meta_boxes() call (or potentially during the do_meta_boxes action).
require_once ABSPATH . 'wp-admin/includes/meta-boxes.php';
$publish_callback_args = null;
if (post_type_supports($post_type, 'revisions') && 'auto-draft' != $post->post_status) {
    $revisions = wp_get_post_revisions($post_ID);
    // We should aim to show the revisions meta box only when there are revisions.
    if (count($revisions) > 1) {
        reset($revisions);
        // Reset pointer for key()
        $publish_callback_args = array('revisions_count' => count($revisions), 'revision_id' => key($revisions));
        add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', null, 'normal', 'core');
    function bdr_page()
    {
        global $wpdb, $bdr_rev_no;
        $bdr_plugin_info = get_plugin_data(__FILE__);
        $bdr_version = $bdr_plugin_info['Version'];
        $bdr_posts = count($wpdb->get_results("SELECT ID\n\t\t\t\tFROM ({$wpdb->posts})\n\t\t\t\tWHERE `post_type` = 'post'"));
        ?>
		<div class="wrap">
			<h2><?php 
        _e('Better Delete Revision Manager', 'bdr');
        ?>
 <font size=1><?php 
        echo $bdr_version;
        ?>
</font></h2>
			<div class="bdr_widget">
				<p><?php 
        _e('You have', 'bdr');
        ?>
 <span><?php 
        echo $bdr_posts;
        ?>
</span> <?php 
        _e('posts', 'bdr');
        ?>
.</p>
				<p><?php 
        _e('Since you started using Better Delete Revision', 'bdr');
        ?>
, <span id="bdr_revs_no"><?php 
        echo $bdr_rev_no;
        ?>
</span> <?php 
        _e('redundant post revisions have been removed!', 'bdr');
        ?>
</p>
			</div><!-- .widget -->
			<?php 
        if (isset($_POST['bdr_get_rev']) && check_admin_referer(plugin_basename(__FILE__))) {
            $bdr_results = $wpdb->get_results("SELECT `ID`,`post_date`,`post_title`,`post_modified`\n\t\t\t\t\tFROM ({$wpdb->posts})\n\t\t\t\t\tWHERE `post_type` = 'revision'\n\t\t\t\t\tORDER BY `ID` DESC");
            if ($bdr_results) {
                $bdr_res_no = count($bdr_results);
                ?>
					<table class="widefat bdr_table">
						<thead>
							<tr>
								<th width="30"><?php 
                _e('Id', 'bdr');
                ?>
</th>
								<th width="450"><?php 
                _e('Title', 'bdr');
                ?>
</th>
								<th width="180"><?php 
                _e('Post date', 'bdr');
                ?>
</th>
								<th width="180"><?php 
                _e('Last modified', 'bdr');
                ?>
</th>
							</tr>
						</thead>
						<tbody>
							<?php 
                for ($i = 0; $i < $bdr_res_no; $i++) {
                    ?>
								<tr>
									<td><?php 
                    echo $bdr_results[$i]->ID;
                    ?>
</td>
									<td><?php 
                    echo $bdr_results[$i]->post_title;
                    ?>
</td>
									<td><?php 
                    echo $bdr_results[$i]->post_date;
                    ?>
</td>
									<td><?php 
                    echo $bdr_results[$i]->post_modified;
                    ?>
</td>
								</tr>
							<?php 
                }
                ?>
						</tbody>
					</table>
					<p><?php 
                _e('Would you like to remove the revision posts?', 'bdr');
                ?>
</p>
					<form class="bdr_form" method="post" action="options-general.php?page=better-delete-revision.php">
						<?php 
                wp_nonce_field(plugin_basename(__FILE__));
                ?>
						<input type="hidden" name="bdr_rev_no" value="<?php 
                echo $bdr_res_no;
                ?>
" />
						<input class="button-primary" type="submit" name="bdr_del_act" value="<?php 
                printf(__('Yes , I would like to delete them! (A Total Of %s)', 'bdr'), $bdr_res_no);
                ?>
" />
						<input class="button" type="submit" name="goback" value="<?php 
                _e('No , I prefer to keep them!', 'bdr');
                ?>
" />
					</form>
				<?php 
            } else {
                ?>
					<div class="updated bdr_no_rev">
						<p>
							<?php 
                _e('Great! You have no revisions now!', 'bdr');
                ?>
						</p>
					</div>
				<?php 
            }
        } elseif (isset($_POST['bdr_del_act']) && check_admin_referer(plugin_basename(__FILE__))) {
            $bdr_ngg_fix = bdr_get_ngg_fix();
            $bdr_revisions = $wpdb->get_results("SELECT `ID` AS revision_id\n\t\t\t\t\tFROM ({$wpdb->posts})\n\t\t\t\t\tWHERE `post_type` = 'revision'\n\t\t\t\t\tORDER BY `ID` DESC");
            if (is_array($bdr_ngg_fix)) {
                remove_action($bdr_ngg_fix['tag'], array($bdr_ngg_fix['class'], $bdr_ngg_fix['method']), $bdr_ngg_fix['priority']);
            }
            foreach ($bdr_revisions as $bdr_revision) {
                wp_delete_post_revision($bdr_revision->revision_id);
            }
            if (is_array($bdr_ngg_fix)) {
                add_action($bdr_ngg_fix['tag'], array($bdr_ngg_fix['class'], $bdr_ngg_fix['method']), $bdr_ngg_fix['priority']);
            }
            $bdr_del_no = $_POST['bdr_rev_no'];
            $bdr_rev_new = $bdr_rev_no + $bdr_del_no;
            update_option('bdr_rev_no', $bdr_rev_new);
            ?>
				<div class="updated bdr_updated">
					<p>
						<strong><?php 
            printf(__('Deleted %s revisions!', 'bdr'), sprintf('<span>%s</span>', $bdr_del_no));
            ?>
</strong>
					</p>
				</div>
				<script type="text/javascript">
					document.getElementById( 'bdr_revs_no' ).innerHTML = <?php 
            echo $bdr_rev_new;
            ?>
;
				</script>
			<?php 
        } elseif (isset($_POST['bdr_maintain_mysql']) && check_admin_referer(plugin_basename(__FILE__))) {
            if (isset($_POST['bdr_operation']) && $_POST['bdr_operation'] == 'OPTIMIZE') {
                $bdr_operation = 'OPTIMIZE';
            } else {
                $bdr_operation = 'CHECK';
            }
            $bdr_tables = $wpdb->get_results('SHOW TABLES IN ' . DB_NAME);
            $bdr_query = "{$bdr_operation} TABLE";
            $bdr_tables_in_db_name = 'Tables_in_' . DB_NAME;
            foreach ($bdr_tables as $k => $v) {
                $bdr_table = $v->{$bdr_tables_in_db_name};
                $bdr_query .= " `{$bdr_table}`,";
            }
            $bdr_query = substr($bdr_query, 0, strlen($bdr_query) - 1);
            $bdr_result = $wpdb->get_results($bdr_query);
            switch ($bdr_operation) {
                case 'OPTIMIZE':
                    ?>
						<h3><?php 
                    _e('Optimization of database completed!', 'bdr');
                    ?>
</h3>
						<?php 
                    break;
                case 'CHECK':
                default:
                    ?>
						<table border="0" class="widefat bdr_table">
							<thead>
								<tr>
									<th><?php 
                    _e('Table', 'bdr');
                    ?>
</th>
									<th><?php 
                    _e('OP', 'bdr');
                    ?>
</th>
									<th><?php 
                    _e('Status', 'bdr');
                    ?>
</th>
								</tr>
							</thead>
							<tbody>
								<?php 
                    foreach ($bdr_result as $j => $o) {
                        ?>
									<tr>
										<?php 
                        foreach ($o as $k => $v) {
                            $bdr_tr_class = $j % 2 == 1 ? 'active alt' : 'inactive';
                            if ($k == 'Msg_type') {
                                continue;
                            }
                            if ($k == 'Msg_text') {
                                if ($v == 'OK') {
                                    ?>
													<td class="<?php 
                                    echo $bdr_tr_class;
                                    ?>
">
														<font color="green">
															<b><?php 
                                    echo $v;
                                    ?>
</b>
														</font>
													</td>
												<?php 
                                } else {
                                    ?>
													<td class="<?php 
                                    echo $bdr_tr_class;
                                    ?>
">
														<font color="red">
															<b><?php 
                                    echo $v;
                                    ?>
</b>
														</font>
													</td>
												<?php 
                                }
                            } else {
                                ?>
												<td class="<?php 
                                echo $bdr_tr_class;
                                ?>
">
													<?php 
                                echo $v;
                                ?>
												</td>
											<?php 
                            }
                        }
                        ?>
									</tr>
								<?php 
                    }
                    ?>
							</tbody>
							<tfoot>
								<tr>
									<th colspan="3">
										<?php 
                    printf(__('If all statuses are %s, then your database does not need any optimization! If any are %s, then click on the following button to optimize your Wordpress database.', 'bdr'), sprintf('<font color="green">%s</font>', __('OK', 'bdr')), sprintf('<font color="red">%s</font>', __('red', 'bdr')));
                    ?>
									</th>
								</tr>
							</tfoot>
						</table>
						<form class="bdr_form" method="post" action="options-general.php?page=better-delete-revision.php">
							<?php 
                    wp_nonce_field(plugin_basename(__FILE__));
                    ?>
							<input name="bdr_operation" type="hidden" value="OPTIMIZE" />
							<input name="bdr_maintain_mysql" type="hidden" value="OPTIMIZE" />
							<input name="submit" type="submit" class="button-primary" value="<?php 
                    _e('Optimize Wordpress Database', 'bdr');
                    ?>
" />
						</form>
						<?php 
                    break;
            }
        } else {
            ?>
				<form class="bdr_form" method="post" action="options-general.php?page=better-delete-revision.php">
					<?php 
            wp_nonce_field(plugin_basename(__FILE__));
            ?>
					<input class="button" type="submit" name="bdr_get_rev" value="<?php 
            _e('Check Revision Posts', 'bdr');
            ?>
" />
					<input class="button" type="submit" name="bdr_maintain_mysql" value="<?php 
            _e('Optimize Your Database', 'bdr');
            ?>
" />
				</form>
			<?php 
        }
        ?>
			<div class="bdr_widget">
				<p>
					<?php 
        _e('Post Revisions are a feature introduced in Wordpress 2.6. Whenever you or Wordpress saves a post or a page, a revision is automatically created and stored in your Wordpress database. Each additional revision will slowly increase the size of your database. If you save a post or page multiple times, your number of revisions will greatly increase overtime. For example, if you have 100 posts and each post has 10 revisions you could be storing up to 1,000 copies of older data!', 'bdr');
        ?>
				</p>
				<br />
				<p>
					<?php 
        _e('The Better Delete Revision plugin is your #1 choice to quickly and easily removing revision from your Wordpress database. Try it out today to see what a lighter and smaller Wordpress database can do for you!', 'bdr');
        ?>
				</p>
				<br />
				<p>
					<?php 
        _e('Thank you for using this plugin! I hope you enjoy it!', 'bdr');
        ?>
				</p>
				<br />
				<p><?php 
        _e('Author:', 'bdr');
        ?>
 <a href="http://www.1e2.it" target="_blank">http://www.1e2.it</a></p>
			</div>
		</div><!-- .wrap -->
	<?php 
    }
Exemple #16
0
/**
 * Creates autosave data for the specified post from $_POST data.
 *
 * @package WordPress
 * @subpackage Post_Revisions
 * @since 2.6.0
 *
 * @uses _wp_translate_postdata()
 * @uses _wp_post_revision_fields()
 *
 * @return unknown
 */
function wp_create_post_autosave($post_id)
{
    $translated = _wp_translate_postdata(true);
    if (is_wp_error($translated)) {
        return $translated;
    }
    $post_author = get_current_user_id();
    // Store one autosave per author. If there is already an autosave, overwrite it.
    if ($old_autosave = wp_get_post_autosave($post_id, $post_author)) {
        $new_autosave = _wp_post_revision_fields($_POST, true);
        $new_autosave['ID'] = $old_autosave->ID;
        $new_autosave['post_author'] = $post_author;
        // If the new autosave is the same content as the post, delete the old autosave.
        $post = get_post($post_id);
        $autosave_is_different = false;
        foreach (array_keys(_wp_post_revision_fields()) as $field) {
            if (normalize_whitespace($new_autosave[$field]) != normalize_whitespace($post->{$field})) {
                $autosave_is_different = true;
                break;
            }
        }
        if (!$autosave_is_different) {
            wp_delete_post_revision($old_autosave->ID);
            return;
        }
        return wp_update_post($new_autosave);
    }
    // _wp_put_post_revision() expects unescaped.
    $post_data = wp_unslash($_POST);
    // Otherwise create the new autosave as a special post revision
    return _wp_put_post_revision($post_data, true);
}
 function get_autosave_notice()
 {
     global $post;
     if ('auto-draft' == $post->post_status) {
         $autosave = false;
     } else {
         $autosave = wp_get_post_autosave($post->ID);
     }
     // Detect if there exists an autosave newer than the post and if that autosave is different than the post
     if ($autosave && mysql2date('U', $autosave->post_modified_gmt, false) > mysql2date('U', $post->post_modified_gmt, false)) {
         foreach (_wp_post_revision_fields() as $autosave_field => $_autosave_field) {
             if (normalize_whitespace($autosave->{$autosave_field}) !== normalize_whitespace($post->{$autosave_field})) {
                 return sprintf(__('There is an autosave of this post that is more recent than the version below. <a href="%s">View the autosave</a>'), get_edit_post_link($autosave->ID));
             }
         }
         // If this autosave isn't different from the current post, begone.
         wp_delete_post_revision($autosave->ID);
     }
     return false;
 }
/**
 * Creates a revision for the current version of a post.
 *
 * Typically used immediately after a post update, as every update is a revision,
 * and the most recent revision always matches the current post.
 *
 * @since 2.6.0
 *
 * @param int $post_id The ID of the post to save as a revision.
 * @return int|WP_Error|void Void or 0 if error, new revision ID, if success.
 */
function wp_save_post_revision($post_id)
{
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    if (!($post = get_post($post_id))) {
        return;
    }
    if (!post_type_supports($post->post_type, 'revisions')) {
        return;
    }
    if ('auto-draft' == $post->post_status) {
        return;
    }
    if (!wp_revisions_enabled($post)) {
        return;
    }
    // Compare the proposed update with the last stored revision verifying that
    // they are different, unless a plugin tells us to always save regardless.
    // If no previous revisions, save one
    if ($revisions = wp_get_post_revisions($post_id)) {
        // grab the last revision, but not an autosave
        foreach ($revisions as $revision) {
            if (false !== strpos($revision->post_name, "{$revision->post_parent}-revision")) {
                $last_revision = $revision;
                break;
            }
        }
        /**
         * Filter whether the post has changed since the last revision.
         *
         * By default a revision is saved only if one of the revisioned fields has changed.
         * This filter can override that so a revision is saved even if nothing has changed.
         *
         * @since 3.6.0
         *
         * @param bool    $check_for_changes Whether to check for changes before saving a new revision.
         *                                   Default true.
         * @param WP_Post $last_revision     The the last revision post object.
         * @param WP_Post $post              The post object.
         *
         */
        if (isset($last_revision) && apply_filters('wp_save_post_revision_check_for_changes', $check_for_changes = true, $last_revision, $post)) {
            $post_has_changed = false;
            foreach (array_keys(_wp_post_revision_fields()) as $field) {
                if (normalize_whitespace($post->{$field}) != normalize_whitespace($last_revision->{$field})) {
                    $post_has_changed = true;
                    break;
                }
            }
            /**
             * Filter whether a post has changed.
             *
             * By default a revision is saved only if one of the revisioned fields has changed.
             * This filter allows for additional checks to determine if there were changes.
             *
             * @since 4.1.0
             *
             * @param bool    $post_has_changed Whether the post has changed.
             * @param WP_Post $last_revision    The last revision post object.
             * @param WP_Post $post             The post object.
             *
             */
            $post_has_changed = (bool) apply_filters('wp_save_post_revision_post_has_changed', $post_has_changed, $last_revision, $post);
            //don't save revision if post unchanged
            if (!$post_has_changed) {
                return;
            }
        }
    }
    $return = _wp_put_post_revision($post);
    // If a limit for the number of revisions to keep has been set,
    // delete the oldest ones.
    $revisions_to_keep = wp_revisions_to_keep($post);
    if ($revisions_to_keep < 0) {
        return $return;
    }
    $revisions = wp_get_post_revisions($post_id, array('order' => 'ASC'));
    $delete = count($revisions) - $revisions_to_keep;
    if ($delete < 1) {
        return $return;
    }
    $revisions = array_slice($revisions, 0, $delete);
    for ($i = 0; isset($revisions[$i]); $i++) {
        if (false !== strpos($revisions[$i]->post_name, 'autosave')) {
            continue;
        }
        wp_delete_post_revision($revisions[$i]->ID);
    }
    return $return;
}
 /**
  * for the current post remove its auto save
  * we don't use the default drafts / autosaves in livepress mode so remove any to stop messages etc.
  */
 function remove_auto_save()
 {
     global $post;
     if (null !== $post) {
         $autosave = wp_get_post_autosave($post->ID);
         if (false !== $autosave) {
             wp_delete_post_revision($autosave->ID);
         }
     }
 }
/**
 * Saves an already existing post as a post revision.
 *
 * Typically used immediately prior to post updates.
 *
 * @package WordPress
 * @subpackage Post_Revisions
 * @since 2.6.0
 *
 * @uses _wp_put_post_revision()
 *
 * @param int $post_id The ID of the post to save as a revision.
 * @return mixed Null or 0 if error, new revision ID, if success.
 */
function wp_save_post_revision($post_id)
{
    // We do autosaves manually with wp_create_post_autosave()
    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
        return;
    }
    // WP_POST_REVISIONS = 0, false
    if (!WP_POST_REVISIONS) {
        return;
    }
    if (!($post = get_post($post_id, ARRAY_A))) {
        return;
    }
    if (!post_type_supports($post['post_type'], 'revisions')) {
        return;
    }
    $return = _wp_put_post_revision($post);
    // WP_POST_REVISIONS = true (default), -1
    if (!is_numeric(WP_POST_REVISIONS) || WP_POST_REVISIONS < 0) {
        return $return;
    }
    // all revisions and (possibly) one autosave
    $revisions = wp_get_post_revisions($post_id, array('order' => 'ASC'));
    // WP_POST_REVISIONS = (int) (# of autosaves to save)
    $delete = count($revisions) - WP_POST_REVISIONS;
    if ($delete < 1) {
        return $return;
    }
    $revisions = array_slice($revisions, 0, $delete);
    for ($i = 0; isset($revisions[$i]); $i++) {
        if (false !== strpos($revisions[$i]->post_name, 'autosave')) {
            continue;
        }
        wp_delete_post_revision($revisions[$i]->ID);
    }
    return $return;
}
Exemple #21
0
 function _delete_last_revision($id)
 {
     $lastRevision = wp_get_post_revisions($id, array('numberposts' => 1));
     foreach ($lastRevision as $revision) {
         wp_delete_post_revision($revision);
     }
 }