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 ( 8970a4...b3ee5f )
by Brad
06:24 queued 03:11
created

FooGallery_CSS_Load_Optimizer   A

Complexity

Total Complexity 38

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 180
rs 9.36
c 0
b 0
f 0
wmc 38
lcom 1
cbo 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A persist_enqueued_styles() 0 15 5
A get_post_ids_from_query() 0 11 3
A include_gallery_css() 0 9 2
B include_gallery_stylesheets_for_post() 0 28 10
C enqueue_style_to_persist() 0 57 11
A create_cache_buster_key() 0 3 1
A get_old_style_post_meta_value() 0 17 5
1
<?php
2
/**
3
 * FooGallery_CSS_Load_Optimizer class which enqueues CSS in the head
4
 */
5
if (!class_exists('class-css-load-optimizer.php')) {
6
7
    class FooGallery_CSS_Load_Optimizer {
8
9
        function __construct() {
10
            add_action( 'wp_enqueue_scripts', array( $this, 'include_gallery_css' ) );
11
            add_action( 'foogallery_enqueue_style', array( $this, 'enqueue_style_to_persist' ), 10, 5 );
12
            add_action( 'wp_footer', array( $this, 'persist_enqueued_styles' ) );
13
        }
14
15
		/**
16
		 * Persist any styles that are enqueued to be persisted
17
		 */
18
        function persist_enqueued_styles() {
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...
19
			global $wp_query, $foogallery_styles_to_persist;
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...
20
21
			//we only want to do this if we are looking at a single post
22
			if ( ! is_singular() ) {
23
				return;
24
			}
25
26
			$post_id = $wp_query->post->ID;
27
			if ( $post_id  && is_array( $foogallery_styles_to_persist ) ) {
28
				foreach( $foogallery_styles_to_persist as $style_handle => $style ) {
29
					add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ), false );
30
				}
31
			}
32
		}
33
34
        /**
35
         * Get the current post ids for the view that is being shown
36
         */
37
        function get_post_ids_from_query() {
38
            global $wp_query;
39
40
            if ( is_singular() ) {
41
                return array( $wp_query->post->ID );
42
            } else if ( is_array( $wp_query->posts ) ) {
43
                return wp_list_pluck( $wp_query->posts, 'ID' );
44
            } else {
45
                return array();
46
            }
47
        }
48
49
        /**
50
         * Checks the post meta for any FooGallery CSS that needs to be added to the head
51
         *
52
         */
53
        function include_gallery_css() {
54
            global $enqueued_foogallery_styles;
55
56
            $enqueued_foogallery_styles = array();
57
58
            foreach( $this->get_post_ids_from_query() as $post_id ) {
59
                $this->include_gallery_stylesheets_for_post( $post_id );
60
            }
61
        }
62
63
        /**
64
         * includes any CSS that needs to be added for a post
65
         *
66
         * @param $post_id int ID of the post
67
         */
68
        function include_gallery_stylesheets_for_post( $post_id ) {
69
            global $enqueued_foogallery_styles;
70
71
            if ( $post_id ) {
72
                //get any foogallery stylesheets that the post might need to include
73
                $css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS);
74
75
				if ( empty( $css ) || !is_array( $css ) ) return;
76
77
                foreach ($css as $css_item) {
78
                    if (!$css_item) continue;
79
                    foreach ($css_item as $handle => $style) {
80
                        //only enqueue the stylesheet once
81
                        if ( !array_key_exists( $handle, $enqueued_foogallery_styles ) ) {
82
                            $cache_buster_key = $handle;
83
                            if ( is_array( $style ) ) {
84
                                $cache_buster_key = $this->create_cache_buster_key( $handle, $style['ver'], array_key_exists( 'site', $style ) ? $style['site'] : '' );
85
                                wp_enqueue_style( $handle, $style['src'], $style['deps'], $style['ver'], $style['media'] );
86
                            } else {
87
                                wp_enqueue_style( $handle, $style );
88
                            }
89
90
                            $enqueued_foogallery_styles[$handle] = $cache_buster_key;
91
                        }
92
                    }
93
                }
94
            }
95
        }
96
97
        /**
98
         * Check to make sure we have added the stylesheets to our custom post meta field,
99
         * so that on next render the stylesheet will be added to the page header
100
         *
101
         * @param $style_handle string The stylesheet handle
102
         * @param $src string The location for the stylesheet
103
         * @param array $deps
104
         * @param bool $ver
105
         * @param string $media
106
         */
107
        function enqueue_style_to_persist($style_handle, $src, $deps = array(), $ver = false, $media = 'all') {
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...
108
            global $wp_query, $enqueued_foogallery_styles, $foogallery_styles_to_persist;
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...
109
110
            //we only want to do this if we are looking at a single post
111
            if ( ! is_singular() ) {
112
                return;
113
            }
114
115
            $post_id = $wp_query->post->ID;
116
            if ( $post_id ) {
117
118
                //check if the saved stylesheet needs to be cache busted
119
                if ( is_array( $enqueued_foogallery_styles ) && array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) {
120
                    $registered_cache_buster_key = $enqueued_foogallery_styles[$style_handle];
121
122
                    //generate the key we want
123
                    $cache_buster_key = $this->create_cache_buster_key( $style_handle, $ver, home_url() );
124
125
                    if ( $registered_cache_buster_key !== $cache_buster_key ) {
126
                        //we need to bust this cached stylesheet!
127
                        $style = $this->get_old_style_post_meta_value( $post_id, $style_handle );
128
129
                        if ( false !== $style ) {
130
                            delete_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ) );
131
132
                            //unset the handle from, to force the save of the post meta
133
                            unset( $enqueued_foogallery_styles[$style_handle] );
134
                        }
135
                    }
136
                }
137
138
                //first check that the template has not been enqueued before
139
                if ( is_array( $enqueued_foogallery_styles ) && ! array_key_exists( $style_handle, $enqueued_foogallery_styles ) ) {
140
141
                    $style = array(
142
                        'src'   => $src,
143
                        'deps'  => $deps,
144
                        'ver'   => $ver,
145
                        'media' => $media,
146
                        'site'  => home_url()
147
                    );
148
149
                    if ( !is_array( $foogallery_styles_to_persist ) ) {
150
						$foogallery_styles_to_persist = array();
151
					}
152
153
					if ( !array_key_exists( $style_handle, $foogallery_styles_to_persist ) ) {
154
						$foogallery_styles_to_persist[$style_handle] = $style;
155
					}
156
157
//                    add_post_meta( $post_id, FOOGALLERY_META_POST_USAGE_CSS, array( $style_handle => $style ), false );
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
158
//
159
//					$cache_buster_key = $this->create_cache_buster_key( $style_handle, $ver, home_url() );
160
//					$enqueued_foogallery_styles[$style_handle] = $cache_buster_key;
161
                }
162
            }
163
        }
164
165
        function create_cache_buster_key( $name, $version, $site = '' ) {
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...
166
            return "{$site}::{$name}_{$version}";
167
        }
168
169
        function get_old_style_post_meta_value( $post_id, $handle_to_find ) {
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...
170
            $css = get_post_meta($post_id, FOOGALLERY_META_POST_USAGE_CSS);
171
172
            foreach ($css as $css_item) {
173
                if ( ! $css_item ) {
174
                    continue;
175
                }
176
                foreach ( $css_item as $handle => $style ) {
177
                    //only enqueue the stylesheet once
178
                    if ( $handle_to_find === $handle ) {
179
                        return $style;
180
                    }
181
                }
182
            }
183
184
            return false;
185
        }
186
    }
187
}