GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — feature/thumb-dimensions ( 3a26b7...7f492d )
by Brad
02:20
created

find_featured_attachment_id()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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)
0 ignored issues
show
Documentation introduced by
The doc-type array(FooGalleryAttachment) could not be parsed: Expected "|" or "end of type", but got "(" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
}