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
|
|
|
add_action( 'pre_get_posts', array( $this, 'force_gallery_ordering' ), 99 ); |
59
|
|
|
|
60
|
|
|
$attachment_query_args = apply_filters( 'foogallery_attachment_get_posts_args', array( |
61
|
|
|
'post_type' => 'attachment', |
62
|
|
|
'posts_per_page' => -1, |
63
|
|
|
'post__in' => $this->foogallery->attachment_ids, |
64
|
|
|
'orderby' => foogallery_sorting_get_posts_orderby_arg( $this->foogallery->sorting ), |
65
|
|
|
'order' => foogallery_sorting_get_posts_order_arg( $this->foogallery->sorting ) |
66
|
|
|
) ); |
67
|
|
|
|
68
|
|
|
$attachment_posts = get_posts( $attachment_query_args ); |
69
|
|
|
|
70
|
|
|
remove_action( 'pre_get_posts', array( $this, 'force_gallery_ordering' ), 99 ); |
71
|
|
|
|
72
|
|
|
$attachments = array_map( array( $this, 'build_attachment' ), $attachment_posts ); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return $attachments; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
function apply_query_args( $query_args ) { |
|
|
|
|
79
|
|
|
global $current_foogallery_arguments; |
80
|
|
|
|
81
|
|
|
//check if a limit has been applied |
82
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['limit'] ) ) { |
83
|
|
|
$query_args['posts_per_page'] = $current_foogallery_arguments['limit']; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
//check if an offset has been applied |
87
|
|
|
if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['offset'] ) ) { |
88
|
|
|
$query_args['offset'] = $current_foogallery_arguments['offset']; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $query_args; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
function build_attachment( $attachment_post ) { |
|
|
|
|
95
|
|
|
$attachment = apply_filters( 'foogallery_attachment_load', FooGalleryAttachment::get( $attachment_post ), $this->foogallery ); |
96
|
|
|
return $attachment; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* This forces the attachments to be fetched using the correct ordering. |
101
|
|
|
* Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
102
|
|
|
* @param $query WP_Query |
103
|
|
|
*/ |
104
|
|
|
public function force_gallery_ordering( $query ) { |
105
|
|
|
//only care about attachments |
106
|
|
|
if ( array_key_exists( 'post_type', $query->query ) && |
107
|
|
|
'attachment' === $query->query['post_type'] ) { |
108
|
|
|
$query->set( 'orderby', foogallery_sorting_get_posts_orderby_arg( $this->foogallery->sorting ) ); |
109
|
|
|
$query->set( 'order', foogallery_sorting_get_posts_order_arg( $this->foogallery->sorting ) ); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns the featured FooGalleryAttachment from the datasource |
115
|
|
|
* @return bool|FooGalleryAttachment |
116
|
|
|
*/ |
117
|
|
|
public function getFeaturedAttachment() { |
118
|
|
|
$attachment_id = $this->find_featured_attachment_id(); |
119
|
|
|
|
120
|
|
|
if ( $attachment_id ) { |
121
|
|
|
return FooGalleryAttachment::get_by_id( $attachment_id ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return false; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
private function find_featured_attachment_id() { |
128
|
|
|
$attachment_id = get_post_thumbnail_id( $this->foogallery->ID ); |
129
|
|
|
|
130
|
|
|
//if no featured image could be found then get the first image |
131
|
|
|
if ( ! $attachment_id && $this->foogallery->attachment_ids ) { |
132
|
|
|
$attachment_id_values = array_values( $this->foogallery->attachment_ids ); |
133
|
|
|
$attachment_id = array_shift( $attachment_id_values ); |
134
|
|
|
} |
135
|
|
|
return $attachment_id; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
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.