class-wp-revisions-control.php 15.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
Erick Hitter committed
128 129
		$post_types = $this->get_post_types();

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
130
		// Plugin setting section.
Erick Hitter's avatar
Erick Hitter committed
131
		register_setting( $this->settings_page, $this->settings_section, array( $this, 'sanitize_options' ) );
132

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

Erick Hitter's avatar
Erick Hitter committed
135
		foreach ( $post_types as $post_type => $name ) {
136 137
			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 ) );
		}
138

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

		// Bulk actions.
		new WP_Revisions_Control_Bulk_Actions( $post_types );
146 147
	}

148
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
149 150 151
	 * PLUGIN SETTINGS SECTION
	 * FOUND UNDER SETTINGS > WRITING
	 */
152

153
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
154
	 * Display assistive text in settings section.
155 156 157
	 */
	public function settings_section_intro() {
		?>
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
158 159
		<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>
160
		<?php
161 162 163

		// 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
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
		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;
180 181
	}

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

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
195
	 * Sanitize plugin settings.
196
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
197
	 * @param array $options Unsanitized settings.
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
198
	 * @return array
199 200
	 */
	public function sanitize_options( $options ) {
Erick Hitter's avatar
Erick Hitter committed
201 202 203 204
		$options_sanitized = array();

		if ( is_array( $options ) ) {
			foreach ( $options as $post_type => $to_keep ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
205 206 207
				$type_length = strlen( $to_keep );

				if ( 0 === $type_length ) {
Erick Hitter's avatar
Erick Hitter committed
208
					$to_keep = -1;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
209 210 211
				} else {
					$to_keep = (int) $to_keep;
				}
212

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
213 214
				// Lowest possible value is -1, used to indicate infinite revisions are stored.
				if ( -1 > $to_keep ) {
215
					$to_keep = -1;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
216
				}
217

Erick Hitter's avatar
Erick Hitter committed
218 219 220 221 222 223 224
				$options_sanitized[ $post_type ] = $to_keep;
			}
		}

		return $options_sanitized;
	}

225
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
226 227
	 * REVISIONS QUANTITY OVERRIDES.
	 */
228

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

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
263
			if ( array_key_exists( $post_type, $settings ) ) {
264
				$qty = $settings[ $post_type ];
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
265
			}
266
		}
Erick Hitter's avatar
Erick Hitter committed
267 268 269 270

		return $qty;
	}

271
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
272 273
	 * POST-LEVEL FUNCTIONALITY.
	 */
274 275

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

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

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
317
			// Add some styling to our metabox additions.
318
			add_action( 'admin_head', array( $this, 'action_admin_head' ), 999 );
319
		}
320 321 322
	}

	/**
323
	 * Render Revisions metabox with plugin's additions
324
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
325
	 * @param WP_Post $post Post object.
326 327 328 329 330 331 332
	 */
	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>
333

334 335 336
			<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
337 338 339 340 341 342 343 344 345 346
				<?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" />'
				);
				?>
347 348 349

				<?php wp_nonce_field( $this->settings_section . '_limit', $this->settings_section . '_limit_nonce', false ); ?>
			</p>
350 351 352 353
		</div><!-- #<?php echo esc_attr( $this->settings_section ); ?> -->
		<?php
	}

354
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
355
	 * Process a post-specific request to purge revisions.
356 357 358 359
	 */
	public function ajax_purge() {
		$post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : false;

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

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

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
372
		// Request is valid if $response is still empty, as no errors arose above.
373
		if ( empty( $response ) ) {
374 375
			$response = $this->do_purge_all( $post_id );
		}
376

377 378 379 380
		// Pass the response back to JS.
		echo json_encode( $response );
		exit;
	}
381

382 383 384 385 386 387 388 389
	/**
	 * 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();
390

391 392 393
		$revisions = wp_get_post_revisions( $post_id );

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

395 396
		foreach ( $revisions as $revision ) {
			wp_delete_post_revision( $revision->ID );
397 398
		}

399 400 401 402 403 404 405 406 407 408 409 410
		$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;
411 412
	}

413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
	/**
	 * Remove any revisions in excess of a post's limit.
	 *
	 * @param int $post_id Post ID to purge of excess revisions.
	 * @return array
	 */
	public function do_purge_excess( $post_id ) {
		$response = array(
			'count' => 0,
		);

		$to_keep = wp_revisions_to_keep( get_post( $post_id ) );

		if ( $to_keep < 0 ) {
			$response['success'] = __(
				'No revisions to remove.',
				'wp_revisions_control'
			);

			return $response;
		}

		$revisions      = wp_get_post_revisions( $post_id );
		$starting_count = count( $revisions );

		if ( $starting_count <= $to_keep ) {
			$response['success'] = __(
				'No revisions to remove.',
				'wp_revisions_control'
			);

			return $response;
		}

		$to_remove         = array_slice( $revisions, $to_keep, null, true );
		$response['count'] = count( $to_remove );

		foreach ( $to_remove as $revision ) {
			wp_delete_post_revision( $revision->ID );
		}

		return $response;
	}

457
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
458
	 * Sanitize and store post-specifiy revisions quantity.
459
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
460
	 * @param int $post_id Post ID.
461 462
	 */
	public function action_save_post( $post_id ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
463 464 465 466 467
		$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 ];
468

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
469
			if ( -1 === $limit || empty( $limit ) ) {
470
				delete_post_meta( $post_id, $this->meta_key_limit );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
471
			} else {
472
				update_post_meta( $post_id, $this->meta_key_limit, absint( $limit ) );
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
473
			}
474 475 476
		}
	}

477
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
478
	 * Add a border between the regular revisions list and this plugin's additions.
479 480
	 */
	public function action_admin_head() {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
481
		?>
482 483
		<style type="text/css">
			#revisionsdiv-wp-rev-ctl #<?php echo esc_attr( $this->settings_section ); ?> {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
484 485 486
				border-top: 1px solid #dfdfdf;
				padding-top: 0;
				margin-top: 20px;
487 488 489 490 491 492 493 494
			}

			#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
495
		<?php
496 497
	}

498
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
499 500
	 * PLUGIN UTILITIES.
	 */
501

Erick Hitter's avatar
Erick Hitter committed
502
	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
503
	 * Retrieve plugin settings.
Erick Hitter's avatar
Erick Hitter committed
504
	 *
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
505
	 * @return array
Erick Hitter's avatar
Erick Hitter committed
506 507
	 */
	private function get_settings() {
508 509
		if ( empty( self::$settings ) ) {
			$post_types = $this->get_post_types();
Erick Hitter's avatar
Erick Hitter committed
510

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

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
513 514 515 516
			if ( ! is_array( $settings ) ) {
				$settings = array();
			}

517
			$merged_settings = array();
Erick Hitter's avatar
Erick Hitter committed
518

519
			foreach ( $post_types as $post_type => $name ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
520
				if ( array_key_exists( $post_type, $settings ) ) {
521
					$merged_settings[ $post_type ] = (int) $settings[ $post_type ];
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
522 523 524
				} else {
					$merged_settings[ $post_type ] = - 1;
				}
525 526 527
			}

			self::$settings = $merged_settings;
Erick Hitter's avatar
Erick Hitter committed
528 529
		}

530
		return self::$settings;
531 532 533
	}

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
534
	 * Retrieve array of supported post types and their labels.
535
	 *
Erick Hitter's avatar
PHPDoc  
Erick Hitter committed
536
	 * @return array
537 538 539
	 */
	private function get_post_types() {
		if ( empty( self::$post_types ) ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
540
			foreach ( get_post_types() as $type ) {
541 542 543
				if ( post_type_supports( $type, 'revisions' ) ) {
					$object = get_post_type_object( $type );

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
544 545 546 547 548
					if ( null === $object ) {
						continue;
					}

					if ( property_exists( $object, 'labels' ) && property_exists( $object->labels, 'name' ) ) {
549
						$name = $object->labels->name;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
550
					} else {
551
						$name = $object->name;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
552
					}
553 554 555 556 557

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

559
		return self::$post_types;
Erick Hitter's avatar
Erick Hitter committed
560
	}
Erick Hitter's avatar
Erick Hitter committed
561 562

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
563
	 * Retrieve number of revisions to keep for a given post type.
Erick Hitter's avatar
Erick Hitter committed
564
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
565 566 567
	 * @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
568 569
	 */
	private function get_revisions_to_keep( $post_type, $blank_for_all = false ) {
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
570
		// wp_revisions_to_keep() accepts a post object, not just the post type.
Erick Hitter's avatar
Erick Hitter committed
571
		// 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
572
		$_post   = new WP_Post( (object) array( 'post_type' => $post_type ) );
Erick Hitter's avatar
Erick Hitter committed
573 574
		$to_keep = wp_revisions_to_keep( $_post );

Erick Hitter's avatar
PHPCS  
Erick Hitter committed
575
		if ( $blank_for_all && -1 === $to_keep ) {
Erick Hitter's avatar
Erick Hitter committed
576
			return '';
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
577
		} else {
Erick Hitter's avatar
Erick Hitter committed
578
			return (int) $to_keep;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
579
		}
Erick Hitter's avatar
Erick Hitter committed
580
	}
581 582

	/**
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
583
	 * Retrieve number of revisions to keep for a give post.
584
	 *
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
585 586
	 * @param int $post_id Post ID.
	 * @return int|string
587 588 589 590
	 */
	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
591
		if ( -1 === $to_keep || empty( $to_keep ) ) {
592
			$to_keep = '';
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
593
		} else {
594
			$to_keep = (int) $to_keep;
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
595
		}
596 597 598

		return $to_keep;
	}
Erick Hitter's avatar
Erick Hitter committed
599
}
Erick Hitter's avatar
PHPCS  
Erick Hitter committed
600

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