0; } /** * Below this balance the dashboard header shows the "Low credits!" badge (D-05). * * Passed to admin-dashboard.js as `lowCreditsThreshold`, so the badge the * server renders and the badge the script switches after a balance update * use the same rule. */ const LOW_CREDITS_THRESHOLD = 10; /** * Which credit badge the dashboard header shows for a balance. * * Without an API key the stored balance is not a known fact yet (the option * only defaults to 0), so no badge is shown. The first validation returns the * real balance and admin-dashboard.js then switches the badge, which keeps a * stale "No credits remaining!" from a key-less page load from standing next * to the fresh count, even if that script never runs. * * @param int $credits Remaining credit balance. * @param bool $has_api_key Whether an API key is configured for this site. * @return string 'empty', 'low' or '' (no badge). */ public static function get_warning_level( $credits, $has_api_key = true ) { if ( ! $has_api_key ) { return ''; } $credits = (int) $credits; if ( $credits <= 0 ) { return 'empty'; } if ( $credits < self::LOW_CREDITS_THRESHOLD ) { return 'low'; } return ''; } /** * The credit hint above the bulk optimization button. * * Each image uses one credit, so the image count is also the credit cost. * Both sentences follow the plural rules of the site language; up to 3.0.0 * a single image read "1 images will use 1 credits". admin-dashboard.js * builds the same two sentences with wp.i18n when the counts change. * * @param int $images Number of images the run would optimize. * @param int $credits Remaining credit balance. * @return string Translated hint, not escaped. */ public static function get_bulk_credit_info( $images, $credits ) { $images = (int) $images; $credits = (int) $credits; $cost = sprintf( /* translators: %1$d: number of images to optimize, also the number of credits the run uses (one per image). */ _n( 'Optimizing %1$d image will use %1$d credit.', 'Optimizing %1$d images will use %1$d credits.', $images, 'altimo' ), $images ); $balance = sprintf( /* translators: %d: number of credits currently available. */ _n( 'You have %d credit.', 'You have %d credits.', $credits, 'altimo' ), $credits ); return $cost . ' ' . $balance; } }