| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Description of AttachmentController |
| 5 |
* |
| 6 |
* @author Ali2Woo Team |
| 7 |
* |
| 8 |
* @autoload: a2wl_init |
| 9 |
* |
| 10 |
* @ajax: true |
| 11 |
* |
| 12 |
* @cron: false |
| 13 |
*/ |
| 14 |
|
| 15 |
namespace AliNext_Lite;; |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class AttachmentController { |
| 20 |
|
| 21 |
public function __construct() { |
| 22 |
add_filter('wp_get_attachment_url', array($this, 'get_attachment_url'), 1000, 2); |
| 23 |
add_filter('wp_calculate_image_srcset', array($this, 'calculate_image_srcset'), 1000, 5); |
| 24 |
} |
| 25 |
|
| 26 |
function get_attachment_url($url, $id) { |
| 27 |
// if not an attached return to default function |
| 28 |
if (!get_post_meta($id, Attachment::KEY_ATTACHED_FILE, true)) { |
| 29 |
return $url; |
| 30 |
} |
| 31 |
|
| 32 |
if (!$post = get_post((int) $id)) { |
| 33 |
return false; |
| 34 |
} |
| 35 |
|
| 36 |
if ('attachment' != $post->post_type) { |
| 37 |
return false; |
| 38 |
} |
| 39 |
|
| 40 |
$new_url = ''; |
| 41 |
if ($file = get_post_meta($post->ID, '_wp_attached_file', true)) { |
| 42 |
if (substr($file, 0, 7) === "http://" || substr($file, 0, 8) === "https://") { |
| 43 |
$new_url = $file; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
if (empty($new_url)) { |
| 48 |
return false; |
| 49 |
} |
| 50 |
|
| 51 |
return $new_url; |
| 52 |
} |
| 53 |
|
| 54 |
function calculate_image_srcset($sources, $size_array, $image_src, $image_meta, $attachment_id) { |
| 55 |
// if not an attached return to default function |
| 56 |
if (!$sources || !get_post_meta($attachment_id, Attachment::KEY_ATTACHED_FILE, true)) { |
| 57 |
return $sources; |
| 58 |
} |
| 59 |
|
| 60 |
$upload_dir = wp_get_upload_dir(); |
| 61 |
$image_baseurl = trailingslashit($upload_dir['baseurl']); |
| 62 |
|
| 63 |
if (is_ssl() && 'https' !== substr($image_baseurl, 0, 5) && wp_parse_url($image_baseurl, PHP_URL_HOST) === $_SERVER['HTTP_HOST']) { |
| 64 |
//TODO in some case, change HTTP to HTTPS not working |
| 65 |
$image_baseurl = set_url_scheme($image_baseurl, 'https'); |
| 66 |
} |
| 67 |
|
| 68 |
foreach ($sources as &$src) { |
| 69 |
$src['url'] = str_replace($image_baseurl, '', $src['url']); |
| 70 |
} |
| 71 |
|
| 72 |
return $sources; |
| 73 |
} |
| 74 |
|
| 75 |
} |
| 76 |
|