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 — develop ( c721e5...0f0dc5 )
by Brad
03:00
created

include_thumb_dimension_attributes()   C

Complexity

Conditions 8
Paths 16

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 16
nop 3
dl 0
loc 36
rs 5.3846
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, 'after_save_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 for each attachment
21
		 * @param $foogallery_id
22
		 */
23
		function calculate_thumbnail_dimensions( $foogallery_id ) {
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...
24
			$foogallery = FooGallery::get_by_id( $foogallery_id );
25
26
			$gallery_template = $foogallery->gallery_template;
27
28
			$template_data = foogallery_get_gallery_template( $gallery_template );
29
30
			//check if the template supports thumbnail dimensions
31
			if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) {
32
33
				$setting_key = "{$gallery_template}_thumbnail_dimensions";
34
35
				$default_thumbnail_dimensions = $foogallery->get_meta( $setting_key, false );
36
37
				$thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $gallery_template, $default_thumbnail_dimensions, $foogallery );
38
39
				if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
40
41
					//$thumbnail_dimensions
42
					$thumb_width  = (int) $thumbnail_dimensions['width'];
43
					$thumb_height = (int) $thumbnail_dimensions['height'];
44
					$thumb_crop   = (bool) $thumbnail_dimensions['crop'];
45
46
					foreach ( $foogallery->attachments() as $attachment ) {
47
48
						$size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop );
49
50
						$size = array(
51
							'width'  => $size_array[4],
52
							'height' => $size_array[5]
53
						);
54
55
						$existing_size                  = get_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true );
56
						$existing_size[$foogallery->ID] = $size;
57
58
						//save the post meta against the attachment
59
						update_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, $existing_size );
60
					}
61
				}
62
			}
63
		}
64
65
		/**
66
		 * Calculate the exact thumb size for the gallery and save the meta data
67
		 * @param $post_id
68
		 * @param $form_post
69
		 */
70
		function after_save_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...
71
			$this->calculate_thumbnail_dimensions( $post_id );
72
		}
73
74
		/**
75
		 * Load the thumb dimension attributes onto the attachment
76
		 * @param $foogallery_attachment
77
		 * @param $foogallery
78
		 *
79
		 * @return mixed
80
		 */
81
		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...
82
			$gallery_template = $foogallery->gallery_template;
83
84
			$template_data = foogallery_get_gallery_template( $gallery_template );
85
86
			if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) {
87
88
				$size = get_post_meta( $foogallery_attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true );
89
				if ( isset( $size ) && is_array( $size ) && array_key_exists( $foogallery->ID, $size ) ) {
90
					$size = $size[$foogallery->ID];
91
92
					$foogallery_attachment->foogallery_id = $foogallery->ID;
93
					$foogallery_attachment->thumb_width   = $size['width'];
94
					$foogallery_attachment->thumb_height  = $size['height'];
95
				}
96
			}
97
98
			return $foogallery_attachment;
99
		}
100
101
		/**
102
		 * Include the thumb dimension html attributes in the rendered HTML
103
		 *
104
		 * @param $attr
105
		 * @param $args
106
		 * @param $foogallery_attachment
107
		 *
108
		 * @return array
109
		 */
110
		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...
111
			//do a check to see if the template has changed
112
			global $current_foogallery_arguments;
113
			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...
114
			//check if we are dealing with a gallery. This check ensures this is not done for albums
115
			if ( isset( $current_foogallery ) ) {
116
117
				if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['template'] ) ) {
118
					//we need to calculate new dynamic dimensions for the thumb
119
					$thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_arguments['template'], false, $current_foogallery_arguments );
120
121
					if ( $thumbnail_dimensions ) {
122
						//$thumbnail_dimensions
123
						$thumb_width  = (int) $thumbnail_dimensions['width'];
124
						$thumb_height = (int) $thumbnail_dimensions['height'];
125
						$thumb_crop   = (bool) $thumbnail_dimensions['crop'];
126
127
						$size_array                           = image_resize_dimensions( $foogallery_attachment->width, $foogallery_attachment->height, $thumb_width, $thumb_height, $thumb_crop );
128
						$foogallery_attachment->foogallery_id = $current_foogallery->ID;
129
						$foogallery_attachment->thumb_width   = $size_array[4];
130
						$foogallery_attachment->thumb_height  = $size_array[5];
131
					}
132
				}
133
134
				if ( isset( $foogallery_attachment->foogallery_id ) ) {
135
					if ( $foogallery_attachment->thumb_width > 0 ) {
136
						$attr['width'] = $foogallery_attachment->thumb_width;
137
					}
138
					if ( $foogallery_attachment->thumb_height > 0 ) {
139
						$attr['height'] = $foogallery_attachment->thumb_height;
140
					}
141
				}
142
			}
143
144
			return $attr;
145
		}
146
	}
147
}