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/video-support ( 96814b )
by Brad
05:36
created

alter_video_link_attributes()   C

Complexity

Conditions 10
Paths 97

Size

Total Lines 69
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 37
nc 97
nop 3
dl 0
loc 69
rs 6.0493
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 8 and the first side effect is on line 11.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Adds video support within FooGallery
4
 */
5
6
if ( ! class_exists( 'FooGallery_Pro_Video' ) ) {
7
8
	define( 'FOOGALLERY_VIDEO_POST_META', '_foogallery_video_data' );
9
	define( 'FOOGALLERY_VIDEO_POST_META_VIDEO_COUNT', '_foogallery_video_count' );
10
11
	require_once plugin_dir_path( __FILE__ ) . 'functions.php';
12
	require_once plugin_dir_path( __FILE__ ) . 'class-foogallery-pro-video-query.php';
13
	require_once plugin_dir_path( __FILE__ ) . 'class-foogallery-pro-video-import.php';
14
15
	class FooGallery_Pro_Video {
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...
16
		/**
17
		 * Wire up everything we need
18
		 */
19
		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...
20
			new FooGallery_Pro_Video_Query();
21
			new FooGallery_Pro_Video_Import();
22
23
			//setup script includes
24
			add_action( 'wp_enqueue_media', array( $this, 'enqueue_assets' ) );
25
26
			//make sure the gallery items render with a video icon
27
			add_filter( 'foogallery_admin_render_gallery_item_extra_classes', array( $this, 'render_gallery_item_with_video_icon' ), 10, 2 );
28
29
			//add attachment custom fields
30
			add_filter( 'foogallery_attachment_custom_fields', array( $this, 'attachment_custom_fields' ) );
31
32
			//add extra fields to all templates
33
			add_filter( 'foogallery_override_gallery_template_fields', array( $this, 'all_template_fields' ) );
34
35
			// add additional templates
36
			add_action( 'admin_footer', array( $this, 'add_media_templates' ) );
37
38
			//load all video info into the attachment, so that it is only done once
39
			add_action( 'foogallery_attachment_instance_after_load', array( $this, 'set_video_flag_on_attachment' ), 10, 2 );
40
41
			//add attributes to front-end anchor
42
			add_filter( 'foogallery_attachment_html_link_attributes', array( $this, 'alter_video_link_attributes' ), 24, 3 );
43
44
			//add class to front-end item
45
			add_filter( 'foogallery_attachment_html_item_classes', array( $this, 'alter_video_item_attributes' ), 24, 3 );
46
47
			//add video icon class to galleries
48
			add_filter( 'foogallery_build_class_attribute', array( $this, 'foogallery_build_class_attribute' ) );
49
50
			//intercept gallery save and calculate how many videos are in the gallery
51
			add_action( 'foogallery_after_save_gallery', array( $this, 'calculate_video_count' ) );
52
53
			//change the image count to include videos if they are present in the gallery
54
			add_filter( 'foogallery_image_count', array( $this, 'include_video_count' ), 10, 2 );
55
56
			//check if the gallery is using foobox free and also has a video and if so, enqueue foobox video scripts.
57
			add_action( 'foogallery_loaded_template', array( $this, 'enqueue_foobox_free_dependencies' ) );
58
59
			//add settings for video
60
			add_filter( 'foogallery_admin_settings_override', array( $this, 'include_video_settings' ) );
61
62
			add_action( 'foogallery_loaded_template', array( $this, 'include_video_embeds' ) );
63
		}
64
65
		/**
66
		 * Enqueue styles and scripts
67
		 */
68
		function enqueue_assets() {
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...
69
			foogallery_enqueue_media_views_script();
70
			foogallery_enqueue_media_views_style();
71
		}
72
73
		/**
74
		 * Include the templates into the page if they are needed
75
		 */
76
		public function add_media_templates() {
77
			if ( wp_script_is( 'foogallery-media-views' ) ) {
78
				foogallery_include_media_views_templates();
79
			}
80
		}
81
82
		/**
83
		 * Add an extra class so that a video icon shows for videos
84
		 *
85
		 * @param $extra_class
86
		 * @param $attachment_post
87
		 *
88
		 * @return string
89
		 */
90
		function render_gallery_item_with_video_icon( $extra_class, $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...
91
			//check if the attachment is a video and append a class
92
			if ( foogallery_is_attachment_video( $attachment_post ) ) {
93
				if ( ! isset( $extra_class ) ) {
94
					$extra_class = '';
95
				}
96
				$extra_class .= 'subtype-foogallery';
97
			}
98
99
			return $extra_class;
100
		}
101
102
		/**
103
		 * Add video specific custom fields.
104
		 *
105
		 * @uses "foogallery_attachment_custom_fields" filter
106
		 *
107
		 * @param array $fields
108
		 *
109
		 * @return array
110
		 */
111
		public function attachment_custom_fields( $fields ) {
112
			$fields['data-width']  = array(
113
				'label'       => __( 'Override Width', 'foogallery' ),
114
				'input'       => 'text',
115
				'application' => 'image/foogallery',
116
			);
117
			$fields['data-height'] = array(
118
				'label'       => __( 'Override Height', 'foogallery' ),
119
				'input'       => 'text',
120
				'application' => 'image/foogallery',
121
			);
122
123
			return $fields;
124
		}
125
126
		/**
127
		 * Add fields to all galleries.
128
		 *
129
		 * @uses "foogallery_override_gallery_template_fields"
130
		 *
131
		 * @param $fields
132
		 *
133
		 * @return mixed
134
		 */
135
		public function all_template_fields( $fields ) {
136
			$fields[] = array(
137
				'id'       => 'video_hover_icon',
138
				'section'  => __( 'Video', 'foogallery' ),
139
				'title'    => __( 'Video Hover Icon', 'foogallery' ),
140
				'type'     => 'htmlicon',
141
				'default'  => 'fg-video-default',
142
				'choices'  => apply_filters(
143
					'foogallery_gallery_template_video_hover_icon_choices', array(
144
						''                 => array( 'label' => __( 'None', 'foogallery' ), 'html' => '<div class="foogallery-setting-video_overlay"></div>' ),
145
						'fg-video-default' => array( 'label' => __( 'Default Icon', 'foogallery' ), 'html' => '<div class="foogallery-setting-video_overlay fg-video-default"></div>' ),
146
						'fg-video-1'       => array( 'label' => __( 'Icon 1', 'foogallery' ), 'html' => '<div class="foogallery-setting-video_overlay fg-video-1"></div>' ),
147
						'fg-video-2'       => array( 'label' => __( 'Icon 2', 'foogallery' ), 'html' => '<div class="foogallery-setting-video_overlay fg-video-2"></div>' ),
148
						'fg-video-3'       => array( 'label' => __( 'Icon 3', 'foogallery' ), 'html' => '<div class="foogallery-setting-video_overlay fg-video-3"></div>' ),
149
						'fg-video-4'       => array( 'label' => __( 'Icon 4', 'foogallery' ), 'html' => '<div class="foogallery-setting-video_overlay fg-video-4"></div>' ),
150
					)
151
				),
152
				'row_data' => array(
153
					'data-foogallery-change-selector' => 'input:radio',
154
					'data-foogallery-preview'         => 'class'
155
				)
156
			);
157
			$fields[] = array(
158
				'id'       => 'video_sticky_icon',
159
				'section'  => __( 'Video', 'foogallery' ),
160
				'title'    => __( 'Sticky Video Icon', 'foogallery' ),
161
				'desc'     => __( 'Always show the video icon for videos in the gallery, and not only when you hover.', 'foogallery' ),
162
				'type'     => 'radio',
163
				'default'  => '',
164
				'spacer'   => '<span class="spacer"></span>',
165
				'choices'  => array(
166
					'fg-video-sticky' => __( 'Yes', 'foogallery' ),
167
					''                => __( 'No', 'foogallery' ),
168
				),
169
				'row_data' => array(
170
					'data-foogallery-change-selector' => 'input:radio',
171
					'data-foogallery-preview'         => 'class'
172
				)
173
			);
174
175
			$fields[] = array(
176
				'id'      => 'video_size_help',
177
				'title'   => __( 'Video Size Help', 'foogallery' ),
178
				'desc'    => __( 'The lightbox video size can be overridden on each individual video by editing the attachment info, and changing the Override Width and Override Height properties.', 'foogallery' ),
179
				'section' => __( 'Video', 'foogallery' ),
180
				'type'    => 'help',
181
			);
182
183
			$fields[] = array(
184
				'id'      => 'video_size',
185
				'section' => __( 'Video', 'foogallery' ),
186
				'title'   => __( 'Lightbox Video Size', 'foogallery' ),
187
				'desc'    => __( 'The default video size when opening videos in the lightbox.', 'foogallery' ),
188
				'type'    => 'select',
189
				'default' => '640x360',
190
				'choices' => array(
191
					'640x360'   => __( '640 x 360', 'foogallery' ),
192
					'854x480'   => __( '854 x 480', 'foogallery' ),
193
					'960x540'   => __( '960 x 540', 'foogallery' ),
194
					'1024x576'  => __( '1024 x 576', 'foogallery' ),
195
					'1280x720'  => __( '1280 x 720 (HD)', 'foogallery' ),
196
					'1366x768'  => __( '1366 x 768', 'foogallery' ),
197
					'1600x900'  => __( '1600 x 900', 'foogallery' ),
198
					'1920x1080' => __( '1920 x 1080 (Full HD)', 'foogallery' ),
199
				),
200
			);
201
202
			$fields[] = array(
203
				'id'      => 'video_autoplay',
204
				'section' => __( 'Video', 'foogallery' ),
205
				'title'   => __( 'Lightbox Autoplay', 'foogallery' ),
206
				'desc'    => __( 'Try to autoplay the video when opened in a lightbox. This will only work with videos hosted on Youtube or Vimeo.', 'foogallery' ),
207
				'type'    => 'radio',
208
				'default' => 'yes',
209
				'spacer'  => '<span class="spacer"></span>',
210
				'choices' => array(
211
					'yes' => __( 'Yes', 'foogallery' ),
212
					'no'  => __( 'No', 'foogallery' ),
213
				),
214
			);
215
216
			return $fields;
217
		}
218
219
		public function set_video_flag_on_attachment( $foogallery_attachment, $post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $post 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...
220
			$foogallery_attachment->is_video = false;
221
222
			if ( foogallery_is_attachment_video( $foogallery_attachment ) ) {
223
				$foogallery_attachment->is_video = true;
224
			}
225
		}
226
227
		/**
228
		 * @uses "foogallery_attachment_html_item_classes" filter
229
		 *
230
		 * @param                             $classes
231
		 * @param                             $args
232
		 * @param object|FooGalleryAttachment $attachment
233
		 *
234
		 * @return mixed
235
		 */
236
		public function alter_video_item_attributes( $classes, $attachment, $args ) {
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
237
			if ( $attachment->is_video ) {
238
				$classes[] = 'fg-video';
239
			}
240
241
			return $classes;
242
		}
243
244
		/**
245
		 * @uses "foogallery_attachment_html_link_attributes" filter
246
		 *
247
		 * @param                             $attr
248
		 * @param                             $args
249
		 * @param object|FooGalleryAttachment $attachment
250
		 *
251
		 * @return mixed
252
		 */
253
		public function alter_video_link_attributes( $attr, $args, $attachment ) {
254
			global $current_foogallery;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
255
256
			if ( $attachment->is_video ) {
257
				$current_foogallery->has_videos = true;
258
259
				$video_data = get_post_meta( $attachment->ID, FOOGALLERY_VIDEO_POST_META, true );
260
261
				$is_embed = false;
262
263
				if ( isset( $video_data['type'] ) ) {
264
265
					//identify the item as an embed and also check if FooBox is the lightbox
266
					$is_embed = 'embed' === $video_data['type'];
267
268
					$attr['data-type'] = $is_embed ? 'embed' : 'video'; //$video_data['type'];
269
				}
270
271
				//set the cover image for the video
272
				$attr['data-cover'] = $attachment->url;
273
274
				//make some changes for embeds
275
				if ( $is_embed ) {
276
					$oembed_data = foogallery_oembed_get_data( $attachment->custom_url );
277
278
					$data = array(
279
						'id'            => 'foogallery_embed_'.$current_foogallery->ID . '-' . $attachment->ID,
280
						'attachment_id' => $attachment->ID,
281
						'url'           => $attachment->custom_url,
282
						'provider'      => $video_data['provider'],
283
						'html'          => $oembed_data->html
284
					);
285
286
					$current_foogallery->video_embeds[] = $data;
287
288
					$attr['href'] = '#' . $data['id'];
289
				} else {
290
					$attr['href'] = foogallery_get_video_url_from_attachment( $attachment );
291
				}
292
293
				//if we have no widths or heights then use video default size
294
				if ( ! isset( $attr['data-width'] ) ) {
295
					$size = foogallery_gallery_template_setting( 'video_size', '640x360' );
296
					list( $width, $height ) = explode( 'x', $size );
297
					$attr['data-width']  = $width;
298
					$attr['data-height'] = $height;
299
				}
300
301
				//override width
302
				$override_width = get_post_meta( $attachment->ID, '_data-width', true );
303
				if ( ! empty( $override_width ) ) {
304
					$attr['data-width'] = intval( $override_width );
305
				}
306
307
				//override height
308
				$override_height = get_post_meta( $attachment->ID, '_data-height', true );
309
				if ( ! empty( $override_height ) ) {
310
					$attr['data-height'] = intval( $override_height );
311
				}
312
313
				$lightbox = foogallery_gallery_template_setting( 'lightbox', 'unknown' );
314
				//if no lightbox is being used then force to open in new tab
315
				if ( 'unknown' === $lightbox || 'none' === $lightbox ) {
316
					$attr['target'] = '_blank';
317
				}
318
			}
319
320
			return $attr;
321
		}
322
323
		public function foogallery_build_class_attribute( $classes ) {
324
			global $current_foogallery;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
325
326
			//first determine if the gallery has any videos
327
			if ( 0 === foogallery_get_gallery_video_count( $current_foogallery->ID ) ) {
328
				return $classes;
329
			}
330
331
			$current_foogallery->has_videos = true;
332
333
			//get the selected video icon
334
			$classes[] = foogallery_gallery_template_setting( 'video_hover_icon', 'fg-video-default' );
335
336
			//include the video sticky class
337
			$classes[] = foogallery_gallery_template_setting( 'video_sticky_icon', '' );;
338
339
			return $classes;
340
		}
341
342
		/**
343
		 * Enqueue any script or stylesheet file dependencies that FooGallery_Pro_Video relies on
344
		 *
345
		 * @param $foogallery FooGallery
346
		 */
347
		function enqueue_foobox_free_dependencies( $foogallery ) {
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...
348
			if ( $foogallery ) {
349
				$video_count = foogallery_get_gallery_video_count( $foogallery->ID );
350
351
				if ( $video_count > 0 ) {
352
353
					$lightbox = foogallery_gallery_template_setting( 'lightbox', 'unknown' );
354
					//we want to add some JS to the front-end ONLY if we are using FooBox Free
355
356
					if ( class_exists( 'Foobox_Free' ) && ( 'foobox' == $lightbox || 'foobox-free' == $lightbox ) ) {
357
						$js = FOOGALLERY_PRO_URL . 'js/foobox.video.min.js';
358
						wp_enqueue_script(
359
							'foogallery-foobox-video',
360
							$js,
361
							array( 'jquery', 'foobox-free-min' ),
362
							FOOGALLERY_VERSION
363
						);
364
					}
365
				}
366
			}
367
		}
368
369
		public function calculate_video_count( $post_id ) {
370
			foogallery_set_gallery_video_count( $post_id );
371
		}
372
373
		public function include_video_count( $image_count_text, $gallery ) {
374
			$count       = sizeof( $gallery->attachment_ids );
375
			$video_count = foogallery_get_gallery_video_count( $gallery->ID );
376
			$image_count = $count - $video_count;
377
378
			return foogallery_gallery_image_count_text( $count, $image_count, $video_count );
379
		}
380
381
		public function include_video_settings( $settings ) {
382
			$settings['settings'][] = array(
383
				'id'      => 'video_default_target',
384
				'title'   => __( 'Default Video Target', 'foogallery' ),
385
				'desc'    => __( 'The default target set for a video when it is imported into the gallery.', 'foogallery' ),
386
				'type'    => 'select',
387
				'default' => '_blank',
388
				'section' => __( 'Gallery Defaults', 'foogallery ' ),
389
				'tab'     => 'general',
390
				'choices' => array(
391
					'default' => __( 'Default', 'foogallery' ),
392
					'_blank'  => __( 'New tab (_blank)', 'foogallery' ),
393
					'_self'   => __( 'Same tab (_self)', 'foogallery' ),
394
					'foobox'  => __( 'FooBox', 'foogallery' ),
395
				),
396
			);
397
			$settings['settings'][] = array(
398
				'id'      => 'language_video_count_none_text',
399
				'title'   => __( 'Video Count None Text', 'foogallery' ),
400
				'type'    => 'text',
401
				'default' => __( 'No images or videos', 'foogallery' ),
402
				'tab'     => 'language',
403
			);
404
			$settings['settings'][] = array(
405
				'id'      => 'language_video_count_single_text',
406
				'title'   => __( 'Video Count Single Text', 'foogallery' ),
407
				'type'    => 'text',
408
				'default' => __( '1 video', 'foogallery' ),
409
				'tab'     => 'language',
410
			);
411
			$settings['settings'][] = array(
412
				'id'      => 'language_video_count_plural_text',
413
				'title'   => __( 'Video Count Many Text', 'foogallery' ),
414
				'type'    => 'text',
415
				'default' => __( '%s videos', 'foogallery' ),
416
				'tab'     => 'language',
417
			);
418
419
			return $settings;
420
		}
421
422
423
		/**
424
		 * Renders any video embeds for the gallery
425
		 *
426
		 * @param FooGallery $gallery
427
		 */
428
		function include_video_embeds( $gallery ) {
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...
429
			if ( isset( $gallery->has_videos ) && $gallery->has_videos ) {
430
431
				?>
432
				<div style="display: none;"><?php
433
434
				foreach ( $gallery->video_embeds as $embed ) {
435
					?>
436
					<div id="<?php echo $embed['id']; ?>" data-provider="<?php echo $embed['provider']; ?>">
437
						<?php echo $embed['html']; ?>
438
					</div>
439
					<?php
440
				}
441
442
				?></div><?php
443
			}
444
		}
445
	}
446
}