Skip to content
This repository has been archived by the owner on Feb 1, 2019. It is now read-only.

Commit

Permalink
Merge pull request #51 from kurtw/timezone
Browse files Browse the repository at this point in the history
Change where and how date/time/datetime is saved
  • Loading branch information
willbarton committed Mar 26, 2015
2 parents ff4c5f7 + bd55bb7 commit 11512c9
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 75 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ We follow the [Semantic Versioning 2.0.0](http://semver.org/) format.
## 1.3.0 - 2015-01-15

### Added
- Arbitrary formset functionality
- Meta-data saving of `date`, `time`, `datetime` fields

### Fixed
- Lots of issues with saving and removing fields
- Some visual issues
- Readability of those field's taxonomy tags
- Format of how each field is saved
2 changes: 1 addition & 1 deletion cms-toolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
available throughout the application and make building complex functionality
in WordPress a little easier.
Version: 1.4.0
Version: 1.5.0
Author: Greg Boone, Aman Kaur, Matthew Duran, Scott Cranfill, Kurt Wall
Author URI: https://github.com/cfpb/
License: Public Domain work of the Federal Government
Expand Down
22 changes: 10 additions & 12 deletions inc/meta-box-callbacks.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace CFPB\Utils\MetaBox;
use \CFPB\Utils\Taxonomy;
use \CFPB\Utils\MetaBox\Models;
use \DateTime;

class Callbacks {
public $taxonomy;
Expand Down Expand Up @@ -39,21 +41,17 @@ public function date( $post_id, $taxonomy, $multiples = false, $data = array(),
global $post;

$rmTerm = 'rm_' . $taxonomy . '_' . $term_num;
if ( isset( $_POST[$rmTerm] ) ) {
if ( isset( $_POST[$rmTerm] ) and !empty( $_POST[$rmTerm] ) ) {
$tounset = get_term_by( 'name', $_POST[$rmTerm], $taxonomy );
if ( $tounset ) {
$this->Taxonomy->remove_post_term( $post_id, $tounset->term_id, $taxonomy );
}
}

if ( isset($data[$taxonomy] ) ) {
$time = strval( strtotime( $data[$taxonomy] ) );
} else {
$time = strtotime('now');
return $time;
}
if ( $time != strtotime('now') ) {
wp_set_object_terms( $post_id, $time, $taxonomy, $append = $multiples );
if ( isset( $data[$taxonomy] ) ) {
Models::save( $post_id, array( $taxonomy => strval( strtotime( $data[$taxonomy] ) ) ) );
}
} elseif ( isset( $data[$taxonomy] ) and !empty( $data[$taxonomy] ) ) {
wp_set_object_terms( $post_id, $data[$taxonomy], $taxonomy, $append = $multiples );
Models::save( $post_id, array( $taxonomy => date( Datetime::ISO8601, strval( strtotime( $data[$taxonomy] ) ) ) ) );
}
}
}
}
12 changes: 6 additions & 6 deletions inc/meta-box-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -521,23 +521,23 @@ public function displayTags( $taxonomy, $type, $timezone = NULL ) {
// Checks if the current set term is wholly numeric (in this case a timestamp)
if ( is_numeric( $term->name ) ) {
if ( $type == 'date' ) {
$natdate = date( 'j F, Y', intval( $term->name ) );
$natdate = date( 'j F Y', intval( $term->name ) );
} elseif ( $type == 'time' ) {
$natdate = date( 'h:ia T', intval( $term->name ) );
} elseif ( $type == 'datetime' ) {
$natdate = date( 'F j, Y @ h:ia T', intval( $term->name ) );
$natdate = date( 'F j Y h:ia T', intval( $term->name ) );
}
?><span><a id="<?php echo esc_attr( $taxonomy ) ?>-check-num-<?php echo esc_attr( $i ) ?>"
class="tagdelbutton <?php echo esc_attr( $term->name ) ?>"><?php
class="tagdelbutton -<?php echo esc_attr( $term->name ) ?>"><?php
echo esc_attr( $term->name );
?></a><?php
?>&nbsp;<?php echo esc_attr( $natdate );
?>&nbsp;<?php echo esc_attr( $term->name );
?></span><?php
} else {
$date = strtotime( $term->name );
?><span><a id="<?php echo esc_attr( $taxonomy ) ?>-check-num-<?php echo esc_attr( $i ) ?>"
class="tagdelbutton <?php echo esc_attr( $date ) ?>"><?php
echo esc_attr( $term->name );
class="tagdelbutton -<?php echo esc_attr( $term->name ) ?>"><?php
echo esc_attr( $date );
?></a><?php
?>&nbsp;<?php echo esc_attr( $term->name );
?></span><?php
Expand Down
51 changes: 25 additions & 26 deletions inc/meta-box-models.php
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,23 @@ public function validate_datetime($field, $post_ID) {
$this->Callbacks->date( $post_ID, $field['taxonomy'], $multiple = $field['multiple'], null, $t );
}
$data = array($field['taxonomy'] => '');
if ( $field['type'] != 'time') {
$month = $field['taxonomy'] . '_month';
$day = $field['taxonomy'] . '_day';
$year = $field['taxonomy'] . '_year';
if ( isset($_POST[$month]) ) {
$data[$field['taxonomy']] .= $_POST[$month];
}
if ( isset( $_POST[$day] ) ) {
$data[$field['taxonomy']] .= ' ' . $_POST[$day];
}
if ( isset( $_POST[$year] ) ) {
$data[$field['taxonomy']] .= ' ' . $_POST[$year];
}
}
if ( $field['type'] == 'datetime' ) {
$data[$field['taxonomy']] .= ' ';
}
if ( $field['type'] != 'date') {
$hour = $field['taxonomy'] . '_hour';
$minute = $field['taxonomy'] . '_minute';
Expand All @@ -349,34 +366,16 @@ public function validate_datetime($field, $post_ID) {
$data[$field['taxonomy']] .= ' ' . $_POST[$timezone][0];
}
}
if ( $field['type'] == 'datetime' ) {
$data[$field['taxonomy']] .= ' ';
}
if ( $field['type'] != 'time') {
$month = $field['taxonomy'] . '_month';
$day = $field['taxonomy'] . '_day';
$year = $field['taxonomy'] . '_year';
if ( isset($_POST[$month]) ) {
$data[$field['taxonomy']] .= $_POST[$month];
}
if ( isset( $_POST[$day] ) ) {
$data[$field['taxonomy']] .= ' ' . $_POST[$day];
}
if ( isset( $_POST[$year] ) ) {
$data[$field['taxonomy']] .= ' ' . $_POST[$year];
}
}
if ( $field['type'] == 'time' ) {
date_default_timezone_set( $_POST[$field['taxonomy'] . '_timezone'][0] );
$date = DateTime::createFromFormat('h:ia T', $data[$field['taxonomy']]);
} elseif ( $field['type'] == 'date' ) {
if ( $field['type'] == 'date' ) {
$date = DateTime::createFromFormat('F j Y', $data[$field['taxonomy']]);
} elseif ( $field['type'] == 'time' ) {
if ( isset( $_POST[$field['taxonomy'] . '_timezone'] ) )
date_default_timezone_set( $_POST[$field['taxonomy'] . '_timezone'][0] );
$date = DateTime::createFromFormat('h:ia T', $data[$field['taxonomy']]);
} elseif ( $field['type'] == 'datetime' ) {
date_default_timezone_set( $_POST[$field['taxonomy'] . '_timezone'][0] );
$date = DateTime::createFromFormat('h:ia T F j Y', $data[$field['taxonomy']]);
}
if ( $field['type'] != 'date') {
$this->save( $post_ID, array( $field['taxonomy'] . '_timezone' => $_POST[$field['taxonomy'] . '_timezone'] ) );
if ( isset( $_POST[$field['taxonomy'] . '_timezone'] ) )
date_default_timezone_set( $_POST[$field['taxonomy'] . '_timezone'][0] );
$date = DateTime::createFromFormat('F j Y h:ia T', $data[$field['taxonomy']]);
}
if ( $date ) {
$this->Callbacks->date( $post_ID, $field['taxonomy'], $multiple = $field['multiple'], $data, null );
Expand Down
3 changes: 1 addition & 2 deletions js/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,8 @@ jQuery('.tagdelbutton').click( function() {
var taxAndTagNum = jQuery(this).attr('id').split('-');
var taxonomy = taxAndTagNum[0];
var tagNum = taxAndTagNum[3];
var term = jQuery(this).attr('class').split(' ')[1];
var term = jQuery(this).attr('class').split('-')[1];
jQuery('input[id=rm_' + taxonomy + '_' + tagNum + ']').val(term);

jQuery(this).parent().html('');
});
jQuery('.filedelbutton').click( function() {
Expand Down
38 changes: 13 additions & 25 deletions tests/test-meta-box-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ function tearDown() {
*/
function testDateExpectsWPSetObjectTermsFromAPI() {
// Arrange
\WP_Mock::wpFunction( 'get_post_meta' );
\WP_Mock::wpFunction( 'remove_post_meta' );
\WP_Mock::wpFunction( 'update_post_meta' );
\WP_Mock::wpFunction('wp_set_object_terms', array('times' => 1));
$post_id = 0;
$taxonomy = 'category';
Expand All @@ -52,8 +55,11 @@ function testDateExpectsWPSetObjectTermsFromAPI() {
*/
function testDateCallsGetTermByWhenAttemptingToDeleteATerm() {
// Arrange
\WP_Mock::wpFunction( 'get_post_meta' );
\WP_Mock::wpFunction( 'remove_post_meta' );
\WP_Mock::wpFunction( 'update_post_meta' );
$c = new Callbacks();
$_POST = array( 'rm_tax_0' => '' );
$_POST = array( 'rm_tax_0' => 'term' );
\WP_Mock::wpFunction('get_term_by', array('times' => 1, 'return' => false ) );
// Act
$c->date( 0, 'tax', false, array(), 0 );
Expand All @@ -72,6 +78,9 @@ function testDateCallsGetTermByWhenAttemptingToDeleteATerm() {
*/
function testDateCallsRemovePostTermWhenAttemptingToDeleteATerm() {
// Arrange
\WP_Mock::wpFunction( 'get_post_meta' );
\WP_Mock::wpFunction( 'remove_post_meta' );
\WP_Mock::wpFunction( 'update_post_meta' );
$_POST = array( 'rm_tax_0' => 'term' );
$term = new \StdClass;
$term->term_id = 1;
Expand All @@ -88,30 +97,6 @@ function testDateCallsRemovePostTermWhenAttemptingToDeleteATerm() {
// Assert
}

/**
* Tests whether the current time will be assigned if non is given.
*
* @group stable
* @group isolated
* @group date
* @group taxonomy_save
*
*/
function testDateExpectsCurrentTime() {
// Arrange
$post_id = 0;
$taxonomy = 'category';
// replace fix time at 12:00 today just in case the test takes longer than 1 second to run (it shouldn't)
self::$now = strtotime('12:00');
$expected = strtotime('now');
// Act
$c = new Callbacks();
$actual = $c->date($post_id, $taxonomy );

// Assert
$this->assertEquals($expected, $actual, 'Time should be equal to the current time, but was not.');
}

/**
* Test whether a post term will be removed if a rm_{taxonomy} key is passed
*
Expand All @@ -122,6 +107,9 @@ function testDateExpectsCurrentTime() {
*/
function testRmTermKeyDateExpectsRemovePostTermCalled() {
// Arrange
\WP_Mock::wpFunction( 'get_post_meta' );
\WP_Mock::wpFunction( 'remove_post_meta' );
\WP_Mock::wpFunction( 'update_post_meta' );
$post_id = 0;
$_POST['rm_category_1'] = 'term';
$taxonomy = 'category';
Expand Down

0 comments on commit 11512c9

Please sign in to comment.