/** * Saves an already existing post as a post revision. * * Typically used immediately prior to post updates. * * @package NXTClass * @subpackage Post_Revisions * @since 2.6.0 * * @uses _nxt_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 nxt_save_post_revision($post_id) { // We do autosaves manually with nxt_create_post_autosave() if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { return; } // nxt_POST_REVISIONS = 0, false if (!nxt_POST_REVISIONS) { return; } if (!($post = get_post($post_id, ARRAY_A))) { return; } if (!post_type_supports($post['post_type'], 'revisions')) { return; } $return = _nxt_put_post_revision($post); // nxt_POST_REVISIONS = true (default), -1 if (!is_numeric(nxt_POST_REVISIONS) || nxt_POST_REVISIONS < 0) { return $return; } // all revisions and (possibly) one autosave $revisions = nxt_get_post_revisions($post_id, array('order' => 'ASC')); // nxt_POST_REVISIONS = (int) (# of autosaves to save) $delete = count($revisions) - nxt_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; } nxt_delete_post_revision($revisions[$i]->ID); } return $return; }
/** * Creates autosave data for the specified post from $_POST data. * * @package NXTClass * @subpackage Post_Revisions * @since 2.6.0 * * @uses _nxt_translate_postdata() * @uses _nxt_post_revision_fields() * * @return unknown */ function nxt_create_post_autosave($post_id) { $translated = _nxt_translate_postdata(true); if (is_nxt_error($translated)) { return $translated; } // Only store one autosave. If there is already an autosave, overwrite it. if ($old_autosave = nxt_get_post_autosave($post_id)) { $new_autosave = _nxt_post_revision_fields($_POST, true); $new_autosave['ID'] = $old_autosave->ID; $new_autosave['post_author'] = get_current_user_id(); return nxt_update_post($new_autosave); } // _nxt_put_post_revision() expects unescaped. $_POST = stripslashes_deep($_POST); // Otherwise create the new autosave as a special post revision return _nxt_put_post_revision($_POST, true); }