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... ( cd983d...5984e4 )
by Brad
02:39
created

include_thumb_dimension_attributes()   C

Complexity

Conditions 7
Paths 15

Size

Total Lines 33
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 19
nc 15
nop 3
dl 0
loc 33
rs 6.7272
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
			$default_thumbnail_dimensions = $foogallery->get_meta( $setting_key, false );
32
33
			$thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $gallery_template, $default_thumbnail_dimensions, $foogallery );
34
35
			if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
36
37
				//$thumbnail_dimensions
38
				$thumb_width  = (int) $thumbnail_dimensions['width'];
39
				$thumb_height = (int) $thumbnail_dimensions['height'];
40
				$thumb_crop   = (bool) $thumbnail_dimensions['crop'];
41
42
				foreach ( $foogallery->attachments() as $attachment ) {
43
44
					$size_array = image_resize_dimensions( $attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop );
45
46
					$size = array(
47
						'width'  => $size_array[4],
48
						'height' => $size_array[5]
49
					);
50
51
					$existing_size                  = get_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true );
52
					$existing_size[$foogallery->ID] = $size;
53
54
					//save the post meta against the attachment
55
					update_post_meta( $attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, $existing_size );
56
				}
57
			}
58
		}
59
60
		/**
61
		 * Load the thumb dimension attributes onto the attachment
62
		 * @param $foogallery_attachment
63
		 * @param $foogallery
64
		 *
65
		 * @return mixed
66
		 */
67
		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...
68
			$size = get_post_meta( $foogallery_attachment->ID, FOOGALLERY_META_THUMB_DIMENSIONS, true );
69
			if ( isset( $size ) && is_array( $size ) && array_key_exists( $foogallery->ID, $size ) ) {
70
				$size = $size[$foogallery->ID];
71
72
				$foogallery_attachment->foogallery_id = $foogallery->ID;
73
				$foogallery_attachment->thumb_width = $size['width'];
74
				$foogallery_attachment->thumb_height = $size['height'];
75
			}
76
77
			return $foogallery_attachment;
78
		}
79
80
		/**
81
		 * Include the thumb dimension html attributes in the rendered HTML
82
		 *
83
		 * @param $attr
84
		 * @param $args
85
		 * @param $foogallery_attachment
86
		 *
87
		 * @return array
88
		 */
89
		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...
90
			//do a check to see if the template has changed
91
			global $current_foogallery_arguments;
92
			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...
93
			if ( isset( $current_foogallery_arguments ) && isset( $current_foogallery_arguments['template'] ) ) {
94
95
				//we need to calculate new dynamic dimensions for the thumb
96
				$thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_arguments['template'], false, $current_foogallery_arguments );
97
98
				if ( $thumbnail_dimensions ) {
99
					//$thumbnail_dimensions
100
					$thumb_width  = (int) $thumbnail_dimensions['width'];
101
					$thumb_height = (int) $thumbnail_dimensions['height'];
102
					$thumb_crop   = (bool) $thumbnail_dimensions['crop'];
103
104
					$size_array                          = image_resize_dimensions( $foogallery_attachment->width, $foogallery_attachment->height, $thumb_width, $thumb_height, $thumb_crop );
105
					$foogallery_attachment->foogallery_id = $current_foogallery->ID;
106
					$foogallery_attachment->thumb_width  = $size_array[4];
107
					$foogallery_attachment->thumb_height = $size_array[5];
108
				}
109
			}
110
111
			if ( isset( $foogallery_attachment->foogallery_id ) ) {
112
				if ( $foogallery_attachment->thumb_width > 0 ) {
113
					$attr['width'] = $foogallery_attachment->thumb_width;
114
				}
115
				if ( $foogallery_attachment->thumb_height > 0 ) {
116
					$attr['height'] = $foogallery_attachment->thumb_height;
117
				}
118
			}
119
120
			return $attr;
121
		}
122
	}
123
}