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... ( a33793...3fa32a )
by Brad
02:31
created

load_thumbnail_dimensions()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 3
nop 2
dl 0
loc 19
rs 8.2222
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
			$template_data = foogallery_get_gallery_template( $gallery_template );
30
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
		 * Load the thumb dimension attributes onto the attachment
67
		 * @param $foogallery_attachment
68
		 * @param $foogallery
69
		 *
70
		 * @return mixed
71
		 */
72
		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...
73
			$gallery_template = $foogallery->gallery_template;
74
75
			$template_data = foogallery_get_gallery_template( $gallery_template );
76
77
			if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) {
78
79
				$size = get_post_meta( $foogallery_attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true );
80
				if ( isset( $size ) && is_array( $size ) && array_key_exists( $foogallery->ID, $size ) ) {
81
					$size = $size[$foogallery->ID];
82
83
					$foogallery_attachment->foogallery_id = $foogallery->ID;
84
					$foogallery_attachment->thumb_width   = $size['width'];
85
					$foogallery_attachment->thumb_height  = $size['height'];
86
				}
87
			}
88
89
			return $foogallery_attachment;
90
		}
91
92
		/**
93
		 * Include the thumb dimension html attributes in the rendered HTML
94
		 *
95
		 * @param $attr
96
		 * @param $args
97
		 * @param $foogallery_attachment
98
		 *
99
		 * @return array
100
		 */
101
		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...
102
			//do a check to see if the template has changed
103
			global $current_foogallery_arguments;
104
			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...
105
			//check if we are dealing with a gallery. This check ensures this is not done for albums
106
			if ( isset( $current_foogallery ) ) {
107
108
				if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['template'] ) ) {
109
					//we need to calculate new dynamic dimensions for the thumb
110
					$thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_arguments['template'], false, $current_foogallery_arguments );
111
112
					if ( $thumbnail_dimensions ) {
113
						//$thumbnail_dimensions
114
						$thumb_width  = (int) $thumbnail_dimensions['width'];
115
						$thumb_height = (int) $thumbnail_dimensions['height'];
116
						$thumb_crop   = (bool) $thumbnail_dimensions['crop'];
117
118
						$size_array                           = image_resize_dimensions( $foogallery_attachment->width, $foogallery_attachment->height, $thumb_width, $thumb_height, $thumb_crop );
119
						$foogallery_attachment->foogallery_id = $current_foogallery->ID;
120
						$foogallery_attachment->thumb_width   = $size_array[4];
121
						$foogallery_attachment->thumb_height  = $size_array[5];
122
					}
123
				}
124
125
				if ( isset( $foogallery_attachment->foogallery_id ) ) {
126
					if ( $foogallery_attachment->thumb_width > 0 ) {
127
						$attr['width'] = $foogallery_attachment->thumb_width;
128
					}
129
					if ( $foogallery_attachment->thumb_height > 0 ) {
130
						$attr['height'] = $foogallery_attachment->thumb_height;
131
					}
132
				}
133
			}
134
135
			return $attr;
136
		}
137
	}
138
}