class-wp-revisions-control.php 14.1 KB
Newer Older
Erick Hitter's avatar
Erick Hitter committed
1
<?php
2 3 4 5 6
/**
 * Main plugin functionality.
 *
 * @package WP_Revisions_Control
 */
Erick Hitter's avatar
Erick Hitter committed
7

8 9 10
/**
 * Class WP_Revisions_Control.
 */
Erick Hitter's avatar
Erick Hitter committed
11 12
class WP_Revisions_Control {
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
13 14 15
	 * Singleton.
	 *
	 * @var static
Erick Hitter's avatar
Erick Hitter committed
16
	 */
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
17
	private static $__instance;
Erick Hitter's avatar
Erick Hitter committed
18 19

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
20 21 22 23 24 25 26 27 28 29 30 31
	 * Filter priority.
	 *
	 * @see $this->filter_priority()
	 *
	 * @var int
	 */
	private static $priority = null;

	/**
	 * Default filter priority.
	 *
	 * @var int
Erick Hitter's avatar
Erick Hitter committed
32
	 */
33 34
	private $priority_default = 50;

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
	/**
	 * 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();
Erick Hitter's avatar
Erick Hitter committed
52

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
53 54 55 56 57
	/**
	 * WordPress options page to display settings on.
	 *
	 * @var string
	 */
58
	private $settings_page = 'writing';
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
59 60 61 62 63 64

	/**
	 * Name of custom settings sections.
	 *
	 * @var string
	 */
65 66
	private $settings_section = 'wp_revisions_control';

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
67 68 69 70 71
	/**
	 * Meta key holding post's revisions limit.
	 *
	 * @var string
	 */
72 73
	private $meta_key_limit = '_wp_rev_ctl_limit';

Erick Hitter's avatar
Erick Hitter committed
74 75 76 77 78 79
	/**
	 * Silence is golden!
	 */
	private function __construct() {}

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
80
	 * Singleton implementation.
Erick Hitter's avatar
Erick Hitter committed
81
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
82
	 * @return static
Erick Hitter's avatar
Erick Hitter committed
83 84
	 */
	public static function get_instance() {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
85 86
		if ( ! is_a( static::$__instance, __CLASS__ ) ) {
			static::$__instance = new self();
Erick Hitter's avatar
Erick Hitter committed
87

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
88
			static::$__instance->setup();
Erick Hitter's avatar
Erick Hitter committed
89 90
		}

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
91
		return static::$__instance;
Erick Hitter's avatar
Erick Hitter committed
92 93
	}

94
	/**
Erick Hitter's avatar
Erick Hitter committed
95
	 * Register actions and filters at `init` so others can interact, if desired.
96 97
	 */
	private function setup() {
98
		add_action( 'plugins_loaded', array( $this, 'action_plugins_loaded' ) );
99 100 101
		add_action( 'init', array( $this, 'action_init' ) );
	}

102
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
103
	 * Load plugin translations.
104 105 106 107 108
	 */
	public function action_plugins_loaded() {
		load_plugin_textdomain( 'wp_revisions_control', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
	}

Erick Hitter's avatar
Erick Hitter committed
109
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
110
	 * Register actions and filters.
Erick Hitter's avatar
Erick Hitter committed
111
	 */
112
	public function action_init() {
113
		add_action( 'admin_init', array( $this, 'action_admin_init' ) );
Erick Hitter's avatar
Erick Hitter committed
114

115
		add_filter( 'wp_revisions_to_keep', array( $this, 'filter_wp_revisions_to_keep' ), $this->plugin_priority(), 2 );
116 117 118
	}

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
119
	 * Register plugin's admin-specific elements.
120
	 *
121
	 * Plugin title is intentionally not translatable.
122 123
	 */
	public function action_admin_init() {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
124
		// Plugin setting section.
Erick Hitter's avatar
Erick Hitter committed
125
		register_setting( $this->settings_page, $this->settings_section, array( $this, 'sanitize_options' ) );
126

127
		add_settings_section( $this->settings_section, 'WP Revisions Control', array( $this, 'settings_section_intro' ), $this->settings_page );
128 129 130 131

		foreach ( $this->get_post_types() as $post_type => $name ) {
			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 ) );
		}
132

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
133
		// Post-level functionality.
134
		add_action( 'add_meta_boxes', array( $this, 'action_add_meta_boxes' ), 10, 2 );
135
		add_action( 'wp_ajax_' . $this->settings_section . '_purge', array( $this, 'ajax_purge' ) );
136
		add_action( 'save_post', array( $this, 'action_save_post' ) );
137 138
	}

139
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
140 141 142
	 * PLUGIN SETTINGS SECTION
	 * FOUND UNDER SETTINGS > WRITING
	 */
143

144
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
145
	 * Display assistive text in settings section.
146 147 148
	 */
	public function settings_section_intro() {
		?>
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
149 150
		<p><?php esc_html_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>
		<p><?php esc_html_e( 'If a post type isn\'t listed, revisions are not enabled for that post type.', 'wp_revisions_control' ); ?></p>
151
		<?php
152 153 154

		// Display a note if the plugin priority is other than the default.
		// Will be useful when debugging issues later.
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
		if ( $this->plugin_priority() !== $this->priority_default ) :
			?>
			<p>
				<?php
				printf(
					/* translators: 1. Filter tag. */
					esc_html__(
						'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 %1$s filter.',
						'wp_revisions_control'
					),
					'<code>wp_revisions_control_priority</code>'
				);
				?>
			</p>
			<?php
		endif;
171 172
	}

173
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
174
	 * Render field for each post type.
175
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
176
	 * @param array $args Field arguments.
177 178
	 */
	public function field_post_type( $args ) {
Erick Hitter's avatar
Erick Hitter committed
179 180
		$revisions_to_keep = $this->get_revisions_to_keep( $args['post_type'], true );
		?>
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
181
		<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" />
Erick Hitter's avatar
Erick Hitter committed
182
		<?php
183 184 185
	}

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
186
	 * Sanitize plugin settings.
187
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
188
	 * @param array $options Unsanitized settings.
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
189
	 * @return array
190 191
	 */
	public function sanitize_options( $options ) {
Erick Hitter's avatar
Erick Hitter committed
192 193 194 195
		$options_sanitized = array();

		if ( is_array( $options ) ) {
			foreach ( $options as $post_type => $to_keep ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
196 197 198
				$type_length = strlen( $to_keep );

				if ( 0 === $type_length ) {
Erick Hitter's avatar
Erick Hitter committed
199
					$to_keep = -1;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
200 201 202
				} else {
					$to_keep = (int) $to_keep;
				}
203

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
204 205
				// Lowest possible value is -1, used to indicate infinite revisions are stored.
				if ( -1 > $to_keep ) {
206
					$to_keep = -1;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
207
				}
208

Erick Hitter's avatar
Erick Hitter committed
209 210 211 212 213 214 215
				$options_sanitized[ $post_type ] = $to_keep;
			}
		}

		return $options_sanitized;
	}

216
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
217 218
	 * REVISIONS QUANTITY OVERRIDES.
	 */
219

220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	/**
	 * Allow others to change the priority this plugin's functionality runs at
	 *
	 * @uses apply_filters
	 * @return int
	 */
	private function plugin_priority() {
		if ( is_null( self::$priority ) ) {
			$plugin_priority = apply_filters( 'wp_revisions_control_priority', $this->priority_default );

			self::$priority = is_numeric( $plugin_priority ) ? (int) $plugin_priority : $this->priority_default;
		}

		return self::$priority;
	}

Erick Hitter's avatar
Erick Hitter committed
236
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
237
	 * Override number of revisions to keep using plugin's settings.
Erick Hitter's avatar
Erick Hitter committed
238
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
239
	 * Can either be post-specific or universal.
240
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
241 242 243
	 * @param int     $qty  Number of revisions to keep.
	 * @param WP_Post $post Post object.
	 * @return int
Erick Hitter's avatar
Erick Hitter committed
244 245
	 */
	public function filter_wp_revisions_to_keep( $qty, $post ) {
246 247 248 249 250 251
		$post_limit = get_post_meta( $post->ID, $this->meta_key_limit, true );

		if ( 0 < strlen( $post_limit ) ) {
			$qty = $post_limit;
		} else {
			$post_type = get_post_type( $post ) ? get_post_type( $post ) : $post->post_type;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
252
			$settings  = $this->get_settings();
Erick Hitter's avatar
Erick Hitter committed
253

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
254
			if ( array_key_exists( $post_type, $settings ) ) {
255
				$qty = $settings[ $post_type ];
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
256
			}
257
		}
Erick Hitter's avatar
Erick Hitter committed
258 259 260 261

		return $qty;
	}

262
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
263 264
	 * POST-LEVEL FUNCTIONALITY.
	 */
265 266

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
267
	 * Override Core's revisions metabox.
268
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
269 270
	 * @param string $post_type Post type.
	 * @param object $post      Post object.
271 272
	 */
	public function action_add_meta_boxes( $post_type, $post ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
273
		if ( post_type_supports( $post_type, 'revisions' ) && 'auto-draft' !== get_post_status() && count( wp_get_post_revisions( $post ) ) > 1 ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
274
			// Replace the metabox.
275
			remove_meta_box( 'revisionsdiv', null, 'normal' );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289
			add_meta_box(
				'revisionsdiv-wp-rev-ctl',
				__(
					'Revisions',
					'wp_revisions_control'
				),
				array(
					$this,
					'revisions_meta_box',
				),
				null,
				'normal',
				'core'
			);
290

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
291
			// A bit of JS for us.
292
			$handle = 'wp-revisions-control-post';
293
			wp_enqueue_script( $handle, plugins_url( 'js/post.js', __FILE__ ), array( 'jquery' ), '20131205', true );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
294 295 296 297 298 299 300 301 302 303 304 305 306
			wp_localize_script(
				$handle,
				$this->settings_section,
				array(
					'namespace'       => $this->settings_section,
					'action_base'     => $this->settings_section,
					'processing_text' => __( 'Processing&hellip;', 'wp_revisions_control' ),
					'ays'             => __( 'Are you sure you want to remove revisions from this post?', 'wp_revisions_control' ),
					'autosave'        => __( 'Autosave', 'wp_revisions_control' ),
					'nothing_text'    => wpautop( __( 'There are no revisions to remove.', 'wp_revisions_control' ) ),
					'error'           => __( 'An error occurred. Please refresh the page and try again.', 'wp_revisions_control' ),
				)
			);
307

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
308
			// Add some styling to our metabox additions.
309
			add_action( 'admin_head', array( $this, 'action_admin_head' ), 999 );
310
		}
311 312 313
	}

	/**
314
	 * Render Revisions metabox with plugin's additions
315
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
316
	 * @param WP_Post $post Post object.
317 318 319 320 321 322 323
	 */
	public function revisions_meta_box( $post ) {
		post_revisions_meta_box( $post );

		?>
		<div id="<?php echo esc_attr( $this->settings_section ); ?>">
			<h4>WP Revisions Control</h4>
324

325 326 327
			<p class="button purge" data-postid="<?php the_ID(); ?>" data-nonce="<?php echo esc_attr( wp_create_nonce( $this->settings_section . '_purge' ) ); ?>"><?php _e( 'Purge these revisions', 'wp_revisions_control' ); ?></p>

			<p>
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
328 329 330 331 332 333 334 335 336 337
				<?php
				printf(
					/* translators: 1. Text input field. */
					esc_html__(
						'Limit this post to %1$s revisions. Leave this field blank for default behavior.',
						'wp_revisions_control'
					),
					'<input type="text" name="' . esc_attr( $this->settings_section ) . '_qty" value="' . (int) $this->get_post_revisions_to_keep( $post->ID ) . '" id="' . esc_attr( $this->settings_section ) . '_qty" size="2" />'
				);
				?>
338 339 340

				<?php wp_nonce_field( $this->settings_section . '_limit', $this->settings_section . '_limit_nonce', false ); ?>
			</p>
341 342 343 344
		</div><!-- #<?php echo esc_attr( $this->settings_section ); ?> -->
		<?php
	}

345
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
346
	 * Process a post-specific request to purge revisions.
347 348 349 350
	 */
	public function ajax_purge() {
		$post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : false;

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
351
		// Hold the current state of this Ajax request.
352 353
		$response = array();

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
354 355
		// Check for necessary data and capabilities.
		if ( ! $post_id ) {
356
			$response['error'] = __( 'No post ID was provided. Please refresh the page and try again.', 'wp_revisions_control' );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
357
		} elseif ( ! check_ajax_referer( $this->settings_section . '_purge', 'nonce', false ) ) {
358
			$response['error'] = __( 'Invalid request. Please refresh the page and try again.', 'wp_revisions_control' );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
359
		} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
360
			$response['error'] = __( 'You are not allowed to edit this post.', 'wp_revisions_control' );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
361
		}
362

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
363
		// Request is valid if $response is still empty, as no errors arose above.
364 365 366 367 368
		if ( empty( $response ) ) {
			$revisions = wp_get_post_revisions( $post_id );

			$count = count( $revisions );

369 370 371
			foreach ( $revisions as $revision ) {
				wp_delete_post_revision( $revision->ID );
			}
372

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
373 374 375 376 377 378 379 380 381
			$response['success'] = sprintf(
				/* translators: 1. Number of removed revisions, already formatted for locale. */
				esc_html__(
					'Removed %1$s revisions associated with this post.',
					'wp_revisions_control'
				),
				number_format_i18n( $count, 0 )
			);

382 383 384
			$response['count'] = $count;
		}

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
385
		// Pass the response back to JS.
386 387 388 389
		echo json_encode( $response );
		exit;
	}

390
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
391
	 * Sanitize and store post-specifiy revisions quantity.
392
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
393
	 * @param int $post_id Post ID.
394 395
	 */
	public function action_save_post( $post_id ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
396 397 398 399 400
		$nonce = $this->settings_section . '_limit_nonce';
		$qty   = $this->settings_section . '_qty';

		if ( isset( $_POST[ $nonce ], $_POST[ $qty ] ) && wp_verify_nonce( sanitize_text_field( $_POST[ $nonce ] ), $this->settings_section . '_limit' ) ) {
			$limit = (int) $_POST[ $qty ];
401

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
402
			if ( -1 === $limit || empty( $limit ) ) {
403
				delete_post_meta( $post_id, $this->meta_key_limit );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
404
			} else {
405
				update_post_meta( $post_id, $this->meta_key_limit, absint( $limit ) );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
406
			}
407 408 409
		}
	}

410
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
411
	 * Add a border between the regular revisions list and this plugin's additions.
412 413
	 */
	public function action_admin_head() {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
414
		?>
415 416
		<style type="text/css">
			#revisionsdiv-wp-rev-ctl #<?php echo esc_attr( $this->settings_section ); ?> {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
417 418 419
				border-top: 1px solid #dfdfdf;
				padding-top: 0;
				margin-top: 20px;
420 421 422 423 424 425 426 427
			}

			#revisionsdiv-wp-rev-ctl #<?php echo esc_attr( $this->settings_section ); ?> h4 {
				border-top: 1px solid #fff;
				padding-top: 1.33em;
				margin-top: 0;
			}
		</style>
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
428
		<?php
429 430
	}

431
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
432 433
	 * PLUGIN UTILITIES.
	 */
434

Erick Hitter's avatar
Erick Hitter committed
435
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
436
	 * Retrieve plugin settings.
Erick Hitter's avatar
Erick Hitter committed
437
	 *
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
438
	 * @return array
Erick Hitter's avatar
Erick Hitter committed
439 440
	 */
	private function get_settings() {
441 442
		if ( empty( self::$settings ) ) {
			$post_types = $this->get_post_types();
Erick Hitter's avatar
Erick Hitter committed
443

444
			$settings = get_option( $this->settings_section, array() );
Erick Hitter's avatar
Erick Hitter committed
445

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
446 447 448 449
			if ( ! is_array( $settings ) ) {
				$settings = array();
			}

450
			$merged_settings = array();
Erick Hitter's avatar
Erick Hitter committed
451

452
			foreach ( $post_types as $post_type => $name ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
453
				if ( array_key_exists( $post_type, $settings ) ) {
454
					$merged_settings[ $post_type ] = (int) $settings[ $post_type ];
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
455 456 457
				} else {
					$merged_settings[ $post_type ] = - 1;
				}
458 459 460
			}

			self::$settings = $merged_settings;
Erick Hitter's avatar
Erick Hitter committed
461 462
		}

463
		return self::$settings;
464 465 466
	}

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
467
	 * Retrieve array of supported post types and their labels.
468
	 *
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
469
	 * @return array
470 471 472
	 */
	private function get_post_types() {
		if ( empty( self::$post_types ) ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
473
			foreach ( get_post_types() as $type ) {
474 475 476
				if ( post_type_supports( $type, 'revisions' ) ) {
					$object = get_post_type_object( $type );

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
477 478 479 480 481
					if ( null === $object ) {
						continue;
					}

					if ( property_exists( $object, 'labels' ) && property_exists( $object->labels, 'name' ) ) {
482
						$name = $object->labels->name;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
483
					} else {
484
						$name = $object->name;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
485
					}
486 487 488 489 490

					self::$post_types[ $type ] = $name;
				}
			}
		}
Erick Hitter's avatar
Erick Hitter committed
491

492
		return self::$post_types;
Erick Hitter's avatar
Erick Hitter committed
493
	}
Erick Hitter's avatar
Erick Hitter committed
494 495

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
496
	 * Retrieve number of revisions to keep for a given post type.
Erick Hitter's avatar
Erick Hitter committed
497
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
498 499 500
	 * @param string $post_type     Post type.
	 * @param bool   $blank_for_all Should blank value be used to indicate all are kept.
	 * @return int|string
Erick Hitter's avatar
Erick Hitter committed
501 502
	 */
	private function get_revisions_to_keep( $post_type, $blank_for_all = false ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
503
		// wp_revisions_to_keep() accepts a post object, not just the post type.
Erick Hitter's avatar
Erick Hitter committed
504
		// 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.
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
505
		$_post   = new WP_Post( (object) array( 'post_type' => $post_type ) );
Erick Hitter's avatar
Erick Hitter committed
506 507
		$to_keep = wp_revisions_to_keep( $_post );

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
508
		if ( $blank_for_all && -1 === $to_keep ) {
Erick Hitter's avatar
Erick Hitter committed
509
			return '';
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
510
		} else {
Erick Hitter's avatar
Erick Hitter committed
511
			return (int) $to_keep;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
512
		}
Erick Hitter's avatar
Erick Hitter committed
513
	}
514 515

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
516
	 * Retrieve number of revisions to keep for a give post.
517
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
518 519
	 * @param int $post_id Post ID.
	 * @return int|string
520 521 522 523
	 */
	private function get_post_revisions_to_keep( $post_id ) {
		$to_keep = get_post_meta( $post_id, $this->meta_key_limit, true );

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
524
		if ( -1 === $to_keep || empty( $to_keep ) ) {
525
			$to_keep = '';
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
526
		} else {
527
			$to_keep = (int) $to_keep;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
528
		}
529 530 531

		return $to_keep;
	}
Erick Hitter's avatar
Erick Hitter committed
532
}
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
533

Erick Hitter's avatar
Erick Hitter committed
534
WP_Revisions_Control::get_instance();