WP Revisions Control - Version 1.0

Version Description

Initial public release

=

Download this release

Release Info

Developer ethitter
Plugin Icon wp plugin WP Revisions Control
Version 1.0
Comparing to
See all releases

Version 1.0

Files changed (2) hide show
  1. readme.txt +41 -0
  2. wp-revisions-control.php +283 -0
readme.txt ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ === WP Revisions Control ===
2
+ Contributors: ethitter
3
+ Donate link: https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=10275434
4
+ Tags: revision, revisions, admin
5
+ Requires at least: 3.6
6
+ Tested up to: 3.6
7
+ Stable tag: 1.0
8
+ License: GPLv2 or later
9
+ License URI: http://www.gnu.org/licenses/gpl-2.0.html
10
+
11
+ Control how many revisions are stored for each post type.
12
+
13
+ == Description ==
14
+
15
+ WordPress 3.6 allows users to control how many revisions are stored for each supported post type. No longer must you rely on the `WP_POST_REVISIONS` constant, which applied universally. This plugin provides an interface for this new functionality.
16
+
17
+ With this plugin enabled, simply visit **Settings > Writing** to specify the number of revisions retained for each post type.
18
+
19
+ Why is this helpful? Revisions are stored in the database, and if many are stored, can cause bloat. This bloat may lead to slower queries, which can have a noticeable performance impact. The value of these revisions also depends on what is being tracked. For example, I may want to store every revision of the posts I write, but only desire to keep the latest five versions of each page on my site. Starting in WordPress 3.6, this control is available. WordPress doesn’t provide a native interface to specify revisions quantities, so I wrote this quick plugin to do so.
20
+
21
+ **Development is over on GitHub: https://github.com/ethitter/WP-Revisions-Control.**
22
+
23
+ == Installation ==
24
+
25
+ 1. Upload wp-revisions-control to /wp-content/plugins/.
26
+ 2. Activate plugin through the WordPress Plugins menu.
27
+ 3. Go to **Settings > Writing** and set the options under **WP Revisions Control**.
28
+
29
+ == Frequently Asked Questions ==
30
+
31
+ = Where do I change the plugin's settings? =
32
+ Navigate to **Settings > Writing** in your WordPress Dashboard, and look for the **WP Revisions Control** section.
33
+
34
+ == Changelog ==
35
+
36
+ = 1.0 =
37
+ Initial public release
38
+
39
+ == Screenshots ==
40
+
41
+ 1. The plugin's settings section, found under **Settings > Writing**.
wp-revisions-control.php ADDED
@@ -0,0 +1,283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /*
3
+ Plugin Name: WP Revisions Control
4
+ Plugin URI: http://www.ethitter.com/plugins/wp-revisions-control/
5
+ Description: Control how many revisions are stored for each post type
6
+ Author: Erick Hitter
7
+ Version: 1.0
8
+ Author URI: http://www.ethitter.com/
9
+
10
+ This program is free software; you can redistribute it and/or modify
11
+ it under the terms of the GNU General Public License as published by
12
+ the Free Software Foundation; either version 2 of the License, or
13
+ (at your option) any later version.
14
+
15
+ This program is distributed in the hope that it will be useful,
16
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
17
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
+ GNU General Public License for more details.
19
+
20
+ You should have received a copy of the GNU General Public License
21
+ along with this program; if not, write to the Free Software
22
+ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23
+ */
24
+
25
+ class WP_Revisions_Control {
26
+ /**
27
+ * Singleton
28
+ */
29
+ private static $__instance = null;
30
+
31
+ /**
32
+ * Class variables
33
+ */
34
+ private static $priority = null; // use $this->plugin_priority()
35
+ private $priority_default = 50;
36
+
37
+ private static $post_types = array(); // use $this->get_post_types()
38
+ private static $settings = array(); // use $this->get_settings()
39
+
40
+ private $settings_page = 'writing';
41
+ private $settings_section = 'wp_revisions_control';
42
+
43
+ /**
44
+ * Silence is golden!
45
+ */
46
+ private function __construct() {}
47
+
48
+ /**
49
+ * Singleton implementation
50
+ *
51
+ * @uses self::setup
52
+ * @return object
53
+ */
54
+ public static function get_instance() {
55
+ if ( ! is_a( self::$__instance, __CLASS__ ) ) {
56
+ self::$__instance = new self;
57
+
58
+ self::$__instance->setup();
59
+ }
60
+
61
+ return self::$__instance;
62
+ }
63
+
64
+ /**
65
+ * Register actions and filters at `init` so others can interact, if desired.
66
+ *
67
+ * @uses add_action
68
+ * @return null
69
+ */
70
+ private function setup() {
71
+ add_action( 'init', array( $this, 'action_init' ) );
72
+ }
73
+
74
+ /**
75
+ * Register actions and filters
76
+ *
77
+ * @uses add_action
78
+ * @uses add_filter
79
+ * @uses this::plugin_priority
80
+ * @return null
81
+ */
82
+ public function action_init() {
83
+ add_action( 'admin_init', array( $this, 'action_admin_init' ) );
84
+
85
+ add_filter( 'wp_revisions_to_keep', array( $this, 'filter_wp_revisions_to_keep' ), $this->plugin_priority(), 2 );
86
+ }
87
+
88
+ /**
89
+ * Register plugin's settings fields
90
+ *
91
+ * Plugin title is intentionally not translatable.
92
+ *
93
+ * @uses register_setting
94
+ * @uses add_settings_section
95
+ * @uses __
96
+ * @uses this::get_post_types
97
+ * @uses add_settings_field
98
+ * @action admin_init
99
+ * @return null
100
+ */
101
+ public function action_admin_init() {
102
+ register_setting( $this->settings_page, $this->settings_section, array( $this, 'sanitize_options' ) );
103
+
104
+ add_settings_section( $this->settings_section, 'WP Revisions Control', array( $this, 'settings_section_intro' ), $this->settings_page );
105
+
106
+ foreach ( $this->get_post_types() as $post_type => $name ) {
107
+ 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 ) );
108
+ }
109
+ }
110
+
111
+ /**
112
+ * Display assistive text in settings section
113
+ *
114
+ * @uses _e
115
+ * @uses this::plugin_priority
116
+ * @return string
117
+ */
118
+ public function settings_section_intro() {
119
+ ?>
120
+ <p><?php _e( 'Set the number of revisions to save for each post type listed. To retain all revisions for a given post type, leave the field empty.', 'wp_revisions_control' ); ?></p>
121
+ <p><?php _e( "If a post type isn't listed, revisions are not enabled for that post type.", 'wp_revisions_control' ); ?></p>
122
+ <?php
123
+
124
+ // Display a note if the plugin priority is other than the default.
125
+ // Will be useful when debugging issues later.
126
+ if ( $this->plugin_priority() !== $this->priority_default ) : ?>
127
+ <p><?php _e( "A local change is causing this plugin's functionality to run at a priority other than the default. If you experience difficulties with the plugin, please unhook any functions from the <code>wp_revisions_control_priority</code> filter.", 'wp_revisions_control' ); ?></p>
128
+ <?php endif;
129
+ }
130
+
131
+ /**
132
+ * Render field for each post type
133
+ *
134
+ * @param array $args
135
+ * @uses this::get_revisions_to_keep
136
+ * @uses esc_attr
137
+ * @return string
138
+ */
139
+ public function field_post_type( $args ) {
140
+ $revisions_to_keep = $this->get_revisions_to_keep( $args['post_type'], true );
141
+ ?>
142
+ <input type="text" name="<?php echo esc_attr( $this->settings_section . '[' . $args['post_type'] . ']' ); ?>" value="<?php echo esc_attr( $revisions_to_keep ); ?>" class="small-text" />
143
+ <?php
144
+ }
145
+
146
+ /**
147
+ * Sanitize plugin settings
148
+ *
149
+ * @param array $options
150
+ * @return array
151
+ */
152
+ public function sanitize_options( $options ) {
153
+ $options_sanitized = array();
154
+
155
+ if ( is_array( $options ) ) {
156
+ foreach ( $options as $post_type => $to_keep ) {
157
+ if ( 0 === strlen( $to_keep ) )
158
+ $to_keep = -1;
159
+ else
160
+ $to_keep = intval( $to_keep );
161
+
162
+ // Lowest possible value is -1, used to indicate infinite revisions are stored
163
+ if ( -1 > $to_keep )
164
+ $to_keep = -1;
165
+
166
+ $options_sanitized[ $post_type ] = $to_keep;
167
+ }
168
+ }
169
+
170
+ return $options_sanitized;
171
+ }
172
+
173
+ /**
174
+ * Allow others to change the priority this plugin's functionality runs at
175
+ *
176
+ * @uses apply_filters
177
+ * @return int
178
+ */
179
+ private function plugin_priority() {
180
+ if ( is_null( self::$priority ) ) {
181
+ $plugin_priority = apply_filters( 'wp_revisions_control_priority', $this->priority_default );
182
+
183
+ self::$priority = is_numeric( $plugin_priority ) ? (int) $plugin_priority : $this->priority_default;
184
+ }
185
+
186
+ return self::$priority;
187
+ }
188
+
189
+ /**
190
+ * Override number of revisions to keep using plugin's settings
191
+ *
192
+ * @uses get_post_type
193
+ * @uses this::get_settings
194
+ * @filter wp_revisions_to_keep
195
+ * @return mixed
196
+ */
197
+ public function filter_wp_revisions_to_keep( $qty, $post ) {
198
+ $post_type = get_post_type( $post ) ? get_post_type( $post ) : $post->post_type;
199
+ $settings = $this->get_settings();
200
+
201
+ if ( array_key_exists( $post_type, $settings ) )
202
+ return $settings[ $post_type ];
203
+
204
+ return $qty;
205
+ }
206
+
207
+ /**
208
+ * Retrieve plugin settings
209
+ *
210
+ * @uses this::get_post_types
211
+ * @uses get_option
212
+ * @return array
213
+ */
214
+ private function get_settings() {
215
+ if ( empty( self::$settings ) ) {
216
+ $post_types = $this->get_post_types();
217
+
218
+ $settings = get_option( $this->settings_section, array() );
219
+
220
+ $merged_settings = array();
221
+
222
+ foreach ( $post_types as $post_type => $name ) {
223
+ if ( array_key_exists( $post_type, $settings ) )
224
+ $merged_settings[ $post_type ] = (int) $settings[ $post_type ];
225
+ else
226
+ $merged_settings[ $post_type ] = -1;
227
+ }
228
+
229
+ self::$settings = $merged_settings;
230
+ }
231
+
232
+ return self::$settings;
233
+ }
234
+
235
+ /**
236
+ * Retrieve array of supported post types and their labels
237
+ *
238
+ * @uses get_post_types
239
+ * @uses post_type_supports
240
+ * @uses get_post_type_object
241
+ * @return array
242
+ */
243
+ private function get_post_types() {
244
+ if ( empty( self::$post_types ) ) {
245
+ $types = get_post_types();
246
+
247
+ foreach ( $types as $type ) {
248
+ if ( post_type_supports( $type, 'revisions' ) ) {
249
+ $object = get_post_type_object( $type );
250
+
251
+ if ( property_exists( $object, 'labels' ) && property_exists( $object->labels, 'name' ) )
252
+ $name = $object->labels->name;
253
+ else
254
+ $name = $object->name;
255
+
256
+ self::$post_types[ $type ] = $name;
257
+ }
258
+ }
259
+ }
260
+
261
+ return self::$post_types;
262
+ }
263
+
264
+ /**
265
+ * Retrieve number of revisions to keep for a given post type
266
+ *
267
+ * @uses WP_Post
268
+ * @uses wp_revisions_to_keep
269
+ * @return mixed
270
+ */
271
+ private function get_revisions_to_keep( $post_type, $blank_for_all = false ) {
272
+ // wp_revisions_to_keep() accepts a post object, not just the post type
273
+ // 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.
274
+ $_post = new WP_Post( (object) array( 'post_type' => $post_type ) );
275
+ $to_keep = wp_revisions_to_keep( $_post );
276
+
277
+ if ( $blank_for_all && -1 == $to_keep )
278
+ return '';
279
+ else
280
+ return (int) $to_keep;
281
+ }
282
+ }
283
+ WP_Revisions_Control::get_instance();