private static function determine_which_images_to_fetch(&$dimensions, &$urls_to_fetch)
{
foreach ($dimensions as $url => $value) {
// Check whether some other callback attached to the filter already provided dimensions for this image.
if (is_array($value)) {
continue;
}
$url_hash = md5($url);
$transient_name = sprintf('amp_img_%s', $url_hash);
$cached_dimensions = get_transient($transient_name);
// If we're able to retrieve the dimensions from a transient, set them and move on.
if (is_array($cached_dimensions)) {
$dimensions[$url] = array('width' => $cached_dimensions[0], 'height' => $cached_dimensions[1]);
continue;
}
// If the value in the transient reflects we couldn't get dimensions for this image the last time we tried, move on.
if (self::STATUS_FAILED_LAST_ATTEMPT === $cached_dimensions) {
$dimensions[$url] = false;
continue;
}
$transient_lock_name = sprintf('amp_lock_%s', $url_hash);
// If somebody is already trying to extract dimensions for this transient right now, move on.
if (false !== get_transient($transient_lock_name)) {
$dimensions[$url] = false;
continue;
}
// Include the image as a url to fetch.
$urls_to_fetch[$url]['url'] = $url;
$urls_to_fetch[$url]['transient_name'] = $transient_name;
$urls_to_fetch[$url]['transient_lock_name'] = $transient_lock_name;
set_transient($transient_lock_name, 1, MINUTE_IN_SECONDS);
}
}