Skip to content

Commit

Permalink
fix: prevent prompt expiration with invalid expiration date
Browse files Browse the repository at this point in the history
  • Loading branch information
adekbadek committed Jun 18, 2024
1 parent e13ba08 commit ad7b43d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions includes/class-newspack-popups-expiry.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,17 @@ public static function revert_expired_to_draft() {
'post_type' => Newspack_Popups::NEWSPACK_POPUPS_CPT,
'posts_per_page' => -1,
'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
'relation' => 'AND',
[
'key' => 'expiration_date',
'value' => gmdate( 'Y-m-d H:i:s' ),
'compare' => '<=',
],
[
'key' => 'expiration_date',
'compare' => 'REGEXP', // phpcs:ignore WordPressVIPMinimum.Performance.RegexpCompare.compare_compare
'value' => '[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}',
],
],
]
);
Expand Down
22 changes: 22 additions & 0 deletions tests/test-popups-expiry.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,26 @@ public function test_prompt_expiry_transition_post_status() {
// Check that the expiration_date meta is now deleted.
$this->assertEmpty( get_post_meta( $post_id, 'expiration_date', true ) );
}

/**
* Test invalid expiration_date value.
*/
public function test_prompt_expiry_invalid_meta() {
// Create a post with an invalid expiration date.
$post_id = $this->factory->post->create(
[
'post_type' => Newspack_Popups::NEWSPACK_POPUPS_CPT,
]
);
// Set post expiration date meta - for some reason setting this as 'meta_input' while post status is publish does not work.
update_post_meta( $post_id, 'expiration_date', '' );

$this->assertEquals( 'publish', get_post_status( $post_id ) );

// This will run in a cron job.
Newspack_Popups_Expiry::revert_expired_to_draft();

// Should not have been reverted to draft.
$this->assertEquals( 'publish', get_post_status( $post_id ) );
}
}

0 comments on commit ad7b43d

Please sign in to comment.