Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
WP Revisions Control
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
WP Plugins
WP Revisions Control
Commits
7e9e5262
Commit
7e9e5262
authored
6 years ago
by
Erick Hitter
Browse files
Options
Downloads
Patches
Plain Diff
PHPCS
parent
60c9912c
Branches
Branches containing commit
Tags
Tags containing commit
1 merge request
!3
Unit tests and coding standards
Pipeline
#1016
failed
6 years ago
Stage: test
Stage: deploy
Changes
1
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
inc/class-wp-revisions-control.php
+113
-133
113 additions, 133 deletions
inc/class-wp-revisions-control.php
with
113 additions
and
133 deletions
inc/class-wp-revisions-control.php
+
113
−
133
View file @
7e9e5262
...
@@ -10,22 +10,65 @@
...
@@ -10,22 +10,65 @@
*/
*/
class
WP_Revisions_Control
{
class
WP_Revisions_Control
{
/**
/**
* Singleton
* Singleton.
*
* @var static
*/
private
static
$__instance
;
/**
* Filter priority.
*
* @see $this->filter_priority()
*
* @var int
*/
*/
private
static
$
__instance
=
null
;
private
static
$
priority
=
null
;
/**
/**
* Class variables
* Default filter priority.
*
* @var int
*/
*/
private
static
$priority
=
null
;
// use $this->plugin_priority()
private
$priority_default
=
50
;
private
$priority_default
=
50
;
private
static
$post_types
=
array
();
// use $this->get_post_types()
/**
private
static
$settings
=
array
();
// use $this->get_settings()
* Supported post types.
*
* @see $this->get_post_types()
*
* @var array
*/
private
static
$post_types
=
array
();
/**
* Plugin settings.
*
* @see $this->get_settings()
*
* @var array
*/
private
static
$settings
=
array
();
/**
* WordPress options page to display settings on.
*
* @var string
*/
private
$settings_page
=
'writing'
;
private
$settings_page
=
'writing'
;
/**
* Name of custom settings sections.
*
* @var string
*/
private
$settings_section
=
'wp_revisions_control'
;
private
$settings_section
=
'wp_revisions_control'
;
/**
* Meta key holding post's revisions limit.
*
* @var string
*/
private
$meta_key_limit
=
'_wp_rev_ctl_limit'
;
private
$meta_key_limit
=
'_wp_rev_ctl_limit'
;
/**
/**
...
@@ -34,26 +77,22 @@ class WP_Revisions_Control {
...
@@ -34,26 +77,22 @@ class WP_Revisions_Control {
private
function
__construct
()
{}
private
function
__construct
()
{}
/**
/**
* Singleton implementation
* Singleton implementation
.
*
*
* @uses self::setup
* @return static
* @return object
*/
*/
public
static
function
get_instance
()
{
public
static
function
get_instance
()
{
if
(
!
is_a
(
s
elf
::
$__instance
,
__CLASS__
)
)
{
if
(
!
is_a
(
s
tatic
::
$__instance
,
__CLASS__
)
)
{
s
elf
::
$__instance
=
new
self
;
s
tatic
::
$__instance
=
new
self
()
;
s
elf
::
$__instance
->
setup
();
s
tatic
::
$__instance
->
setup
();
}
}
return
s
elf
::
$__instance
;
return
s
tatic
::
$__instance
;
}
}
/**
/**
* Register actions and filters at `init` so others can interact, if desired.
* Register actions and filters at `init` so others can interact, if desired.
*
* @uses add_action
* @return null
*/
*/
private
function
setup
()
{
private
function
setup
()
{
add_action
(
'plugins_loaded'
,
array
(
$this
,
'action_plugins_loaded'
)
);
add_action
(
'plugins_loaded'
,
array
(
$this
,
'action_plugins_loaded'
)
);
...
@@ -61,24 +100,14 @@ class WP_Revisions_Control {
...
@@ -61,24 +100,14 @@ class WP_Revisions_Control {
}
}
/**
/**
* Load plugin translations
* Load plugin translations.
*
* @uses load_plugin_textdomain
* @uses plugin_basename
* @action plugins_loaded
* @return null
*/
*/
public
function
action_plugins_loaded
()
{
public
function
action_plugins_loaded
()
{
load_plugin_textdomain
(
'wp_revisions_control'
,
false
,
dirname
(
plugin_basename
(
__FILE__
)
)
.
'/languages/'
);
load_plugin_textdomain
(
'wp_revisions_control'
,
false
,
dirname
(
plugin_basename
(
__FILE__
)
)
.
'/languages/'
);
}
}
/**
/**
* Register actions and filters
* Register actions and filters.
*
* @uses add_action
* @uses add_filter
* @uses this::plugin_priority
* @return null
*/
*/
public
function
action_init
()
{
public
function
action_init
()
{
add_action
(
'admin_init'
,
array
(
$this
,
'action_admin_init'
)
);
add_action
(
'admin_init'
,
array
(
$this
,
'action_admin_init'
)
);
...
@@ -87,20 +116,12 @@ class WP_Revisions_Control {
...
@@ -87,20 +116,12 @@ class WP_Revisions_Control {
}
}
/**
/**
* Register plugin's admin-specific elements
* Register plugin's admin-specific elements
.
*
*
* Plugin title is intentionally not translatable.
* Plugin title is intentionally not translatable.
*
* @uses register_setting
* @uses add_settings_section
* @uses __
* @uses this::get_post_types
* @uses add_settings_field
* @action admin_init
* @return null
*/
*/
public
function
action_admin_init
()
{
public
function
action_admin_init
()
{
// Plugin setting section
// Plugin setting section
.
register_setting
(
$this
->
settings_page
,
$this
->
settings_section
,
array
(
$this
,
'sanitize_options'
)
);
register_setting
(
$this
->
settings_page
,
$this
->
settings_section
,
array
(
$this
,
'sanitize_options'
)
);
add_settings_section
(
$this
->
settings_section
,
'WP Revisions Control'
,
array
(
$this
,
'settings_section_intro'
),
$this
->
settings_page
);
add_settings_section
(
$this
->
settings_section
,
'WP Revisions Control'
,
array
(
$this
,
'settings_section_intro'
),
$this
->
settings_page
);
...
@@ -109,23 +130,19 @@ class WP_Revisions_Control {
...
@@ -109,23 +130,19 @@ class WP_Revisions_Control {
add_settings_field
(
$this
->
settings_section
.
'-'
.
$post_type
,
$name
,
array
(
$this
,
'field_post_type'
),
$this
->
settings_page
,
$this
->
settings_section
,
array
(
'post_type'
=>
$post_type
)
);
add_settings_field
(
$this
->
settings_section
.
'-'
.
$post_type
,
$name
,
array
(
$this
,
'field_post_type'
),
$this
->
settings_page
,
$this
->
settings_section
,
array
(
'post_type'
=>
$post_type
)
);
}
}
// Post-level functionality
// Post-level functionality
.
add_action
(
'add_meta_boxes'
,
array
(
$this
,
'action_add_meta_boxes'
),
10
,
2
);
add_action
(
'add_meta_boxes'
,
array
(
$this
,
'action_add_meta_boxes'
),
10
,
2
);
add_action
(
'wp_ajax_'
.
$this
->
settings_section
.
'_purge'
,
array
(
$this
,
'ajax_purge'
)
);
add_action
(
'wp_ajax_'
.
$this
->
settings_section
.
'_purge'
,
array
(
$this
,
'ajax_purge'
)
);
add_action
(
'save_post'
,
array
(
$this
,
'action_save_post'
)
);
add_action
(
'save_post'
,
array
(
$this
,
'action_save_post'
)
);
}
}
/**
/**
*
*
PLUGIN SETTINGS SECTION
* PLUGIN SETTINGS SECTION
*
*
FOUND UNDER SETTINGS > WRITING
* FOUND UNDER SETTINGS > WRITING
*
*/
*/
/**
/**
* Display assistive text in settings section
* Display assistive text in settings section.
*
* @uses _e
* @uses this::plugin_priority
* @return string
*/
*/
public
function
settings_section_intro
()
{
public
function
settings_section_intro
()
{
?>
?>
...
@@ -141,12 +158,9 @@ class WP_Revisions_Control {
...
@@ -141,12 +158,9 @@ class WP_Revisions_Control {
}
}
/**
/**
* Render field for each post type
* Render field for each post type
.
*
*
* @param array $args
* @param array $args Field arguments.
* @uses this::get_revisions_to_keep
* @uses esc_attr
* @return string
*/
*/
public
function
field_post_type
(
$args
)
{
public
function
field_post_type
(
$args
)
{
$revisions_to_keep
=
$this
->
get_revisions_to_keep
(
$args
[
'post_type'
],
true
);
$revisions_to_keep
=
$this
->
get_revisions_to_keep
(
$args
[
'post_type'
],
true
);
...
@@ -156,9 +170,9 @@ class WP_Revisions_Control {
...
@@ -156,9 +170,9 @@ class WP_Revisions_Control {
}
}
/**
/**
* Sanitize plugin settings
* Sanitize plugin settings
.
*
*
* @param array $options
* @param array $options
Unsanitized settings.
* @return array
* @return array
*/
*/
public
function
sanitize_options
(
$options
)
{
public
function
sanitize_options
(
$options
)
{
...
@@ -166,14 +180,18 @@ class WP_Revisions_Control {
...
@@ -166,14 +180,18 @@ class WP_Revisions_Control {
if
(
is_array
(
$options
)
)
{
if
(
is_array
(
$options
)
)
{
foreach
(
$options
as
$post_type
=>
$to_keep
)
{
foreach
(
$options
as
$post_type
=>
$to_keep
)
{
if
(
0
===
strlen
(
$to_keep
)
)
$type_length
=
strlen
(
$to_keep
);
if
(
0
===
$type_length
)
{
$to_keep
=
-
1
;
$to_keep
=
-
1
;
else
}
else
{
$to_keep
=
intval
(
$to_keep
);
$to_keep
=
(
int
)
$to_keep
;
}
// Lowest possible value is -1, used to indicate infinite revisions are stored
// Lowest possible value is -1, used to indicate infinite revisions are stored
.
if
(
-
1
>
$to_keep
)
if
(
-
1
>
$to_keep
)
{
$to_keep
=
-
1
;
$to_keep
=
-
1
;
}
$options_sanitized
[
$post_type
]
=
$to_keep
;
$options_sanitized
[
$post_type
]
=
$to_keep
;
}
}
...
@@ -183,8 +201,8 @@ class WP_Revisions_Control {
...
@@ -183,8 +201,8 @@ class WP_Revisions_Control {
}
}
/**
/**
*
*
REVISIONS QUANTITY OVERRIDES
* REVISIONS QUANTITY OVERRIDES
.
*
*/
*/
/**
/**
* Allow others to change the priority this plugin's functionality runs at
* Allow others to change the priority this plugin's functionality runs at
...
@@ -203,15 +221,13 @@ class WP_Revisions_Control {
...
@@ -203,15 +221,13 @@ class WP_Revisions_Control {
}
}
/**
/**
* Override number of revisions to keep using plugin's settings
* Override number of revisions to keep using plugin's settings
.
*
*
* Can either be post-specific or universal
* Can either be post-specific or universal
.
*
*
* @uses get_post_meta
* @param int $qty Number of revisions to keep.
* @uses get_post_type
* @param WP_Post $post Post object.
* @uses this::get_settings
* @return int
* @filter wp_revisions_to_keep
* @return mixed
*/
*/
public
function
filter_wp_revisions_to_keep
(
$qty
,
$post
)
{
public
function
filter_wp_revisions_to_keep
(
$qty
,
$post
)
{
$post_limit
=
get_post_meta
(
$post
->
ID
,
$this
->
meta_key_limit
,
true
);
$post_limit
=
get_post_meta
(
$post
->
ID
,
$this
->
meta_key_limit
,
true
);
...
@@ -222,42 +238,31 @@ class WP_Revisions_Control {
...
@@ -222,42 +238,31 @@ class WP_Revisions_Control {
$post_type
=
get_post_type
(
$post
)
?
get_post_type
(
$post
)
:
$post
->
post_type
;
$post_type
=
get_post_type
(
$post
)
?
get_post_type
(
$post
)
:
$post
->
post_type
;
$settings
=
$this
->
get_settings
();
$settings
=
$this
->
get_settings
();
if
(
array_key_exists
(
$post_type
,
$settings
)
)
if
(
array_key_exists
(
$post_type
,
$settings
)
)
{
$qty
=
$settings
[
$post_type
];
$qty
=
$settings
[
$post_type
];
}
}
}
return
$qty
;
return
$qty
;
}
}
/**
/**
*
*
POST-LEVEL FUNCTIONALITY
* POST-LEVEL FUNCTIONALITY
.
*
*/
*/
/**
/**
* Override Core's revisions metabox
* Override Core's revisions metabox
.
*
*
* @param string $post_type
* @param string $post_type Post type.
* @param object $post
* @param object $post Post object.
* @uses post_type_supports
* @uses get_post_status
* @uses wp_get_post_revisions
* @uses remove_meta_box
* @uses add_meta_box
* @uses wp_enqueue_script
* @uses plugins_url
* @uses wp_localize_script
* @uses wpautop
* @uses add_action
* @action add_meta_boxes
* @return null
*/
*/
public
function
action_add_meta_boxes
(
$post_type
,
$post
)
{
public
function
action_add_meta_boxes
(
$post_type
,
$post
)
{
if
(
post_type_supports
(
$post_type
,
'revisions'
)
&&
'auto-draft'
!=
get_post_status
()
&&
count
(
wp_get_post_revisions
(
$post
)
)
>
1
)
{
if
(
post_type_supports
(
$post_type
,
'revisions'
)
&&
'auto-draft'
!=
get_post_status
()
&&
count
(
wp_get_post_revisions
(
$post
)
)
>
1
)
{
// Replace the metabox
// Replace the metabox
.
remove_meta_box
(
'revisionsdiv'
,
null
,
'normal'
);
remove_meta_box
(
'revisionsdiv'
,
null
,
'normal'
);
add_meta_box
(
'revisionsdiv-wp-rev-ctl'
,
__
(
'Revisions'
,
'wp_revisions_control'
),
array
(
$this
,
'revisions_meta_box'
),
null
,
'normal'
,
'core'
);
add_meta_box
(
'revisionsdiv-wp-rev-ctl'
,
__
(
'Revisions'
,
'wp_revisions_control'
),
array
(
$this
,
'revisions_meta_box'
),
null
,
'normal'
,
'core'
);
// A bit of JS for us
// A bit of JS for us
.
$handle
=
'wp-revisions-control-post'
;
$handle
=
'wp-revisions-control-post'
;
wp_enqueue_script
(
$handle
,
plugins_url
(
'js/post.js'
,
__FILE__
),
array
(
'jquery'
),
'20131205'
,
true
);
wp_enqueue_script
(
$handle
,
plugins_url
(
'js/post.js'
,
__FILE__
),
array
(
'jquery'
),
'20131205'
,
true
);
wp_localize_script
(
$handle
,
$this
->
settings_section
,
array
(
wp_localize_script
(
$handle
,
$this
->
settings_section
,
array
(
...
@@ -270,7 +275,7 @@ class WP_Revisions_Control {
...
@@ -270,7 +275,7 @@ class WP_Revisions_Control {
'error'
=>
__
(
'An error occurred. Please refresh the page and try again.'
,
'wp_revisions_control'
)
'error'
=>
__
(
'An error occurred. Please refresh the page and try again.'
,
'wp_revisions_control'
)
)
);
)
);
// Add some styling to our metabox additions
// Add some styling to our metabox additions
.
add_action
(
'admin_head'
,
array
(
$this
,
'action_admin_head'
),
999
);
add_action
(
'admin_head'
,
array
(
$this
,
'action_admin_head'
),
999
);
}
}
}
}
...
@@ -278,13 +283,7 @@ class WP_Revisions_Control {
...
@@ -278,13 +283,7 @@ class WP_Revisions_Control {
/**
/**
* Render Revisions metabox with plugin's additions
* Render Revisions metabox with plugin's additions
*
*
* @uses post_revisions_meta_box
* @param WP_Post $post Post object.
* @uses the_ID
* @uses esc_attr
* @uses wp_create_nonce
* @uses this::get_post_revisions_to_keep
* @uses wp_nonce_field
* @return string
*/
*/
public
function
revisions_meta_box
(
$post
)
{
public
function
revisions_meta_box
(
$post
)
{
post_revisions_meta_box
(
$post
);
post_revisions_meta_box
(
$post
);
...
@@ -305,14 +304,7 @@ class WP_Revisions_Control {
...
@@ -305,14 +304,7 @@ class WP_Revisions_Control {
}
}
/**
/**
* Process a post-specific request to purge revisions
* Process a post-specific request to purge revisions.
*
* @uses __
* @uses check_ajax_referer
* @uses current_user_can
* @uses wp_get_post_revisions
* @uses number_format_i18n
* @return string
*/
*/
public
function
ajax_purge
()
{
public
function
ajax_purge
()
{
$post_id
=
isset
(
$_REQUEST
[
'post_id'
]
)
?
(
int
)
$_REQUEST
[
'post_id'
]
:
false
;
$post_id
=
isset
(
$_REQUEST
[
'post_id'
]
)
?
(
int
)
$_REQUEST
[
'post_id'
]
:
false
;
...
@@ -348,12 +340,9 @@ class WP_Revisions_Control {
...
@@ -348,12 +340,9 @@ class WP_Revisions_Control {
}
}
/**
/**
* Sanitize and store post-specifiy revisions quantity
* Sanitize and store post-specifiy revisions quantity
.
*
*
* @uses wp_verify_nonce
* @param int $post_id Post ID.
* @uses update_post_meta
* @action save_post
* @return null
*/
*/
public
function
action_save_post
(
$post_id
)
{
public
function
action_save_post
(
$post_id
)
{
if
(
isset
(
$_POST
[
$this
->
settings_section
.
'_limit_nonce'
]
)
&&
wp_verify_nonce
(
$_POST
[
$this
->
settings_section
.
'_limit_nonce'
],
$this
->
settings_section
.
'_limit'
)
&&
isset
(
$_POST
[
$this
->
settings_section
.
'_qty'
]
)
)
{
if
(
isset
(
$_POST
[
$this
->
settings_section
.
'_limit_nonce'
]
)
&&
wp_verify_nonce
(
$_POST
[
$this
->
settings_section
.
'_limit_nonce'
],
$this
->
settings_section
.
'_limit'
)
&&
isset
(
$_POST
[
$this
->
settings_section
.
'_qty'
]
)
)
{
...
@@ -367,11 +356,7 @@ class WP_Revisions_Control {
...
@@ -367,11 +356,7 @@ class WP_Revisions_Control {
}
}
/**
/**
* Add a border between the regular revisions list and this plugin's additions
* Add a border between the regular revisions list and this plugin's additions.
*
* @uses esc_attr
* @action admin_head
* @return string
*/
*/
public
function
action_admin_head
()
{
public
function
action_admin_head
()
{
?>
?>
...
@@ -392,14 +377,12 @@ class WP_Revisions_Control {
...
@@ -392,14 +377,12 @@ class WP_Revisions_Control {
}
}
/**
/**
*
*
PLUGIN UTILITIES
* PLUGIN UTILITIES
.
*
*/
*/
/**
/**
* Retrieve plugin settings
* Retrieve plugin settings
.
*
*
* @uses this::get_post_types
* @uses get_option
* @return array
* @return array
*/
*/
private
function
get_settings
()
{
private
function
get_settings
()
{
...
@@ -424,11 +407,8 @@ class WP_Revisions_Control {
...
@@ -424,11 +407,8 @@ class WP_Revisions_Control {
}
}
/**
/**
* Retrieve array of supported post types and their labels
* Retrieve array of supported post types and their labels
.
*
*
* @uses get_post_types
* @uses post_type_supports
* @uses get_post_type_object
* @return array
* @return array
*/
*/
private
function
get_post_types
()
{
private
function
get_post_types
()
{
...
@@ -453,14 +433,14 @@ class WP_Revisions_Control {
...
@@ -453,14 +433,14 @@ class WP_Revisions_Control {
}
}
/**
/**
* Retrieve number of revisions to keep for a given post type
* Retrieve number of revisions to keep for a given post type
.
*
*
* @
uses WP_Post
* @
param string $post_type Post type.
* @
uses wp_revisions_to_keep
* @
param bool $blank_for_all Should blank value be used to indicate all are kept.
* @return
mixed
* @return
int|string
*/
*/
private
function
get_revisions_to_keep
(
$post_type
,
$blank_for_all
=
false
)
{
private
function
get_revisions_to_keep
(
$post_type
,
$blank_for_all
=
false
)
{
// wp_revisions_to_keep() accepts a post object, not just the post type
// wp_revisions_to_keep() accepts a post object, not just the post type
.
// We construct a new WP_Post object to ensure anything hooked to the wp_revisions_to_keep filter has the same basic data WP provides.
// We construct a new WP_Post object to ensure anything hooked to the wp_revisions_to_keep filter has the same basic data WP provides.
$_post
=
new
WP_Post
(
(
object
)
array
(
'post_type'
=>
$post_type
)
);
$_post
=
new
WP_Post
(
(
object
)
array
(
'post_type'
=>
$post_type
)
);
$to_keep
=
wp_revisions_to_keep
(
$_post
);
$to_keep
=
wp_revisions_to_keep
(
$_post
);
...
@@ -472,11 +452,10 @@ class WP_Revisions_Control {
...
@@ -472,11 +452,10 @@ class WP_Revisions_Control {
}
}
/**
/**
* Retrieve number of revisions to keep for a give post
* Retrieve number of revisions to keep for a give post
.
*
*
* @param int $post_id
* @param int $post_id Post ID.
* @uses get_post_meta
* @return int|string
* @return mixed
*/
*/
private
function
get_post_revisions_to_keep
(
$post_id
)
{
private
function
get_post_revisions_to_keep
(
$post_id
)
{
$to_keep
=
get_post_meta
(
$post_id
,
$this
->
meta_key_limit
,
true
);
$to_keep
=
get_post_meta
(
$post_id
,
$this
->
meta_key_limit
,
true
);
...
@@ -489,4 +468,5 @@ class WP_Revisions_Control {
...
@@ -489,4 +468,5 @@ class WP_Revisions_Control {
return
$to_keep
;
return
$to_keep
;
}
}
}
}
WP_Revisions_Control
::
get_instance
();
WP_Revisions_Control
::
get_instance
();
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment