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 #67 from kurtw/tax-timezone
Browse files Browse the repository at this point in the history
Term description of date fields have timezone
  • Loading branch information
Kurt Wall committed Jun 30, 2015
2 parents 55871db + a67bd62 commit 932c8f0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 11 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
We follow the [Semantic Versioning 2.0.0](http://semver.org/) format.


## 2.2.1 - 2015-06-25

### Changed
- `time`/`datetime` fields saved under a taxonomy now save the timezone in the
term description

## 2.2.0 - 2015-06-23

### Changed
Expand Down
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: 2.2.0
Version: 2.2.1
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
8 changes: 6 additions & 2 deletions inc/meta-box-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,20 @@ public function replace_Taxonomy($Taxonomy) {
* @param boolean $multiples, Determines whether the term shoud append (true) or replace (false) existing terms
* @return identical to wp_set_object_terms
*/
public function date( $post_id, $taxonomy, $multiples = false, $date = array(), $term_num = null ) {
public function date( $post_id, $taxonomy, $multiples = false, $date = array(), $timezone = null, $term_num = null ) {
global $post;
$rmTerm = 'rm_' . $taxonomy . '_' . $term_num;
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 );
}
} elseif ( isset( $date ) ) {
} elseif ( ! empty( $date ) ) {
wp_set_object_terms( $post_id, $date->format('c'), $taxonomy, $append = $multiples );
if ( isset( $timezone ) ) {
$term = get_term_by( 'name', $date->format('c'), $taxonomy );
wp_update_term( $term->term_id, $taxonomy, $args = array( 'description' => $timezone ) );
}
}
}
}
8 changes: 4 additions & 4 deletions inc/meta-box-models.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public function validate_datetime( $post_ID, $field, &$validated ) {
}
}
foreach ( $terms_to_remove as $t ) {
$this->Callbacks->date( $post_ID, $field['taxonomy'], $multiple = $field['multiple'], null, $t );
$this->Callbacks->date( $post_ID, $field['taxonomy'], $multiple = $field['multiple'], null, null, $t );
}
$data = array($field['key'] => '');
if ( $field['type'] != 'time') {
Expand Down Expand Up @@ -336,7 +336,7 @@ public function validate_datetime( $post_ID, $field, &$validated ) {
$data[$field['key']] .= ' ' . $_POST[$timezone][0];
}
}
$timezone = null;
$timezone = $_POST[$field['key'] . '_timezone'][0];
if ( $field['type'] == 'date' ) {
$date = DateTime::createFromFormat('F j Y', $data[$field['key']]);
} else {
Expand All @@ -354,9 +354,9 @@ public function validate_datetime( $post_ID, $field, &$validated ) {
}
if ( $date ) {
if ( $field['key'] == $field['taxonomy'] ) {
$this->Callbacks->date( $post_ID, $field['taxonomy'], $multiple = $field['multiple'], $date, null );
$this->Callbacks->date( $post_ID, $field['taxonomy'], $multiple = $field['multiple'], $date, $timezone, null );
}
$validated = array( 'date' => $date->format( Datetime::ISO8601 ), 'timezone' => $_POST[$field['key'] . '_timezone'][0] );
$validated = array( 'date' => $date->format( 'c' ), 'timezone' => $timezone );
}
}

Expand Down
32 changes: 29 additions & 3 deletions tests/test-meta-box-callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function testDateCallsGetTermByWhenAttemptingToDeleteATerm() {
$_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 );
$c->date( 0, 'tax', false, array(), null, 0 );
// Assert
}

Expand Down Expand Up @@ -94,7 +94,7 @@ function testDateCallsRemovePostTermWhenAttemptingToDeleteATerm() {
$c = new Callbacks();
$c->replace_Taxonomy( $Taxonomy );
// Act
$c->date( 0, 'tax', false, array(), 0 );
$c->date( 0, 'tax', false, array(), null, 0 );
// Assert
}

Expand Down Expand Up @@ -124,9 +124,35 @@ function testRmTermKeyDateExpectsRemovePostTermCalled() {
$c->replace_Taxonomy($Mock);

// Act
$c->date($post_id, $taxonomy, false, array(), 1);
$c->date($post_id, $taxonomy, false, array(), null, 1);

// Assert: test fails if remove_post_term is called more or fewer than 1 time.
}

/**
* Tests whether the date() method properly calls wp_set_objeect_terms,
* get_term_by, wp_update_term
*
* @group isolated
* @group stable
* @group date
* @group taxonomy_save
*
*/
function testDateGetTermByAndWPUpdateTermCalledOnceForDateData() {
// Arrange
\WP_Mock::wpFunction('wp_set_object_terms', array('times' => 1));
$term = new \StdClass;
$term->term_id = 1;
\WP_Mock::wpFunction('get_term_by', array('times' => 1, 'return' => $term));
\WP_Mock::wpFunction('wp_update_term', array('times' => 1));
$post_id = 0;
$taxonomy = 'category';
$data = Datetime::createFromFormat('F j Y', 'January 12 2000');
// Act
$c = new Callbacks();
$c->date($post_id, $taxonomy, false, $data, 'America/New_York');
// Assert
}
}
?>
3 changes: 2 additions & 1 deletion tests/test-meta_box_models.php
Original file line number Diff line number Diff line change
Expand Up @@ -938,7 +938,8 @@ function testInvalidDateFieldExpectsCallbackNotCalled() {
'post_ID' => 1,
'category_year' => '' ,
'category_month' => 'January',
'category_day' => '01');
'category_day' => '01',
'category_timezone' => 'America/New_York');
$anything = $this->anything();

// act
Expand Down

0 comments on commit 932c8f0

Please sign in to comment.