From ab7d1483cb063ae8a14c0af08098d3cf5bee1d71 Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Tue, 31 Jan 2023 10:00:45 +0000 Subject: [PATCH 1/2] Return -1, 0, or 1 on uksort --- php/class-connect.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/php/class-connect.php b/php/class-connect.php index f48f7fb9c..99ec20335 100644 --- a/php/class-connect.php +++ b/php/class-connect.php @@ -452,7 +452,14 @@ public function history( $days = 1 ) { uksort( $history[ $plan ], static function ( $a, $b ) { - return strtotime( $a ) > strtotime( $b ); + $a = strtotime( $a ); + $b = strtotime( $b ); + + if ( $a === $b ) { + return 0; + } + + return $a > $b ? - 1 : 1; } ); $history[ $plan ] = array_slice( $history[ $plan ], -30 ); From a716a08061cb62f01f05904883c83457345fac1c Mon Sep 17 00:00:00 2001 From: Marco Pereirinha Date: Tue, 31 Jan 2023 10:01:22 +0000 Subject: [PATCH 2/2] Fix the upgrade of the history, and flush it, if needed --- php/class-connect.php | 47 +++++++++++++++++-------------------------- 1 file changed, 19 insertions(+), 28 deletions(-) diff --git a/php/class-connect.php b/php/class-connect.php index 99ec20335..d7387bd73 100644 --- a/php/class-connect.php +++ b/php/class-connect.php @@ -480,38 +480,29 @@ static function ( $a, $b ) { public function upgrade_settings( $previous_version, $new_version ) { // Check if we need to upgrade the history. if ( version_compare( $previous_version, '3.1.0', '<' ) ) { - $history = get_option( self::META_KEYS['history'], array() ); - $plan = false; - - if ( ! empty( $this->usage['plan'] ) ) { - $plan = $this->usage['plan']; - } elseif ( ! empty( $this->credentials['cloud_name'] ) ) { - $plan = $this->credentials['cloud_name']; - } - - // Check whether history has migrated. - if ( ! empty( $plan ) && ! empty( $history[ $plan ] ) ) { - return; - } - - // Fix history. - $new_history = array(); - foreach ( $history as $date => $data ) { - $new_history[ $plan ][ $date ] = $data; - } + add_action( + 'cloudinary_ready', + function () { + $history = get_option( self::META_KEYS['history'], array() ); + $plan = false; + + if ( ! empty( $this->usage['plan'] ) ) { + $plan = $this->usage['plan']; + } elseif ( ! empty( $this->credentials['cloud_name'] ) ) { + $plan = $this->credentials['cloud_name']; + } - foreach ( $new_history as &$data ) { - uksort( - $data, - static function ( $a, $b ) { - return strtotime( $a ) < strtotime( $b ); + // Check whether history has migrated. + if ( ! empty( $plan ) && ! empty( $history[ $plan ] ) ) { + return; } - ); - $data = array_reverse( array_slice( $data, 0, 30 ) ); - } + // Cleanup history. + $new_history = array(); - update_option( self::META_KEYS['history'], $new_history, false ); + update_option( self::META_KEYS['history'], $new_history, false ); + } + ); } }