Skip to content

Commit

Permalink
Support i18n in story budget date picker
Browse files Browse the repository at this point in the history
  • Loading branch information
cojennin committed Dec 15, 2019
1 parent 8317aa3 commit fa452cc
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 37 deletions.
15 changes: 10 additions & 5 deletions common/js/ef_date.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ jQuery(document).ready(function($) {
controlType: 'select',
});

$('.date-pick')
.datepicker({
dateFormat: 'M dd yy',
firstDay: ef_week_first_day
});
var datePicks = jQuery( ".date-pick" );
datePicks.each( function() {
var $datePicker = jQuery( this );

$datePicker.datepicker( {
firstDay: ef_week_first_day,
altField: '#' + $datePicker.prop( "id" ) + "_hidden",
altFormat: "yy-mm-dd",
} );
} );
});
88 changes: 56 additions & 32 deletions modules/story-budget/story-budget.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,23 +188,55 @@ function register_term_columns() {
*/
function handle_form_date_range_change() {

if ( !isset( $_POST['ef-story-budget-range-submit'], $_POST['ef-story-budget-number-days'], $_POST['ef-story-budget-start-date'] ) )
if ( !isset( $_POST['ef-story-budget-range-submit'], $_POST['ef-story-budget-number-days'], $_POST['ef-story-budget-start-date_hidden'] ) )
return;

if ( !wp_verify_nonce( $_POST['nonce'], 'change-date' ) )
wp_die( $this->module->messages['nonce-failed'] );

$current_user = wp_get_current_user();
$user_filters = $this->get_user_meta( $current_user->ID, self::usermeta_key_prefix . 'filters', true );
$user_filters['start_date'] = date( 'Y-m-d', strtotime( $_POST['ef-story-budget-start-date'] ) );
$user_filters['number_days'] = (int)$_POST['ef-story-budget-number-days'];
if ( $user_filters['number_days'] <= 1 )
$user_filters['number_days'] = 1;
$new_filters = [
'start_date' => $_POST['ef-story-budget-start-date_hidden'],
'number_days' => (int)$_POST['ef-story-budget-number-days']
];
$user_filters = $this->update_user_filters_from_form_date_range_change( $current_user, $new_filters );

$this->update_user_meta( $current_user->ID, self::usermeta_key_prefix . 'filters', $user_filters );
wp_redirect( menu_page_url( $this->module->slug, false ) );
exit;
}

/**
* Handles updating the users
*/
function update_user_filters_from_form_date_range_change( $current_user, $new_filters ) {
$existing_filters = $this->get_user_meta( $current_user->ID, self::usermeta_key_prefix . 'filters', true );

// Default start date value
if ( isset( $new_filters['start_date'] ) ) {
// Validate that it's a legitimate date
$valid_date = DateTime::createFromFormat( 'Y-m-d', $new_filters['start_date'] );

if ( $valid_date === false ) {
$start_date = date_i18n( 'Y-m-d' );
} else {
$start_date = $valid_date->format( 'Y-m-d' );
}

// Set the start_date filter (to new value or default)
$existing_filters['start_date'] = $start_date;
}

if ( isset( $new_filters['number_days'] ) ) {
if ( $new_filters['number_days'] <= 1 ) {
$existing_filters['number_days'] = 1;
} else {
$existing_filters['number_days'] = $new_filters['number_days'];
}
}

return $existing_filters;
}

/**
* Get the number of columns to show on the story budget
Expand Down Expand Up @@ -320,32 +352,24 @@ function story_budget() {
* @since 0.7
*/
function story_budget_time_range() {

$output = '<form method="POST" action="' . menu_page_url( $this->module->slug, false ) . '">';

$start_date_value = '<input type="text" id="ef-story-budget-start-date" name="ef-story-budget-start-date"'
. ' size="10" class="date-pick" value="'
. esc_attr( date_i18n( get_option( 'date_format' ), strtotime( $this->user_filters['start_date'] ) ) ) . '" /><span class="form-value">';

$start_date_value .= esc_html( date_i18n( get_option( 'date_format' ), strtotime( $this->user_filters['start_date'] ) ) );
$start_date_value .= '</span>';

$number_days_value = '<input type="text" id="ef-story-budget-number-days" name="ef-story-budget-number-days"'
. ' size="3" maxlength="3" value="'
. esc_attr( $this->user_filters['number_days'] ) . '" /><span class="form-value">' . esc_html( $this->user_filters['number_days'] )
. '</span>';

$output .= sprintf( _x( 'starting %1$s showing %2$s %3$s', '%1$s = start date, %2$s = number of days, %3$s = translation of \'Days\'', 'edit-flow' ), $start_date_value, $number_days_value, _n( 'day', 'days', $this->user_filters['number_days'], 'edit-flow' ) );
$output .= '&nbsp;&nbsp;<span class="change-date-buttons">';
$output .= '<input id="ef-story-budget-range-submit" name="ef-story-budget-range-submit" type="submit"';
$output .= ' class="button-primary" value="' . __( 'Change', 'edit-flow' ) . '" />';
$output .= '&nbsp;';
$output .= '<a class="change-date-cancel hidden" href="#">' . __( 'Cancel', 'edit-flow' ) . '</a>';
$output .= '<a class="change-date" href="#">' . __( 'Change', 'edit-flow' ) . '</a>';
$output .= wp_nonce_field( 'change-date', 'nonce', 'change-date-nonce', false );
$output .= '</span></form>';

echo $output;
?>
<form method="POST" action="<?php echo esc_attr( menu_page_url( $this->module->slug, false ) ); ?>">
<?php _e( 'starting', 'edit-flow' ); ?>
<span class="form-value"><?php echo esc_html( date_i18n( get_option( 'date_format' ), strtotime( $this->user_filters['start_date'] ) ) ); ?></span>
<input type="text" id="ef-story-budget-start-date" name="ef-story-budget-start-date" size="20" autocomplete="off" class="date-pick" value="<?php echo esc_attr( date_i18n( get_option( 'date_format' ), strtotime( $this->user_filters['start_date'] ) ) ); ?>" />
<input type="hidden" id="ef-story-budget-start-date_hidden" name="ef-story-budget-start-date_hidden" />
<?php _e( 'showing', 'edit-flow' ); ?>
<input type="text" id="ef-story-budget-number-days" name="ef-story-budget-number-days" size="3" maxlength="3" value="<?php echo esc_attr( $this->user_filters['number_days'] ); ?>" />
<span class="form-value"><?php echo esc_html( $this->user_filters['number_days'] ); ?></span>
<?php echo esc_html( _n( 'day', 'days', $this->user_filters['number_days'], 'edit-flow' ) ); ?>
<span class="change-date-buttons">
<input id="ef-story-budget-range-submit" name="ef-story-budget-range-submit" type="submit" class="button-primary" value="<?php echo esc_attr( __( 'Change', 'edit-flow' ) ); ?>" />
<a class="change-date-cancel hidden" href="#"><?php echo esc_html( __( 'Cancel', 'edit-flow' ) ); ?></a>
<a class="change-date" href="#"><?php echo esc_html( __( 'Change', 'edit-flow' ) ); ?></a>
</span>
<?php echo wp_nonce_field( 'change-date', 'nonce', 'change-date-nonce', false ); ?>
</form>
<?php
}

/**
Expand Down
116 changes: 116 additions & 0 deletions tests/test-edit-flow-story-budget.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

class WP_Test_Edit_Flow_Story_Budget extends WP_UnitTestCase {

protected static $admin_user_id;

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

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

/**
* Test that the story budget date filter handles valid date
*/
function test_story_budget_set_start_date_filter() {
global $edit_flow;

$user = get_user_by( 'id', self::$admin_user_id );

wp_set_current_user( self::$admin_user_id );

// Users filters need to be set (they're set by default)
$edit_flow->story_budget->update_user_filters();

$new_filters['start_date'] = '2019-12-01';

$users_filters = $edit_flow->story_budget->update_user_filters_from_form_date_range_change( $user, $new_filters );

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

/**
* Test that the story budget date filter handles invalid date
*/
function test_story_budget_set_start_date_filter_invalid() {
global $edit_flow;

$user = get_user_by( 'id', self::$admin_user_id );

wp_set_current_user( self::$admin_user_id );

// Users filters need to be set (they're set by default)
$edit_flow->story_budget->update_user_filters();

$new_filters['start_date'] = 'not a date';

$users_filters = $edit_flow->story_budget->update_user_filters_from_form_date_range_change( $user, $new_filters );

$this->assertEquals( date( 'Y-m-d' ), $users_filters['start_date'] );
}

/**
* Test that the story budget number of days filter handles valid number of days
*/
function test_story_budget_set_number_days_filter() {
global $edit_flow;

$user = get_user_by( 'id', self::$admin_user_id );

wp_set_current_user( self::$admin_user_id );

// Users filters need to be set (they're set by default)
$edit_flow->story_budget->update_user_filters();

$new_filters['number_days'] = 10;

$users_filters = $edit_flow->story_budget->update_user_filters_from_form_date_range_change( $user, $new_filters );

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

/**
* Test that the story budget number of days filter handles invalid number of days
*/
function test_story_budget_set_number_days_filter_invalid() {
global $edit_flow;

$user = get_user_by( 'id', self::$admin_user_id );

wp_set_current_user( self::$admin_user_id );

// Users filters need to be set (they're set by default)
$edit_flow->story_budget->update_user_filters();

$new_filters['number_days'] = 'not days';

$users_filters = $edit_flow->story_budget->update_user_filters_from_form_date_range_change( $user, $new_filters );

$this->assertEquals( 1, $users_filters['number_days'] );
}

/**
* Test that the story budget handles both valid date and number of days filters
*/
function test_story_budget_set_date_and_number_days_filters() {
global $edit_flow;

$user = get_user_by( 'id', self::$admin_user_id );

wp_set_current_user( self::$admin_user_id );

// Users filters need to be set (they're set by default)
$edit_flow->story_budget->update_user_filters();

$new_filters['start_date'] = '2019-12-01';
$new_filters['number_days'] = 10;

$users_filters = $edit_flow->story_budget->update_user_filters_from_form_date_range_change( $user, $new_filters );

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

0 comments on commit fa452cc

Please sign in to comment.