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 — develop ( 3bd383...ce1d31 )
by Brad
02:29
created

FooGallery_Cache::fetch_gallery_html_from_cache()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 24
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 13
nc 4
nop 3
dl 0
loc 24
rs 8.6845
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
25
26
			add_filter( 'foogallery_load_gallery_template', array( $this, 'fetch_gallery_html_from_cache' ), 10, 3 );
27
		}
28
29
		/**
30
		 * Save the HTML output of the gallery after the gallery has been saved
31
		 *
32
		 * @param $post_id
33
		 * @param $form_post
34
		 */
35
		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...
36
			$this->cache_gallery_html_output( $post_id );
37
		}
38
39
		/**
40
		 * Save the HTML output of the gallery to post meta so that it can be used in future requests
41
		 *
42
		 * @param $foogallery_id
43
		 */
44
		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...
45
			//check if caching is disabled and quit early
46
			if ( 'on' === foogallery_get_setting( 'disable_html_cache' ) ) {
47
				return;
48
			}
49
50
			//we need a way to force the gallery to render it's output every time it is saved
51
			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...
52
			$foogallery_force_gallery_cache = true;
53
54
			//capture the html output
55
			ob_start();
56
			foogallery_render_gallery( $foogallery_id );
57
			$gallery_html = ob_get_contents();
58
			ob_end_clean();
59
60
			//save the output to post meta for later use
61
			update_post_meta( $foogallery_id, FOOGALLERY_META_CACHE, $gallery_html );
62
63
			$foogallery_force_gallery_cache = false;
64
		}
65
66
		/**
67
		 * Override the template output
68
		 *
69
		 * @param $override
70
		 * @param $gallery
71
		 * @param $template_location
72
		 *
73
		 * @return bool
74
		 */
75
		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...
76
			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...
77
			if ( $foogallery_force_gallery_cache ) {
78
				return false;
79
			}
80
81
			//check if caching is disabled and quit early
82
			if ( 'on' === foogallery_get_setting( 'disable_html_cache' ) ) {
83
				return false;
84
			}
85
86
			$gallery_cache = get_post_meta( $gallery->ID, FOOGALLERY_META_CACHE, true );
87
88
			if ( !empty( $gallery_cache ) ) {
89
				//output the cached gallery html
90
				echo $gallery_cache;
91
				return true; //return that we will override
92
			} else {
93
				//we should cache the result for next time
94
				$this->cache_gallery_html_output( $gallery->ID );
95
			}
96
97
			return false;
98
		}
99
100
		/**
101
		 * Add some caching settings
102
		 * @param $settings
103
		 *
104
		 * @return array
105
		 */
106
		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...
107
108
			$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...
109
				'id'      => 'disable_html_cache',
110
				'title'   => __( 'Disable HTML Cache', 'foogallery' ),
111
				'desc'    => __( 'The gallery HTML is cached by default. You can choose to disable the cache for debugging purposes. It is NOT recommended.', 'foogallery' ),
112
				'type'    => 'checkbox',
113
				'tab'     => 'general',
114
				'section' => __( 'Cache', 'foogallery' )
115
			);
116
117
			$cache_settings[] = array(
118
				'id'      => 'clear_html_cache',
119
				'title'   => __( 'Clear HTML Cache', 'foogallery' ),
120
				'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() ),
121
				'type'    => 'clear_gallery_cache_button',
122
				'tab'     => 'general',
123
				'section' => __( 'Cache', 'foogallery' )
124
			);
125
126
			$new_settings = array_merge( $cache_settings, $settings['settings'] );
127
128
			$settings['settings'] = $new_settings;
129
130
			return $settings;
131
		}
132
133
		/**
134
		 * Render any custom setting types to the settings page
135
		 */
136
		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...
137
			if ('clear_gallery_cache_button' === $args['type'] ) { ?>
138
				<div id="foogallery_clear_html_cache_container">
139
					<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' ); ?>">
140
					<span id="foogallery_clear_html_cache_spinner" style="position: absolute" class="spinner"></span>
141
				</div>
142
			<?php }
143
		}
144
145
		/**
146
		 * AJAX endpoint for clearing all gallery caches
147
		 */
148
		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...
149
			if ( check_admin_referer( 'foogallery_clear_html_cache' ) ) {
150
151
				$galleries = foogallery_get_all_galleries();
152
153
				foreach( $galleries as $gallery ) {
154
					delete_post_meta( $gallery->ID, FOOGALLERY_META_CACHE );
155
				}
156
157
				_e('The cache for all galleries has been cleared!', 'foogallery' );
158
				die();
0 ignored issues
show
Coding Style Compatibility introduced by
The method ajax_clear_all_caches() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
159
			}
160
		}
161
	}
162
}