Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make text_datetime_timestamp_timezone save the correct Datetime object #1468

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

mklasen
Copy link

@mklasen mklasen commented Oct 20, 2022

Description

Add the timezone offset on the saved Datetime object and remove it on field load.

Note: this PR needs improvement (see high risk below)

Motivation and Context

Currently, the timezone offset is only applied to the UTC saved date. This PR applies the offset on the stored Datetime object as well.
Fixes #1465

Risk Level

Low risk - small changes, all related to the text_datetime_timestamp_timezone field.

High risk that needs to be resolved: Existing data will also receive an offset modification on field load. This means the input value changes when the user loads the page, the new value is saved when the user saves the page.

Testing procedure

  1. Add code sample below to your environment
  2. Create a new post
  3. See how the Date is not saved correctly
  4. Apply the changes in this PR

The code sample below shows the issue:

<?php
/**
 * Plugin Name: CMB2 text_datetime_timestamp_timezone Test
 */

add_action(
	'cmb2_admin_init',
	function() {

		$details = new_cmb2_box(
			array(
				'id'           => 'cmb2_test',
				'title'        => __( 'Details', 'cmb2' ),
				'object_types' => array( 'post' ),
			)
		);

		$details->add_field(
			array(
				'name'        => 'Date',
				'type'        => 'text_datetime_timestamp_timezone',
				'id'          => 'date',
				'after_field' => function() {
					$serialized_date = get_post_meta( get_the_ID(), 'date', true );
					if ( ! empty( $serialized_date ) ) {
						$date = maybe_unserialize( $serialized_date );
						echo '<br><br>Date/Time: ' . $date->format( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ) . ' e' ) . '<br>';

					}
				},
			)
		);

	}
);

Types of changes

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

Screenshots

Screenshot of UTC saved date and saved object after save in the current develop branch:

image

Screenshot of UTC saved date and saved object after save in this branch:

image

@mklasen mklasen changed the title Make sure text_datetime_timestamp_timezone saves the correct Datetime object Make text_datetime_timestamp_timezone save the correct Datetime object Oct 20, 2022
@jtsternberg
Copy link
Member

Thank you for submitting this, and doing the work. This looks good, but the "high risk" bit you mentioned will need to be resolved. I don't have time to investigate/resolve, so I'll need your thoughts/work on it instead. We could maybe always use the saved UTC value to build the value to be seen, and then the offset wouldn't need to be adjusted.

@mklasen
Copy link
Author

mklasen commented Oct 24, 2022

Great, that sounds like a good approach. I'll have another look later this week!

@mklasen
Copy link
Author

mklasen commented Nov 16, 2022

Ok - that was a lie - it IS on my list to get back to though!

@mklasen
Copy link
Author

mklasen commented Nov 18, 2022

I just had another look; I could just run a get_post_meta to retrieve the UTC value and populate the field values with that, but that doesn't really seem to follow the CMB2 standards..

The UTC value is stored as a supporting field.

I'd love to discuss this a bit more, to see what the right approach would be. Meanwhile, maybe just update docs to advice using the UTC saved value?

@jtsternberg
Copy link
Member

jtsternberg commented Dec 2, 2022

Meanwhile, maybe just update docs to advice using the UTC saved value?

I definitely think that's the safest approach that can be done now, if you're up for it. the Docs are a wiki, so you should be able to update them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

text_datetime_timestamp_timezone field returns wrong datetime object
2 participants