class-wp-revisions-control.php 14.4 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
	 */
	public function action_plugins_loaded() {
106 107 108 109 110
		load_plugin_textdomain(
			'wp_revisions_control',
			false,
			dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/'
		);
111 112
	}

Erick Hitter's avatar
Erick Hitter committed
113
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
114
	 * Register actions and filters.
Erick Hitter's avatar
Erick Hitter committed
115
	 */
116
	public function action_init() {
117
		add_action( 'admin_init', array( $this, 'action_admin_init' ) );
Erick Hitter's avatar
Erick Hitter committed
118

119
		add_filter( 'wp_revisions_to_keep', array( $this, 'filter_wp_revisions_to_keep' ), $this->plugin_priority(), 2 );
120 121 122
	}

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

131
		add_settings_section( $this->settings_section, 'WP Revisions Control', array( $this, 'settings_section_intro' ), $this->settings_page );
132 133 134 135

		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 ) );
		}
136

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

143
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
144 145 146
	 * PLUGIN SETTINGS SECTION
	 * FOUND UNDER SETTINGS > WRITING
	 */
147

148
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
149
	 * Display assistive text in settings section.
150 151 152
	 */
	public function settings_section_intro() {
		?>
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
153 154
		<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>
155
		<?php
156 157 158

		// 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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		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;
175 176
	}

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

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

		if ( is_array( $options ) ) {
			foreach ( $options as $post_type => $to_keep ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
200 201 202
				$type_length = strlen( $to_keep );

				if ( 0 === $type_length ) {
Erick Hitter's avatar
Erick Hitter committed
203
					$to_keep = -1;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
204 205 206
				} else {
					$to_keep = (int) $to_keep;
				}
207

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

Erick Hitter's avatar
Erick Hitter committed
213 214 215 216 217 218 219
				$options_sanitized[ $post_type ] = $to_keep;
			}
		}

		return $options_sanitized;
	}

220
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
221 222
	 * REVISIONS QUANTITY OVERRIDES.
	 */
223

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
	/**
	 * 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
240
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
241
	 * Override number of revisions to keep using plugin's settings.
Erick Hitter's avatar
Erick Hitter committed
242
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
243
	 * Can either be post-specific or universal.
244
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
245 246 247
	 * @param int     $qty  Number of revisions to keep.
	 * @param WP_Post $post Post object.
	 * @return int
Erick Hitter's avatar
Erick Hitter committed
248 249
	 */
	public function filter_wp_revisions_to_keep( $qty, $post ) {
250 251 252 253 254 255
		$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
256
			$settings  = $this->get_settings();
Erick Hitter's avatar
Erick Hitter committed
257

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
258
			if ( array_key_exists( $post_type, $settings ) ) {
259
				$qty = $settings[ $post_type ];
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
260
			}
261
		}
Erick Hitter's avatar
Erick Hitter committed
262 263 264 265

		return $qty;
	}

266
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
267 268
	 * POST-LEVEL FUNCTIONALITY.
	 */
269 270

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
271
	 * Override Core's revisions metabox.
272
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
273 274
	 * @param string $post_type Post type.
	 * @param object $post      Post object.
275 276
	 */
	public function action_add_meta_boxes( $post_type, $post ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
277
		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
278
			// Replace the metabox.
279
			remove_meta_box( 'revisionsdiv', null, 'normal' );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293
			add_meta_box(
				'revisionsdiv-wp-rev-ctl',
				__(
					'Revisions',
					'wp_revisions_control'
				),
				array(
					$this,
					'revisions_meta_box',
				),
				null,
				'normal',
				'core'
			);
294

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
295
			// A bit of JS for us.
296
			$handle = 'wp-revisions-control-post';
Erick Hitter's avatar
Erick Hitter committed
297
			wp_enqueue_script( $handle, plugins_url( 'js/post.js', __DIR__ ), array( 'jquery' ), '20131205', true );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
298 299 300 301 302 303 304 305 306 307 308 309 310
			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' ),
				)
			);
311

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
312
			// Add some styling to our metabox additions.
313
			add_action( 'admin_head', array( $this, 'action_admin_head' ), 999 );
314
		}
315 316 317
	}

	/**
318
	 * Render Revisions metabox with plugin's additions
319
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
320
	 * @param WP_Post $post Post object.
321 322 323 324 325 326 327
	 */
	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>
328

329 330 331
			<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
332 333 334 335 336 337 338 339 340 341
				<?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" />'
				);
				?>
342 343 344

				<?php wp_nonce_field( $this->settings_section . '_limit', $this->settings_section . '_limit_nonce', false ); ?>
			</p>
345 346 347 348
		</div><!-- #<?php echo esc_attr( $this->settings_section ); ?> -->
		<?php
	}

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

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

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
358 359
		// Check for necessary data and capabilities.
		if ( ! $post_id ) {
360
			$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
361
		} elseif ( ! check_ajax_referer( $this->settings_section . '_purge', 'nonce', false ) ) {
362
			$response['error'] = __( 'Invalid request. Please refresh the page and try again.', 'wp_revisions_control' );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
363
		} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
364
			$response['error'] = __( 'You are not allowed to edit this post.', 'wp_revisions_control' );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
365
		}
366

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
367
		// Request is valid if $response is still empty, as no errors arose above.
368
		if ( empty( $response ) ) {
369 370
			$response = $this->do_purge_all( $post_id );
		}
371

372 373 374 375
		// Pass the response back to JS.
		echo json_encode( $response );
		exit;
	}
376

377 378 379 380 381 382 383 384
	/**
	 * Remove all revisions from a given post ID.
	 *
	 * @param int $post_id Post ID to purge of revisions.
	 * @return array
	 */
	public function do_purge_all( $post_id ) {
		$response = array();
385

386 387 388
		$revisions = wp_get_post_revisions( $post_id );

		$count = count( $revisions );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
389

390 391
		foreach ( $revisions as $revision ) {
			wp_delete_post_revision( $revision->ID );
392 393
		}

394 395 396 397 398 399 400 401 402 403 404 405
		$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 )
		);

		$response['count'] = $count;

		return $response;
406 407
	}

408
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
409
	 * Sanitize and store post-specifiy revisions quantity.
410
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
411
	 * @param int $post_id Post ID.
412 413
	 */
	public function action_save_post( $post_id ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
414 415 416 417 418
		$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 ];
419

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
420
			if ( -1 === $limit || empty( $limit ) ) {
421
				delete_post_meta( $post_id, $this->meta_key_limit );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
422
			} else {
423
				update_post_meta( $post_id, $this->meta_key_limit, absint( $limit ) );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
424
			}
425 426 427
		}
	}

428
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
429
	 * Add a border between the regular revisions list and this plugin's additions.
430 431
	 */
	public function action_admin_head() {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
432
		?>
433 434
		<style type="text/css">
			#revisionsdiv-wp-rev-ctl #<?php echo esc_attr( $this->settings_section ); ?> {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
435 436 437
				border-top: 1px solid #dfdfdf;
				padding-top: 0;
				margin-top: 20px;
438 439 440 441 442 443 444 445
			}

			#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
446
		<?php
447 448
	}

449
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
450 451
	 * PLUGIN UTILITIES.
	 */
452

Erick Hitter's avatar
Erick Hitter committed
453
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
454
	 * Retrieve plugin settings.
Erick Hitter's avatar
Erick Hitter committed
455
	 *
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
456
	 * @return array
Erick Hitter's avatar
Erick Hitter committed
457 458
	 */
	private function get_settings() {
459 460
		if ( empty( self::$settings ) ) {
			$post_types = $this->get_post_types();
Erick Hitter's avatar
Erick Hitter committed
461

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

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
464 465 466 467
			if ( ! is_array( $settings ) ) {
				$settings = array();
			}

468
			$merged_settings = array();
Erick Hitter's avatar
Erick Hitter committed
469

470
			foreach ( $post_types as $post_type => $name ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
471
				if ( array_key_exists( $post_type, $settings ) ) {
472
					$merged_settings[ $post_type ] = (int) $settings[ $post_type ];
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
473 474 475
				} else {
					$merged_settings[ $post_type ] = - 1;
				}
476 477 478
			}

			self::$settings = $merged_settings;
Erick Hitter's avatar
Erick Hitter committed
479 480
		}

481
		return self::$settings;
482 483 484
	}

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
485
	 * Retrieve array of supported post types and their labels.
486
	 *
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
487
	 * @return array
488 489 490
	 */
	private function get_post_types() {
		if ( empty( self::$post_types ) ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
491
			foreach ( get_post_types() as $type ) {
492 493 494
				if ( post_type_supports( $type, 'revisions' ) ) {
					$object = get_post_type_object( $type );

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
495 496 497 498 499
					if ( null === $object ) {
						continue;
					}

					if ( property_exists( $object, 'labels' ) && property_exists( $object->labels, 'name' ) ) {
500
						$name = $object->labels->name;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
501
					} else {
502
						$name = $object->name;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
503
					}
504 505 506 507 508

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

510
		return self::$post_types;
Erick Hitter's avatar
Erick Hitter committed
511
	}
Erick Hitter's avatar
Erick Hitter committed
512 513

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
514
	 * Retrieve number of revisions to keep for a given post type.
Erick Hitter's avatar
Erick Hitter committed
515
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
516 517 518
	 * @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
519 520
	 */
	private function get_revisions_to_keep( $post_type, $blank_for_all = false ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
521
		// wp_revisions_to_keep() accepts a post object, not just the post type.
Erick Hitter's avatar
Erick Hitter committed
522
		// 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
523
		$_post   = new WP_Post( (object) array( 'post_type' => $post_type ) );
Erick Hitter's avatar
Erick Hitter committed
524 525
		$to_keep = wp_revisions_to_keep( $_post );

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
526
		if ( $blank_for_all && -1 === $to_keep ) {
Erick Hitter's avatar
Erick Hitter committed
527
			return '';
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
528
		} else {
Erick Hitter's avatar
Erick Hitter committed
529
			return (int) $to_keep;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
530
		}
Erick Hitter's avatar
Erick Hitter committed
531
	}
532 533

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
534
	 * Retrieve number of revisions to keep for a give post.
535
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
536 537
	 * @param int $post_id Post ID.
	 * @return int|string
538 539 540 541
	 */
	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
542
		if ( -1 === $to_keep || empty( $to_keep ) ) {
543
			$to_keep = '';
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
544
		} else {
545
			$to_keep = (int) $to_keep;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
546
		}
547 548 549

		return $to_keep;
	}
Erick Hitter's avatar
Erick Hitter committed
550
}
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
551

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