Skip to content

Commit

Permalink
Merge pull request #595 from Automattic/fix/status-filter-dropdowns
Browse files Browse the repository at this point in the history
Statuses for Story Budget and Calendar should be Edit Flow specific s…
  • Loading branch information
cojennin committed Apr 4, 2020
2 parents a04d1f0 + adeafe5 commit e708d9f
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 28 deletions.
52 changes: 36 additions & 16 deletions modules/calendar/calendar.php
Expand Up @@ -651,7 +651,7 @@ function view_calendar() {
}

// we sort by post statuses....... eventually
$post_statuses = $this->get_post_statuses();
$post_statuses = $this->get_calendar_post_stati();
?>
<div class="wrap">
<div id="ef-calendar-title"><!-- Calendar Title -->
Expand Down Expand Up @@ -746,7 +746,7 @@ function view_calendar() {
if ( !empty( $week_posts[$week_single_date] ) ) {
$week_posts_by_status = array();
foreach( $post_statuses as $post_status ) {
$week_posts_by_status[$post_status->slug] = array();
$week_posts_by_status[$post_status->name] = array();
}
// These statuses aren't handled by custom statuses or post statuses
$week_posts_by_status['private'] = array();
Expand Down Expand Up @@ -1231,14 +1231,13 @@ function get_calendar_posts_for_week( $args = array(), $context = 'dashboard' )
// Unpublished as a status is just an array of everything but 'publish'.
if ( 'unpublish' == $args['post_status'] ) {
$args['post_status'] = '';
$post_stati = get_post_stati();
unset($post_stati['inherit'], $post_stati['auto-draft'], $post_stati['trash'], $post_stati['publish'] );
$post_stati = wp_filter_object_list( $this->get_calendar_post_stati(), array( 'name' => 'publish' ), 'not' );

if ( ! apply_filters( 'ef_show_scheduled_as_unpublished', false ) ) {
unset( $post_stati['future'] );
}
foreach ( $post_stati as $post_status ) {
$args['post_status'] .= $post_status . ', ';
$post_stati = wp_filter_object_list( $post_stati, array( 'name' => 'future' ), 'not' );
}

$args['post_status'] .= join( wp_list_pluck( $post_stati, 'name' ), ',' );
}
// The WP functions for printing the category and author assign a value of 0 to the default
// options, but passing this to the query is bad (trashed and auto-draft posts appear!), so
Expand Down Expand Up @@ -1698,9 +1697,9 @@ function sanitize_filter( $key, $dirty_value ) {
switch( $key ) {
case 'post_status':
// Whitelist-based validation for this parameter
$valid_statuses = get_post_stati();
$valid_statuses = wp_list_pluck( $this->get_calendar_post_stati(), 'name' );
$valid_statuses[] = 'unpublish';
unset( $valid_statuses['inherit'], $valid_statuses['auto-draft'], $valid_statuses['trash'] );

if ( in_array( $dirty_value, $valid_statuses ) )
return $dirty_value;
else
Expand Down Expand Up @@ -1730,16 +1729,13 @@ function sanitize_filter( $key, $dirty_value ) {
function calendar_filter_options( $select_id, $select_name, $filters ) {
switch( $select_id ){
case 'post_status':
$post_stati = get_post_stati();
unset( $post_stati['inherit'], $post_stati['auto-draft'], $post_stati['trash'] );
$post_stati = $this->get_calendar_post_stati();
?>
<select id="<?php echo $select_id; ?>" name="<?php echo $select_name; ?>" >
<option value=""><?php _e( 'View all statuses', 'edit-flow' ); ?></option>
<?php
foreach ( $post_stati as $post_status ) {
$value = $post_status;
$status = get_post_status_object($post_status);
echo "<option value='" . esc_attr( $value ) . "' " . selected( $value, $filters['post_status'] ) . ">" . esc_html( $status->label ) . "</option>";
foreach ( $post_stati as $status ) {
echo "<option value='" . esc_attr( $status->name ) . "' " . selected( $status->name, $filters['post_status'] ) . ">" . esc_html( $status->label ) . "</option>";
}
?>
<option value="unpublish" <?php selected( 'unpublish', $filters['post_status'] ) ?> > <?php echo __( 'Unpublished', 'edit-flow' ) ?> </option>
Expand Down Expand Up @@ -1851,6 +1847,30 @@ public function fix_post_date_on_update_part_two( $post_ID, $post_after, $post_b
$wpdb->update( $wpdb->posts, array( 'post_date' => $post_date ), array( 'ID' => $post_ID ) );
clean_post_cache( $post_ID );
}

/**
* Returns a list of custom status objects used by the calendar
*
* @return array An array of StdClass objects representing statuses
*/
public function get_calendar_post_stati() {
$post_stati = get_post_stati( array(), 'object' );
$custom_status_slugs = wp_list_pluck( $this->get_post_statuses(), 'slug' );
$custom_status_slugs[] = 'future';
$custom_status_slugs[] = 'publish';

$custom_status_slug_keys = array_flip( $custom_status_slugs );

$final_statuses = [];

foreach( $post_stati as $status ) {
if ( !empty( $custom_status_slug_keys[ $status->name ] ) ) {
$final_statuses[] = $status;
}
}

return apply_filters( 'ef_calendar_post_stati', $final_statuses );
}

} // EF_Calendar

Expand Down
44 changes: 32 additions & 12 deletions modules/story-budget/story-budget.php
Expand Up @@ -405,14 +405,13 @@ function get_posts_for_term( $term, $args = null ) {
// Unpublished as a status is just an array of everything but 'publish'.
if ( 'unpublish' == $args['post_status'] ) {
$args['post_status'] = '';
$post_stati = get_post_stati();
unset( $post_stati['inherit'], $post_stati['auto-draft'], $post_stati['trash'], $post_stati['publish'] );
$post_stati = wp_filter_object_list( $this->get_budget_post_stati(), array( 'name' => 'publish' ), 'not' );

if ( ! apply_filters( 'ef_show_scheduled_as_unpublished', false ) ) {
unset( $post_stati['future'] );
}
foreach ( $post_stati as $post_status ) {
$args['post_status'] .= $post_status . ', ';
$post_stati = wp_filter_object_list( $post_stati, array( 'name' => 'future' ), 'not' );
}

$args['post_status'] .= join( wp_list_pluck( $post_stati, 'name' ), ',' );
}

// Filter by post_author if it's set
Expand Down Expand Up @@ -748,16 +747,13 @@ function story_budget_filters() {
function story_budget_filter_options( $select_id, $select_name, $filters ) {
switch( $select_id ) {
case 'post_status':
$post_stati = get_post_stati();
unset( $post_stati['inherit'], $post_stati['auto-draft'], $post_stati['trash'] );
$post_stati = $this->get_budget_post_stati();
?>
<select id="post_status" name="post_status"><!-- Status selectors -->
<option value=""><?php _e( 'View all statuses', 'edit-flow' ); ?></option>
<?php
foreach ( $post_stati as $post_status ) {
$value = $post_status;
$status = get_post_status_object($post_status)->label;
echo '<option value="' . esc_attr( $value ) . '" ' . selected( $value, $filters['post_status'] ) . '>' . esc_html( $status ) . '</option>';
foreach ( $post_stati as $status ) {
echo '<option value="' . esc_attr( $status->name ) . '" ' . selected( $status->name, $filters['post_status'] ) . '>' . esc_html( $status->label ) . '</option>';
}
echo '<option value="unpublish"' . selected('unpublish', $filters['post_status']) . '>' . __( 'Unpublished', 'edit-flow' ) . '</option>';
?>
Expand Down Expand Up @@ -793,5 +789,29 @@ function story_budget_filter_options( $select_id, $select_name, $filters ) {
break;
}
}

/**
* Returns a list of custom status objects used by the story budget
*
* @return array An array of StdClass objects representing statuses
*/
public function get_budget_post_stati() {
$post_stati = get_post_stati( array(), 'object' );
$custom_status_slugs = wp_list_pluck( $this->get_post_statuses(), 'slug' );
$custom_status_slugs[] = 'future';
$custom_status_slugs[] = 'publish';

$custom_status_slug_keys = array_flip( $custom_status_slugs );

$final_statuses = [];

foreach( $post_stati as $status ) {
if ( !empty( $custom_status_slug_keys[ $status->name ] ) ) {
$final_statuses[] = $status;
}
}

return apply_filters( 'ef_budget_post_stati', $final_statuses );
}

}
67 changes: 67 additions & 0 deletions tests/test-edit-flow-calendar.php
@@ -0,0 +1,67 @@
<?php

class WP_Test_Edit_Flow_Calendar extends WP_UnitTestCase {

protected static $admin_user_id;
protected static $EF_Calendar;

public static function wpSetUpBeforeClass( $factory ) {
global $edit_flow;

self::$admin_user_id = $factory->user->create( array( 'role' => 'administrator' ) );

$edit_flow->custom_status->install();
$edit_flow->custom_status->init();

$edit_flow->calendar->install();
$edit_flow->calendar->init();
}

public static function wpTearDownAfterClass() {
self::delete_user( self::$admin_user_id );
}

/**
* Test that calendar has default custom statuses
*/
public function test_calendar_custom_statuses() {
global $edit_flow;

$statuses = array_map(
function( $status ) {
return $status->name;
},
$edit_flow->calendar->get_calendar_post_stati()
);

$this->assertContains( 'future', $statuses );
$this->assertContains( 'pitch', $statuses );
}

/**
* Test that calendar can show registered status
*/
public function test_calendar_custom_statuses_registered() {
global $edit_flow;

$new_custom_status = array(
'term' => __( 'New Custom Status' ),
'args' => array(
'slug' => 'new-custom-status',
'description' => 'description',
'position' => 6,
),
);

$edit_flow->custom_status->add_custom_status( $new_custom_status['term'], $new_custom_status['args'] );

$statuses = array_map(
function( $status ) {
return $status->name;
},
$edit_flow->calendar->get_calendar_post_stati()
);

$this->assertContains( 'future', $statuses );
}
}
44 changes: 44 additions & 0 deletions tests/test-edit-flow-story-budget.php
Expand Up @@ -112,5 +112,49 @@ public function test_story_budget_set_date_and_number_days_filters() {

$this->assertEquals( 10, $users_filters['number_days'] );
$this->assertEquals( '2019-12-01', $users_filters['start_date'] );
}

/**
* Test that calendar has default custom statuses
*/
public function test_calendar_custom_statuses() {
global $edit_flow;

$statuses = array_map(
function( $status ) {
return $status->name;
},
$edit_flow->calendar->get_calendar_post_stati()
);

$this->assertContains( 'future', $statuses );
$this->assertContains( 'pitch', $statuses );
}

/**
* Test that calendar can show registered status
*/
public function test_calendar_custom_statuses_registered() {
global $edit_flow;

$new_custom_status = array(
'term' => __( 'New Custom Status' ),
'args' => array(
'slug' => 'new-custom-status',
'description' => 'description',
'position' => 6,
),
);

$edit_flow->custom_status->add_custom_status( $new_custom_status['term'], $new_custom_status['args'] );

$statuses = array_map(
function( $status ) {
return $status->name;
},
$edit_flow->calendar->get_calendar_post_stati()
);

$this->assertContains( 'future', $statuses );
}
}

0 comments on commit e708d9f

Please sign in to comment.