Automattic / msm-sitemap

Comprehensive sitemaps for your WordPress VIP site. Joint collaboration between Metro.co.uk, WordPress VIP, Alley Interactive, Maker Media, 10up, and others.
73 stars 37 forks source link

PHP 8.1: Fatal error, when 'post_date' is '0000-00-00 00:00:00' #171

Closed zablose closed 1 year ago

zablose commented 1 year ago

Hi there,

If a published post of any type has 'post_date' equals to '0000-00-00 00:00:00', on PHP 8.1, when generating sitemap, fatal error will be thrown.

wp msm-sitemap generate-sitemap

Fatal error: Uncaught TypeError: checkdate(): Argument #2 ($day) must be of type int, string given in /var/www/html/wp-includes/functions.php:7071
Stack trace:
#0 /var/www/html/wp-includes/functions.php(7071): checkdate('-1', '', '22-1')
#1 /var/www/html/wp-includes/post.php(4918): wp_checkdate('-1', '', '22-1', '22-11-18')
#2 /var/www/html/wp-includes/post.php(4189): wp_resolve_post_date('22-11-18', '')
#3 /var/www/html/wp-content/plugins/msm-sitemap/msm-sitemap.php(435): wp_insert_post(Array)
#4 /var/www/html/wp-content/plugins/msm-sitemap/includes/wp-cli.php(154): Metro_Sitemap::generate_sitemap_for_date('22-11-18')
...

The problem in this SQL query.

Due to lack of check for zero date, function 'get_post_year_range' returns range [0,2022]. After that function 'check_year_has_posts' returns [22,2022] and this is how we are getting date '22-11-18'.

Suggested fix:

$oldest_post_date_year = $wpdb->get_var( "SELECT DISTINCT YEAR(post_date) as year FROM $wpdb->posts WHERE post_status = 'publish' AND post_date > 0 ORDER BY year ASC LIMIT 1" );

I hope, you will find time, to fix this.

Thank you.

mchanDev commented 1 year ago

Thanks for this, this has been accepted in #174