Skip to content
Snippets Groups Projects
Commit a91c0aec authored by Erick Hitter's avatar Erick Hitter
Browse files

Don't require a Gutenberg-enabled post to be saved to purge excess revisions.

parent 424b16ff
No related branches found
No related tags found
1 merge request!12Introduce native Gutenberg controls
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins'), 'version' => 'caf115ed5017b4ad3c90');
<?php return array('dependencies' => array('wp-api-fetch', 'wp-components', 'wp-compose', 'wp-data', 'wp-edit-post', 'wp-element', 'wp-i18n', 'wp-plugins'), 'version' => '7f1755eabefc886809fd');
!function(){"use strict";var e={989:function(e){e.exports=window.wp.apiFetch},609:function(e){e.exports=window.wp.components},333:function(e){e.exports=window.wp.compose},818:function(e){e.exports=window.wp.data},67:function(e){e.exports=window.wp.editPost},307:function(e){e.exports=window.wp.element},736:function(e){e.exports=window.wp.i18n},817:function(e){e.exports=window.wp.plugins}},t={};function o(n){var r=t[n];if(void 0!==r)return r.exports;var i=t[n]={exports:{}};return e[n](i,i.exports,o),i.exports}o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,{a:t}),t},o.d=function(e,t){for(var n in t)o.o(t,n)&&!o.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},function(){var e=o(307);const t=o(989),{Button:n,Modal:r,TextControl:i}=o(609),{compose:s}=o(333),{withSelect:l,withDispatch:c}=o(818),{PluginDocumentSettingPanel:a}=o(67),{useState:u}=o(307),{__:__,_n:_n,sprintf:p}=o(736),{registerPlugin:w}=o(817),d=window.wpRevisionsControlBlockEditorSettings.metaKey,v="wp-revisions-control",m=s([l((e=>{const{getCurrentPostRevisionsCount:t,getEditedPostAttribute:o}=e("core/editor"),n=t(),r=o("meta")[d];return{limit:r,showPurgeButton:Boolean(r)&&n>parseInt(r)}})),c(((e,o,n)=>{let{}=o,{select:r}=n;return{manualPurge:()=>{const o=r("core/editor").getCurrentPostId();t({path:`/wp-revisions-control/v1/schedule/${o}`,method:"PUT"}).then((t=>{let o,n;t?(o="success",n=__("Excess revisions scheduled for removal.","wp_revisions_control")):(o="error",n=__("Failed to schedule excess revisions for removal.","wp_revisions_control")),e("core/notices").createNotice(o,n,{id:"wp-revisions-control-scheduled-purge",isDismissible:!0,type:"snackbar"})}))},updateMeta:t=>{e("core/editor").editPost({meta:{[d]:t}})}}}))])((t=>{let{limit:o,manualPurge:s,showPurgeButton:l,updateMeta:c}=t;return(0,e.createElement)(a,{name:v,title:__("WP Revisions Control","wp_revisions_control"),className:v},(0,e.createElement)(i,{label:__("Number of revisions to retain:","wp_revisions_control"),help:__("Leave blank to keep all revisions.","wp_revisions_control"),value:o,onChange:c}),l&&((t,o)=>{const[i,s]=u(!1),l=()=>s(!1),c=parseInt(t,10),a=0===c?__("This will remove all revisions.","wp_revisions_control"):p(
!function(){"use strict";var e={989:function(e){e.exports=window.wp.apiFetch},609:function(e){e.exports=window.wp.components},333:function(e){e.exports=window.wp.compose},818:function(e){e.exports=window.wp.data},67:function(e){e.exports=window.wp.editPost},307:function(e){e.exports=window.wp.element},736:function(e){e.exports=window.wp.i18n},817:function(e){e.exports=window.wp.plugins}},t={};function o(n){var r=t[n];if(void 0!==r)return r.exports;var i=t[n]={exports:{}};return e[n](i,i.exports,o),i.exports}o.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return o.d(t,{a:t}),t},o.d=function(e,t){for(var n in t)o.o(t,n)&&!o.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},o.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},function(){var e=o(307);const t=o(989),{Button:n,Modal:r,TextControl:i}=o(609),{compose:s}=o(333),{withSelect:l,withDispatch:c}=o(818),{PluginDocumentSettingPanel:a}=o(67),{useState:u}=o(307),{__:__,_n:_n,sprintf:p}=o(736),{registerPlugin:w}=o(817),d=window.wpRevisionsControlBlockEditorSettings.metaKey,v="wp-revisions-control",m=s([l((e=>{const{getCurrentPostRevisionsCount:t,getEditedPostAttribute:o}=e("core/editor"),n=t(),r=o("meta")[d];return{limit:r,showPurgeButton:Boolean(r)&&n>parseInt(r)}})),c(((e,o,n)=>{let{limit:r}=o,{select:i}=n;return{manualPurge:()=>{const o=i("core/editor").getCurrentPostId();t({path:`/wp-revisions-control/v1/schedule/${o}/${parseInt(r,10)}`,method:"PUT"}).then((t=>{let o,n;t?(o="success",n=__("Excess revisions scheduled for removal.","wp_revisions_control")):(o="error",n=__("Failed to schedule excess revisions for removal.","wp_revisions_control")),e("core/notices").createNotice(o,n,{id:"wp-revisions-control-scheduled-purge",isDismissible:!0,type:"snackbar"})}))},updateMeta:t=>{e("core/editor").editPost({meta:{[d]:t}})}}}))])((t=>{let{limit:o,manualPurge:s,showPurgeButton:l,updateMeta:c}=t;return(0,e.createElement)(a,{name:v,title:__("WP Revisions Control","wp_revisions_control"),className:v},(0,e.createElement)(i,{label:__("Number of revisions to retain:","wp_revisions_control"),help:__("Leave blank to keep all revisions.","wp_revisions_control"),value:o,onChange:c}),l&&((t,o)=>{const[i,s]=u(!1),l=()=>s(!1),c=parseInt(t,10),a=0===c?__("This will remove all revisions.","wp_revisions_control"):p(
/* translators: 1. Number of revisions to keep. */
_n("This will remove all but the most-recent revision.","This will remove all but the %1$d most-recent revisions.",c,"wp_revisions_control"),t);return(0,e.createElement)(e.Fragment,null,(0,e.createElement)(n,{isSecondary:!0,onClick:()=>s(!0)},__("Purge excess revisions","wp_revisions_control")),i&&(0,e.createElement)(r,{title:__("Purge excess revisions","wp_revisions_control"),contentLabel:a,onRequestClose:l},(0,e.createElement)("p",null,a),(0,e.createElement)(n,{isPrimary:!0,onClick:()=>{l(),o()}},__("Purge","wp_revisions_control"))," ",(0,e.createElement)(n,{isSecondary:!0,onClick:l},__("Cancel","wp_revisions_control"))))})(o,s))}));w(v,{render:m,icon:"backup"})}()}();
\ No newline at end of file
......@@ -125,12 +125,15 @@ const RevisionsControl = compose( [
showPurgeButton,
};
} ),
withDispatch( ( dispatch, {}, { select } ) => {
withDispatch( ( dispatch, { limit }, { select } ) => {
const manualPurge = () => {
const postId = select( 'core/editor' ).getCurrentPostId();
apiFetch( {
path: `/wp-revisions-control/v1/schedule/${ postId }`,
path: `/wp-revisions-control/v1/schedule/${ postId }/${ parseInt(
limit,
10
) }`,
method: 'PUT',
} ).then( ( result ) => {
let noticeType;
......
......@@ -33,7 +33,7 @@ class Block_Editor {
private function setup() {
add_action( 'rest_api_init', array( $this, 'action_rest_api_init' ) );
add_filter( 'is_protected_meta', array( $this, 'filter_is_protected_meta' ), 10, 2 );
add_action( $this->cron_action, array( WP_Revisions_Control::get_instance(), 'do_purge_excess' ) );
add_action( $this->cron_action, array( WP_Revisions_Control::get_instance(), 'do_purge_excess' ), 10, 2 );
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
}
......@@ -78,17 +78,23 @@ class Block_Editor {
register_rest_route(
'wp-revisions-control/v1',
'schedule/(?P<id>[\d]+)',
'schedule/(?P<id>[\d]+)/(?P<limit_override>[\d]+)',
array(
'methods' => 'PUT',
'callback' => array( $this, 'rest_api_schedule_purge' ),
'permission_callback' => array( $this, 'rest_api_permission_callback' ),
'args' => array(
'id' => array(
'id' => array(
'required' => true,
'type' => 'integer',
'validate_callback' => array( $this, 'rest_api_validate_id' ),
),
'limit_override' => array(
'required' => false,
'type' => 'integer',
'default' => null,
'validate_callback' => array( $this, 'rest_api_validate_id' ),
),
),
'show_in_index' => false,
)
......@@ -130,6 +136,7 @@ class Block_Editor {
$this->cron_action,
array(
$request->get_param( 'id' ),
$request->get_param( 'limit_override' ),
)
);
......
......@@ -413,15 +413,19 @@ class WP_Revisions_Control {
/**
* Remove any revisions in excess of a post's limit.
*
* @param int $post_id Post ID to purge of excess revisions.
* @param int $post_id Post ID to purge of excess revisions.
* @param int|null $limit_override Optional. Override post's revisions
* limit.
* @return array
*/
public function do_purge_excess( $post_id ) {
public function do_purge_excess( $post_id, $limit_override = null ) {
$response = array(
'count' => 0,
);
$to_keep = wp_revisions_to_keep( get_post( $post_id ) );
$to_keep = null !== $limit_override
? $limit_override
: wp_revisions_to_keep( get_post( $post_id ) );
if ( $to_keep < 0 ) {
$response['success'] = __(
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment