-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclass-koken-sync.php
465 lines (372 loc) · 10.2 KB
/
class-koken-sync.php
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
<?php
class KokenSync {
/**
* Plugin version
*
* Used for cache-busting and script file references.
*/
const VERSION = '0.2.0';
/**
* Koken URL
*/
protected $koken_url = null;
/**
* Plugin slug
*/
protected $plugin_slug = 'koken-sync';
/**
* Plugin path
*/
protected $plugin_path;
/**
* Instance of this class
*/
protected static $instance = null;
/**
* Initialize the plugin
*/
private function __construct() {
$this->koken_url = get_option( 'koken_url' );
$this->plugin_path = plugin_dir_path( __FILE__ );
}
/**
* Return the plugin slug
*/
public function get_plugin_slug() {
return $this->plugin_slug;
}
/**
* Return the plugin path
*/
public function get_plugin_path() {
return $this->plugin_path;
}
/**
* Get Koken URL
*/
public function get_koken_url() {
return $this->koken_url;
}
/**
* Return an instance of this class
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* Plugin activation
*/
public static function activate() {
self::add_tables();
}
/**
* Plugin deactivation
*/
public static function deactivate() {
self::remove_tables();
}
/**
* Add tables (on activation)
*/
private static function add_tables() {
global $wpdb;
$tables = array(
array(
'name' => self::table_name('albums'),
'query' => "
CREATE TABLE " . self::table_name('albums') . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
album_id bigint NOT NULL,
album_order mediumint(9) NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
title tinytext NOT NULL,
slug tinytext NOT NULL,
status VARCHAR(255) DEFAULT 'unpublished' NOT NULL,
summary text NOT NULL,
description text NOT NULL,
image_count mediumint(9) NOT NULL,
password tinytext NOT NULL,
modified datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
synced_time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
UNIQUE KEY id (id),
UNIQUE KEY (album_id)
);"
),
array(
'name' => self::table_name('images'),
'query' => "
CREATE TABLE " . self::table_name('images') . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
image_id bigint NOT NULL,
time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
title tinytext NOT NULL,
slug tinytext NOT NULL,
caption text NOT NULL,
visibility tinytext NOT NULL,
modified datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
cache_path text NOT NULL,
UNIQUE KEY id (id),
UNIQUE KEY (image_id)
);"
),
array(
'name' => self::table_name('albums_images'),
'query' => "
CREATE TABLE " . self::table_name('albums_images') . " (
album_id bigint NOT NULL,
image_id bigint NOT NULL,
sort_order bigint NOT NULL,
PRIMARY KEY (album_id,image_id)
);"
),
array(
'name' => self::table_name('keywords'),
'query' => "
CREATE TABLE " . self::table_name('keywords') . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
keyword_id BINARY(16) NOT NULL,
slug VARCHAR(255) NOT NULL,
keyword tinytext NOT NULL,
UNIQUE KEY id (id),
UNIQUE KEY (keyword_id)
);"
),
array(
'name' => self::table_name('keywords_images'),
'query' => "
CREATE TABLE " . self::table_name('keywords_images') . " (
keyword_id BINARY(16) NOT NULL,
image_id bigint NOT NULL,
PRIMARY KEY (keyword_id,image_id)
);"
),
);
// Run queries
foreach ( $tables as $table ) {
if ( ! $wpdb->get_var( "SHOW TABLES LIKE '{$table['name']}'" ) ) {
$wpdb->query( $table['query'] );
}
}
}
/**
* Remove tables (on deactivation)
*/
private static function remove_tables() {
global $wpdb;
$tables = array('albums', 'images', 'albums_images', 'keywords', 'keywords_images');
// Run query
foreach ( $tables as $table ) {
$wpdb->query("DROP TABLE IF EXISTS " . self::table_name( $table ));
}
}
/**
* Get a table name
*/
public static function table_name( $name ) {
global $wpdb;
return $wpdb->prefix . 'koken_sync_' . $name;
}
/**
* Get single album by id
*/
public static function get_album( $args = array() )
{
global $wpdb;
extract( wp_parse_args( $args, array(
'album_id' => null,
'album_slug' => null
) ) );
if ( is_null( 'album_id' ) && is_null( 'album_slug' ) ) {
return false;
}
// Build query
$query = "SELECT * FROM " . self::table_name( 'albums' ) . " albums";
// Query by album id
if ( $album_id ) {
$query .= " WHERE albums.album_id = %d";
$params[] = $album_id;
}
// Query by album slug
if ( ! $album_id && $album_slug ) {
$query .= " WHERE albums.slug = %s";
$params[] = $album_slug;
}
if ( ! empty( $params ) ) {
$query = $wpdb->prepare( $query, implode(',', $params) );
}
return $wpdb->get_row( $query );
}
/**
* Get albums
*/
public static function get_albums( $args = array() ) {
global $wpdb;
extract( wp_parse_args( $args, array(
'synced' => true,
'status' => 'published',
'orderby' => 'album_order',
'order' => 'ASC'
) ) );
$query = "SELECT * FROM " . self::table_name('albums');
$params = array();
// Convenience WHERE so we can use ANDs below
$query .= " WHERE id > 0";
if ( $synced ) {
$query .= " AND synced_time != '0000-00-00 00:00:00'";
}
if ( $status ) {
$query .= " AND status = %s";
$params[] = $status;
}
if ( $orderby ) {
$query .= " ORDER BY " . $orderby . " " . $order;
}
if ( ! empty( $params ) ) {
$query = $wpdb->prepare( $query, implode(',', $params) );
}
return $wpdb->get_results( $query );
}
/**
* Get images
*/
public static function get_images( $args = array() ) {
global $wpdb;
extract( wp_parse_args( $args, array(
'orderby' => 'sort_order',
'order' => 'ASC',
// Get images from specific album
'album_id' => NULL,
'album_slug' => NULL,
// Get specific images
'image_slug' => NULL,
// Eager load associations: array('albums', 'keywords')
'load' => array()
) ) );
// If album slug is 'all', assign null
$album_slug = $album_slug == 'all' ? NULL : $album_slug;
$query = "
SELECT DISTINCT images.*
FROM " . self::table_name('images') . " images
INNER JOIN " . self::table_name('albums_images') . " albums_images
ON images.image_id = albums_images.image_id
INNER JOIN " . self::table_name('albums') . " albums
ON albums_images.album_id = albums.album_id
WHERE albums.status = 'published'
";
$params = array();
if ( $album_id ) {
$query .= " AND albums.album_id = %d";
$params[] = $album_id;
}
if ( ! $album_id && $album_slug ) {
$query .= " AND albums.slug = %s";
$params[] = $album_slug;
}
if ( $image_slug ) {
$query .= " AND images.slug = %s";
$params[] = $image_slug;
}
if ( $orderby ) {
$query .= " ORDER BY {$orderby} {$order}";
}
// Prepare params
if ( ! empty( $params ) ) {
$query = $wpdb->prepare( $query, implode(',', $params) );
}
// Run image query
$images = $wpdb->get_results( $query );
if ( empty( $images ) ) {
return false;
}
// Eager load associations?
if ( ! empty( $load ) ) {
$images = self::load_image_associations( $images, $load );
}
return $images;
}
/**
* Load image associations
*/
protected static function load_image_associations( $images, $associations ) {
global $wpdb;
$image_ids = array();
$results = array();
$collected_images = array();
foreach ( $images as $image ) {
$image_ids[] = $image->image_id;
$collected_images[ $image->image_id ] = $image;
}
// Run queries for each association
foreach ( $associations as $type ) {
$singular_type = rtrim( $type, 's' );
$results[ $type ] = $wpdb->get_results("
SELECT " . $type . ".*, " . $type . "_images.image_id
FROM " . self::table_name( $type . '_images' ) . " " . $type . "_images
INNER JOIN " . self::table_name( $type ) . " " . $type . "
ON " . $type . "_images." . $singular_type . "_id = " . $type . "." . $singular_type . "_id
WHERE " . $type . "_images.image_id IN (" . implode( ',', $image_ids ) . ")
");
}
if ( empty( $results ) ) {
return;
}
foreach ( $results as $type => $values ) {
foreach ( $values as $value ) {
$collected_images[ $value->image_id ]->{$type}[] = $value;
}
}
return array_values( $collected_images );
}
/**
* Get image source
*/
static function get_image_src( $args = array() ) {
$defaults = array(
'image' => null,
'width' => 300,
'height' => 300,
'quality' => 80,
'sharpening' => 60,
'resolution' => null
);
extract( wp_parse_args( $args, $defaults ) );
$cache_path = unserialize( $image->cache_path );
$prefix = $cache_path->prefix;
$extension = $cache_path->extension;
$url = $prefix . $width . '.' . $height . '.' . $quality . '.' . $sharpening;
if ( isset( $resolution ) ) {
$url .= '.' . $resolution;
}
$url .= '.' . $extension;
return $url;
}
/**
* Returns keywords
*/
static function get_keywords( $args = array() )
{
global $wpdb;
extract( wp_parse_args( $args, array(
'show_empty' => false
) ) );
$query = "
SELECT DISTINCT keywords.*
FROM " . self::table_name( 'albums' ) . " albums
INNER JOIN " . self::table_name( 'albums_images' ) . " albums_images
ON albums.album_id = albums_images.album_id
INNER JOIN " . self::table_name( 'keywords_images' ) . " keywords_images
ON albums_images.image_id = keywords_images.image_id
INNER JOIN " . self::table_name( 'keywords' ) . " keywords
ON keywords_images.keyword_id = keywords.keyword_id
";
if ( ! $show_empty ) {
$query .= "WHERE albums.status = 'published'";
}
// Run query
return $wpdb->get_results( $query );
}
}