Skip to content

Commit

Permalink
Merge pull request #939 from Codeinwp/fix/import_timezone
Browse files Browse the repository at this point in the history
fix: change item_date to convert to site timezone on import
  • Loading branch information
vytisbulkevicius authored May 30, 2024
2 parents a9e78d6 + 40acd78 commit 8ddf302
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
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'];

// Get translated item title.
Expand Down Expand Up @@ -1581,9 +1581,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

0 comments on commit 8ddf302

Please sign in to comment.