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... ( 680944...fadc9b )
by Brad
04:57 queued 28s
created

FooGallery::load_settings()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 4
nop 1
dl 0
loc 20
rs 8.8571
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
		$this->settings = array();
34
	}
35
36
	/**
37
	 * private gallery load function
38
	 * @param $post
39
	 */
40
	private function load( $post ) {
41
		$this->_post = $post;
42
		$this->ID = $post->ID;
43
		$this->slug = $post->post_name;
44
		$this->name = $post->post_title;
45
		$this->author = $post->post_author;
46
		$this->post_status = $post->post_status;
47
48
		$attachment_meta = get_post_meta( $post->ID, FOOGALLERY_META_ATTACHMENTS, true );
49
		$this->attachment_ids = is_array( $attachment_meta ) ? array_filter( $attachment_meta ) : array();
50
51
		$this->load_meta( $post->ID );
52
53
		do_action( 'foogallery_foogallery_instance_after_load', $this, $post );
54
	}
55
56
	/**
57
	 * private meta data load function
58
	 * @param $post_id int
59
	 */
60
	private function load_meta( $post_id ) {
61
		$this->gallery_template = get_post_meta( $post_id, FOOGALLERY_META_TEMPLATE, true );
62
		$this->settings = $this->load_settings( $post_id );
63
		$this->custom_css = get_post_meta( $post_id, FOOGALLERY_META_CUSTOM_CSS, true );
64
		$this->sorting = get_post_meta( $post_id, FOOGALLERY_META_SORT, true );
65
		$this->datasource_name = get_post_meta( $post_id, FOOGALLERY_META_DATASOURCE, true );
66
		if ( empty( $this->datasource_name ) ) {
67
			$this->datasource_name = foogallery_default_datasource();
68
		}
69
        $this->retina = get_post_meta( $post_id, FOOGALLERY_META_RETINA, true );
70
		$this->force_use_original_thumbs = 'true' === get_post_meta( $post_id, FOOGALLERY_META_FORCE_ORIGINAL_THUMBS, true );
71
	}
72
73
	private function load_settings( $post_id ) {
74
		$settings = get_post_meta( $post_id, FOOGALLERY_META_SETTINGS, true );
75
76
		//the gallery is considered new if the template has not been set
77
		$is_new = empty( $this->gallery_template );
78
79
		//if we have no settings, and the gallery is not new, then allow for an upgrade
80
		if ( empty( $settings ) && !$is_new ) {
81
			$settings = apply_filters( 'foogallery_settings_upgrade', $settings, $this );
82
		}
83
84
		//if we still have no settings, then get default settings for the gallery template
85
        if ( empty( $settings ) && !$is_new ) {
86
		    $settings = foogallery_build_default_settings_for_gallery_template( $this->gallery_template );
87
88
            $settings = apply_filters('foogallery_default_settings-' . $this->gallery_template, $settings, $this);
89
        }
90
91
		return $settings;
92
	}
93
94
	/**
95
	 * private function to load a gallery by an id
96
	 * @param $post_id
97
	 */
98
	private function load_by_id( $post_id ) {
99
		$post = get_post( $post_id );
100
		if ( $post ) {
101
			$this->load( $post );
102
		}
103
	}
104
105
	/**
106
	 * private function to load a gallery by the slug.
107
	 * Will be used when loading gallery shortcodes
108
	 * @param $slug
109
	 */
110
	private function load_by_slug( $slug ) {
111
		if ( ! empty( $slug ) ) {
112
			$args = array(
113
				'name'        => $slug,
114
				'numberposts' => 1,
115
				'post_type'   => FOOGALLERY_CPT_GALLERY,
116
			);
117
118
			$galleries = get_posts( $args );
119
120
			if ( $galleries ) {
121
				$this->load( $galleries[0] );
122
			}
123
		}
124
	}
125
126
	/**
127
	 * Static function to build a dynamic gallery that does not exist in the database
128
	 * @param $template
129
	 * @param $attachment_ids
130
	 *
131
	 * @return FooGallery
132
	 */
133
	public static function dynamic( $template, $attachment_ids ) {
134
		$gallery = new self( null );
135
136
		$gallery->gallery_template = $template;
137
		$gallery->attachment_ids = $attachment_ids;
138
139
		//loads all meta data from the default gallery
140
		$default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
141
		if ( $default_gallery_id > 0 ) {
142
			$gallery->load_meta( $default_gallery_id );
143
		}
144
145
		return $gallery;
146
	}
147
148
	/**
149
	 * Static function to load a Gallery instance by passing in a post object
150
	 * @static
151
	 *
152
	 * @param $post
153
	 *
154
	 * @return FooGallery
155
	 */
156
	public static function get( $post ) {
157
		return new self( $post );
158
	}
159
160
	/**
161
	 * Static function to load a Gallery instance by post id
162
	 *
163
	 * @param $post_id
164
	 *
165
	 * @return FooGallery | boolean
166
	 */
167
	public static function get_by_id( $post_id ) {
168
		$gallery = new self();
169
		$gallery->load_by_id( $post_id );
170
		if ( ! $gallery->does_exist() ) {
171
			return false;
172
		}
173
		return $gallery;
174
	}
175
176
	/**
177
	 * Static function to load a gallery instance by passing in a gallery slug
178
	 *
179
	 * @param string $slug
180
	 *
181
	 * @return FooGallery | boolean
182
	 */
183
	public static function get_by_slug( $slug ) {
184
		$gallery = new self();
185
		$gallery->load_by_slug( $slug );
186
		if ( ! $gallery->does_exist() ) {
187
			return false;
188
		}
189
		return $gallery;
190
	}
191
192
	/**
193
	 * Get a setting using the current template and meta key
194
	 * @param $key
195
	 * @param $default
196
	 *
197
	 * @return mixed|null
198
	 */
199
	function get_setting( $key, $default ) {
200
		return $this->get_meta( "{$this->gallery_template}_$key", $default );
201
	}
202
203
	/**
204
	 * Get a meta value using a full key
205
	 * @param $key
206
	 * @param $default
207
	 *
208
	 * @return mixed|null
209
	 */
210
	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...
211
		if ( ! is_array( $this->settings ) ) {
212
			return $default;
213
		}
214
215
		$value = array_key_exists( $key, $this->settings ) ? $this->settings[ $key ] : null;
216
217
		if ( $value === null ) {
218
			return $default;
219
		}
220
221
		return $value;
222
	}
223
224
	function is_checked( $key, $default = false ) {
225
		if ( ! is_array( $this->settings ) ) {
226
			return $default;
227
		}
228
229
		return array_key_exists( $key, $this->settings );
230
	}
231
232
	/**
233
	 * Returns the number of attachments in the current gallery
234
	 * @return int
235
	 */
236
	public function attachment_count() {
237
		return $this->datasource()->getCount();
238
	}
239
240
	/**
241
	 * Checks if the gallery has attachments
242
	 * @return bool
243
	 */
244
	public function has_attachments() {
245
		return $this->attachment_count() > 0;
246
	}
247
248
	/**
249
	 * Checks if the gallery exists
250
	 * @return bool
251
	 */
252
	public function does_exist() {
253
		return $this->ID > 0;
254
	}
255
256
	/**
257
	 * Returns true if the gallery is published
258
	 * @return bool
259
	 */
260
	public function is_published() {
261
		return $this->post_status === 'publish';
262
	}
263
264
	/**
265
	 * Returns true if the gallery is newly created and not yet saved
266
	 */
267
	public function is_new() {
268
		$template = get_post_meta( $this->ID, FOOGALLERY_META_TEMPLATE, true );
269
		return empty( $template );
270
	}
271
272
	/**
273
	 * Get a comma separated list of attachment ids
274
	 * @return string
275
	 */
276
	public function attachment_id_csv() {
277
		return $this->datasource()->getSerializedData();
278
	}
279
280
	/**
281
	 * Lazy load the attachments for the gallery
282
	 *
283
	 * @return array
284
	 */
285
	public function attachments() {
286
		//lazy load the attachments for performance
287
		if ( $this->_attachments === false ) {
288
			$this->_attachments = $this->datasource()->getAttachments();
289
		}
290
291
		return $this->_attachments;
292
	}
293
294
	/**
295
	 * @deprecated 1.3.0 This is now moved into the datasource implementation
296
	 *
297
	 * This forces the attachments to be fetched using the correct ordering.
298
	 * Some plugins / themes override this globally for some reason, so this is a preventative measure to ensure sorting is correct
299
	 * @param $query WP_Query
300
	 */
301
	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...
302
		_deprecated_function( __FUNCTION__, '1.3.0' );
303
	}
304
305
	/**
306
	 * Output the shortcode for the gallery
307
	 *
308
	 * @return string
309
	 */
310
	public function shortcode() {
311
		return foogallery_build_gallery_shortcode( $this->ID );
312
	}
313
314
	/**
315
	 * @deprecated 1.3.0 This is now moved into the datasource implementation
316
	 *
317
	 * @return int|mixed|string
318
	 */
319
	public function find_featured_attachment_id() {
320
		_deprecated_function( __FUNCTION__, '1.3.0' );
321
322
		return 0;
323
	}
324
325
	/**
326
	 * Gets the featured image FooGalleryAttachment object. If no featured image is set, then get back the first image in the gallery
327
	 *
328
	 * @return bool|FooGalleryAttachment
329
	 */
330
	public function featured_attachment() {
331
		return $this->datasource()->getFeaturedAttachment();
332
	}
333
334
	/**
335
	 * @deprecated 1.3.0 This is now moved into the datasource implementation
336
	 *
337
	 * @param string $size
338
	 * @param bool   $icon
339
	 *
340
	 * @return bool
341
	 */
342
	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...
343
		_deprecated_function( __FUNCTION__, '1.3.0' );
344
345
		return false;
346
	}
347
348
	/**
349
	 * @deprecated 1.3.0 This is now moved into the datasource implementation
350
	 *
351
	 * Get an HTML img element representing the featured image for the gallery
352
	 *
353
	 * @param string $size Optional, default is 'thumbnail'.
354
	 * @param bool $icon Optional, default is false. Whether it is an icon.
355
	 *
356
	 * @return string HTML img element or empty string on failure.
357
	 */
358
	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...
359
		_deprecated_function( __FUNCTION__, '1.3.0' );
360
361
		return '';
362
	}
363
364
	public function image_count() {
365
		$no_images_text = foogallery_get_setting( 'language_images_count_none_text',   __( 'No images', 'foogallery' ) );
366
		$singular_text  = foogallery_get_setting( 'language_images_count_single_text', __( '1 image', 'foogallery' ) );
367
		$plural_text    = foogallery_get_setting( 'language_images_count_plural_text', __( '%s images', 'foogallery' ) );
368
369
		$count = $this->attachment_count();
370
371
		switch ( $count ) {
372
			case 0:
373
				$count_text = $no_images_text === false ? __( 'No images', 'foogallery' ) : $no_images_text;
374
				break;
375
			case 1:
376
				$count_text = $singular_text === false ? __( '1 image', 'foogallery' ) : $singular_text;
377
				break;
378
			default:
379
				$count_text = sprintf( $plural_text === false ?  __( '%s images', 'foogallery' ) : $plural_text, $count );
380
		}
381
382
		return apply_filters( 'foogallery_image_count', $count_text, $this );
383
	}
384
385
	/**
386
	 * Returns a safe name for the gallery, in case there has been no title set
387
	 *
388
	 * @return string
389
	 */
390
	public function safe_name() {
391
		return empty( $this->name ) ?
392
				sprintf( __( '%s #%s', 'foogallery' ), foogallery_plugin_name(), $this->ID ) :
393
				$this->name;
394
	}
395
396
	public function find_usages() {
397
		return get_posts( array(
398
			'post_type'      => array( 'post', 'page', ),
399
			'post_status'    => array( 'draft', 'publish', ),
400
			'posts_per_page' => -1,
401
			'orderby'        => 'post_type',
402
			'meta_query'     => array(
403
				array(
404
					'key'     => FOOGALLERY_META_POST_USAGE,
405
					'value'   => $this->ID,
406
					'compare' => 'IN',
407
				),
408
			),
409
		) );
410
	}
411
412
	public function gallery_template_details() {
413
		if ( ! empty( $this->gallery_template ) ) {
414
415
			foreach ( foogallery_gallery_templates() as $template ) {
416
				if ( $this->gallery_template == $template['slug'] ) {
417
					return $template;
418
				}
419
			}
420
		}
421
422
		return false;
423
	}
424
425
	/**
426
	 * Returns the name of the gallery template
427
	 * @return string|void
428
	 */
429
	public function gallery_template_name() {
430
		$template = $this->gallery_template_details();
431
		if ( false !== $template ) {
432
			return $template['name'];
433
		}
434
		return __( 'Unknown', 'foogallery' );
435
	}
436
437
	public function gallery_template_has_field_of_type( $field_type ) {
438
		$gallery_template_details = $this->gallery_template_details();
439
440
		if ( false != $gallery_template_details ) {
441
			if ( array_key_exists( 'fields', $gallery_template_details ) ) {
442
				foreach ( $gallery_template_details['fields'] as $field ) {
443
					if ( $field_type == $field['type'] ) {
444
						return true;
445
					}
446
				}
447
			}
448
		}
449
		return false;
450
	}
451
452
	/**
453
	 * Loads default settings from another gallery if it is set on the settings page
454
	 */
455
	public function load_default_settings_if_new() {
456
		if ( $this->is_new() ) {
457
			$default_gallery_id = foogallery_get_setting( 'default_gallery_settings' );
458
			//loads all meta data from the default gallery
459
			$this->load_meta( $default_gallery_id );
460
		}
461
	}
462
463
	/**
464
	 * Returns the current gallery datasource object
465
	 *
466
	 * @returns IFooGalleryDatasource
467
	 */
468
	public function datasource() {
469
		//lazy load the datasource only when needed
470
		if ( $this->_datasource === false ) {
471
			$this->_datasource = foogallery_instantiate_datasource( $this->datasource_name );
472
			$this->_datasource->setGallery( $this );
473
		}
474
475
		return $this->_datasource;
476
	}
477
}
478