Amazon_S3_And_CloudFront::get_attachment_s3_url PHP Method

get_attachment_s3_url() public method

Get the S3 URL for an attachment
public get_attachment_s3_url ( integer $post_id, array $s3object, null | integer $expires = null, null | string | array $size = null, null | array $meta = null, array $headers = [] ) : mixed | WP_Error
$post_id integer
$s3object array
$expires null | integer
$size null | string | array
$meta null | array
$headers array
return mixed | WP_Error
    public function get_attachment_s3_url($post_id, $s3object, $expires = null, $size = null, $meta = null, $headers = array())
    {
        $scheme = $this->get_s3_url_scheme();
        // We don't use $this->get_s3object_region() here because we don't want
        // to make an AWS API call and slow down page loading
        if (isset($s3object['region']) && self::DEFAULT_REGION !== $s3object['region']) {
            $region = $this->translate_region($s3object['region']);
        } else {
            $region = '';
        }
        $size = $this->maybe_convert_size_to_string($post_id, $size);
        // Force use of secured URL when ACL has been set to private
        if (is_null($expires)) {
            if (is_null($size) && isset($s3object['acl']) && self::PRIVATE_ACL === $s3object['acl']) {
                // Full size URL private
                $expires = self::DEFAULT_EXPIRES;
            }
            if (!is_null($size) && isset($s3object['sizes'][$size]['acl']) && self::PRIVATE_ACL === $s3object['sizes'][$size]['acl']) {
                // Alternative size URL private
                $expires = self::DEFAULT_EXPIRES;
            }
        }
        if (!is_null($size)) {
            if (is_null($meta)) {
                $meta = get_post_meta($post_id, '_wp_attachment_metadata', true);
            }
            if (is_wp_error($meta)) {
                return $meta;
            }
            if (!empty($meta) && isset($meta['sizes'][$size]['file'])) {
                $size_prefix = dirname($s3object['key']);
                $size_file_prefix = '.' === $size_prefix ? '' : $size_prefix . '/';
                $s3object['key'] = $size_file_prefix . $meta['sizes'][$size]['file'];
            }
        }
        if (!is_null($expires) && $this->is_plugin_setup()) {
            try {
                $expires = time() + $expires;
                $secure_url = $this->get_s3client($region)->getObjectUrl($s3object['bucket'], $s3object['key'], $expires, $headers);
                return apply_filters('as3cf_get_attachment_secure_url', $secure_url, $s3object, $post_id, $expires, $headers);
            } catch (Exception $e) {
                return new WP_Error('exception', $e->getMessage());
            }
        }
        $s3object['key'] = $this->maybe_update_cloudfront_path($s3object['key']);
        $domain_bucket = $this->get_s3_url_domain($s3object['bucket'], $region, $expires);
        $file = $this->encode_filename_in_path($s3object['key']);
        $url = $scheme . '://' . $domain_bucket . '/' . $file;
        return apply_filters('as3cf_get_attachment_url', $url, $s3object, $post_id, $expires, $headers);
    }

Usage Example

 /**
  * Replace local URLs with S3 ones for srcset image sources
  *
  * @param array  $sources
  * @param array  $size_array
  * @param string $image_src
  * @param array  $image_meta
  * @param int    $attachment_id
  *
  * @return array
  */
 public function wp_calculate_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id)
 {
     if (!($s3object = $this->as3cf->get_attachment_s3_info($attachment_id))) {
         // Attachment not uploaded to S3, abort
         return $sources;
     }
     foreach ($sources as $width => $source) {
         $filename = basename($source['url']);
         $size = $this->find_image_size_from_width($image_meta['sizes'], $width, $filename);
         $s3_url = $this->as3cf->get_attachment_s3_url($attachment_id, $s3object, null, $size, $image_meta);
         if (false === $s3_url || is_wp_error($s3_url)) {
             continue;
         }
         $sources[$width]['url'] = $s3_url;
     }
     return $sources;
 }
Amazon_S3_And_CloudFront