Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display custom statuses in post states #579

Merged
merged 7 commits into from
Jan 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 26 additions & 59 deletions modules/custom-status/custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ function init() {
add_action( 'admin_notices', array( $this, 'no_js_notice' ) );
add_action( 'admin_print_scripts', array( $this, 'post_admin_header' ) );

// Add custom statuses to the post states.
add_filter( 'display_post_states', array( $this, 'add_status_to_post_states' ), 10, 2 );

// Methods for handling the actions of creating, making default, and deleting post stati
add_action( 'admin_init', array( $this, 'handle_add_custom_status' ) );
add_action( 'admin_init', array( $this, 'handle_edit_custom_status' ) );
Expand All @@ -100,15 +103,6 @@ function init() {
add_action( 'wp_ajax_update_status_positions', array( $this, 'handle_ajax_update_status_positions' ) );
add_action( 'wp_ajax_inline_save_status', array( $this, 'ajax_inline_save_status' ) );

// Hook to add the status column to Manage Posts

add_filter( 'manage_posts_columns', array( $this, '_filter_manage_posts_columns') );
add_action( 'manage_posts_custom_column', array( $this, '_filter_manage_posts_custom_column') );

// We need these for pages (http://core.trac.wordpress.org/browser/tags/3.3.1/wp-admin/includes/class-wp-posts-list-table.php#L283)
add_filter( 'manage_pages_columns', array( $this, '_filter_manage_posts_columns' ) );
add_action( 'manage_pages_custom_column', array( $this, '_filter_manage_posts_custom_column' ) );

// These seven-ish methods are hacks for fixing bugs in WordPress core
add_action( 'admin_init', array( $this, 'check_timestamp_on_publish' ) );
add_filter( 'wp_insert_post_data', array( $this, 'fix_custom_status_timestamp' ), 10, 2 );
Expand All @@ -125,9 +119,6 @@ function init() {

// Pagination for custom post statuses when previewing posts
add_filter( 'wp_link_pages_link', array( $this, 'modify_preview_link_pagination_url' ), 10, 2 );

// Filter through Post States and run a function to check if they are also a Status
add_filter( 'display_post_states', array( $this, 'check_if_post_state_is_status' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -315,7 +306,6 @@ function action_admin_enqueue_scripts() {
// Custom javascript to modify the post status dropdown where it shows up
if ( $this->is_whitelisted_page() ) {
wp_enqueue_script( 'edit_flow-custom_status', $this->module_url . 'lib/custom-status.js', array( 'jquery','post' ), EDIT_FLOW_VERSION, true );
wp_enqueue_style( 'edit_flow-custom_status', $this->module_url . 'lib/custom-status.css', false, EDIT_FLOW_VERSION, 'all' );
wp_localize_script('edit_flow-custom_status', '__ef_localize_custom_status', array(
'no_change' => esc_html__( "— No Change —", 'edit-flow' ),
'published' => esc_html__( 'Published', 'edit-flow' ),
Expand Down Expand Up @@ -705,59 +695,36 @@ function reassign_post_status( $old_status, $new_status = '' ) {
}

/**
* Insert new column header for post status after the title column
* Display our custom post statuses in post listings when needed.
*
* @param array $posts_columns Columns currently shown on the Edit Posts screen
* @return array Same array as the input array with a "status" column added after the "title" column
* @param array $post_states An array of post display states.
* @param WP_Post $post The current post object.
*
* @return array $post_states
*/
function _filter_manage_posts_columns( $posts_columns ) {
// Return immediately if the supplied parameter isn't an array (which shouldn't happen in practice?)
// http://wordpress.org/support/topic/plugin-edit-flow-bug-shows-2-drafts-when-there-are-none-leads-to-error-messages
if ( !is_array( $posts_columns ) )
return $posts_columns;

// Only do it for the post types this module is activated for
if ( !in_array( $this->get_current_post_type(), $this->get_post_types_for_module( $this->module ) ) )
return $posts_columns;

$result = array();
foreach ( $posts_columns as $key => $value ) {
if ($key == 'title') {
$result[$key] = $value;
$result['status'] = __('Status', 'edit-flow');
} else $result[$key] = $value;
public function add_status_to_post_states( $post_states, $post ) {
if ( ! in_array( $post->post_type, $this->get_post_types_for_module( $this->module ), true ) ) {
// Return early if this post type doesn't support custom statuses.
return $post_states;
}
return $result;

}
$post_status = get_post_status_object( get_post_status( $post->ID ) );

/**
* Adds a Post's status to its row on the Edit page
*
* @param string $column_name
**/
function _filter_manage_posts_custom_column( $column_name ) {

if ( $column_name == 'status' ) {
global $post;
$post_status_obj = get_post_status_object( get_post_status( $post->ID ) );
echo esc_html( $post_status_obj->label );
$filtered_status = isset( $_REQUEST['post_status'] ) ? $_REQUEST['post_status'] : '';
if ( $filtered_status === $post_status->name ) {
// No need to display the post status if a specific status was already requested.
return $post_states;
}
}
/**
* Check if Post State is a Status and display if it is not.
*
* @param array $post_states An array of post display states.
*/
function check_if_post_state_is_status( $post_states, $post ) {

$statuses = get_post_status_object( get_post_status( $post->ID ) );
foreach ( $post_states as $state ) {
if ( $state !== $statuses->label ) {
echo '<span class="show"></span>';
}
$statuses_to_ignore = array( 'future', 'trash', 'publish' );
if ( in_array( $post_status->name, $statuses_to_ignore, true ) ) {
// Let WP core handle these more gracefully.
return $post_states;
}


// Add the post status to display. Will also ensure the same status isn't shown twice.
$post_states[ $post_status->name ] = $post_status->label;

return $post_states;
}

Expand Down Expand Up @@ -1520,7 +1487,7 @@ public function fix_preview_link_part_three( $preview_link, $query_args ) {
}
}
}
return remove_query_arg( [ 'preview_nonce' ], $preview_link );
return remove_query_arg( array( 'preview_nonce' ), $preview_link );
}

/**
Expand Down
7 changes: 0 additions & 7 deletions modules/custom-status/lib/custom-status.css

This file was deleted.

67 changes: 35 additions & 32 deletions tests/test-edit-flow-custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class WP_Test_Edit_Flow_Custom_Status extends WP_UnitTestCase {
protected static $admin_user_id;
protected static $ef_custom_status;


public static function wpSetUpBeforeClass( $factory ) {
self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );

self::$ef_custom_status = new EF_Custom_Status();
self::$ef_custom_status->install();
self::$ef_custom_status->init();
Expand Down Expand Up @@ -151,7 +151,7 @@ function test_insert_post_pitch_post_date_gmt_unset() {

/**
* When a post_date is in the future check that post_date_gmt
* is not set when the status is not 'future'
* is not set when the status is not 'future'
*/
function test_insert_scheduled_post_gmt_set() {
$future_date = strftime( "%Y-%m-%d %H:%M:%S", strtotime('+1 day') );
Expand Down Expand Up @@ -209,9 +209,9 @@ function test_fix_sample_permalink_html_on_pitch_when_pretty_permalinks_are_disa
global $pagenow;
wp_set_current_user( self::$admin_user_id );

$p = self::factory()->post->create( array(
'post_status' => 'pitch',
'post_author' => self::$admin_user_id
$p = self::factory()->post->create( array(
'post_status' => 'pitch',
'post_author' => self::$admin_user_id,
) );

$pagenow = 'index.php';
Expand All @@ -232,8 +232,8 @@ function test_fix_sample_permalink_html_on_pitch_when_pretty_permalinks_are_enab

$this->set_permalink_structure( '/%postname%/' );

$p = self::factory()->post->create( array(
'post_status' => 'pending',
$p = self::factory()->post->create( array(
'post_status' => 'pending',
'post_name' => 'baz-صورة',
'post_author' => self::$admin_user_id
) );
Expand All @@ -256,10 +256,10 @@ function test_fix_sample_permalink_html_on_publish_when_pretty_permalinks_are_en
$this->set_permalink_structure( '/%postname%/' );

// Published posts should use published permalink
$p = self::factory()->post->create( array(
'post_status' => 'publish',
$p = self::factory()->post->create( array(
'post_status' => 'publish',
'post_name' => 'foo-صورة',
'post_author' => self::$admin_user_id
'post_author' => self::$admin_user_id,
) );

wp_set_current_user( self::$admin_user_id );
Expand Down Expand Up @@ -334,39 +334,42 @@ public function test_fix_get_sample_permalink_should_respect_hierarchy_of_publis
$this->assertSame( 'child-page', $actual[1] );
}

/**
* Validate the usage of $post in `check_if_post_state_is_status` hook with status shown
*/
public function test_check_if_post_state_is_status_shown() {
public function test_ensure_post_state_is_added() {
$post = self::factory()->post->create( array(
'post_type' => 'post',
'post_title' => 'Post',
'post_type' => 'post',
'post_title' => 'Post',
'post_status' => 'pitch',
'post_author' => self::$admin_user_id
) );

ob_start();
$post_states = apply_filters( 'display_post_states', [ 'Pitch', 'Liveblog' ], get_post( $post ) );
$output = ob_get_clean();
$post_states = apply_filters( 'display_post_states', array(), get_post( $post ) );
$this->assertArrayHasKey( 'pitch', $post_states );
}

public function test_ensure_post_state_is_skipped_for_unsupported_post_type() {
$post = self::factory()->post->create( array(
'post_type' => 'customposttype',
'post_title' => 'Post',
'post_status' => 'pitch',
'post_author' => self::$admin_user_id,
) );

$this->assertContains( '<span class="show"></span>', $output );
$post_states = apply_filters( 'display_post_states', array(), get_post( $post ) );
$this->assertFalse( array_key_exists( 'pitch', $post_states ) );
}

/**
* Validate the usage of $post in `check_if_post_state_is_status` hook with status not shown
*/
public function test_check_if_post_state_is_status_not_shown() {
public function test_ensure_post_state_is_skipped_when_filtered() {
$post = self::factory()->post->create( array(
'post_type' => 'post',
'post_title' => 'Post',
'post_type' => 'post',
'post_title' => 'Post',
'post_status' => 'pitch',
'post_author' => self::$admin_user_id
) );

ob_start();
$post_states = apply_filters( 'display_post_states', [ 'Pitch' ], get_post( $post ) );
$output = ob_get_clean();
// Act like the status has been filtered.
$_REQUEST['post_status'] = 'pitch';

$this->assertNotContains( '<span class="show"></span>', $output );
$post_states = apply_filters( 'display_post_states', array(), get_post( $post ) );
$this->assertFalse( array_key_exists( 'pitch', $post_states ) );
}
}
}