Example #1
0
/**
 * Inserts post data into the posts table as a post revision.
 *
 * @since 2.6.0
 * @access private
 *
 * @param int|WP_Post|array|null $post     Post ID, post object OR post array.
 * @param bool                   $autosave Optional. Is the revision an autosave?
 * @return int|WP_Error WP_Error or 0 if error, new revision ID if success.
 */
function _wp_put_post_revision($post = null, $autosave = false)
{
    if (is_object($post)) {
        $post = get_object_vars($post);
    } elseif (!is_array($post)) {
        $post = get_post($post, ARRAY_A);
    }
    if (!$post || empty($post['ID'])) {
        return new WP_Error('invalid_post', __('Invalid post ID.'));
    }
    if (isset($post['post_type']) && 'revision' == $post['post_type']) {
        return new WP_Error('post_type', __('Cannot create a revision of a revision'));
    }
    $post = _wp_post_revision_data($post, $autosave);
    $post = wp_slash($post);
    //since data is from db
    $revision_id = wp_insert_post($post);
    if (is_wp_error($revision_id)) {
        return $revision_id;
    }
    if ($revision_id) {
        /**
         * Fires once a revision has been saved.
         *
         * @since 2.6.0
         *
         * @param int $revision_id Post revision ID.
         */
        do_action('_wp_put_post_revision', $revision_id);
    }
    return $revision_id;
}
Example #2
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);
}
Example #3
0
 /**
  * @ticket 26042
  */
 function test_wp_get_post_revisions_should_order_by_ID_when_post_date_matches()
 {
     $post = self::factory()->post->create_and_get(array('post_title' => 'some-post', 'post_type' => 'post', 'post_content' => 'some_content'));
     $post = (array) $post;
     $post_revision_fields = _wp_post_revision_data($post);
     $post_revision_fields = wp_slash($post_revision_fields);
     $revision_ids = array();
     $date = date('Y-m-d H:i:s', time() - 10);
     for ($j = 1; $j < 3; $j++) {
         // Manually modify dates to ensure they're the same.
         $post_revision_fields['post_date'] = $date;
         $post_revision_fields['post_date_gmt'] = $date;
         $revision_id = wp_insert_post($post_revision_fields);
         $revision_ids[] = $revision_id;
     }
     rsort($revision_ids);
     $revisions = wp_get_post_revisions($post['ID']);
     $this->assertEquals($revision_ids, array_values(wp_list_pluck($revisions, 'ID')));
 }