-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathclass-relationship.php
More file actions
318 lines (272 loc) · 8 KB
/
class-relationship.php
File metadata and controls
318 lines (272 loc) · 8 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
<?php
/**
* Relationship class for the Cloudinary plugin.
*
* @package Cloudinary
*/
namespace Cloudinary\Relate;
use Cloudinary\Sync;
use Cloudinary\Utils;
use function Cloudinary\get_plugin_instance;
/**
* Class Relationship
*
* @property string|null $post_state
* @property string|null $public_hash
* @property string|null $public_id
* @property string|null $signature
* @property string|null $transformations
* @property string|null $text_overlay
* @property string|null $image_overlay
* @property string|null $sized_url
* @property string|null $media_context
*/
class Relationship {
/**
* The relationship post id.
*
* @var int
*/
protected $post_id;
/**
* Flag to save the relationship on shutdown.
*
* @var bool
*/
protected $save_on_shutdown = false;
/**
* Holds the asset type.
*
* @var string
*/
public $asset_type;
/**
* Holds the media context.
*
* @var string
*/
public $context;
/**
* The constructor.
*
* @param int $post_id The relationship post id.
*/
public function __construct( $post_id ) {
$this->post_id = (int) $post_id;
$this->asset_type = get_plugin_instance()->get_component( 'media' )->get_media_type( $this->post_id );
$this->context = Utils::get_media_context( $post_id );
}
/**
* Get the relationship data.
*
* @return array|null
*/
public function get_data() {
$key = $this->post_id . $this->context;
$cache_data = wp_cache_get( $key, Sync::META_KEYS['cloudinary'] );
if ( $cache_data ) {
return $cache_data;
}
global $wpdb;
$table_name = Utils::get_relationship_table();
$default_contexts = Utils::get_media_context_things();
// If the context is in the default contexts, we want to query for all of them.
// This ensures that a media uploaded with a previous default context will still be found, even if the default context has changed since it was uploaded.
$contexts = in_array( $this->context, $default_contexts, true ) ? $default_contexts : array( $this->context );
// Create the context query placeholders by filling an array with the correct number of %s placeholders and then imploding it into a string.
$context_query = implode( ', ', array_fill( 0, count( $contexts ), '%s' ) );
// Prepare arguments for the SQL query.
$query_args = array_merge(
array( $this->post_id ),
$contexts,
array( $this->context )
);
// phpcs:ignore WordPress.DB
$sql = $wpdb->prepare(
"SELECT * FROM {$table_name} WHERE `post_id` = %d AND `media_context` IN ({$context_query}) ORDER BY FIELD(`media_context`, %s) DESC LIMIT 1", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.PreparedSQL.NotPrepared
$query_args
);
$data = $wpdb->get_row( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB
self::set_cache( $this->post_id, $this->context, $data );
return $data;
}
/**
* Set a relationship data value.
*
* @param string $key The key.
* @param mixed $value The value.
*/
public function __set( $key, $value ) {
$data = $this->get_data();
$data[ $key ] = $value;
self::set_cache( $this->post_id, $this->context, $data );
}
/**
* Get a relationship data value.
*
* @param string $key The key.
*
* @return mixed The value.
*/
public function __get( $key ) {
$return = null;
$data_cache = $this->get_data();
if ( isset( $data_cache[ $key ] ) ) {
$return = $data_cache[ $key ];
}
return $return;
}
/**
* Check the existence of a relationship data value.
*
* @param string $key The key.
*
* @return bool
*/
public function __isset( $key ) {
$data_cache = $this->get_data();
return isset( $data_cache[ $key ] );
}
/**
* Set the save on shutdown flag.
*/
public function save() {
if ( ! $this->save_on_shutdown ) {
$this->save_on_shutdown = true;
add_action( 'shutdown', array( $this, 'do_save' ) );
add_action( 'shutdown', array( $this, 'flush_cache' ), 100 );
}
}
/**
* Save the relationship data.
*
* @return bool
*/
public function do_save() {
global $wpdb;
$data = $this->get_data();
$update = false;
if ( ! empty( $data['id'] ) ) {
$update = $wpdb->update( Utils::get_relationship_table(), $data, array( 'id' => $data['id'] ), array( '%s' ), array( '%d' ) );// phpcs:ignore WordPress.DB
}
return $update;
}
/**
* Flush the cache.
*
* @return void
*/
public function flush_cache() {
do_action( 'cloudinary_flush_cache', false );
}
/**
* Preload bulk relationships.
*
* @param array $post_ids The post ids.
*/
public static function preload( $post_ids ) {
// Check if the post_ids are objects.
$maybe_ids = array_column( $post_ids, 'ID' );
if ( ! empty( $maybe_ids ) ) {
// If so, make sure to just use the IDs.
$post_ids = $maybe_ids;
}
global $wpdb;
$table_name = Utils::get_relationship_table();
// Do the public_ids.
$list = implode( ', ', array_fill( 0, count( $post_ids ), '%d' ) );
$where = "post_id IN ( {$list} )";
$sql = $wpdb->prepare( "SELECT * FROM {$table_name} WHERE {$where}", $post_ids ); // phpcs:ignore WordPress.DB
$posts = $wpdb->get_results( $sql, ARRAY_A ); // phpcs:ignore WordPress.DB
foreach ( $posts as $post ) {
$media_context = Utils::get_media_context( $post['post_id'] );
self::set_cache( $post['post_id'], $media_context, $post );
}
}
/**
* Set the relationship data cache.
*
* @param int $post_id The post id.
* @param string $media_context The media context.
* @param array $data The data.
*/
public static function set_cache( $post_id, $media_context, $data ) {
$key = $post_id . $media_context;
return wp_cache_set( $key, $data, Sync::META_KEYS['cloudinary'] );
}
/**
* Delete the relationship.
*/
public function delete() {
global $wpdb;
$table_name = Utils::get_relationship_table();
$wpdb->delete( // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
$table_name,
array(
'post_id' => $this->post_id,
'media_context' => $this->context,
),
array(
'%d',
'%s',
)
);
$this->delete_cache();
}
/**
* Delete the relationship cache.
*/
public function delete_cache() {
$key = $this->post_id . $this->media_context;
wp_cache_delete( $key, Sync::META_KEYS['cloudinary'] );
}
/**
* Get a relationship object.
*
* @param int $post_id The relationship post id.
*
* @return Relationship|null The relationship object or null if not found.
*/
public static function get_relationship( $post_id ) {
static $cache = array();
if ( ! isset( $cache[ $post_id ] ) ) {
$cache[ $post_id ] = new self( $post_id );
}
return $cache[ $post_id ];
}
/**
* Get the attachment IDs by the public ID.
*
* @param string $public_id The public ID.
*
* @return array
*/
public static function get_ids_by_public_id( $public_id ) {
global $wpdb;
$table_name = Utils::get_relationship_table();
$media_context = Utils::get_media_context();
$ids = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM {$table_name} WHERE public_id = %s AND media_context = %s", $public_id, $media_context ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
return array_map( 'intval', $ids );
}
/**
* Get the relationship in a different context.
*
* @param string $context The context.
*
* @return Relationship|null
*/
public function get_contextualized_relationship( $context ) {
$relationship = null;
if ( ! empty( $this->context ) && $this->context !== $context ) {
global $wpdb;
$table_name = Utils::get_relationship_table();
$sql = $wpdb->prepare( "SELECT post_id FROM {$table_name} WHERE url_hash = %s AND media_context = %s", $this->url_hash, $context ); // phpcs:ignore WordPress.DB
$post_id = $wpdb->get_var( $sql ); // phpcs:ignore WordPress.DB
if ( $post_id ) {
$relationship = self::get_relationship( $post_id );
$relationship->context = $context;
}
}
return $relationship;
}
}