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... ( b693b9...cbf425 )
by Brad
02:40
created

include_thumb_dimension_attributes()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 21
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 9
nc 13
nop 3
dl 0
loc 21
rs 7.551
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class to calculate thumb dimensions for a gallery
4
 * Date: 21/03/2017
5
 */
6
if ( ! class_exists( 'FooGallery_Thumbnail_Dimensions' ) ) {
7
8
	class FooGallery_Thumbnail_Dimensions {
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...
9
10
		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...
11
			if ( is_admin() ) {
12
				add_action( 'foogallery_after_save_gallery', array( $this, 'calculate_thumbnail_dimensions' ), 9, 2 );
13
			}
14
15
			add_filter( 'foogallery_attachment_load', array( $this, 'load_thumbnail_dimensions' ), 10, 2 );
16
			add_filter( 'foogallery_attachment_html_image_attributes', array( $this, 'include_thumb_dimension_attributes' ), 10, 3 );
17
		}
18
19
		/**
20
		 * Calculate the exact thumb size for the gallery and save the meta data
21
		 * @param $post_id
22
		 * @param $form_post
23
		 */
24
		function calculate_thumbnail_dimensions( $post_id, $form_post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_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...
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...
25
			$foogallery = FooGallery::get_by_id( $post_id );
26
27
			$gallery_template = $foogallery->gallery_template;
28
29
			$setting_key = "{$gallery_template}_thumbnail_dimensions";
30
31
			$thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $gallery_template, $foogallery->get_meta( $setting_key, false ), $foogallery );
32
33
			if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
34
35
				//$thumbnail_dimensions
36
				$thumb_width  = (int) $thumbnail_dimensions['width'];
37
				$thumb_height = (int) $thumbnail_dimensions['height'];
38
				$thumb_crop   = (bool) $thumbnail_dimensions['crop'];
39
40
				foreach ( $foogallery->attachments() as $attachment ) {
41
42
					$size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop );
43
44
					$size = array(
45
						'width'  => $size_array[4],
46
						'height' => $size_array[5]
47
					);
48
49
					$existing_size                  = get_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true );
50
					$existing_size[$foogallery->ID] = $size;
51
52
					//save the post meta against the attachment
53
					update_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, $existing_size );
54
				}
55
			}
56
		}
57
58
		/**
59
		 * Load the thumb dimension attributes onto the attachment
60
		 * @param $foogallery_attachment
61
		 * @param $foogallery
62
		 *
63
		 * @return mixed
64
		 */
65
		function load_thumbnail_dimensions( $foogallery_attachment, $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...
66
			$size = get_post_meta( $foogallery_attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true );
67
			if ( isset( $size ) && is_array( $size ) && array_key_exists( $foogallery->ID, $size ) ) {
68
				$size = $size[$foogallery->ID];
69
70
				$foogallery_attachment->foogallery_id = $foogallery->ID;
71
				$foogallery_attachment->thumb_width = $size['width'];
72
				$foogallery_attachment->thumb_height = $size['height'];
73
			}
74
75
			return $foogallery_attachment;
76
		}
77
78
		/**
79
		 * Include the thumb dimension html attributes in the rendered HTML
80
		 *
81
		 * @param $attr
82
		 * @param $args
83
		 * @param $foogallery_attachment
84
		 *
85
		 * @return array
86
		 */
87
		function include_thumb_dimension_attributes( $attr, $args, $foogallery_attachment ) {
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...
88
			if ( isset( $foogallery_attachment->foogallery_id ) ) {
89
				//do a check to see if the values have changed
90
91
				if ( $foogallery_attachment->thumb_width > 0 && array_key_exists( 'width', $attr ) ) {
92
					if ( intval( $attr['width'] ) !== $foogallery_attachment->thumb_width ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
93
						//we need to recalculate dimensions
94
					}
95
				}
96
97
98
				if ( $foogallery_attachment->thumb_width > 0 ) {
99
					$attr['width'] = $foogallery_attachment->thumb_width;
100
				}
101
				if ( $foogallery_attachment->thumb_height > 0 ) {
102
					$attr['height'] = $foogallery_attachment->thumb_height;
103
				}
104
			}
105
106
			return $attr;
107
		}
108
	}
109
}