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... ( 9f6bad...85df04 )
by Brad
02:37
created

FooGallery::load_settings()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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