Skip to content
Snippets Groups Projects

Unit tests and coding standards

Merged Erick Hitter requested to merge fix/2-phpcs-phpunit into master
Files
4
@@ -10,22 +10,65 @@
*/
class WP_Revisions_Control {
/**
* Singleton
* Singleton.
*
* @var static
*/
private static $__instance;
/**
* Filter priority.
*
* @see $this->filter_priority()
*
* @var int
*/
private static $__instance = null;
private static $priority = null;
/**
* Class variables
* Default filter priority.
*
* @var int
*/
private static $priority = null; // use $this->plugin_priority()
private $priority_default = 50;
private static $post_types = array(); // use $this->get_post_types()
private static $settings = array(); // use $this->get_settings()
/**
* 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';
/**
@@ -34,26 +77,22 @@ class WP_Revisions_Control {
private function __construct() {}
/**
* Singleton implementation
* Singleton implementation.
*
* @uses self::setup
* @return object
* @return static
*/
public static function get_instance() {
if ( ! is_a( self::$__instance, __CLASS__ ) ) {
self::$__instance = new self;
if ( ! is_a( static::$__instance, __CLASS__ ) ) {
static::$__instance = new self();
self::$__instance->setup();
static::$__instance->setup();
}
return self::$__instance;
return static::$__instance;
}
/**
* Register actions and filters at `init` so others can interact, if desired.
*
* @uses add_action
* @return null
*/
private function setup() {
add_action( 'plugins_loaded', array( $this, 'action_plugins_loaded' ) );
@@ -61,24 +100,14 @@ class WP_Revisions_Control {
}
/**
* Load plugin translations
*
* @uses load_plugin_textdomain
* @uses plugin_basename
* @action plugins_loaded
* @return null
* Load plugin translations.
*/
public function action_plugins_loaded() {
load_plugin_textdomain( 'wp_revisions_control', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Register actions and filters
*
* @uses add_action
* @uses add_filter
* @uses this::plugin_priority
* @return null
* Register actions and filters.
*/
public function action_init() {
add_action( 'admin_init', array( $this, 'action_admin_init' ) );
@@ -87,20 +116,12 @@ class WP_Revisions_Control {
}
/**
* Register plugin's admin-specific elements
* Register plugin's admin-specific elements.
*
* Plugin title is intentionally not translatable.
*
* @uses register_setting
* @uses add_settings_section
* @uses __
* @uses this::get_post_types
* @uses add_settings_field
* @action admin_init
* @return null
*/
public function action_admin_init() {
// Plugin setting section
// 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 );
@@ -109,44 +130,50 @@ class WP_Revisions_Control {
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
// 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' ) );
}
/**
** PLUGIN SETTINGS SECTION
** FOUND UNDER SETTINGS > WRITING
**/
* PLUGIN SETTINGS SECTION
* FOUND UNDER SETTINGS > WRITING
*/
/**
* Display assistive text in settings section
*
* @uses _e
* @uses this::plugin_priority
* @return string
* Display assistive text in settings section.
*/
public function settings_section_intro() {
?>
<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>
<p><?php _e( "If a post type isn't listed, revisions are not enabled for that post type.", 'wp_revisions_control' ); ?></p>
<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>
<?php
// Display a note if the plugin priority is other than the default.
// Will be useful when debugging issues later.
if ( $this->plugin_priority() !== $this->priority_default ) : ?>
<p><?php printf( __( "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 %s filter.", 'wp_revisions_control' ), '<code>wp_revisions_control_priority</code>' ); ?></p>
<?php endif;
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;
}
/**
* Render field for each post type
* Render field for each post type.
*
* @param array $args
* @uses this::get_revisions_to_keep
* @uses esc_attr
* @return string
* @param array $args Field arguments.
*/
public function field_post_type( $args ) {
$revisions_to_keep = $this->get_revisions_to_keep( $args['post_type'], true );
@@ -156,9 +183,9 @@ class WP_Revisions_Control {
}
/**
* Sanitize plugin settings
* Sanitize plugin settings.
*
* @param array $options
* @param array $options Unsanitized settings.
* @return array
*/
public function sanitize_options( $options ) {
@@ -166,14 +193,18 @@ class WP_Revisions_Control {
if ( is_array( $options ) ) {
foreach ( $options as $post_type => $to_keep ) {
if ( 0 === strlen( $to_keep ) )
$type_length = strlen( $to_keep );
if ( 0 === $type_length ) {
$to_keep = -1;
else
$to_keep = intval( $to_keep );
} else {
$to_keep = (int) $to_keep;
}
// Lowest possible value is -1, used to indicate infinite revisions are stored
if ( -1 > $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;
}
@@ -183,8 +214,8 @@ class WP_Revisions_Control {
}
/**
** REVISIONS QUANTITY OVERRIDES
**/
* REVISIONS QUANTITY OVERRIDES.
*/
/**
* Allow others to change the priority this plugin's functionality runs at
@@ -203,15 +234,13 @@ class WP_Revisions_Control {
}
/**
* Override number of revisions to keep using plugin's settings
* Override number of revisions to keep using plugin's settings.
*
* Can either be post-specific or universal
* Can either be post-specific or universal.
*
* @uses get_post_meta
* @uses get_post_type
* @uses this::get_settings
* @filter wp_revisions_to_keep
* @return mixed
* @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 );
@@ -220,57 +249,63 @@ class WP_Revisions_Control {
$qty = $post_limit;
} else {
$post_type = get_post_type( $post ) ? get_post_type( $post ) : $post->post_type;
$settings = $this->get_settings();
$settings = $this->get_settings();
if ( array_key_exists( $post_type, $settings ) )
if ( array_key_exists( $post_type, $settings ) ) {
$qty = $settings[ $post_type ];
}
}
return $qty;
}
/**
** POST-LEVEL FUNCTIONALITY
**/
* POST-LEVEL FUNCTIONALITY.
*/
/**
* Override Core's revisions metabox
* Override Core's revisions metabox.
*
* @param string $post_type
* @param object $post
* @uses post_type_supports
* @uses get_post_status
* @uses wp_get_post_revisions
* @uses remove_meta_box
* @uses add_meta_box
* @uses wp_enqueue_script
* @uses plugins_url
* @uses wp_localize_script
* @uses wpautop
* @uses add_action
* @action add_meta_boxes
* @return null
* @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
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
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', __FILE__ ), array( 'jquery' ), '20131205', true );
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' )
) );
// Add some styling to our metabox additions
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&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' ),
)
);
// Add some styling to our metabox additions.
add_action( 'admin_head', array( $this, 'action_admin_head' ), 999 );
}
}
@@ -278,13 +313,7 @@ class WP_Revisions_Control {
/**
* Render Revisions metabox with plugin's additions
*
* @uses post_revisions_meta_box
* @uses the_ID
* @uses esc_attr
* @uses wp_create_nonce
* @uses this::get_post_revisions_to_keep
* @uses wp_nonce_field
* @return string
* @param WP_Post $post Post object.
*/
public function revisions_meta_box( $post ) {
post_revisions_meta_box( $post );
@@ -296,7 +325,16 @@ class WP_Revisions_Control {
<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>
<?php printf( __( 'Limit this post to %s revisions. Leave this field blank for default behavior.', 'wp_revisions_control' ), '<input type="text" name="' . $this->settings_section . '_qty" value="' . $this->get_post_revisions_to_keep( $post->ID ) . '" id="' . $this->settings_section . '_qty" size="2" />' ); ?>
<?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" />'
);
?>
<?php wp_nonce_field( $this->settings_section . '_limit', $this->settings_section . '_limit_nonce', false ); ?>
</p>
@@ -305,81 +343,94 @@ class WP_Revisions_Control {
}
/**
* Process a post-specific request to purge revisions
*
* @uses __
* @uses check_ajax_referer
* @uses current_user_can
* @uses wp_get_post_revisions
* @uses number_format_i18n
* @return string
* Process a post-specific request to purge revisions.
*/
public function ajax_purge() {
$post_id = isset( $_REQUEST['post_id'] ) ? (int) $_REQUEST['post_id'] : false;
// Hold the current state of this Ajax request
// Hold the current state of this Ajax request.
$response = array();
// Check for necessary data and capabilities
if ( ! $post_id )
// Check for necessary data and capabilities.
if ( ! $post_id ) {
$response['error'] = __( 'No post ID was provided. Please refresh the page and try again.', 'wp_revisions_control' );
elseif ( ! check_ajax_referer( $this->settings_section . '_purge', 'nonce', false ) )
} elseif ( ! check_ajax_referer( $this->settings_section . '_purge', 'nonce', false ) ) {
$response['error'] = __( 'Invalid request. Please refresh the page and try again.', 'wp_revisions_control' );
elseif ( ! current_user_can( 'edit_post', $post_id ) )
} elseif ( ! current_user_can( 'edit_post', $post_id ) ) {
$response['error'] = __( 'You are not allowed to edit this post.', 'wp_revisions_control' );
}
// Request is valid if $response is still empty, as no errors arose above
// Request is valid if $response is still empty, as no errors arose above.
if ( empty( $response ) ) {
$revisions = wp_get_post_revisions( $post_id );
$response = $this->do_purge_all( $post_id );
}
$count = count( $revisions );
// Pass the response back to JS.
echo json_encode( $response );
exit;
}
foreach ( $revisions as $revision ) {
wp_delete_post_revision( $revision->ID );
}
/**
* 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();
$revisions = wp_get_post_revisions( $post_id );
$response['success'] = sprintf( __( 'Removed %s revisions associated with this post.', 'wp_revisions_control' ), number_format_i18n( $count, 0 ) );
$response['count'] = $count;
$count = count( $revisions );
foreach ( $revisions as $revision ) {
wp_delete_post_revision( $revision->ID );
}
// Pass the response back to JS
echo json_encode( $response );
exit;
$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;
}
/**
* Sanitize and store post-specifiy revisions quantity
* Sanitize and store post-specifiy revisions quantity.
*
* @uses wp_verify_nonce
* @uses update_post_meta
* @action save_post
* @return null
* @param int $post_id Post ID.
*/
public function action_save_post( $post_id ) {
if ( isset( $_POST[ $this->settings_section . '_limit_nonce' ] ) && wp_verify_nonce( $_POST[ $this->settings_section . '_limit_nonce' ], $this->settings_section . '_limit' ) && isset( $_POST[ $this->settings_section . '_qty' ] ) ) {
$limit = $_POST[ $this->settings_section . '_qty' ];
$nonce = $this->settings_section . '_limit_nonce';
$qty = $this->settings_section . '_qty';
if ( -1 == $limit || empty( $limit ) )
if ( isset( $_POST[ $nonce ], $_POST[ $qty ] ) && wp_verify_nonce( sanitize_text_field( $_POST[ $nonce ] ), $this->settings_section . '_limit' ) ) {
$limit = (int) $_POST[ $qty ];
if ( -1 === $limit || empty( $limit ) ) {
delete_post_meta( $post_id, $this->meta_key_limit );
else
} else {
update_post_meta( $post_id, $this->meta_key_limit, absint( $limit ) );
}
}
}
/**
* Add a border between the regular revisions list and this plugin's additions
*
* @uses esc_attr
* @action admin_head
* @return string
* Add a border between the regular revisions list and this plugin's additions.
*/
public function action_admin_head() {
?>
?>
<style type="text/css">
#revisionsdiv-wp-rev-ctl #<?php echo esc_attr( $this->settings_section ); ?> {
border-top: 1px solid #dfdfdf;
padding-top: 0;
margin-top: 20px;
border-top: 1px solid #dfdfdf;
padding-top: 0;
margin-top: 20px;
}
#revisionsdiv-wp-rev-ctl #<?php echo esc_attr( $this->settings_section ); ?> h4 {
@@ -388,18 +439,16 @@ class WP_Revisions_Control {
margin-top: 0;
}
</style>
<?php
<?php
}
/**
** PLUGIN UTILITIES
**/
* PLUGIN UTILITIES.
*/
/**
* Retrieve plugin settings
* Retrieve plugin settings.
*
* @uses this::get_post_types
* @uses get_option
* @return array
*/
private function get_settings() {
@@ -408,13 +457,18 @@ class WP_Revisions_Control {
$settings = get_option( $this->settings_section, array() );
if ( ! is_array( $settings ) ) {
$settings = array();
}
$merged_settings = array();
foreach ( $post_types as $post_type => $name ) {
if ( array_key_exists( $post_type, $settings ) )
if ( array_key_exists( $post_type, $settings ) ) {
$merged_settings[ $post_type ] = (int) $settings[ $post_type ];
else
$merged_settings[ $post_type ] = -1;
} else {
$merged_settings[ $post_type ] = - 1;
}
}
self::$settings = $merged_settings;
@@ -424,25 +478,25 @@ class WP_Revisions_Control {
}
/**
* Retrieve array of supported post types and their labels
* Retrieve array of supported post types and their labels.
*
* @uses get_post_types
* @uses post_type_supports
* @uses get_post_type_object
* @return array
*/
private function get_post_types() {
if ( empty( self::$post_types ) ) {
$types = get_post_types();
foreach ( $types as $type ) {
foreach ( get_post_types() as $type ) {
if ( post_type_supports( $type, 'revisions' ) ) {
$object = get_post_type_object( $type );
if ( property_exists( $object, 'labels' ) && property_exists( $object->labels, 'name' ) )
if ( null === $object ) {
continue;
}
if ( property_exists( $object, 'labels' ) && property_exists( $object->labels, 'name' ) ) {
$name = $object->labels->name;
else
} else {
$name = $object->name;
}
self::$post_types[ $type ] = $name;
}
@@ -453,40 +507,42 @@ class WP_Revisions_Control {
}
/**
* Retrieve number of revisions to keep for a given post type
* Retrieve number of revisions to keep for a given post type.
*
* @uses WP_Post
* @uses wp_revisions_to_keep
* @return mixed
* @param string $post_type Post type.
* @param bool $blank_for_all Should blank value be used to indicate all are kept.
* @return int|string
*/
private function get_revisions_to_keep( $post_type, $blank_for_all = false ) {
// wp_revisions_to_keep() accepts a post object, not just the post type
// wp_revisions_to_keep() accepts a post object, not just the post type.
// 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.
$_post = new WP_Post( (object) array( 'post_type' => $post_type ) );
$_post = new WP_Post( (object) array( 'post_type' => $post_type ) );
$to_keep = wp_revisions_to_keep( $_post );
if ( $blank_for_all && -1 == $to_keep )
if ( $blank_for_all && -1 === $to_keep ) {
return '';
else
} else {
return (int) $to_keep;
}
}
/**
* Retrieve number of revisions to keep for a give post
* Retrieve number of revisions to keep for a give post.
*
* @param int $post_id
* @uses get_post_meta
* @return mixed
* @param int $post_id Post ID.
* @return int|string
*/
private function get_post_revisions_to_keep( $post_id ) {
$to_keep = get_post_meta( $post_id, $this->meta_key_limit, true );
if ( -1 == $to_keep || empty( $to_keep ) )
if ( -1 === $to_keep || empty( $to_keep ) ) {
$to_keep = '';
else
} else {
$to_keep = (int) $to_keep;
}
return $to_keep;
}
}
WP_Revisions_Control::get_instance();
Loading