MediaAttachmentByUrl.php
3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
namespace WPML\MediaTranslation;
class MediaAttachmentByUrl {
/**
* @var wpdb
*/
private $wpdb;
/**
* @var string
*/
private $url;
/**
* @var string
*/
private $language;
const SIZE_SUFFIX_REGEXP = '/-([0-9]+)x([0-9]+)\.([a-z]{3,4})$/';
const CACHE_KEY_PREFIX = 'attachment-id-from-guid-';
const CACHE_GROUP = 'wpml-media-setup';
const CACHE_EXPIRATION = 1800;
public $cache_hit_flag = null;
/**
* WPML_Media_Attachment_By_URL constructor.
*
* @param \wpdb $wpdb
* @param string $url
* @param string $language
*/
public function __construct( \wpdb $wpdb, $url, $language ) {
$this->url = $url;
$this->language = $language;
$this->wpdb = $wpdb;
}
public function get_id() {
if ( ! $this->url ) {
return 0;
}
$cache_key = self::CACHE_KEY_PREFIX . md5( $this->language . '#' . $this->url );
$attachment_id = wp_cache_get( $cache_key, self::CACHE_GROUP, false, $this->cache_hit_flag );
if ( ! $this->cache_hit_flag ) {
$attachment_id = $this->get_id_from_guid();
if ( ! $attachment_id ) {
$attachment_id = $this->get_id_from_meta();
}
wp_cache_add( $cache_key, $attachment_id, self::CACHE_GROUP, self::CACHE_EXPIRATION );
}
return $attachment_id;
}
private function get_id_from_guid() {
$attachment_id = $this->wpdb->get_var(
$this->wpdb->prepare(
"
SELECT ID FROM {$this->wpdb->posts} p
JOIN {$this->wpdb->prefix}icl_translations t ON t.element_id = p.ID
WHERE t.element_type='post_attachment' AND t.language_code=%s AND p.guid=%s
",
$this->language,
$this->url
)
);
return $attachment_id;
}
private function get_id_from_meta() {
$uploads_dir = wp_get_upload_dir();
$relative_path = ltrim( preg_replace( '@^' . $uploads_dir['baseurl'] . '@', '', $this->url ), '/' );
// using _wp_attached_file
$attachment_id = $this->wpdb->get_var(
$this->wpdb->prepare(
"
SELECT post_id
FROM {$this->wpdb->postmeta} p
JOIN {$this->wpdb->prefix}icl_translations t ON t.element_id = p.post_id
WHERE p.meta_key='_wp_attached_file' AND p.meta_value=%s
AND t.element_type='post_attachment' AND t.language_code=%s
",
$relative_path,
$this->language
)
);
// using attachment meta (fallback)
if ( ! $attachment_id && preg_match( self::SIZE_SUFFIX_REGEXP, $relative_path ) ) {
$attachment_id = $this->get_attachment_image_from_meta_fallback( $relative_path );
}
return $attachment_id;
}
private function get_attachment_image_from_meta_fallback( $relative_path ) {
$attachment_id = null;
$relative_path_original = preg_replace( self::SIZE_SUFFIX_REGEXP, '.$3', $relative_path );
$attachment_id_original = $this->wpdb->get_var(
$this->wpdb->prepare(
"
SELECT p.post_id
FROM {$this->wpdb->postmeta} p
JOIN {$this->wpdb->prefix}icl_translations t ON t.element_id = p.post_id
WHERE p.meta_key='_wp_attached_file' AND p.meta_value=%s
AND t.element_type='post_attachment' AND t.language_code=%s
",
$relative_path_original,
$this->language
)
);
// validate size
if ( $attachment_id_original ) {
$attachment_meta_data = wp_get_attachment_metadata( $attachment_id_original );
if ( $this->validate_image_size( $relative_path, $attachment_meta_data ) ) {
$attachment_id = $attachment_id_original;
}
}
return $attachment_id;
}
private function validate_image_size( $path, $attachment_meta_data ) {
$valid = false;
$file_name = basename( $path );
foreach ( $attachment_meta_data['sizes'] as $size ) {
if ( $file_name === $size['file'] ) {
$valid = true;
break;
}
}
return $valid;
}
}