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 — feature/gallery-template-clien... ( 1ace21...8f3dfe )
by Brad
02:32
created

FooGallery_Cache::is_caching_enabled()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 7
nc 3
nop 0
dl 0
loc 15
rs 9.2
c 0
b 0
f 0
1
<?php
2
/**
3
 * Class used to cache gallery HTML output to save requests to the database
4
 * Date: 20/03/2017
5
 */
6
if ( ! class_exists( 'FooGallery_Cache' ) ) {
7
8
	class FooGallery_Cache {
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
		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...
11
			if ( is_admin() ) {
12
				//intercept the gallery save and save the html output to post meta
13
				add_action( 'foogallery_after_save_gallery', array( $this, 'cache_gallery_html_output_after_save' ), 10, 2 );
14
15
				//add some settings to allow the clearing and disabling of the cache
16
				add_filter( 'foogallery_admin_settings_override', array( $this, 'add_cache_settings' ) );
17
18
				//render the clear HTML cache button
19
				add_action( 'foogallery_admin_settings_custom_type_render_setting', array( $this, 'render_settings' ) );
20
21
				// Ajax call for clearing HTML cache
22
				add_action( 'wp_ajax_foogallery_clear_html_cache', array( $this, 'ajax_clear_all_caches' ) );
23
24
				add_action( 'foogallery_admin_new_version_detected', array( $this, 'clear_cache_on_update' ) );
25
26
				//clear the gallery caches when settings are updated or reset
27
				add_action( 'foogallery_settings_updated', array( $this, 'clear_all_gallery_caches' ) );
28
				add_action( 'foogallery_settings_reset', array( $this, 'clear_all_gallery_caches' ) );
29
			}
30
31
			add_filter( 'foogallery_load_gallery_template', array( $this, 'fetch_gallery_html_from_cache' ), 10, 3 );
32
33
			add_filter( 'foogallery_html_cache_disabled', array( $this, 'disable_html_cache' ), 10, 3 );
34
		}
35
36
		/**
37
		 * Override if the gallery html cache is disabled
38
		 *
39
		 * @param $disabled bool
40
		 * @param $gallery FooGallery
41
		 * @return bool
42
		 */
43
		function disable_html_cache( $disabled, $gallery ) {
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...
44
45
			//check if the gallery sorting is random
46
			if ( 'rand' === $gallery->sorting ) {
47
				$disabled = true;
48
			}
49
50
			return $disabled;
51
		}
52
53
		/**
54
		 * Save the HTML output of the gallery after the gallery has been saved
55
		 *
56
		 * @param $post_id
57
		 * @param $form_post
58
		 */
59
		function cache_gallery_html_output_after_save( $post_id, $form_post ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form_post 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...
60
			$this->cache_gallery_html_output( $post_id );
61
		}
62
63
		/**
64
		 * Save the HTML output of the gallery to post meta so that it can be used in future requests
65
		 *
66
		 * @param $foogallery_id
67
		 */
68
		function cache_gallery_html_output( $foogallery_id ) {
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...
69
			$caching_enabled = $this->is_caching_enabled();
70
71
			//check if caching is disabled and quit early
72
			if ( $caching_enabled ) {
73
				return;
74
			}
75
76
			//we need a way to force the gallery to render it's output every time it is saved
77
			global $foogallery_force_gallery_cache;
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...
78
			$foogallery_force_gallery_cache = true;
79
80
			//capture the html output
81
			ob_start();
82
			foogallery_render_gallery( $foogallery_id );
83
			$gallery_html = ob_get_contents();
84
			ob_end_clean();
85
86
			if ( $caching_enabled ) {
87
				//save the output to post meta for later use
88
				update_post_meta( $foogallery_id, FOOGALLERY_META_CACHE, $gallery_html );
89
			}
90
91
			$foogallery_force_gallery_cache = false;
92
		}
93
94
		function is_caching_enabled() {
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...
95
			global $foogallery_gallery_preview;
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...
96
97
			//never cache if showing a preview
98
			if ( isset( $foogallery_gallery_preview ) && true === $foogallery_gallery_preview ) {
99
				return false;
100
			}
101
102
			//next, check the settings
103
			if ( 'on' === foogallery_get_setting( 'disable_html_cache' ) ) {
104
				return true;
105
			}
106
107
			return false;
108
		}
109
110
		/**
111
		 * Override the template output
112
		 *
113
		 * @param $override
114
		 * @param $gallery
115
		 * @param $template_location
116
		 *
117
		 * @return bool
118
		 */
119
		function fetch_gallery_html_from_cache( $override, $gallery, $template_location ) {
0 ignored issues
show
Unused Code introduced by
The parameter $template_location 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...
120
			global $foogallery_force_gallery_cache;
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...
121
			if ( $foogallery_force_gallery_cache ) {
122
				return false;
123
			}
124
125
			//check if caching is disabled and quit early
126
			if ( !$this->is_caching_enabled() ) {
127
				return false;
128
			}
129
130
			//allow extensions of others disable the html cache
131
			if ( apply_filters( 'foogallery_html_cache_disabled', false, $gallery ) ) {
132
				return false;
133
			}
134
135
			$gallery_cache = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE, true );
136
137
			if ( !empty( $gallery_cache ) && is_string( $gallery_cache ) ) {
138
				//output the cached gallery html
139
				echo $gallery_cache;
140
				return true; //return that we will override
141
			} else {
142
				//we should cache the result for next time
143
				$this->cache_gallery_html_output( $gallery->ID );
144
			}
145
146
			return false;
147
		}
148
149
		/**
150
		 * Add some caching settings
151
		 * @param $settings
152
		 *
153
		 * @return array
154
		 */
155
		function add_cache_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...
156
157
			$cache_settings[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$cache_settings was never initialized. Although not strictly required by PHP, it is generally a good practice to add $cache_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...
158
				'id'      => 'disable_html_cache',
159
				'title'   => __( 'Disable HTML Cache', 'foogallery' ),
160
				'desc'    => __( 'The gallery HTML is cached by default. You can choose to disable the cache for debugging purposes. It is NOT recommended.', 'foogallery' ),
161
				'type'    => 'checkbox',
162
				'tab'     => 'general',
163
				'section' => __( 'Cache', 'foogallery' )
164
			);
165
166
			$cache_settings[] = array(
167
				'id'      => 'clear_html_cache',
168
				'title'   => __( 'Clear HTML Cache', 'foogallery' ),
169
				'desc'    => sprintf( __( '%s caches the gallery HTML output to improve page performance. Use this button to clear the gallery HTML that has been cached across all galleries.', 'foogallery' ), foogallery_plugin_name() ),
170
				'type'    => 'clear_gallery_cache_button',
171
				'tab'     => 'general',
172
				'section' => __( 'Cache', 'foogallery' )
173
			);
174
175
			$new_settings = array_merge( $cache_settings, $settings['settings'] );
176
177
			$settings['settings'] = $new_settings;
178
179
			return $settings;
180
		}
181
182
		/**
183
		 * Render any custom setting types to the settings page
184
		 */
185
		function render_settings( $args ) {
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 ('clear_gallery_cache_button' === $args['type'] ) { ?>
187
				<div id="foogallery_clear_html_cache_container">
188
					<input type="button" data-nonce="<?php echo esc_attr( wp_create_nonce( 'foogallery_clear_html_cache' ) ); ?>" class="button-primary foogallery_clear_html_cache" value="<?php _e( 'Clear Gallery HTML Cache', 'foogallery' ); ?>">
189
					<span id="foogallery_clear_html_cache_spinner" style="position: absolute" class="spinner"></span>
190
				</div>
191
			<?php }
192
		}
193
194
		/**
195
		 * AJAX endpoint for clearing all gallery caches
196
		 */
197
		function ajax_clear_all_caches() {
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...
198
			if ( check_admin_referer( 'foogallery_clear_html_cache' ) ) {
199
200
				$this->clear_all_gallery_caches();
201
202
				_e('The cache for all galleries has been cleared!', 'foogallery' );
203
				die();
204
			}
205
		}
206
207
		/**
208
		 * Clears all caches for all galleries
209
		 */
210
		function clear_all_gallery_caches() {
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...
211
			delete_post_meta_by_key( FOOGALLERY_META_CACHE );
212
		}
213
214
		/**
215
		 * Clear all caches when the plugin has been updated. This is to account for changes in the HTML when a new version is released.
216
		 */
217
		function clear_cache_on_update() {
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...
218
			$this->clear_all_gallery_caches();
219
		}
220
	}
221
}