fooplugins /
foogallery
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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 |
||
| 9 | { |
||
| 10 | |||
| 11 | function __construct() |
||
| 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) { |
||
| 37 | global $current_foogallery; |
||
| 38 | global $current_foogallery_template; |
||
| 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) |
||
| 79 | { |
||
| 80 | global $current_foogallery; |
||
| 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) |
||
| 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) |
||
| 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 ) { |
||
| 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 ) { |
||
| 200 | |||
| 201 | $lazy_settings[] = array( |
||
|
0 ignored issues
–
show
|
|||
| 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 | } |
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:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey 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.