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/gallery-template-clien... ( 2e8a82...140c9b )
by Brad
02:36
created

FooGallery::get_setting()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Class FooGallery
5
 *
6
 * An easy to use wrapper class for a FooGallery gallery post
7
 */
8
class FooGallery extends stdClass {
9
10
	/**
11
	 * private constructor
12
	 *
13
	 * @param null $post
14
	 */
15
	private function __construct( $post = null ) {
16
		$this->set_defaults();
17
18
		if ( $post !== null ) {
19
			$this->load( $post );
20
		}
21
	}
22
23
	/**
24
	 *  Sets the default when a new gallery is instantiated
25
	 */
26
	private function set_defaults() {
27
		$this->_post = null;
28
		$this->ID = 0;
29
		$this->attachment_ids = array();
30
		$this->_attachments = false;
31
		$this->datasource_name = foogallery_default_datasource();
32
		$this->_datasource = false;
33
	}
34
35
	/**
36
	 * private gallery load function
37
	 * @param $post
38
	 */
39
	private function load( $post ) {
40
		$this->_post = $post;
41
		$this->ID = $post->ID;
42
		$this->slug = $post->post_name;
43
		$this->name = $post->post_title;
44
		$this->author = $post->post_author;
45
		$this->post_status = $post->post_status;
46
47
		$attachment_meta = get_post_meta( $post->ID, FOOGALLERY_META_ATTACHMENTS, true );
48
		$this->attachment_ids = is_array( $attachment_meta ) ? array_filter( $attachment_meta ) : array();
49
50
		$this->load_meta( $post->ID );
51
52
		do_action( 'foogallery_foogallery_instance_after_load', $this, $post );
53
	}
54
55
	/**
56
	 * private meta data load function
57
	 * @param $post_id int
58
	 */
59
	private function load_meta( $post_id ) {
60
		$this->gallery_template = get_post_meta( $post_id, FOOGALLERY_META_TEMPLATE, true );
61
		$this->settings = get_post_meta( $post_id, FOOGALLERY_META_SETTINGS, true );
62
		$this->custom_css = get_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS, true );
63
		$this->sorting = get_post_meta( $post_id, FOOGALLERY_META_SORT, true );
64
		$this->datasource_name = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, true );
65
		if ( empty( $this->datasource_name ) ) {
66
			$this->datasource_name = foogallery_default_datasource();
67
		}
68
        $this->retina = get_post_meta( $post_id, FOOGALLERY_META_RETINA, true );
69
		$this->force_use_original_thumbs = 'true' === get_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, true );
70
		$this->settings_version = get_post_meta( $post_id, FOOGALLERY_META_SETTINGS_VERSION, true );
71
	}
72
73
	/**
74
	 * private function to load a gallery by an id
75
	 * @param $post_id
76
	 */
77
	private function load_by_id( $post_id ) {
78
		$post = get_post( $post_id );
79
		if ( $post ) {
80
			$this->load( $post );
81
		}
82
	}
83
84
	/**
85
	 * private function to load a gallery by the slug.
86
	 * Will be used when loading gallery shortcodes
87
	 * @param $slug
88
	 */
89
	private function load_by_slug( $slug ) {
90
		if ( ! empty( $slug ) ) {
91
			$args = array(
92
				'name'        => $slug,
93
				'numberposts' => 1,
94
				'post_type'   => FOOGALLERY_CPT_GALLERY,
95
			);
96
97
			$galleries = get_posts( $args );
98
99
			if ( $galleries ) {
100
				$this->load( $galleries[0] );
101
			}
102
		}
103
	}
104
105
	/**
106
	 * Static function to load a Gallery instance by passing in a post object
107
	 * @static
108
	 *
109
	 * @param $post
110
	 *
111
	 * @return FooGallery
112
	 */
113
	public static function get( $post ) {
114
		return new self( $post );
115
	}
116
117
	/**
118
	 * Static function to load a Gallery instance by post id
119
	 *
120
	 * @param $post_id
121
	 *
122
	 * @return FooGallery | boolean
123
	 */
124
	public static function get_by_id( $post_id ) {
125
		$gallery = new self();
126
		$gallery->load_by_id( $post_id );
127
		if ( ! $gallery->does_exist() ) {
128
			return false;
129
		}
130
		return $gallery;
131
	}
132
133
	/**
134
	 * Static function to load a gallery instance by passing in a gallery slug
135
	 *
136
	 * @param string $slug
137
	 *
138
	 * @return FooGallery | boolean
139
	 */
140
	public static function get_by_slug( $slug ) {
141
		$gallery = new self();
142
		$gallery->load_by_slug( $slug );
143
		if ( ! $gallery->does_exist() ) {
144
			return false;
145
		}
146
		return $gallery;
147
	}
148
149
	/**
150
	 * Get a setting using the current template and meta key
151
	 * @param $key
152
	 * @param $default
153
	 *
154
	 * @return mixed|null
155
	 */
156
	function get_setting( $key, $default ) {
157
		return $this->get_meta( "{$this->gallery_template}_$key", $default );
158
	}
159
160
	/**
161
	 * Get a meta value using a full key
162
	 * @param $key
163
	 * @param $default
164
	 *
165
	 * @return mixed|null
166
	 */
167
	function get_meta( $key, $default ) {
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...
168
		if ( ! is_array( $this->settings ) ) {
169
			return $default;
170
		}
171
172
		$value = array_key_exists( $key, $this->settings ) ? $this->settings[ $key ] : null;
173
174
		if ( $value === null ) {
175
			return $default;
176
		}
177
178
		return $value;
179
	}
180
181
	function is_checked( $key, $default = false ) {
182
		if ( ! is_array( $this->settings ) ) {
183
			return $default;
184
		}
185
186
		return array_key_exists( $key, $this->settings );
187
	}
188
189
	/**
190
	 * Returns the number of attachments in the current gallery
191
	 * @return int
192
	 */
193
	public function attachment_count() {
194
		return $this->datasource()->getCount();
195
	}
196
197
	/**
198
	 * Checks if the gallery has attachments
199
	 * @return bool
200
	 */
201
	public function has_attachments() {
202
		return $this->attachment_count() > 0;
203
	}
204
205
	/**
206
	 * Checks if the gallery exists
207
	 * @return bool
208
	 */
209
	public function does_exist() {
210
		return $this->ID > 0;
211
	}
212
213
	/**
214
	 * Returns true if the gallery is published
215
	 * @return bool
216
	 */
217
	public function is_published() {
218
		return $this->post_status === 'publish';
219
	}
220
221
	/**
222
	 * Returns true if the gallery is newly created and not yet saved
223
	 */
224
	public function is_new() {
225
		$settings = get_post_meta( $this->ID, FOOGALLERY_META_SETTINGS, true );
226
		return empty( $settings );
227
	}
228
229
	/**
230
	 * Get a comma separated list of attachment ids
231
	 * @return string
232
	 */
233
	public function attachment_id_csv() {
234
		return $this->datasource()->getSerializedData();
235
	}
236
237
	/**
238
	 * Lazy load the attachments for the gallery
239
	 *
240
	 * @return array
241
	 */
242
	public function attachments() {
243
		//lazy load the attachments for performance
244
		if ( $this->_attachments === false ) {
245
			$this->_attachments = $this->datasource()->getAttachments();
246
		}
247
248
		return $this->_attachments;
249
	}
250
251
	/**
252
	 * @deprecated 1.3.0 This is now moved into the datasource implementation
253
	 *
254
	 * This forces the attachments to be fetched using the correct ordering.
255
	 * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct
256
	 * @param $query WP_Query
257
	 */
258
	public function force_gallery_ordering( $query ) {
0 ignored issues
show
Unused Code introduced by
The parameter $query is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
259
		_deprecated_function( __FUNCTION__, '1.3.0' );
260
	}
261
262
	/**
263
	 * Output the shortcode for the gallery
264
	 *
265
	 * @return string
266
	 */
267
	public function shortcode() {
268
		return foogallery_build_gallery_shortcode( $this->ID );
269
	}
270
271
	/**
272
	 * @deprecated 1.3.0 This is now moved into the datasource implementation
273
	 *
274
	 * @return int|mixed|string
275
	 */
276
	public function find_featured_attachment_id() {
277
		_deprecated_function( __FUNCTION__, '1.3.0' );
278
279
		return 0;
280
	}
281
282
	/**
283
	 * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery
284
	 *
285
	 * @return bool|FooGalleryAttachment
286
	 */
287
	public function featured_attachment() {
288
		return $this->datasource()->getFeaturedAttachment();
289
	}
290
291
	/**
292
	 * @deprecated 1.3.0 This is now moved into the datasource implementation
293
	 *
294
	 * @param string $size
295
	 * @param bool   $icon
296
	 *
297
	 * @return bool
298
	 */
299
	public function featured_image_src( $size = 'thumbnail', $icon = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $size is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $icon is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
300
		_deprecated_function( __FUNCTION__, '1.3.0' );
301
302
		return false;
303
	}
304
305
	/**
306
	 * @deprecated 1.3.0 This is now moved into the datasource implementation
307
	 *
308
	 * Get an HTML img element representing the featured image for the gallery
309
	 *
310
	 * @param string $size Optional, default is 'thumbnail'.
311
	 * @param bool $icon Optional, default is false. Whether it is an icon.
312
	 *
313
	 * @return string HTML img element or empty string on failure.
314
	 */
315
	public function featured_image_html( $size = 'thumbnail', $icon = false ) {
0 ignored issues
show
Unused Code introduced by
The parameter $size is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $icon is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
316
		_deprecated_function( __FUNCTION__, '1.3.0' );
317
318
		return '';
319
	}
320
321
	public function image_count() {
322
		$no_images_text = foogallery_get_setting( 'language_images_count_none_text',   __( 'No images', 'foogallery' ) );
323
		$singular_text  = foogallery_get_setting( 'language_images_count_single_text', __( '1 image', 'foogallery' ) );
324
		$plural_text    = foogallery_get_setting( 'language_images_count_plural_text', __( '%s images', 'foogallery' ) );
325
326
		$count = $this->attachment_count();
327
328
		switch ( $count ) {
329
			case 0:
330
				$count_text = $no_images_text === false ? __( 'No images', 'foogallery' ) : $no_images_text;
331
				break;
332
			case 1:
333
				$count_text = $singular_text === false ? __( '1 image', 'foogallery' ) : $singular_text;
334
				break;
335
			default:
336
				$count_text = sprintf( $plural_text === false ?  __( '%s images', 'foogallery' ) : $plural_text, $count );
337
		}
338
339
		return apply_filters( 'foogallery_image_count', $count_text, $this );
340
	}
341
342
	/**
343
	 * Returns a safe name for the gallery, in case there has been no title set
344
	 *
345
	 * @return string
346
	 */
347
	public function safe_name() {
348
		return empty( $this->name ) ?
349
				sprintf( __( '%s #%s', 'foogallery' ), foogallery_plugin_name(), $this->ID ) :
350
				$this->name;
351
	}
352
353
	public function find_usages() {
354
		return get_posts( array(
355
			'post_type'      => array( 'post', 'page', ),
356
			'post_status'    => array( 'draft', 'publish', ),
357
			'posts_per_page' => -1,
358
			'orderby'        => 'post_type',
359
			'meta_query'     => array(
360
				array(
361
					'key'     => FOOGALLERY_META_POST_USAGE,
362
					'value'   => $this->ID,
363
					'compare' => 'IN',
364
				),
365
			),
366
		) );
367
	}
368
369
	public function gallery_template_details() {
370
		if ( ! empty( $this->gallery_template ) ) {
371
372
			foreach ( foogallery_gallery_templates() as $template ) {
373
				if ( $this->gallery_template == $template['slug'] ) {
374
					return $template;
375
				}
376
			}
377
		}
378
379
		return false;
380
	}
381
382
	/**
383
	 * Returns the name of the gallery template
384
	 * @return string|void
385
	 */
386
	public function gallery_template_name() {
387
		$template = $this->gallery_template_details();
388
		if ( false !== $template ) {
389
			return $template['name'];
390
		}
391
		return __( 'Unknown', 'foogallery' );
392
	}
393
394
	public function gallery_template_has_field_of_type( $field_type ) {
395
		$gallery_template_details = $this->gallery_template_details();
396
397
		if ( false != $gallery_template_details ) {
398
			if ( array_key_exists( 'fields', $gallery_template_details ) ) {
399
				foreach ( $gallery_template_details['fields'] as $field ) {
400
					if ( $field_type == $field['type'] ) {
401
						return true;
402
					}
403
				}
404
			}
405
		}
406
		return false;
407
	}
408
409
	/**
410
	 * Loads default settings from another gallery if it is set on the settings page
411
	 */
412
	public function load_default_settings_if_new() {
413
		if ( $this->is_new() ) {
414
			$default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
415
			//loads all meta data from the default gallery
416
			$this->load_meta( $default_gallery_id );
417
		}
418
	}
419
420
	/**
421
	 * Returns the current gallery datasource object
422
	 *
423
	 * @returns IFooGalleryDatasource
424
	 */
425
	public function datasource() {
426
		//lazy load the datasource only when needed
427
		if ( $this->_datasource === false ) {
428
			$this->_datasource = foogallery_instantiate_datasource( $this->datasource_name );
429
			$this->_datasource->setGallery( $this );
430
		}
431
432
		return $this->_datasource;
433
	}
434
}
435