From 1acc8110d5a53023b5d4aebac055258cab83619d Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 27 Mar 2023 16:40:15 +0200 Subject: [PATCH 01/18] Date field mapper --- src/Pods/Data/Map_Field_Values.php | 58 ++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index 06c79b8622..7912406ccc 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -58,6 +58,7 @@ public function map_value( $field, $traverse, $field_data, $obj = null, $params 'context_info', 'calculation', 'image_fields', + 'date_fields', 'avatar', ]; @@ -703,6 +704,63 @@ public function image_fields( $field, $traverse, $field_data, $obj ) { return pods_traverse( $traverse_params, $value ); } + /** + * Map the date field value. + * + * @since 2.9.14 + * + * @param string $field The first field name in the path. + * @param string[] $traverse The list of fields in the path excluding the first field name. + * @param null|Field|Object_Field $field_data The field data or null if not a field. + * @param Pods|null $obj The Pods object or null if not set. + * + * @return null|mixed The date field value or null if there was no match. + */ + public function date_fields( $field, $traverse, $field_data, $obj = null ) { + + // Default date field handlers. + $date_fields = array( + 'date', + 'time', + 'datetime', + ); + + // Skip if the field exists. + if ( $field_data && ! in_array( $field_data->get_type(), $date_fields, true ) ) { + return null; + } + + $object_type = $obj ? $obj->pod_data->get_type() : null; + + if ( 'post_type' === $object_type ) { + $date_fields[] = 'post_date'; + $date_fields[] = 'post_date_gmt'; + } + + // Handle special field tags. + if ( ! in_array( $field, $date_fields, true ) ) { + return null; + } + + $value = $obj->field( $field, array( 'raw' => true ) ); + + if ( empty( $traverse[0] ) || empty( $traverse[1] ) ) { + return null; + } + + switch ( $traverse[0] ) { + case '_format': + $format = $traverse[1]; + $value = date_i18n( $format, strtotime( $value ) ); + break; + default: + return null; + break; + } + + return $value; + } + /** * Map the matching avatar field value. * From 7bc694daa76372146893e1329bbaceadaae7755e Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 27 Mar 2023 16:40:41 +0200 Subject: [PATCH 02/18] Include modified fields --- src/Pods/Data/Map_Field_Values.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index 7912406ccc..47ae79080e 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -735,6 +735,8 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { if ( 'post_type' === $object_type ) { $date_fields[] = 'post_date'; $date_fields[] = 'post_date_gmt'; + $date_fields[] = 'post_modified'; + $date_fields[] = 'post_modified_gmt'; } // Handle special field tags. From 5aea65a102be74bbfa19b25ccf1ea304a4327de2 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 27 Mar 2023 16:45:26 +0200 Subject: [PATCH 03/18] Wordpress default formats --- src/Pods/Data/Map_Field_Values.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index 47ae79080e..0aac581376 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -746,13 +746,30 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { $value = $obj->field( $field, array( 'raw' => true ) ); + // No fields modifiers found. if ( empty( $traverse[0] ) || empty( $traverse[1] ) ) { return null; } switch ( $traverse[0] ) { case '_format': - $format = $traverse[1]; + $format = $traverse[1]; + $wp_format = in_array( strtolower( $format ), array( 'wp', 'wordpress' ), true ); + + if ( $wp_format ) { + switch ( $field ) { + case 'date': + $format = get_option( 'date_format' ); + break; + case 'time': + $format = get_option( 'time_format' ); + break; + default: + $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + break; + } + } + $value = date_i18n( $format, strtotime( $value ) ); break; default: From 694ca34ea2fc0dac5ea1c1106cefe87b5e2ac8bd Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 27 Mar 2023 16:50:35 +0200 Subject: [PATCH 04/18] Improve default support --- src/Pods/Data/Map_Field_Values.php | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index 0aac581376..a5099a70e1 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -756,18 +756,22 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { $format = $traverse[1]; $wp_format = in_array( strtolower( $format ), array( 'wp', 'wordpress' ), true ); - if ( $wp_format ) { - switch ( $field ) { - case 'date': - $format = get_option( 'date_format' ); - break; - case 'time': - $format = get_option( 'time_format' ); - break; - default: - $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); - break; - } + switch ( $format ) { + case 'date': + case 'wp_date': + case 'wordpress_date': + $format = get_option( 'date_format' ); + break; + case 'time': + case 'wp_time': + case 'wordpress_time': + $format = get_option( 'time_format' ); + break; + case 'datetime': + case 'wp': + case 'wordpress': + $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + break; } $value = date_i18n( $format, strtotime( $value ) ); From 3b96da764d8dca1232dbabcbdc5eb28f69a748cb Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 27 Mar 2023 16:51:06 +0200 Subject: [PATCH 05/18] Remove redundant code --- src/Pods/Data/Map_Field_Values.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index a5099a70e1..abd9953e74 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -754,7 +754,6 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { switch ( $traverse[0] ) { case '_format': $format = $traverse[1]; - $wp_format = in_array( strtolower( $format ), array( 'wp', 'wordpress' ), true ); switch ( $format ) { case 'date': From aee04c08f605ffd1b254f637001e6bba6702abb2 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 11:44:21 +0200 Subject: [PATCH 06/18] Add test for date field mapper --- tests/codeception/wpunit/Pods/MappingTest.php | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/codeception/wpunit/Pods/MappingTest.php b/tests/codeception/wpunit/Pods/MappingTest.php index e2ba04a505..533cfdfe47 100644 --- a/tests/codeception/wpunit/Pods/MappingTest.php +++ b/tests/codeception/wpunit/Pods/MappingTest.php @@ -557,4 +557,33 @@ public function test_image_fields_image_attachment() { $this->assertContains( '-123x123.jpg', pods_data_field( null, 'image_attachment_src.' . $attachment_id . '.123x123' ) ); } + /** + * @covers \Pods\Data\Map_Field_Values::date_fields + */ + public function test_date_fields() { + + $api = pods_api(); + + $datetime_field = 'test_datetime'; + + $field_params = [ + 'pod_id' => $this->pod_id, + 'name' => $datetime_field, + 'label' => 'Test Date Time', + 'type' => 'datetime', + ]; + + $api->save_field( $field_params ); + + $pod = pods( $this->pod_name, $this->item_id ); + + $timestamp = time(); + + $pod->save( array( $datetime_field => date_i18n( \PodsField_DateTime::$storage_format, $timestamp ) ) ); + + $format = 'y-m-d'; + + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + } + } From 9433688f8074fed04a34c5b72a6d6070428a238c Mon Sep 17 00:00:00 2001 From: Jory Hogeveen Date: Wed, 29 Mar 2023 11:56:46 +0200 Subject: [PATCH 07/18] Apply suggestions from code review Co-authored-by: Scott Kingsley Clark --- src/Pods/Data/Map_Field_Values.php | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index abd9953e74..31858f9e4d 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -725,27 +725,21 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { 'datetime', ); - // Skip if the field exists. + // Skip if the field exists and is NOT a date/time/datetime field type. if ( $field_data && ! in_array( $field_data->get_type(), $date_fields, true ) ) { return null; } - $object_type = $obj ? $obj->pod_data->get_type() : null; - - if ( 'post_type' === $object_type ) { - $date_fields[] = 'post_date'; - $date_fields[] = 'post_date_gmt'; - $date_fields[] = 'post_modified'; - $date_fields[] = 'post_modified_gmt'; - } - - // Handle special field tags. - if ( ! in_array( $field, $date_fields, true ) ) { - return null; + // Handle getting current field value. + if ( $field_data ) { + $value = $obj->field( $field, [ + 'raw' => true, + 'bypass_map_field_values' => true, + ] ); + } else { + $value = current_time( 'mysql' ); } - $value = $obj->field( $field, array( 'raw' => true ) ); - // No fields modifiers found. if ( empty( $traverse[0] ) || empty( $traverse[1] ) ) { return null; From cb952e9bd18666ad4e759108a0c02e5362d27657 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 11:57:35 +0200 Subject: [PATCH 08/18] Reference to PodsForm::date_field_types --- src/Pods/Data/Map_Field_Values.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index 31858f9e4d..ee6d5de01e 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -718,7 +718,10 @@ public function image_fields( $field, $traverse, $field_data, $obj ) { */ public function date_fields( $field, $traverse, $field_data, $obj = null ) { - // Default date field handlers. + /** + * Default date field handlers. + * @see \PodsForm::date_field_types + */ $date_fields = array( 'date', 'time', From 7ec5480822ab6aaee1be5cc94e18e7a585c38e9f Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 11:58:11 +0200 Subject: [PATCH 09/18] Do not return a default value --- src/Pods/Data/Map_Field_Values.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index ee6d5de01e..eed8fee022 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -740,7 +740,7 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { 'bypass_map_field_values' => true, ] ); } else { - $value = current_time( 'mysql' ); + return null; } // No fields modifiers found. From 2d7e82b4b68da6325e02b0f2ebaa56b58e60408c Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 12:00:20 +0200 Subject: [PATCH 10/18] Simply require field data --- src/Pods/Data/Map_Field_Values.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index eed8fee022..9bfc59b291 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -718,6 +718,11 @@ public function image_fields( $field, $traverse, $field_data, $obj ) { */ public function date_fields( $field, $traverse, $field_data, $obj = null ) { + // Skip if there is no information about this field. + if ( ! $field_data ) { + return null; + } + /** * Default date field handlers. * @see \PodsForm::date_field_types @@ -728,20 +733,16 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { 'datetime', ); - // Skip if the field exists and is NOT a date/time/datetime field type. - if ( $field_data && ! in_array( $field_data->get_type(), $date_fields, true ) ) { + // Skip if the field is NOT a date/time/datetime field type. + if ( ! in_array( $field_data->get_type(), $date_fields, true ) ) { return null; } // Handle getting current field value. - if ( $field_data ) { - $value = $obj->field( $field, [ - 'raw' => true, - 'bypass_map_field_values' => true, - ] ); - } else { - return null; - } + $value = $obj->field( $field, [ + 'raw' => true, + 'bypass_map_field_values' => true, + ] ); // No fields modifiers found. if ( empty( $traverse[0] ) || empty( $traverse[1] ) ) { From ee2e9ea66fec9f2bc3d63b239e5ce61901aeaeff Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 12:08:15 +0200 Subject: [PATCH 11/18] Default formatting based on field type --- src/Pods/Data/Map_Field_Values.php | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/Pods/Data/Map_Field_Values.php b/src/Pods/Data/Map_Field_Values.php index 9bfc59b291..c283ba2ab6 100644 --- a/src/Pods/Data/Map_Field_Values.php +++ b/src/Pods/Data/Map_Field_Values.php @@ -733,8 +733,10 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { 'datetime', ); + $field_type = $field_data->get_type(); + // Skip if the field is NOT a date/time/datetime field type. - if ( ! in_array( $field_data->get_type(), $date_fields, true ) ) { + if ( ! in_array( $field_type, $date_fields, true ) ) { return null; } @@ -751,7 +753,7 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { switch ( $traverse[0] ) { case '_format': - $format = $traverse[1]; + $format = $traverse[1]; switch ( $format ) { case 'date': @@ -767,7 +769,13 @@ public function date_fields( $field, $traverse, $field_data, $obj = null ) { case 'datetime': case 'wp': case 'wordpress': - $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + if ( 'time' === $field_type ) { + $format = get_option( 'time_format' ); + } elseif ( 'date' === $field_type ) { + $format = get_option( 'date_format' ); + } else { + $format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + } break; } From eca2f3c08c88ac426829c20a99f5056bd9b841fa Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 12:11:46 +0200 Subject: [PATCH 12/18] Add tests for predefined formats --- tests/codeception/wpunit/Pods/MappingTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/codeception/wpunit/Pods/MappingTest.php b/tests/codeception/wpunit/Pods/MappingTest.php index 533cfdfe47..081348c228 100644 --- a/tests/codeception/wpunit/Pods/MappingTest.php +++ b/tests/codeception/wpunit/Pods/MappingTest.php @@ -584,6 +584,25 @@ public function test_date_fields() { $format = 'y-m-d'; $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + $format = 'Y.m.d'; + + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + $format = 'wp'; + $wp_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + $format = 'wp_date'; + $wp_format = get_option( 'date_format' ); + + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + $format = 'wp_time'; + $wp_format = get_option( 'time_format' ); + + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); } } From 87365f09e7f0e1c119186aa859f600f9d7417c64 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 12:16:10 +0200 Subject: [PATCH 13/18] Tests for all date/time field types --- tests/codeception/wpunit/Pods/MappingTest.php | 73 ++++++++++++++++++- 1 file changed, 71 insertions(+), 2 deletions(-) diff --git a/tests/codeception/wpunit/Pods/MappingTest.php b/tests/codeception/wpunit/Pods/MappingTest.php index 081348c228..d559b47b87 100644 --- a/tests/codeception/wpunit/Pods/MappingTest.php +++ b/tests/codeception/wpunit/Pods/MappingTest.php @@ -564,6 +564,9 @@ public function test_date_fields() { $api = pods_api(); + /** + * DATETIME FIELD + */ $datetime_field = 'test_datetime'; $field_params = [ @@ -581,11 +584,11 @@ public function test_date_fields() { $pod->save( array( $datetime_field => date_i18n( \PodsField_DateTime::$storage_format, $timestamp ) ) ); - $format = 'y-m-d'; + $format = 'y-m-d H:s'; $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); - $format = 'Y.m.d'; + $format = 'Y.m.d H|s'; $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); @@ -603,6 +606,72 @@ public function test_date_fields() { $wp_format = get_option( 'time_format' ); $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + /** + * DATE FIELD + */ + $date_field = 'test_date'; + + $field_params = [ + 'pod_id' => $this->pod_id, + 'name' => $date_field, + 'label' => 'Test Date', + 'type' => 'date', + ]; + + $api->save_field( $field_params ); + + $pod = pods( $this->pod_name, $this->item_id ); + + $timestamp = time(); + + $pod->save( array( $datetime_field => date_i18n( \PodsField_Date::$storage_format, $timestamp ) ) ); + + $format = 'y-m-d'; + + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + $format = 'Y.m.d'; + + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + $format = 'wp'; + $wp_format = get_option( 'date_format' ); + + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + /** + * TIME FIELD + */ + $time_field = 'test_time'; + + $field_params = [ + 'pod_id' => $this->pod_id, + 'name' => $time_field, + 'label' => 'Test Time', + 'type' => 'time', + ]; + + $api->save_field( $field_params ); + + $pod = pods( $this->pod_name, $this->item_id ); + + $timestamp = time(); + + $pod->save( array( $datetime_field => date_i18n( \PodsField_Time::$storage_format, $timestamp ) ) ); + + $format = 'H:i:s'; + + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + $format = 'H|i'; + + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + + $format = 'wp'; + $wp_format = get_option( 'time_format' ); + + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); } } From 17db26e81ccf7a32f5eb5f8286502c3c6cbcf469 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 12:17:04 +0200 Subject: [PATCH 14/18] Fix var names --- tests/codeception/wpunit/Pods/MappingTest.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/tests/codeception/wpunit/Pods/MappingTest.php b/tests/codeception/wpunit/Pods/MappingTest.php index d559b47b87..c8e803401a 100644 --- a/tests/codeception/wpunit/Pods/MappingTest.php +++ b/tests/codeception/wpunit/Pods/MappingTest.php @@ -625,20 +625,20 @@ public function test_date_fields() { $timestamp = time(); - $pod->save( array( $datetime_field => date_i18n( \PodsField_Date::$storage_format, $timestamp ) ) ); + $pod->save( array( $date_field => date_i18n( \PodsField_Date::$storage_format, $timestamp ) ) ); $format = 'y-m-d'; - $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $date_field . '._format.' . $format ) ); $format = 'Y.m.d'; - $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $date_field . '._format.' . $format ) ); $format = 'wp'; $wp_format = get_option( 'date_format' ); - $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $date_field . '._format.' . $format ) ); /** * TIME FIELD @@ -658,20 +658,20 @@ public function test_date_fields() { $timestamp = time(); - $pod->save( array( $datetime_field => date_i18n( \PodsField_Time::$storage_format, $timestamp ) ) ); + $pod->save( array( $time_field => date_i18n( \PodsField_Time::$storage_format, $timestamp ) ) ); $format = 'H:i:s'; - $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $time_field . '._format.' . $format ) ); $format = 'H|i'; - $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $time_field . '._format.' . $format ) ); $format = 'wp'; $wp_format = get_option( 'time_format' ); - $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $time_field . '._format.' . $format ) ); } } From 3d19a600b7468249a03b90a18c3ebf4000dee663 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 13:05:30 +0200 Subject: [PATCH 15/18] Do not parse through field methods if the value is already mapped --- classes/Pods.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/classes/Pods.php b/classes/Pods.php index 424c3194d3..d87e75a1df 100644 --- a/classes/Pods.php +++ b/classes/Pods.php @@ -827,6 +827,7 @@ public function field( $name, $single = null, $raw = false ) { $is_tableless_field = in_array( $field_type, $tableless_field_types, true ); $is_repeatable_field = $field_data instanceof Field && $field_data->is_repeatable(); $is_pick_field = 'pick' === $field_type; + $is_mapped = false; // Handle output specific handling. if ( $is_pick_field ) { @@ -925,11 +926,11 @@ public function field( $name, $single = null, $raw = false ) { $value = $map_field_values->map_value( $first_field, $traverse_fields, $is_field_set ? $field_data : null, $this, $params ); - $object_field_found = null !== $value; + $is_mapped = null !== $value; } // Continue regular field parsing. - if ( false === $object_field_found ) { + if ( false === $object_field_found && false === $is_mapped ) { $params->traverse = array( $params->name ); if ( false !== strpos( $params->name, '.' ) ) { @@ -1696,7 +1697,7 @@ public function field( $name, $single = null, $raw = false ) { $value = array_merge( ...$value ); } - if ( ! empty( $field_data ) && ( $params->display || ! $params->raw ) && ! $params->in_form && ! $params->raw_display ) { + if ( ! $is_mapped && ! empty( $field_data ) && ( $params->display || ! $params->raw ) && ! $params->in_form && ! $params->raw_display ) { if ( $params->display || ( ( $params->get_meta || $params->deprecated ) && ! in_array( $field_data['type'], $tableless_field_types, true ) ) ) { $post_temp = false; $old_post = null; From 8917aac541e9b87e9de81ee84ac2098faf9c2162 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Wed, 29 Mar 2023 15:54:50 +0200 Subject: [PATCH 16/18] Refactor test method --- tests/codeception/wpunit/Pods/MappingTest.php | 69 +++++++++---------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/tests/codeception/wpunit/Pods/MappingTest.php b/tests/codeception/wpunit/Pods/MappingTest.php index c8e803401a..b99f783c0d 100644 --- a/tests/codeception/wpunit/Pods/MappingTest.php +++ b/tests/codeception/wpunit/Pods/MappingTest.php @@ -562,11 +562,12 @@ public function test_image_fields_image_attachment() { */ public function test_date_fields() { - $api = pods_api(); - /** - * DATETIME FIELD + * Setup. */ + + $api = pods_api(); + $datetime_field = 'test_datetime'; $field_params = [ @@ -578,12 +579,42 @@ public function test_date_fields() { $api->save_field( $field_params ); + $date_field = 'test_date'; + + $field_params = [ + 'pod_id' => $this->pod_id, + 'name' => $date_field, + 'label' => 'Test Date', + 'type' => 'date', + ]; + + $api->save_field( $field_params ); + + $time_field = 'test_time'; + + $field_params = [ + 'pod_id' => $this->pod_id, + 'name' => $time_field, + 'label' => 'Test Time', + 'type' => 'time', + ]; + + $api->save_field( $field_params ); + $pod = pods( $this->pod_name, $this->item_id ); $timestamp = time(); $pod->save( array( $datetime_field => date_i18n( \PodsField_DateTime::$storage_format, $timestamp ) ) ); + $pod->save( array( $date_field => date_i18n( \PodsField_Date::$storage_format, $timestamp ) ) ); + + $pod->save( array( $time_field => date_i18n( \PodsField_Time::$storage_format, $timestamp ) ) ); + + /** + * DATETIME FIELD + */ + $format = 'y-m-d H:s'; $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); @@ -610,22 +641,6 @@ public function test_date_fields() { /** * DATE FIELD */ - $date_field = 'test_date'; - - $field_params = [ - 'pod_id' => $this->pod_id, - 'name' => $date_field, - 'label' => 'Test Date', - 'type' => 'date', - ]; - - $api->save_field( $field_params ); - - $pod = pods( $this->pod_name, $this->item_id ); - - $timestamp = time(); - - $pod->save( array( $date_field => date_i18n( \PodsField_Date::$storage_format, $timestamp ) ) ); $format = 'y-m-d'; @@ -643,22 +658,6 @@ public function test_date_fields() { /** * TIME FIELD */ - $time_field = 'test_time'; - - $field_params = [ - 'pod_id' => $this->pod_id, - 'name' => $time_field, - 'label' => 'Test Time', - 'type' => 'time', - ]; - - $api->save_field( $field_params ); - - $pod = pods( $this->pod_name, $this->item_id ); - - $timestamp = time(); - - $pod->save( array( $time_field => date_i18n( \PodsField_Time::$storage_format, $timestamp ) ) ); $format = 'H:i:s'; From 8dd5cf9f181f7c79f1a222e6d5f1c8cc5510dfe3 Mon Sep 17 00:00:00 2001 From: JoryHogeveen Date: Mon, 3 Apr 2023 20:31:22 +0200 Subject: [PATCH 17/18] Add object field tests --- tests/codeception/wpunit/Pods/MappingTest.php | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/tests/codeception/wpunit/Pods/MappingTest.php b/tests/codeception/wpunit/Pods/MappingTest.php index b99f783c0d..b07ce56263 100644 --- a/tests/codeception/wpunit/Pods/MappingTest.php +++ b/tests/codeception/wpunit/Pods/MappingTest.php @@ -603,6 +603,46 @@ public function test_date_fields() { $pod = pods( $this->pod_name, $this->item_id ); + /** + * POST DATE FIELD + */ + + $timestamp = get_post_timestamp( $this->item_id ); + $field = 'post_date'; + $alias = 'date'; + + $format = 'y-m-d H:s'; + + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + + $format = 'Y.m.d H|s'; + + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + + $format = 'wp'; + $wp_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); + + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + + $format = 'wp_date'; + $wp_format = get_option( 'date_format' ); + + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + + $format = 'wp_time'; + $wp_format = get_option( 'time_format' ); + + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + + /** + * Custom field tests. + */ + $timestamp = time(); $pod->save( array( $datetime_field => date_i18n( \PodsField_DateTime::$storage_format, $timestamp ) ) ); From fe1a4b300fb3f3771674e3092148cf5ffb01c5e2 Mon Sep 17 00:00:00 2001 From: Scott Kingsley Clark Date: Wed, 5 Apr 2023 23:45:53 -0500 Subject: [PATCH 18/18] Adjust test --- tests/codeception/wpunit/Pods/MappingTest.php | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/codeception/wpunit/Pods/MappingTest.php b/tests/codeception/wpunit/Pods/MappingTest.php index b07ce56263..49e78d8f61 100644 --- a/tests/codeception/wpunit/Pods/MappingTest.php +++ b/tests/codeception/wpunit/Pods/MappingTest.php @@ -611,33 +611,33 @@ public function test_date_fields() { $field = 'post_date'; $alias = 'date'; - $format = 'y-m-d H:s'; + $format = 'y-m-d!!H:s'; - $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); - $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $field . '._format.' . $format ), 'Value of ' . $field . '._format.' . $format . ' is not what was expected' ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $alias . '._format.' . $format ), 'Value of ' . $alias . '._format.' . $format . ' is not what was expected' ); - $format = 'Y.m.d H|s'; + $format = 'Y.m.d!!H|s'; - $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); - $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $field . '._format.' . $format ), 'Value of ' . $field . '._format.' . $format . ' is not what was expected' ); + $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $alias . '._format.' . $format ), 'Value of ' . $alias . '._format.' . $format . ' is not what was expected' ); $format = 'wp'; $wp_format = get_option( 'date_format' ) . ' ' . get_option( 'time_format' ); - $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); - $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ), 'Value of ' . $field . '._format.' . $format . ' is not what was expected' ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ), 'Value of ' . $alias . '._format.' . $format . ' is not what was expected' ); $format = 'wp_date'; $wp_format = get_option( 'date_format' ); - $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); - $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ), 'Value of ' . $field . '._format.' . $format . ' is not what was expected' ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ), 'Value of ' . $alias . '._format.' . $format . ' is not what was expected' ); $format = 'wp_time'; $wp_format = get_option( 'time_format' ); - $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ) ); - $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ) ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $field . '._format.' . $format ), 'Value of ' . $field . '._format.' . $format . ' is not what was expected' ); + $this->assertEquals( date_i18n( $wp_format, $timestamp ), $pod->field( $alias . '._format.' . $format ), 'Value of ' . $alias . '._format.' . $format . ' is not what was expected' ); /** * Custom field tests. @@ -655,11 +655,11 @@ public function test_date_fields() { * DATETIME FIELD */ - $format = 'y-m-d H:s'; + $format = 'y-m-d!!H:s'; $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) ); - $format = 'Y.m.d H|s'; + $format = 'Y.m.d!!H|s'; $this->assertEquals( date_i18n( $format, $timestamp ), $pod->field( $datetime_field . '._format.' . $format ) );