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

fix: change item_date to convert to site timezone on import #939

Merged
merged 1 commit into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions includes/admin/feedzy-rss-feeds-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ private function run_job( $job, $max ) {
}

// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$item_date = date( get_option( 'date_format' ) . ' at ' . get_option( 'time_format' ), $item['item_date'] );
$item_date = wp_date( get_option( 'date_format' ) . ' at ' . get_option( 'time_format' ), $item['item_date'] );
$item_date = $item['item_date_formatted'];

// Transform any structure like [[{"value":"[#item_title]"}]] to [#item_title].
Expand Down Expand Up @@ -1576,9 +1576,9 @@ private function run_job( $job, $max ) {
}

// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$item_date = date( 'Y-m-d H:i:s', $item['item_date'] );
$item_date = wp_date( 'Y-m-d H:i:s', $item['item_date'] );
// phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$now = date( 'Y-m-d H:i:s' );
$now = wp_date( 'Y-m-d H:i:s' );
if ( trim( $import_date ) === '' ) {
$post_date = $now;
}
Expand Down
66 changes: 66 additions & 0 deletions tests/test-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Test_Feedzy_Import extends WP_UnitTestCase {

private $import_limit = 1;

private $original_dates = array();

/**
* Sets up the test methods.
*/
Expand Down Expand Up @@ -303,6 +305,70 @@ public function test_attachement_import() {
$this->assertTrue( $attachments[0]->post_mime_type === 'image/jpeg' );
}

/**
* Utility method to capture feed dates before import.
*
* @param array $feed_items The feed items.
* @param array $options The options.
* @param mixed $feed The feed.
* @param string $feedURL The feed URL.
* @param int $sizes The sizes.
*
* @return array
*/
public function mock_feed_dates( $feed_items, $options, $feed, $feedURL, $sizes ) {
$formated_date = date( 'Y-m-d H:i:s', $feed_items[0]['item_date'] );
$this->original_dates[] = $formated_date;
return $feed_items;
}

/**
* Test date import with UTC zero.
*/
public function test_date_import_with_utc_zero() {
update_option( 'gmt_offset', 0 ); // ensure UTC is 0

add_filter( 'feedzy_get_feed_array', [ $this, 'mock_feed_dates' ], 10, 5 );

$post = $this->test_feedzy_imports( $this->get_rand_name(), $this->get_rand_name(), $this->get_two_rand_feeds(), '[#item_content]', false, 'post' );

$imported_date = $post->post_date;

// check if the date is in the original dates
$this->assertTrue( in_array( $imported_date, $this->original_dates, true ) );

remove_filter( 'feedzy_get_feed_array', [ $this, 'mock_feed_dates' ], 10, 5 );
}

/**
* Test date import with UTC +2.
*/
public function test_date_import_with_utc_plus_two() {
update_option( 'gmt_offset', 2 ); // ensure UTC is +2

add_filter( 'feedzy_get_feed_array', [ $this, 'mock_feed_dates' ], 10, 5 );

$post = $this->test_feedzy_imports( $this->get_rand_name(), $this->get_rand_name(), $this->get_two_rand_feeds(), '[#item_content]', false, 'post' );

$imported_date = $post->post_date;

// check if the date is not in the original dates
$this->assertTrue( ! in_array( $imported_date, $this->original_dates, true ) );

// shift original dates by 2 hours
$shifted_dates = array_map(
function( $date ) {
return date( 'Y-m-d H:i:s', strtotime( $date . ' +2 hours' ) );
},
$this->original_dates
);

// check if the imported date is in the shifted dates
$this->assertTrue( in_array( $imported_date, $shifted_dates, true ) );

remove_filter( 'feedzy_get_feed_array', [ $this, 'mock_feed_dates' ], 10, 5 );
update_option( 'gmt_offset', 0 ); // reset UTC
}

/**
* Utility method to generate a random 5 char string.
Expand Down
Loading