class-wpml-media-attachment-by-url-query.php
6.01 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace WPML\Media\Classes;
use WPML\FP\Obj;
class WPML_Media_Attachment_By_URL_Query {
/**
* @var wpdb
*/
private $wpdb;
/**
* @var array
*/
private $id_from_guid_cache = [];
/**
* @var array
*/
private $id_from_meta_cache = [];
/**
* @var boolean Used in tests
*/
private $was_last_fetch_from_cache = false;
/**
* \WPML\Media\Classes\WPML_Media_Attachment_By_URL_Query constructor.
*
* @param \wpdb $wpdb
*/
public function __construct( \wpdb $wpdb ) {
$this->wpdb = $wpdb;
}
/**
* @return boolean
*/
public function getWasLastFetchFromCache() {
return $this->was_last_fetch_from_cache;
}
/**
* Sometimes multiple rows are returned for one language(language_code field)/url(guid field) or language(language_code field)/relativePath(meta_value field) pair.
* We should set only first result in such cases same as with original get_var call, otherwise code with cache will not work in the same way as the original code.
* Example: [[post_id = 1, lang = en, url = otgs.com], [post_id = 2, lang = en, url = otgs.com]] => only first entry should be set to cache, second should be ignored.
*
* @param array $item
* @param string $cache_prop
* @param string $item_index_in_cache
*/
private function setItemToCache( $item, $cache_prop, $item_index_in_cache ) {
if ( array_key_exists( $item_index_in_cache, $this->$cache_prop ) ) {
return;
}
$this->{$cache_prop}[$item_index_in_cache] = $item;
}
/**
* @param array $source_items
*/
private function filterItems( $source_items ) {
return array_values( array_filter( array_unique( $source_items ) ) );
}
/**
* @param string $language
* @param array $urls
*/
public function prefetchAllIdsFromGuids( $language, $urls ) {
$urls = $this->filterItems( $urls );
$urls = array_filter( $urls, function( $url ) use ( $language ) {
$index = md5( $language . $url );
return ! array_key_exists( $index, $this->id_from_guid_cache );
} );
if ( 0 === count( $urls ) ) {
return;
}
$sql = '';
$sql .= "SELECT p.ID AS post_id, p.guid, t.language_code FROM {$this->wpdb->posts} p ";
$sql .= "JOIN {$this->wpdb->prefix}icl_translations t ON t.element_id = p.ID ";
$sql .= "WHERE t.element_type='post_attachment' AND t.language_code=%s ";
$sql .= 'AND p.guid IN (' . wpml_prepare_in( $urls ) . ')';
// phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
$results = $this->wpdb->get_results( $this->wpdb->prepare( $sql, $language ), ARRAY_A );
foreach ( $results as $result ) {
$index = md5( $result['language_code'] . $result['guid'] );
$this->setItemToCache( $result, 'id_from_guid_cache', $index );
}
// We should put not found values into the cache too, otherwise they will be still queried later.
$urls_count = count( $urls );
for ( $i = 0; $i < $urls_count; $i++ ) {
$index = md5( $language . $urls[ $i ] );
$this->setItemToCache( null, 'id_from_guid_cache', $index );
}
}
/**
* @param string $language
* @param string $url
*/
public function getIdFromGuid( $language, $url ) {
$this->was_last_fetch_from_cache = false;
$index = md5( $language . $url );
if ( array_key_exists( $index, $this->id_from_guid_cache ) ) {
$this->was_last_fetch_from_cache = true;
return ( $this->id_from_guid_cache[ $index ] ) ? $this->id_from_guid_cache[ $index ]['post_id'] : null;
}
$attachment_id = $this->wpdb->get_var(
// phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
$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
",
$language,
$url
)
);
return $attachment_id;
}
/**
* @param string $language
* @param array $pathes
*/
public function prefetchAllIdsFromMetas( $language, $pathes ) {
$pathes = $this->filterItems( $pathes );
$pathes = array_filter( $pathes, function( $path ) use ( $language ) {
$index = md5( $language . $path );
return ! array_key_exists( $index, $this->id_from_meta_cache );
} );
if ( 0 === count( $pathes ) ) {
return;
}
$sql = '';
$sql .= "SELECT p.post_id, t.language_code, p.meta_value FROM {$this->wpdb->postmeta} p ";
$sql .= "JOIN {$this->wpdb->prefix}icl_translations t ON t.element_id = p.post_id ";
$sql .= "WHERE p.meta_key='_wp_attached_file' AND t.element_type='post_attachment' AND t.language_code=%s ";
$sql .= 'AND p.meta_value IN (' . wpml_prepare_in( $pathes ) . ')';
// phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
$results = $this->wpdb->get_results( $this->wpdb->prepare( $sql, $language ), ARRAY_A );
foreach ( $results as $result ) {
$index = md5( $result['language_code'] . $result['meta_value'] );
$this->setItemToCache( $result, 'id_from_meta_cache', $index );
}
// We should put not found values into the cache too, otherwise they will be still queried later.
$pathes_count = count( $pathes );
for ( $i = 0; $i < $pathes_count; $i++ ) {
$index = md5( $language . $pathes[ $i ] );
$this->setItemToCache( null, 'id_from_meta_cache', $index );
}
}
/**
* @param string $relative_path
* @param string $language
*/
public function getIdFromMeta( $relative_path, $language ) {
$this->was_last_fetch_from_cache = false;
$index = md5( $language . $relative_path );
if ( array_key_exists( $index, $this->id_from_meta_cache ) ) {
$this->was_last_fetch_from_cache = true;
return ( $this->id_from_meta_cache[ $index ] ) ? $this->id_from_meta_cache[ $index ]['post_id'] : null;
}
$attachment_id = $this->wpdb->get_var(
// phpcs:disable WordPress.WP.PreparedSQL.NotPrepared
$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,
$language
)
);
return $attachment_id;
}
}