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_LazyLoad::add_lazyload_field()   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 2
nop 2
dl 0
loc 26
rs 8.5806
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
            }
64
        }
65
66
        /**
67
         * @param array $attr
68
         * @param array $args
69
         * @param FooGalleryAttachment $attachment
70
         * @return mixed
71
         */
72
        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...
73
        {
74
            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...
75
76
            if ($current_foogallery !== null) {
77
78
                if (isset($current_foogallery->lazyload_support) && true === $current_foogallery->lazyload_support) {
79
                    if (isset($attr['src'])) {
80
                        //rename src => data-src
81
                        $src = $attr['src'];
82
                        unset($attr['src']);
83
                        $attr['data-src-fg'] = $src;
84
                    }
85
86
                    if (isset($attr['srcset'])) {
87
                        //rename srcset => data-srcset
88
                        $src = $attr['srcset'];
89
                        unset($attr['srcset']);
90
                        $attr['data-srcset-fg'] = $src;
91
                    }
92
                }
93
            }
94
95
            return $attr;
96
        }
97
98
99
        /**
100
         * Add the required lazy load options if needed
101
         *
102
         * @param $attributes array
103
         * @param $gallery FooGallery
104
         *
105
         * @return array
106
         */
107
        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...
108
        {
109
            if ( isset( $gallery->lazyload_support ) && true === $gallery->lazyload_support ) {
110
                $options['lazy'] = $gallery->lazyload_enabled && !$gallery->lazyload_forced_disabled;
111
                $options['src'] = 'data-src-fg';
112
                $options['srcset'] = 'data-srcset-fg';
113
            }
114
            return $options;
115
        }
116
117
        /**
118
         * Add lazyload field to the gallery template if supported
119
         *
120
         * @param $fields
121
         * @param $template
122
         *
123
         * @return array
124
         */
125
        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...
126
        {
127
            //check if the template supports lazy loading
128
            if ( $template && array_key_exists( 'lazyload_support', $template ) && true === $template['lazyload_support'] ) {
129
130
                $fields[] = array(
131
                    'id'      => 'lazyload',
132
                    'title'   => __( 'Lazy Loading', 'foogallery' ),
133
                    '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' ),
134
                    'section' => __( 'Advanced', 'foogallery' ),
135
                    'type'     => 'radio',
136
                    'spacer'   => '<span class="spacer"></span>',
137
                    'default'  => '',
138
                    'choices'  => array(
139
                        '' => __( 'Enable Lazy Loading', 'foogallery' ),
140
                        'disabled' => __( 'Disable Lazy Loading', 'foogallery' ),
141
                    ),
142
                    'row_data' => array(
143
                        'data-foogallery-change-selector' => 'input:radio',
144
                        'data-foogallery-preview' => 'shortcode'
145
                    )
146
                );
147
            }
148
149
            return $fields;
150
        }
151
152
        /**
153
         * Build up a arguments used in the preview of the gallery
154
         * @param $args
155
         * @param $post_data
156
         * @param $template
157
         *
158
         * @return mixed
159
         */
160
        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...
161
            if ( isset( $post_data[FOOGALLERY_META_SETTINGS][$template . '_lazyload'] ) ) {
162
                $args['lazyload'] = $post_data[FOOGALLERY_META_SETTINGS][$template . '_lazyload'];
163
            }
164
            return $args;
165
        }
166
167
168
        /**
169
         * Add some global settings
170
         * @param $settings
171
         *
172
         * @return array
173
         */
174
        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...
175
176
            $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...
177
                'id'      => 'disable_lazy_loading',
178
                'title'   => __( 'Disable Lazy Loading', 'foogallery' ),
179
                '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' ),
180
                'type'    => 'checkbox',
181
                'tab'     => 'general',
182
                'section' => __( 'Lazy Loading', 'foogallery' )
183
            );
184
185
            $new_settings = array_merge( $lazy_settings, $settings['settings'] );
186
187
            $settings['settings'] = $new_settings;
188
189
            return $settings;
190
        }
191
    }
192
}