cabrerahector / wordpress-popular-posts

WordPress Popular Posts - A highly customizable WordPress widget that displays your most popular posts.
https://wordpress.org/plugins/wordpress-popular-posts/
GNU General Public License v2.0
279 stars 83 forks source link

Helper::is_timestamp() is throwing a PHP notice when the Pageviews cache feature is enabled #268

Closed cabrerahector closed 4 years ago

cabrerahector commented 4 years ago

Describe the bug

An user reported here that WordPress Popular Posts throws the following PHP notice when the Pageviews cache feature is enabled:

E_WARNING: DateTime::construct(): Failed to parse time string (@2020-08-11 18:26) at position 8 (-): Double timezone specification in DateTime::construct called at /wp-content/plugins/wordpress-popular-posts/src/Helper.php (164)

in WordPressPopularPosts\Helper::is_timestamp called at /wp-content/plugins/wordpress-popular-posts/src/Rest/Controller.php (207)

in WordPressPopularPosts\Rest\Controller::update_views_count called at /wp-includes/rest-api/class-wp-rest-server.php (1015) in WP_REST_Server::dispatch called at /wp-includes/rest-api/class-wp-rest-server.php (342) in WP_REST_Server::serve_request called at /wp-includes/rest-api.php (312) in rest_api_loaded called at /wp-includes/class-wp-hook.php (287) in WP_Hook::apply_filters called at /wp-includes/class-wp-hook.php (311) in WP_Hook::do_action called at /wp-includes/plugin.php (544) in do_action_ref_array called at /wp-includes/class-wp.php (388) in WP::parse_request called at /wp-includes/class-wp.php (739) in WP::main called at /wp-includes/functions.php (1274) in wp called at /wp-blog-header.php (16) in require called at /index.php (17)

I was able to reproduce the issue by executing the is_timestamp() function several times in a for loop. Oddly enough, it didn't happen all the time. Sometimes the function returned a boolean value (as expected), others it triggered said error.

Environment:

cabrerahector commented 4 years ago

This version of the is_timestamp() function apparently works as expected according to the user who was nice enough to test it on their environment:

/**
 * Checks whether a string is a valid timestamp.
 *
 * @since   5.2.0
 * @param   string  $string
 * @return  bool
 */
public static function is_timestamp($string)
{
    if (
        ( is_int($string) || ctype_digit($string) ) 
        && strtotime(date('Y-m-d H:i:s', $string)) === (int) $string
    ) {
        return true;
    }

    return false;
}