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.

FooGallery_LazyLoad::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 20
rs 9.6
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class used to handle lazy loading for gallery templates
4
 * Date: 20/03/2017
5
 */
6
if ( ! class_exists( 'FooGallery_LazyLoad' ) ) {
7
8
	class FooGallery_LazyLoad
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
11
        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...
12
        {
13
            //determine lazy loading for the gallery once up front before the template is loaded
14
            add_action('foogallery_located_template', array($this, 'determine_lazyloading_for_gallery'));
15
16
            //change the image src attribute to data attributes if lazy loading is enabled
17
            add_filter('foogallery_attachment_html_image_attributes', array($this, 'change_src_attributes'), 99, 3);
18
19
            //add the lazy load attributes to the gallery container
20
            add_filter('foogallery_build_container_data_options', array($this, 'add_lazyload_options'), 10, 3);
21
22
            //add common fields to the templates that support it
23
            add_filter('foogallery_override_gallery_template_fields', array($this, 'add_lazyload_field'), 100, 2);
24
25
            //build up preview arguments
26
            add_filter( 'foogallery_preview_arguments', array( $this, 'preview_arguments' ), 10, 3 );
27
28
            //add some settings to allow forcing of the lazy loading to be disabled
29
            add_filter( 'foogallery_admin_settings_override', array( $this, 'add_settings' ) );
30
        }
31
32
        /**
33
         * Determine all the lazu loading variables that can be set on a gallery
34
         * @param $foogallery
35
         */
36
        function determine_lazyloading_for_gallery($foogallery) {
0 ignored issues
show
Unused Code introduced by
The parameter $foogallery 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...
37
            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...
38
            global $current_foogallery_template;
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...
39
40
            if ($current_foogallery !== null) {
41
                //make sure we only do this once for better performance
42
                if (!isset($current_foogallery->lazyload_support)) {
43
44
                    //load the gallery template
45
                    $template_info = foogallery_get_gallery_template($current_foogallery_template);
46
47
                    //check if the template supports lazy loading
48
                    $lazyloading_support = isset($template_info['lazyload_support']) &&
49
                        true === $template_info['lazyload_support'];
50
51
                    //set if lazy loading is supported for the gallery
52
                    $current_foogallery->lazyload_support = apply_filters('foogallery_lazy_load', $lazyloading_support, $current_foogallery, $current_foogallery_template);
53
54
                    //set if lazy loading is enabled for the gallery
55
                    $lazyloading_default = '';
56
                    $lazyloading_enabled = foogallery_gallery_template_setting('lazyload', $lazyloading_default) === '';
57
                    $current_foogallery->lazyload_enabled = $lazyloading_enabled;
58
59
                    //set if lazy loading is forced to disabled for all galleries
60
                    $lazyloading_forced_disabled = foogallery_get_setting('disable_lazy_loading') === 'on';
61
                    $current_foogallery->lazyload_forced_disabled = $lazyloading_forced_disabled;
62
63
                    //check if we are inside a feed. Always disable lazy load when shown within a feed
64
					if ( is_feed() ) {
65
						$current_foogallery->lazyload_forced_disabled = true;
66
					}
67
68
                }
69
            }
70
        }
71
72
        /**
73
         * @param array $attr
74
         * @param array $args
75
         * @param FooGalleryAttachment $attachment
76
         * @return mixed
77
         */
78
        function change_src_attributes($attr, $args, $attachment)
0 ignored issues
show
Unused Code introduced by
The parameter $args 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...
Unused Code introduced by
The parameter $attachment 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...
79
        {
80
            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...
81
82
            if ($current_foogallery !== null) {
83
84
            	//check that lazy loading was not disabled from the Gallery Settings -> Advanced Tab
85
            	if ( isset( $current_foogallery->lazyload_enabled ) ) {
86
            		if ( false === $current_foogallery->lazyload_enabled ) {
87
						return $attr;
88
					}
89
				}
90
91
				//check that lazy loading was not disabled from Global Settings or any other reason
92
				if ( isset( $current_foogallery->lazyload_forced_disabled ) ) {
93
					if ( true === $current_foogallery->lazyload_forced_disabled ) {
94
						return $attr;
95
					}
96
				}
97
98
                if (isset($current_foogallery->lazyload_support) && true === $current_foogallery->lazyload_support) {
99
                    if (isset($attr['src'])) {
100
                        //rename src => data-src
101
                        $src = $attr['src'];
102
                        unset($attr['src']);
103
                        $attr['data-src-fg'] = $src;
104
                    }
105
106
                    if (isset($attr['srcset'])) {
107
                        //rename srcset => data-srcset
108
                        $src = $attr['srcset'];
109
                        unset($attr['srcset']);
110
                        $attr['data-srcset-fg'] = $src;
111
                    }
112
                }
113
            }
114
115
            return $attr;
116
        }
117
118
119
        /**
120
         * Add the required lazy load options if needed
121
         *
122
         * @param $attributes array
123
         * @param $gallery FooGallery
124
         *
125
         * @return array
126
         */
127
        function add_lazyload_options($options, $gallery, $attributes)
0 ignored issues
show
Unused Code introduced by
The parameter $attributes 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...
128
        {
129
            if ( isset( $gallery->lazyload_support ) && true === $gallery->lazyload_support ) {
130
                $options['lazy'] = $gallery->lazyload_enabled && !$gallery->lazyload_forced_disabled;
131
                if ( $options['lazy'] ) {
132
					$options['src']    = 'data-src-fg';
133
					$options['srcset'] = 'data-srcset-fg';
134
				} else {
135
					$options['src']    = 'src';
136
					$options['srcset'] = 'srcset';
137
				}
138
            }
139
            return $options;
140
        }
141
142
        /**
143
         * Add lazyload field to the gallery template if supported
144
         *
145
         * @param $fields
146
         * @param $template
147
         *
148
         * @return array
149
         */
150
        function add_lazyload_field($fields, $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...
151
        {
152
            //check if the template supports lazy loading
153
            if ( $template && array_key_exists( 'lazyload_support', $template ) && true === $template['lazyload_support'] ) {
154
155
                $fields[] = array(
156
                    'id'      => 'lazyload',
157
                    'title'   => __( 'Lazy Loading', 'foogallery' ),
158
                    'desc'    => __( 'If you choose to disable lazy loading, then all thumbnails will be loaded at once. This means you will lose the performance improvements that lazy loading gives you.', 'foogallery' ),
159
                    'section' => __( 'Advanced', 'foogallery' ),
160
                    'type'     => 'radio',
161
                    'spacer'   => '<span class="spacer"></span>',
162
                    'default'  => '',
163
                    'choices'  => array(
164
                        '' => __( 'Enable Lazy Loading', 'foogallery' ),
165
                        'disabled' => __( 'Disable Lazy Loading', 'foogallery' ),
166
                    ),
167
                    'row_data' => array(
168
                        'data-foogallery-change-selector' => 'input:radio',
169
                        'data-foogallery-preview' => 'shortcode'
170
                    )
171
                );
172
            }
173
174
            return $fields;
175
        }
176
177
        /**
178
         * Build up a arguments used in the preview of the gallery
179
         * @param $args
180
         * @param $post_data
181
         * @param $template
182
         *
183
         * @return mixed
184
         */
185
        function preview_arguments( $args, $post_data, $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...
186
            if ( isset( $post_data[FOOGALLERY_META_SETTINGS][$template . '_lazyload'] ) ) {
187
                $args['lazyload'] = $post_data[FOOGALLERY_META_SETTINGS][$template . '_lazyload'];
188
            }
189
            return $args;
190
        }
191
192
193
        /**
194
         * Add some global settings
195
         * @param $settings
196
         *
197
         * @return array
198
         */
199
        function add_settings( $settings ) {
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...
200
201
            $lazy_settings[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$lazy_settings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $lazy_settings = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
202
                'id'      => 'disable_lazy_loading',
203
                'title'   => __( 'Disable Lazy Loading', 'foogallery' ),
204
                'desc'    => __( 'This will disable lazy loading for ALL galleries. This is not recommended, but is sometimes needed when there are problems with the galleries displaying on some installs.', 'foogallery' ),
205
                'type'    => 'checkbox',
206
                'tab'     => 'general',
207
                'section' => __( 'Lazy Loading', 'foogallery' )
208
            );
209
210
            $new_settings = array_merge( $lazy_settings, $settings['settings'] );
211
212
            $settings['settings'] = $new_settings;
213
214
            return $settings;
215
        }
216
    }
217
}