filter_priority() * * @var int */ private static $priority = null; /** * Default filter priority. * * @var int */ private $priority_default = 50; /** * 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'; /** * Name of custom settings sections. * * @var string */ private $settings_section = 'wp_revisions_control'; /** * Meta key holding post's revisions limit. * * @var string */ private $meta_key_limit = '_wp_rev_ctl_limit'; /** * Silence is golden! */ private function __construct() {} /** * Singleton implementation. * * @return static */ public static function get_instance() { if ( ! is_a( static::$__instance, __CLASS__ ) ) { static::$__instance = new self(); static::$__instance->setup(); } return static::$__instance; } /** * Register actions and filters at `init` so others can interact, if desired. */ private function setup() { add_action( 'plugins_loaded', array( $this, 'action_plugins_loaded' ) ); add_action( 'init', array( $this, 'action_init' ) ); } /** * Load plugin translations. */ public function action_plugins_loaded() { load_plugin_textdomain( 'wp_revisions_control', false, dirname( dirname( plugin_basename( __FILE__ ) ) ) . '/languages/' ); } /** * Register actions and filters. */ public function action_init() { add_action( 'admin_init', array( $this, 'action_admin_init' ) ); add_filter( 'wp_revisions_to_keep', array( $this, 'filter_wp_revisions_to_keep' ), $this->plugin_priority(), 2 ); } /** * Register plugin's admin-specific elements. * * Plugin title is intentionally not translatable. */ public function action_admin_init() { $post_types = $this->get_post_types(); // Plugin setting section. 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 ); foreach ( $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 ) ); } // Post-level functionality. 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( 'save_post', array( $this, 'action_save_post' ) ); // Bulk actions. WP_Revisions_Control_Bulk_Actions::get_instance( $post_types ); } /** * PLUGIN SETTINGS SECTION * FOUND UNDER SETTINGS > WRITING */ /** * Display assistive text in settings section. */ public function settings_section_intro() { ?>
plugin_priority() !== $this->priority_default ) : ?>wp_revisions_control_priority' ); ?>
get_revisions_to_keep( $args['post_type'], true ); ?> $to_keep ) { $type_length = strlen( $to_keep ); if ( 0 === $type_length ) { $to_keep = -1; } else { $to_keep = (int) $to_keep; } // Lowest possible value is -1, used to indicate infinite revisions are stored. if ( -1 > $to_keep ) { $to_keep = -1; } $options_sanitized[ $post_type ] = $to_keep; } } return $options_sanitized; } /** * REVISIONS QUANTITY OVERRIDES. */ /** * 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; } /** * Override number of revisions to keep using plugin's settings. * * Can either be post-specific or universal. * * @param int $qty Number of revisions to keep. * @param WP_Post $post Post object. * @return int */ public function filter_wp_revisions_to_keep( $qty, $post ) { $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; $settings = $this->get_settings(); if ( array_key_exists( $post_type, $settings ) ) { $qty = $settings[ $post_type ]; } } return $qty; } /** * POST-LEVEL FUNCTIONALITY. */ /** * Override Core's revisions metabox. * * @param string $post_type Post type. * @param object $post Post object. */ 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 ) { // Replace the metabox. 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' ); // A bit of JS for us. $handle = 'wp-revisions-control-post'; wp_enqueue_script( $handle, plugins_url( 'js/post.js', __DIR__ ), array( 'jquery' ), '20131205', true ); wp_localize_script( $handle, $this->settings_section, array( 'namespace' => $this->settings_section, 'action_base' => $this->settings_section, 'processing_text' => __( 'Processing…', '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' ), ) ); // Add some styling to our metabox additions. add_action( 'admin_head', array( $this, 'action_admin_head' ), 999 ); } } /** * Render Revisions metabox with plugin's additions * * @param WP_Post $post Post object. */ public function revisions_meta_box( $post ) { post_revisions_meta_box( $post ); ?>settings_section ) . '_qty" value="' . esc_attr( $this->get_post_revisions_to_keep( $post->ID ) ) . '" id="' . esc_attr( $this->settings_section ) . '_qty" size="2" />' ); ?> settings_section . '_limit', $this->settings_section . '_limit_nonce', false ); ?>