1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* The default Gallery Datasource which pulls attachments from the WP media library |
4
|
|
|
*/ |
5
|
|
|
if ( ! class_exists( 'FooGalleryDatasource_MediaLibrary' ) ) { |
6
|
|
|
|
7
|
|
|
class FooGalleryDatasource_MediaLibrary implements IFooGalleryDatasource { |
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @var FooGallery |
11
|
|
|
*/ |
12
|
|
|
private $foogallery; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Sets the FooGallery object we are dealing with |
16
|
|
|
* |
17
|
|
|
* @param $foogallery FooGallery |
18
|
|
|
*/ |
19
|
|
|
public function setGallery( $foogallery ) { |
20
|
|
|
$this->foogallery = $foogallery; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
function __construct() { |
|
|
|
|
24
|
|
|
//attachment_count |
25
|
|
|
//attachment_id_csv |
26
|
|
|
//attachments |
27
|
|
|
//find_featured_attachment_id |
28
|
|
|
//featured_attachment |
29
|
|
|
//featured_image_html |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Returns the number of attachments used from the media library |
34
|
|
|
* @return int |
35
|
|
|
*/ |
36
|
|
|
public function getCount() { |
37
|
|
|
return sizeof( $this->foogallery->attachment_ids ); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns a serialized string that represents the media in the datasource. |
42
|
|
|
* This string is persisted when saving a FooGallery |
43
|
|
|
* |
44
|
|
|
* @return string |
45
|
|
|
*/ |
46
|
|
|
public function getSerializedData() { |
47
|
|
|
if ( is_array( $this->foogallery->attachment_ids ) ) { |
48
|
|
|
return implode( ',', $this->foogallery->attachment_ids ); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
return ''; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns an array of FooGalleryAttachments from the datasource |
56
|
|
|
* @return array(FooGalleryAttachment) |
|
|
|
|
57
|
|
|
*/ |
58
|
|
|
public function getAttachments() { |
59
|
|
|
$attachments = array(); |
60
|
|
|
|
61
|
|
|
if ( ! empty( $this->foogallery->attachment_ids ) ) { |
62
|
|
|
|
63
|
|
|
add_action( 'pre_get_posts', array( $this, 'force_gallery_ordering' ), 99 ); |
64
|
|
|
|
65
|
|
|
$attachment_query_args = apply_filters( 'foogallery_attachment_get_posts_args', array( |
66
|
|
|
'post_type' => 'attachment', |
67
|
|
|
'posts_per_page' => -1, |
68
|
|
|
'post__in' => $this->foogallery->attachment_ids, |
69
|
|
|
'orderby' => foogallery_sorting_get_posts_orderby_arg( $this->foogallery->sorting ), |
70
|
|
|
'order' => foogallery_sorting_get_posts_order_arg( $this->foogallery->sorting ) |
71
|
|
|
) ); |
72
|
|
|
|
73
|
|
|
$attachment_posts = get_posts( $attachment_query_args ); |
74
|
|
|
|
75
|
|
|
remove_action( 'pre_get_posts', array( $this, 'force_gallery_ordering' ), 99 ); |
76
|
|
|
|
77
|
|
|
$attachments = array_map( array( $this, 'build_attachment' ), $attachment_posts ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $attachments; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
function build_attachment( $attachment_post ) { |
|
|
|
|
84
|
|
|
$attachment = apply_filters( 'foogallery_attachment_load', FooGalleryAttachment::get( $attachment_post ), $this->foogallery ); |
85
|
|
|
return $attachment; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* This forces the attachments to be fetched using the correct ordering. |
90
|
|
|
* Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
91
|
|
|
* @param $query WP_Query |
92
|
|
|
*/ |
93
|
|
|
public function force_gallery_ordering( $query ) { |
94
|
|
|
//only care about attachments |
95
|
|
|
if ( array_key_exists( 'post_type', $query->query ) && |
96
|
|
|
'attachment' === $query->query['post_type'] ) { |
97
|
|
|
$query->set( 'orderby', foogallery_sorting_get_posts_orderby_arg( $this->foogallery->sorting ) ); |
98
|
|
|
$query->set( 'order', foogallery_sorting_get_posts_order_arg( $this->foogallery->sorting ) ); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Returns the featured FooGalleryAttachment from the datasource |
104
|
|
|
* @return bool|FooGalleryAttachment |
105
|
|
|
*/ |
106
|
|
|
public function getFeaturedAttachment() { |
107
|
|
|
$attachment_id = $this->find_featured_attachment_id(); |
108
|
|
|
|
109
|
|
|
if ( $attachment_id ) { |
110
|
|
|
return FooGalleryAttachment::get_by_id( $attachment_id ); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return false; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function find_featured_attachment_id() { |
117
|
|
|
$attachment_id = get_post_thumbnail_id( $this->foogallery->ID ); |
118
|
|
|
|
119
|
|
|
//if no featured image could be found then get the first image |
120
|
|
|
if ( ! $attachment_id && $this->foogallery->attachment_ids ) { |
121
|
|
|
$attachment_id_values = array_values( $this->foogallery->attachment_ids ); |
122
|
|
|
$attachment_id = array_shift( $attachment_id_values ); |
123
|
|
|
} |
124
|
|
|
return $attachment_id; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.