@@ -363,7 +363,8 @@ public function get_usage_stat( $type, $stat = null ) {
363363 if ( 'limit ' === $ stat && isset ( $ this ->usage [ $ type ]['usage ' ] ) ) {
364364 $ value = $ this ->usage [ $ type ]['usage ' ];
365365 } elseif ( 'used_percent ' === $ stat && isset ( $ this ->usage [ $ type ]['credits_usage ' ] ) ) {
366- $ value = $ this ->usage [ $ type ]['credits_usage ' ];
366+ // Calculate percentage based on credit limit and usage.
367+ $ value = round ( $ this ->usage [ $ type ]['credits_usage ' ]/$ this ->usage ['credits ' ]['limit ' ] * 100 , 2 );
367368 }
368369 }
369370 }
@@ -432,28 +433,27 @@ public function get_config() {
432433 */
433434 public function usage_notices () {
434435 if ( ! empty ( $ this ->usage ) ) {
435- $ usage_type = 'used_percent ' ;
436- if ( isset ( $ this ->usage ['credits ' ] ) ) {
437- $ usage_type = 'credits_usage ' ;
438- }
439436 foreach ( $ this ->usage as $ stat => $ values ) {
440437
441- if ( ! is_array ( $ values ) || ! isset ( $ values [ $ usage_type ] ) || 0 > $ values [ $ usage_type ] ) {
438+ if ( ! is_array ( $ values ) ) {
439+ continue ;
440+ }
441+ $ usage = $ this ->get_usage_stat ( $ stat , 'used_percent ' );
442+ if ( empty ( $ usage ) ) {
442443 continue ;
443444 }
444-
445445 $ link = null ;
446446 $ link_text = null ;
447- if ( 90 <= $ values [ $ usage_type ] ) {
447+ if ( 90 <= $ usage ) {
448448 // 90% used - show error.
449449 $ level = 'error ' ;
450450 $ link = 'https://cloudinary.com/console/lui/upgrade_options ' ;
451451 $ link_text = __ ( 'upgrade your account ' , 'cloudinary ' );
452- } elseif ( 80 <= $ values [ $ usage_type ] ) {
453- $ level = 'warning ' ;
452+ } elseif ( 80 <= $ usage ) {
453+ $ level = 'warning ' ;
454454 $ link_text = __ ( 'upgrade your account ' , 'cloudinary ' );
455- } elseif ( 70 <= $ values [ $ usage_type ] ) {
456- $ level = 'neutral ' ;
455+ } elseif ( 70 <= $ usage ) {
456+ $ level = 'neutral ' ;
457457 $ link_text = __ ( 'upgrade your account ' , 'cloudinary ' );
458458 } else {
459459 continue ;
@@ -465,7 +465,7 @@ public function usage_notices() {
465465 'cloudinary '
466466 ),
467467 ucwords ( $ stat ),
468- $ values [ $ usage_type ] . '% ' ,
468+ $ usage . '% ' ,
469469 $ link ,
470470 $ link_text
471471 );
0 commit comments