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
|
|
|
add_filter( 'foogallery_attachment_get_posts_args', array( $this, 'apply_query_args' ) ); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns the number of attachments used from the media library |
29
|
|
|
* @return int |
30
|
|
|
*/ |
31
|
|
|
public function getCount() { |
32
|
|
|
return sizeof( $this->foogallery->attachment_ids ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns a serialized string that represents the media in the datasource. |
37
|
|
|
* This string is persisted when saving a FooGallery |
38
|
|
|
* |
39
|
|
|
* @return string |
40
|
|
|
*/ |
41
|
|
|
public function getSerializedData() { |
42
|
|
|
if ( is_array( $this->foogallery->attachment_ids ) ) { |
43
|
|
|
return implode( ',', $this->foogallery->attachment_ids ); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
return ''; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Returns an array of FooGalleryAttachments from the datasource |
51
|
|
|
* @return array(FooGalleryAttachment) |
|
|
|
|
52
|
|
|
*/ |
53
|
|
|
public function getAttachments() { |
54
|
|
|
$attachments = array(); |
55
|
|
|
|
56
|
|
|
if ( ! empty( $this->foogallery->attachment_ids ) ) { |
57
|
|
|
|
58
|
|
|
global $current_foogallery_arguments; |
59
|
|
|
|
60
|
|
|
//check if a sorting override has been applied |
61
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['sort'] ) ) { |
62
|
|
|
$this->foogallery->sorting = $current_foogallery_arguments['sort']; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
add_action( 'pre_get_posts', array( $this, 'force_gallery_ordering' ), 99 ); |
66
|
|
|
add_action( 'pre_get_posts', array( $this, 'force_suppress_filters' ), PHP_INT_MAX ); |
67
|
|
|
|
68
|
|
|
$attachment_query_args = apply_filters( 'foogallery_attachment_get_posts_args', array( |
69
|
|
|
'post_type' => 'attachment', |
70
|
|
|
'posts_per_page' => -1, |
71
|
|
|
'post__in' => $this->foogallery->attachment_ids, |
72
|
|
|
'orderby' => foogallery_sorting_get_posts_orderby_arg( $this->foogallery->sorting ), |
73
|
|
|
'order' => foogallery_sorting_get_posts_order_arg( $this->foogallery->sorting ) |
74
|
|
|
) ); |
75
|
|
|
|
76
|
|
|
$attachment_posts = get_posts( $attachment_query_args ); |
77
|
|
|
|
78
|
|
|
remove_action( 'pre_get_posts', array( $this, 'force_gallery_ordering' ), 99 ); |
79
|
|
|
remove_action( 'pre_get_posts', array( $this, 'force_suppress_filters' ), PHP_INT_MAX ); |
80
|
|
|
|
81
|
|
|
$attachments = array_map( array( $this, 'build_attachment' ), $attachment_posts ); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $attachments; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
function apply_query_args( $query_args ) { |
|
|
|
|
88
|
|
|
global $current_foogallery_arguments; |
89
|
|
|
|
90
|
|
|
//check if a limit has been applied |
91
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['limit'] ) ) { |
92
|
|
|
$query_args['posts_per_page'] = $current_foogallery_arguments['limit']; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
//check if an offset has been applied |
96
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['offset'] ) ) { |
97
|
|
|
$query_args['offset'] = $current_foogallery_arguments['offset']; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $query_args; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
function build_attachment( $attachment_post ) { |
|
|
|
|
104
|
|
|
$attachment = apply_filters( 'foogallery_attachment_load', FooGalleryAttachment::get( $attachment_post ), $this->foogallery ); |
105
|
|
|
return $attachment; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* This forces the attachments to be fetched using the correct ordering. |
110
|
|
|
* Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
111
|
|
|
* @param $query WP_Query |
112
|
|
|
*/ |
113
|
|
|
public function force_gallery_ordering( $query ) { |
114
|
|
|
//only care about attachments |
115
|
|
|
if ( array_key_exists( 'post_type', $query->query ) && |
116
|
|
|
'attachment' === $query->query['post_type'] ) { |
117
|
|
|
$query->set( 'orderby', foogallery_sorting_get_posts_orderby_arg( $this->foogallery->sorting ) ); |
118
|
|
|
$query->set( 'order', foogallery_sorting_get_posts_order_arg( $this->foogallery->sorting ) ); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* This forces the attachments to be fetched without any other filters. |
124
|
|
|
* Some plugins override attachment queries, so this is a preventative measure to ensure sorting is correct |
125
|
|
|
* @param $query WP_Query |
126
|
|
|
*/ |
127
|
|
|
public function force_suppress_filters( $query ) { |
128
|
|
|
//only care about attachments |
129
|
|
|
if ( array_key_exists( 'post_type', $query->query ) && |
130
|
|
|
'attachment' === $query->query['post_type'] ) { |
131
|
|
|
$query->set( 'suppress_filters', true ); |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Returns the featured FooGalleryAttachment from the datasource |
137
|
|
|
* @return bool|FooGalleryAttachment |
138
|
|
|
*/ |
139
|
|
|
public function getFeaturedAttachment() { |
140
|
|
|
$attachment_id = $this->find_featured_attachment_id(); |
141
|
|
|
|
142
|
|
|
if ( $attachment_id ) { |
143
|
|
|
return FooGalleryAttachment::get_by_id( $attachment_id ); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
private function find_featured_attachment_id() { |
150
|
|
|
$attachment_id = get_post_thumbnail_id( $this->foogallery->ID ); |
151
|
|
|
|
152
|
|
|
//if no featured image could be found then get the first image |
153
|
|
|
if ( ! $attachment_id && $this->foogallery->attachment_ids ) { |
154
|
|
|
$attachment_id_values = array_values( $this->foogallery->attachment_ids ); |
155
|
|
|
$attachment_id = array_shift( $attachment_id_values ); |
156
|
|
|
} |
157
|
|
|
return $attachment_id; |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
} |
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.