forked from setola/Wordpress-Theme-Utils-Classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GalleryHelper.class.php
583 lines (507 loc) · 16.5 KB
/
GalleryHelper.class.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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
<?php
/**
* Stores the GalleryHelper class definition
*/
/**
* Manages the markup for a generic set of images
* Extend this with your nice and candy classes!
* @author etessore
* @version 1.0.2
* @package classes
* @subpackage image manager
* @todo remove using of FeatureWithAssets
*/
abstract class GalleryHelper /*extends FeatureWithAssets*/{
/**
* @var string the unique id for the current gallery
* it will be automatically created if not specified
*/
public $unid = false;
/**
* @var String the html template
*/
public $tpl;
/**
* @var array the list of images
*/
public $images = array();
/**
* @var int the maximum number of images to show
*/
public $image_number;
/**
* @var array Stores some static html
*/
public $static_markup;
/**
*
* @var array options for timthumb
*/
public $timthumb_opts;
/**
* @var string WordPress media size name
*/
public $media_dimension;
/**
* Search the media gallery for suitable images.
*
* This is the order:
* Check if current post has attached images
* Check the default language if has attached images
* Check if frontpage has attached images
* Check if frontpage has attached images in default language
* Build a placeholder
*/
public function get_images(){
$args = array();
if($this->image_number){
$args['numberposts'] = $this->image_number;
}
$this->add_images(self::get_images_from_post($args));// = self::get_images_from_post();
if(!$this->has_images()){
$this->add_images(self::get_images_from_main_language($args));
}
if(!$this->has_images() && HotelManager::$enabled){
$this->add_images(self::get_images_from_closest_hotel($args));
}
if(!$this->has_images() && HotelManager::$enabled){
$this->add_images(self::get_images_from_closest_hotel_in_default_language($args));
}
if(!$this->has_images()){
$this->add_images(self::get_images_from_frontpage($args));
}
if(!$this->has_images()){
$this->add_images(self::get_images_from_homepage_in_default_language($args));
}
if(!$this->has_images()){
$dimensions = ImageGenerator::get_dimensions($this->media_dimension);
if($dimensions['width'] == 'unknown') return $this;
$image = new ImageGenerator();
$image
->set('width', $dimensions['width'])
->set('height', $dimensions['height'])
->set('bg_color', 'cccccc')
->set('text_color', '222222');
$this->add_image(array(
'src' => $image->get_image_src(),
'alt' => $image->get_image_alt(),
'width' => $image->get_image_width(),
'height'=> $image->get_image_height()
));
}
return $this;
}
/**
* Check if the current gallery has one or more images
*
* Useful to check if jquery.cycle() is needed
*
* @return boolean true if there is at least two image
*/
public function has_images(){
return count($this->images) >= 1;
}
/**
* Checks if the current gallery has more no images
* @return boolean true if there is no image in the current set
*/
public function is_empty(){
return empty($this->images);
}
/**
* Get the images attached to a post
* @param array $args
* @return Ambigous <multitype:, boolean, multitype:Ambigous <NULL> >
*/
public static function get_images_from_post($args=array()){
$post_id = (!isset($args['post_parent']) || is_null($args['post_parent']))
? get_the_ID()
: $args['post_parent'];
$defaults = array(
'post_parent' => $post_id,
'post_type' => 'attachment',
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC'
);
if(function_exists('has_post_thumbnail') && has_post_thumbnail($post_id)){
$defaults['exclude'] = get_post_thumbnail_id($post_id);
}
if(taxonomy_exists('media_tag') && isset($args['tax_query'])){
$args['tax_query'] = wp_parse_args(
$args['tax_query'],
array(
'taxonomy' => 'media_tag',
'field' => 'slug',
'terms' => 'slideshow',
'operator' => 'IN'
)
);
}
return get_children(wp_parse_args($args, $defaults));
}
/**
* Get the images from the main language translation of the post
*
* Uses WPML to retrieve the post translation in default language
* and queries it for attached images.
*
* @uses icl_object_id()
* @param array $args arguments to pass to WP_Query
* @return Ambigous <Ambigous, multitype:, boolean, multitype:Ambigous <NULL> >
*/
public static function get_images_from_main_language($args=array()){
if(!isset($args['post_parent']) || is_null($args['post_parent'])){
$args['post_parent'] = get_the_ID();
}
global $sitepress;
if(!empty($sitepress)){
$post_id = icl_object_id(
$args['post_parent'],
get_post_type($args['post_parent']),
true,
$sitepress->get_default_language()
);
if($post_id == 0) return;
$args['post_parent'] = $post_id;
}
return self::get_images_from_post($args);
}
/**
* Gets images attached to the frontpage
* @param array $args arguments to pass to WP_Query
* @return array list of images
*/
public static function get_images_from_frontpage($args=array()){
$args['post_parent'] = get_option('page_on_front');
return self::get_images_from_post($args);
}
/**
* Gets images attached to the frontpage in default language translation
* @param array $args arguments to pass to WP_Query
* @return Ambigous <Ambigous, multitype:, boolean, multitype:Ambigous <NULL> >
*/
public static function get_images_from_homepage_in_default_language($args=array()){
$args['post_parent'] = get_option('page_on_front');
return self::get_images_from_main_language($args);
}
/**
* Gets images from the first post marked as 'hotel' by HotelManager
* @return Ambigous <Ambigous, multitype:, boolean, multitype:Ambigous <NULL> >
*/
public static function get_images_from_closest_hotel($args=array()){
$args['post_parent'] = HotelManager::get_hotel_id();
return self::get_images_from_post($args);
}
/**
* Gets images from the first post marked as 'hotel' by HotelManager in default language
* @return Ambigous <Ambigous, multitype:, boolean, multitype:Ambigous <NULL> >
*/
public static function get_images_from_closest_hotel_in_default_language($args=array()){
$args['post_parent'] = HotelManager::get_hotel_id(null, true);
return self::get_images_from_post($args);
}
/**
* Set the unique id for this gallery
* @param string $unid the unique
* @return GalleryHelper $this for chainability
*/
public function set_uid($unid){
$this->unid = $unid;
return $this;
}
/**
* Set the maximum number of images to show
* @param int $number the number
* @return GalleryHelper $this for chainability
*/
public function limit_images_number($number){
$this->image_number = $number;
return $this;
}
/**
* Set the timthumb options
* If set_wp_media_dimension() is called it will prevale on this.
* @param array $options
*/
public function set_timthumb_options($options){
$this->timthumb_opts = $options;
return $this;
}
/**
* Sets the dimension for this gallery.
* If set this option will prevale on timthumb
* @param string $dimension WordPress media dimension name
* @return GalleryHelper $this for chainability
*/
public function set_wp_media_dimension($dimension){
$this->media_dimension = $dimension;
return $this;
}
/**
* Set the html template for this gallery
* @param string $tpl the template
* @return GalleryHelper $this for chainability
*/
public function set_template($tpl){
$this->tpl = $tpl;
return $this;
}
/**
* Set the static markup; ie: prev\next\loading divs
* @param string $key the string has to be substituted
* @param string $markup html markup
* @return GalleryHelper $this for chainability
*/
public function set_markup($key, $markup){
$this->static_markup[$key] = $markup;
return $this;
}
/**
* Replaces the markup in $this->tpl %tag%s with the one
* in the corresponding value of $this->static_markup[tag].
*/
public function replace_markup(){
return str_replace(
array_map(
create_function('$k', 'return "%".$k."%";'),
array_keys($this->static_markup)
),
array_values($this->static_markup),
$this->tpl
);
}
/**
* Adds an image to the current set
* @param string|int $img the image: if is an int it
* will be retrieved from the wp media, elsewhere it is an html tag
* @return GalleryHelper $this for chainability
*/
public function add_image($img){
if(is_numeric($img)){
$img = intval($img);
$p = get_post(intval($img));
if($p->ID != $img) return $this;
}
$this->images[] = $img;
return $this;
}
/**
* Add some images to the current set
* @param array $images the list of images to be added
* @return GalleryHelper $this for chainability
*/
public function add_images($images){
if(count($images)){
foreach($images as $img){
$this->add_image($img);
}
}
return $this;
}
/**
* Calculates the unique id for the current gallery
* @return GalleryHelper $this for chainability
*/
public function calculate_unique(){
$this->unid = uniqid('gallery_');
return $this;
}
/**
* Checks if the $index image of the list is
* a wordpress media id or an image object
* @returns the src attribute for the $index image of the set
* @param int $index the index of the images list
*/
protected function get_image_src($index){
$toret = $this->images[$index];
if(is_numeric($this->images[$index])){
//$toret = wp_get_attachment_url($this->images[$index]);
$image = wp_get_attachment_image_src($this->images[$index], $this->media_dimension);
$toret = $image[0];
} elseif(is_object($this->images[$index])){
//$toret = wp_get_attachment_url($this->images[$index]->ID);
$image = wp_get_attachment_image_src($this->images[$index]->ID, $this->media_dimension);
$toret = $image[0];
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['src']))
$toret = $this->images[$index]['src'];
}
if($this->timthumb_opts && empty($this->media_dimension)){
if(is_array($this->timthumb_opts) && !isset($this->timthumb_opts['render'])){
$this->timthumb_opts['render'] = http_build_query($this->timthumb_opts);
}
$toret = $toret.'?'.$this->timthumb_opts['render'];
}
return $toret;
}
/**
* Retrieves the path for the given image
* If it is an external image it returns the src attributes
* @param int $index the index of the images list
* @return string path to disc
*/
protected function get_image_path($index){
//return wp_get_attachment_metadata($this->images[$index]);
$toret = $this->images[$index];
if(is_numeric($this->images[$index])){
//$toret = wp_get_attachment_url($this->images[$index]);
$toret = get_attached_file($this->images[$index], $this->media_dimension);
} elseif(is_object($this->images[$index])){
//$toret = wp_get_attachment_url($this->images[$index]->ID);
$toret = get_attached_file($this->images[$index]->ID, $this->media_dimension);
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['src']))
$toret = $this->images[$index]['src'];
}
return $toret;
}
/**
* Get the width for the n-th image of the list
* @param int $index the index of the images list
*/
protected function get_image_width($index){
if(isset($this->timthumb_opts['w'])){
return $this->timthumb_opts['w'];
} elseif(is_int($this->images[$index])){
$image = wp_get_attachment_image_src($this->images[$index], $this->media_dimension);
if(!empty($image[1])){
return $image[1];
}
} elseif(is_object($this->images[$index])){
$image = wp_get_attachment_image_src($this->images[$index]->ID, $this->media_dimension);
if(!empty($image[1])){
return $image[1];
}
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['width']))
return $this->images[$index]['width'];
}
return '100%';
}
/**
* Get the height for the n-th image of the list
* @param int $index the index of the images list
*/
protected function get_image_height($index){
if(isset($this->timthumb_opts['h'])){
return $this->timthumb_opts['h'];
} elseif(is_int($this->images[$index])){
$image = wp_get_attachment_image_src($this->images[$index], $this->media_dimension);
if(!empty($image[2])){
return $image[2];
}
} elseif(is_object($this->images[$index])){
$image = wp_get_attachment_image_src($this->images[$index]->ID, $this->media_dimension);
if(!empty($image[2])){
return $image[2];
}
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['height']))
return $this->images[$index]['height'];
}
return '100%';
}
/**
* Checks if the $index image of the list is
* a wordpress media id or an image object
* @returns the id attribute for the $index image of the set
* @param int $index the index of the images list
*/
protected function get_image_id($index){
if(is_numeric($this->images[$index])){
return $this->images[$index];
} elseif(is_object($this->images[$index])){
return $this->images[$index]->ID;
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['id']))
return $this->images[$index]['id'];
}
}
/**
* Retrieves the title for the image with given index
* @param int $index the index of the images list
* @returns the title attribute for the $index image of the set
*/
protected function get_image_title($index){
$toret = $this->images[$index];
if(is_numeric($this->images[$index])){
$toret = get_the_title($this->images[$index]);
} elseif(is_object($this->images[$index])){
$toret = get_the_title($this->images[$index]->ID);
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['title']))
$toret = $this->images[$index]['title'];
}
return $toret;
}
/**
* Checks if the $index image of the list is
* a wordpress media id or an image object
* @returns the alt attribute for the $index image of the set
* @param int $index the index of the images list
*/
protected function get_image_alt($index){
$toret = $this->images[$index];
if(is_numeric($this->images[$index])){
$toret = get_post_meta($this->images[$index], '_wp_attachment_image_alt', true);
} elseif(is_object($this->images[$index])){
$toret = get_post_meta($this->images[$index]->ID, '_wp_attachment_image_alt', true);
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['alt']))
$toret = $this->images[$index]['alt'];
}
return $toret;
}
/**
* Checks if the $index image of the list is
* a wordpress media id or an image object
* @returns the caption for the $index image of the set
* @param int $index the index of the images list
*/
protected function get_image_caption($index){
$toret = $this->images[$index];
if(is_numeric($this->images[$index])){
$post = get_post($this->images[$index]);
$toret = $post->post_excerpt;
} elseif(is_object($this->images[$index])){
$toret = $this->images[$index]->post_excerpt;
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['caption']))
$toret = $this->images[$index]['caption'];
else
$toret = '';
}
return $toret;
}
/**
* Checks if the $index image of the list is
* a wordpress media id or an image object
* @returns the description for the $index image of the set
* @param int $index the index of the images list
*/
protected function get_image_description($index){
$toret = $this->images[$index];
if(is_numeric($this->images[$index])){
$post = get_post($this->images[$index]);
$toret = $post->post_content;
} elseif(is_object($this->images[$index])){
$toret = $this->images[$index]->post_content;
} elseif(is_array($this->images[$index])){
if(isset($this->images[$index]['description']))
$toret = $this->images[$index]['description'];
}
return $toret;
}
/**
* Retrieves the markup for the current gallery
* @return the markup for the current gallery
*/
abstract public function get_markup();
/**
* Echoes the markup for the current gallery
*/
public function the_markup(){
echo $this->get_markup();
}
}