Skip to content
Closed
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
72 changes: 14 additions & 58 deletions modules/custom-status/custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,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 @@ -127,7 +118,7 @@ function init() {
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 );
add_filter( 'display_post_states', array( $this, 'display_actual_post_state' ), 10, 2 );
}

/**
Expand Down Expand Up @@ -705,60 +696,25 @@ function reassign_post_status( $old_status, $new_status = '' ) {
}

/**
* Insert new column header for post status after the title column
*
* @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
*/
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;
}
return $result;

}

/**
* 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 );
}
}
/**
* Check if Post State is a Status and display if it is not.
* Display the actual post state where needed
*
* @param array $post_states An array of post display states.
*/
function check_if_post_state_is_status($post_states) {
function display_actual_post_state( $post_states, $post ) {
$status = get_post_status_object( get_post_status( $post->ID ) );
$status_key = $status->name;

global $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>';
}
if ( $status_key === 'future' ) {
$status_key = 'scheduled';
}

if ( !isset( $_REQUEST['post_status'] )
&& !isset( $post_states[ $status_key ] )
&& 'publish' !== $post->post_status ) {
return array_merge( [ $status->label ], $post_states );
} else {
return $post_states;
}
}

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

This file was deleted.

15 changes: 0 additions & 15 deletions modules/custom-status/lib/custom-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,4 @@ jQuery(document).ready(function() {

return name;
}

// If we're on the Manage Posts screen, remove the trailing dashes left behind once we hide the post-state span (the status).
// We do this since we already add a custom column for post status on the screen since custom statuses are a core part of EF.
if ( jQuery('.post-state').length > 0 ) {
ef_remove_post_title_trailing_dashes();
}

// Remove the " - " in between a post title and the post-state span (separately hidden via CSS).
// This will not affect the dash before post-state-format spans.
function ef_remove_post_title_trailing_dashes() {
jQuery('.post-title.column-title strong').each(function() {
jQuery(this).html(jQuery(this).html().replace(/(.*) - (<span class="post-state".*<\/span>)$/g, "$1$2"));
});
}

});
125 changes: 124 additions & 1 deletion tests/test-edit-flow-custom-status.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,43 @@ class WP_Test_Edit_Flow_Custom_Status extends WP_UnitTestCase {

protected static $admin_user_id;
protected static $EF_Custom_Status;

protected static $table_posts = array();
protected static $post_statuses = [
'draft' => 'Draft',
'publish' => 'Published',
'future' => 'Scheduled',
'pitch' => 'Pitch'];

/**
* @var WP_Posts_List_Table
*/
protected $table;

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();

$index = 0;
foreach ( self::$post_statuses as $status => $name ) {
$args = array(
'post_type' => 'post',
'post_title' => sprintf( 'A Post %d', $index ),
'post_status' => $status
);
if ( $status === 'future' ) {
$future_date = strftime( "%Y-%m-%d %H:%M:%S" , strtotime( '+1 day' ) );

$args = array_merge(
$args,
array( 'post_date' => $future_date )
);
}
self::$table_posts[ $status ] = $factory->post->create_and_get($args);
$index += 1;
}
}

public static function wpTearDownAfterClass() {
Expand All @@ -21,6 +51,8 @@ public static function wpTearDownAfterClass() {
function setUp() {
parent::setUp();

$this->table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit' ) );

global $pagenow;
$pagenow = 'post.php';
}
Expand Down Expand Up @@ -332,4 +364,95 @@ public function test_fix_get_sample_permalink_should_respect_hierarchy_of_publis
$this->assertSame( home_url() . '/publish-parent-page/%pagename%/', $actual[0] );
$this->assertSame( 'child-page', $actual[1] );
}

/**
* The post status should be shown or hidden depending on the screen and status.
* On the "All" screen, all statuses should be shown except "Publish"
*/
public function test_post_state_is_shown_on_all() {
foreach( self::$post_statuses as $post_status => $status_name ) {
$output = $this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 1,
'post_status' => $post_status
) );

$this->assertContains( self::$table_posts[ $post_status ]->post_title, $output );
/**
* On edit post list screen, publish status does not appear in post title
*/
if ( 'publish' === $post_status ) {
$this->assertNotContains( "<span class='post-state'>$status_name</span>", $output );
} else {
$this->assertContains( "<span class='post-state'>$status_name</span>", $output );
}
}
}

/**
* The post status on the "Scheduled" screen should be shown
*/
public function test_post_state_is_shown_on_scheduled() {
$_REQUEST['post_status'] = 'future';

$output = $this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 1,
'post_status' => 'future'
) );

$this->assertContains( self::$table_posts[ 'future' ]->post_title, $output );
$this->assertContains( "<span class='post-state'>" . self::$post_statuses[ 'future' ] . "</span>", $output );
}

/**
* The post status on the "Pitch" screen should be hidden
*/
public function test_post_state_is_not_shown_on_pitch() {
$_REQUEST['post_status'] = 'pitch';

$output = $this->_test_list_hierarchical_page( array(
'paged' => 1,
'posts_per_page' => 1,
'post_status' => 'pitch'
) );

$this->assertContains( self::$table_posts[ 'pitch' ]->post_title, $output );
$this->assertNotContains( "<span class='post-state'>" . self::$post_statuses[ 'pitch' ] . "</span>", $output );
}

/**
* Helper function to test the output of a page which uses `WP_Posts_List_Table`.
*
* @param array $args Query args for the list of posts.
*/
protected function _test_list_hierarchical_page( array $args ) {
$matches = array();
$_REQUEST['paged'] = $args['paged'];
$GLOBALS['per_page'] = $args['posts_per_page'];
$args = array_merge(
array(
'post_type' => 'post',
),
$args
);
// Mimic the behaviour of `wp_edit_posts_query()`:
if ( ! isset( $args['orderby'] ) ) {
$args['orderby'] = 'menu_order title';
$args['order'] = 'asc';
$args['posts_per_page'] = -1;
$args['posts_per_archive_page'] = -1;
}
$posts = new WP_Query( $args );
ob_start();
$this->table->set_hierarchical_display( true );
$this->table->display_rows( $posts->posts );
$output = ob_get_clean();
// Clean up.
unset( $_REQUEST['paged'] );
unset( $GLOBALS['per_page'] );
preg_match_all( '|<tr[^>]*>|', $output, $matches );

return $output;
}
}