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 — master ( bb6361...a2a740 )
by Brad
04:28
created

FooGallery_Thumbnail_Dimensions   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 125
rs 10
c 0
b 0
f 0
wmc 26
lcom 0
cbo 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
B does_gallery_template_use_thumbnail_dimensions() 0 13 5
A empty_dimensions() 0 9 4
C calculate_all_thumbnail_dimensions() 0 48 11
B include_thumb_dimension_attributes() 0 19 5
1
<?php
2
/**
3
 * Class to calculate thumb dimensions for a gallery. The default gallery templates
4
 *  require width and height attributes on the thumb img tags. In some cases these need to be
5
 *  calculated based on the aspect ratio of the individual thumbs.
6
 *
7
 * Date: 21/03/2017
8
 */
9
10
11
if ( ! class_exists( 'FooGallery_Thumbnail_Dimensions' ) ) {
12
13
    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...
14
    {
15
        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...
16
        {
17
            //hook into the filter that build up the img attributes
18
            // and add the width and height attributes if the gallery requires them
19
            add_filter( 'foogallery_attachment_html_image_attributes', array( $this, 'include_thumb_dimension_attributes' ), 10, 3 );
20
21
            //calculate the thumbnail dimensions for all attachments in the gallery
22
            // for the specific gallery template that is being used.
23
            add_action( 'foogallery_located_template', array( $this, 'calculate_all_thumbnail_dimensions' ) );
24
        }
25
26
        /**
27
         * Helper function to check if the gallery requires thumbnail dimensions
28
         * @param $gallery_template string
29
         * @return bool
30
         */
31
        function does_gallery_template_use_thumbnail_dimensions( $gallery_template ) {
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...
32
            if ( empty( $gallery_template ) ) return false;
33
34
            //first do a check if the template needs thumbnail dimensions calculated
35
            $template_data = foogallery_get_gallery_template( $gallery_template );
36
37
            if ( $template_data && array_key_exists( 'thumbnail_dimensions', $template_data ) && true === $template_data['thumbnail_dimensions'] ) {
38
                //this template requires thumb dimensions to be provided
39
                return true;
40
            }
41
42
            return false;
43
        }
44
45
        function empty_dimensions( $thumbnail_dimensions ) {
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...
46
            if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
47
                $thumb_width = (int)$thumbnail_dimensions['width'];
48
                $thumb_height = (int)$thumbnail_dimensions['height'];
49
50
                return $thumb_width === 0 && $thumb_height === 0;
51
            }
52
            return true;
53
        }
54
55
        /**
56
         * Calculates all the thumbnail dimensions for the gallery
57
         * @param $foogallery FooGallery
58
         */
59
        function calculate_all_thumbnail_dimensions( $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...
60
            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...
61
            global $current_foogallery_template;
62
            global $current_foogallery_arguments;
63
64
            //check if we are dealing with a gallery. This check ensures this is not done for albums
65
            if ( isset( $current_foogallery ) && isset( $current_foogallery_template ) ) {
66
67
                //first do a check if the template needs thumbnail dimensions calculated
68
                if ($this->does_gallery_template_use_thumbnail_dimensions( $current_foogallery_template ) ) {
69
70
                    //load the thumbnail dimensions specific to the gallery, taking preference to arguments
71
                    $thumbnail_dimensions = apply_filters( 'foogallery_calculate_thumbnail_dimensions-' . $current_foogallery_template, array(), $current_foogallery_arguments );
72
73
                    //if we have no dimensions then load them from the gallery settings
74
                    if ( $this->empty_dimensions( $thumbnail_dimensions ) ) {
75
                        $thumbnail_dimensions = apply_filters( 'foogallery_template_thumbnail_dimensions-' . $current_foogallery_template, $thumbnail_dimensions, $current_foogallery );
76
                    }
77
78
                    if ( isset( $thumbnail_dimensions ) && is_array( $thumbnail_dimensions ) ) {
79
80
                        //$thumbnail_dimensions
81
                        $thumb_width = (int)$thumbnail_dimensions['width'];
82
                        $thumb_height = (int)$thumbnail_dimensions['height'];
83
                        $thumb_crop = (bool)$thumbnail_dimensions['crop'];
84
85
                        //set the appropriate arguments on the attachments so that they can be
86
                        // picked up and used in the 'include_thumb_dimension_attributes' function below
87
                        foreach ($foogallery->attachments() as $attachment) {
88
                            if ( $thumb_crop && $thumb_width > 0 && $thumb_height > 0 ) {
89
                                //we have set width and height and crop = true
90
                                //we do not need to calculate the dimensions
91
                                $calculated_thumb_width = $thumb_width;
92
                                $calculated_thumb_height = $thumb_height;
93
                            } else {
94
                                $size_array = image_resize_dimensions($attachment->width, $attachment->height, $thumb_width, $thumb_height, $thumb_crop);
95
                                $calculated_thumb_width = $size_array[4];
96
                                $calculated_thumb_height = $size_array[5];
97
                            }
98
99
                            $attachment->has_thumbnail_dimensions = true;
100
                            $attachment->thumb_width = $calculated_thumb_width;
101
                            $attachment->thumb_height = $calculated_thumb_height;
102
                        }
103
                    }
104
                }
105
            }
106
        }
107
108
        /**
109
         * Include the thumb dimension html attributes in the rendered HTML
110
         *
111
         * @param $attr
112
         * @param $args
113
         * @param $foogallery_attachment
114
         *
115
         * @return array
116
         */
117
        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...
118
            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...
119
120
            //check if we are dealing with a gallery. This check ensures this is not done for albums
121
            if ( isset( $current_foogallery ) ) {
122
123
                //check if we have anything set
124
                if ( isset( $foogallery_attachment->has_thumbnail_dimensions ) ) {
125
                    if ( $foogallery_attachment->thumb_width > 0 ) {
126
                        $attr['width'] = $foogallery_attachment->thumb_width;
127
                    }
128
                    if ( $foogallery_attachment->thumb_height > 0 ) {
129
                        $attr['height'] = $foogallery_attachment->thumb_height;
130
                    }
131
                }
132
            }
133
134
            return $attr;
135
        }
136
137
    }
138
}