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( 'FooGalleryAttachment', 'get' ), $attachment_posts ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $attachments; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* This forces the attachments to be fetched using the correct ordering. |
85
|
|
|
* Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct |
86
|
|
|
* @param $query WP_Query |
87
|
|
|
*/ |
88
|
|
|
public function force_gallery_ordering( $query ) { |
89
|
|
|
//only care about attachments |
90
|
|
|
if ( array_key_exists( 'post_type', $query->query ) && |
91
|
|
|
'attachment' === $query->query['post_type'] ) { |
92
|
|
|
$query->set( 'orderby', foogallery_sorting_get_posts_orderby_arg( $this->foogallery->sorting ) ); |
93
|
|
|
$query->set( 'order', foogallery_sorting_get_posts_order_arg( $this->foogallery->sorting ) ); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the featured FooGalleryAttachment from the datasource |
99
|
|
|
* @return bool|FooGalleryAttachment |
100
|
|
|
*/ |
101
|
|
|
public function getFeaturedAttachment() { |
102
|
|
|
// TODO: Implement getFeaturedAttachment() method. |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
|
106
|
|
|
} |
107
|
|
|
} |
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.