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... ( 5984e4...5bc5a5 )
by Brad
02:42
created

FooGallery_Template_Loader::render_template()   F

Complexity

Conditions 14
Paths 306

Size

Total Lines 96
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 47
nc 306
nop 1
dl 0
loc 96
rs 3.7522
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * Template loader for FooGallery
5
 *
6
 * @package FooGallery
7
 * @author  Brad vincent
8
 */
9
class FooGallery_Template_Loader {
10
11
	/**
12
	 * Locates and renders the gallery based on the template
13
	 * Will look in the following locations
14
	 *  wp-content/themes/{child-theme}/foogallery/gallery-{template}.php
15
	 *    wp-content/themes/{theme}/foogallery/gallery-{template}.php
16
	 *  wp-content/plugins/foogallery/templates/gallery-{template}.php
17
	 *
18
	 * @param $args array       Arguments passed in from the shortcode
19
	 */
20
	public function render_template( $args ) {
21
		//do some work before we locate the template
22
		global $current_foogallery;
23
		global $current_foogallery_arguments;
24
		global $current_foogallery_template;
25
26
		//set the arguments
27
		$current_foogallery_arguments = $args;
28
29
		//load our gallery
30
		$current_foogallery = $this->find_gallery( $args );
31
32
		if ( false === $current_foogallery ) {
33
			//we could not find the gallery!
34
			_e( 'The gallery was not found!', 'foogallery' );
35
			return;
36
		}
37
38
		//check if the gallery is password protected
39
		if ( post_password_required( $current_foogallery->_post ) ) {
40
			echo get_the_password_form( $current_foogallery->_post );
41
			return;
42
		}
43
44
		//find the gallery template we will use to render the gallery
45
		$current_foogallery_template = $this->get_arg( $args, 'template', $current_foogallery->gallery_template );
46
		//set a default if we have no gallery template
47
		if ( empty( $current_foogallery_template ) ) {
48
			$current_foogallery_template = foogallery_get_default( 'gallery_template' );
49
		}
50
51
		//override the template if needed
52
		if ( $current_foogallery->gallery_template !== $current_foogallery_template ) {
53
			$current_foogallery->gallery_template = $current_foogallery_template;
54
		}
55
56
		//potentially override attachment_ids from arguments
57
		$attachment_ids = $this->get_arg( $args, 'attachment_ids', false );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
		if ( $attachment_ids ) {
59
			$current_foogallery->attachment_ids = explode( ',', $attachment_ids );
60
		}
61
62
		//check if we have any attachments
63
		if ( ! $current_foogallery->has_attachments() ) {
64
			//no attachments!
65
			do_action( "foogallery_template_no_attachments-($current_foogallery_template)", $current_foogallery );
66
		} else {
67
68
			//create locator instance
69
			$loader = $this->create_locator_instance();
70
71
			if ( false !== ( $template_location = $loader->locate_file( "gallery-{$current_foogallery_template}.php" ) ) ) {
72
73
				//we have found a template!
74
				do_action( 'foogallery_located_template', $current_foogallery );
75
				do_action( "foogallery_located_template-{$current_foogallery_template}", $current_foogallery );
76
77
				//try to include some JS, but allow template to opt-out based on some condition
78
				if ( false !== apply_filters( "foogallery_template_load_js-{$current_foogallery_template}", true, $current_foogallery ) ) {
79
					if ( false !== ( $js_location = $loader->locate_file( "gallery-{$current_foogallery_template}.js" ) ) ) {
80
						$js_deps = apply_filters( "foogallery_template_js_deps-{$current_foogallery_template}", array(), $current_foogallery );
81
						$js_ver = apply_filters( "foogallery_template_js_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery );
82
						wp_enqueue_script( "foogallery-template-{$current_foogallery_template}", $js_location['url'], $js_deps, $js_ver );
83
						do_action( 'foogallery_template_enqueue_script', $current_foogallery_template, $js_location['url'] );
84
					}
85
				}
86
87
				//try to include some CSS, but allow template to opt-out based on some condition
88
				if ( false !== apply_filters( "foogallery_template_load_css-{$current_foogallery_template}", true, $current_foogallery ) ) {
89
					if ( false !== ( $css_location = $loader->locate_file( "gallery-{$current_foogallery_template}.css" ) ) ) {
90
						$css_deps = apply_filters( "foogallery_template_css_deps-{$current_foogallery_template}", array(), $current_foogallery );
91
						$css_ver = apply_filters( "foogallery_template_css_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery );
92
						foogallery_enqueue_style( "foogallery-template-{$current_foogallery_template}", $css_location['url'], $css_deps, $css_ver );
93
					}
94
				}
95
96
				//finally include the actual php template!
97
				if ( $template_location ) {
98
					$this->load_gallery_template( $current_foogallery, $template_location['path'] );
99
				}
100
101
				//cater for lightbox extensions needing to add styles and javascript
102
				$lightbox = foogallery_gallery_template_setting( 'lightbox' );
103
				if ( !empty( $lightbox ) ) {
104
					do_action( "foogallery_template_lightbox-{$lightbox}", $current_foogallery );
105
				}
106
107
				//we have loaded all files, now let extensions do some stuff
108
				do_action( "foogallery_loaded_template", $current_foogallery );
109
				do_action( "foogallery_loaded_template-($current_foogallery_template)", $current_foogallery );
110
			} else {
111
				//we could not find a template!
112
				_e( 'No gallery template found!', 'foogallery' );
113
			}
114
		}
115
	}
116
117
	/***
118
	 * Loads a gallery template location and wraps the calls so that it can be intercepted
119
	 *
120
	 * @param FooGallery $gallery
121
	 * @param string $template_location
122
	 */
123
	function load_gallery_template($gallery, $template_location) {
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...
124
125
		$override_load_template = apply_filters( 'foogallery_load_gallery_template', false, $gallery, $template_location );
126
127
		if ( $override_load_template ) {
128
			//if we have overridden the loading of the template, then we can exit without doing anything further
129
			return;
130
		}
131
132
		//if we get to this point, then we need to load the template as per normal
133
		load_template( $template_location, false );
134
	}
135
136
    /**
137
     * Creates a locator instance used for including template files
138
     *
139
     *
140
     */
141
    public function create_locator_instance() {
142
        $instance_name = FOOGALLERY_SLUG . '_gallery_templates';
143
        $loader        = new Foo_Plugin_File_Locator_v1( $instance_name, FOOGALLERY_FILE, 'templates', FOOGALLERY_SLUG );
144
145
        //allow extensions to very easily add pickup locations for their files
146
        $this->add_extension_pickup_locations( $loader, apply_filters( $instance_name . '_files', array() ) );
147
148
        return $loader;
149
    }
150
151
	/**
152
	 * Add pickup locations to the loader to make it easier for extensions
153
	 *
154
	 * @param $loader Foo_Plugin_File_Locator_v1
155
	 * @param $extension_files array
156
	 */
157
	function add_extension_pickup_locations( $loader, $extension_files ) {
158
		if ( count( $extension_files ) > 0 ) {
159
			$position = 120;
160
			foreach ( $extension_files as $file ) {
161
162
				//add pickup location for php template
163
				$loader->add_location( $position, array(
164
					'path' => trailingslashit( plugin_dir_path( $file ) ),
165
					'url'  => trailingslashit( plugin_dir_url( $file ) )
166
				) );
167
168
				$position++;
169
170
				//add pickup location for extensions js folder
171
				$loader->add_location( $position, array(
172
					'path' => trailingslashit( plugin_dir_path( $file ) . 'js' ),
173
					'url'  => trailingslashit( plugin_dir_url( $file ) . 'js' )
174
				) );
175
176
				$position++;
177
178
				//add pickup location for extension css folder
179
				$loader->add_location( $position, array(
180
					'path' => trailingslashit( plugin_dir_path( $file ) . 'css' ),
181
					'url'  => trailingslashit( plugin_dir_url( $file ) . 'css' )
182
				) );
183
184
				$position++;
185
186
			}
187
		}
188
	}
189
190
	/**
191
	 * load the gallery based on either the id or slug, passed in via arguments
192
	 *
193
	 * @param $args array       Arguments passed in from the shortcode
194
	 *
195
	 * @return bool|FooGallery  The gallery object we want to render
196
	 */
197
	function find_gallery( $args ) {
198
199
		$id = intval( $this->get_arg( $args, 'id' ), 0 );
200
		$gallery = $this->get_arg( $args, 'gallery', 0 );
201
202
		if ( $id > 0 ) {
203
			//load gallery by ID
204
			return FooGallery::get_by_id( $id );
205
		}
206
207
		//take into account the cases where id is passed in via the 'gallery' attribute
208
		if ( intval( $gallery ) > 0 ) {
209
			//we have an id, so load
210
			return FooGallery::get_by_id( intval( $gallery ) );
211
		} else if ( !empty( $gallery ) ) {
212
			//we are dealing with a slug
213
			return FooGallery::get_by_slug( $gallery );
214
		}
215
216
		//if we get here then we have no id or gallery attribute, so try to build a dynamic gallery
217
218
		//we can only build up a dynamic gallery if attachment_ids are passed in
219
		$attachment_ids = $this->get_arg( $args, 'attachment_ids', false );
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
220
221
		if ( $attachment_ids ) {
222
			$template = $this->get_arg( $args, 'template', foogallery_get_default( 'gallery_template' ) );
223
			return FooGallery::dynamic( $template, explode( ',', $attachment_ids) );
224
		}
225
226
		return false;
227
	}
228
229
	/**
230
	 * Helper to get an argument value from an array arguments
231
	 *
232
	 * @param $args    Array    the array of arguments to search
233
	 * @param $key     string   the key of the argument you are looking for
234
	 * @param $default string   a default value if the argument is not found
235
	 *
236
	 * @return string
237
	 */
238
	function get_arg( $args, $key, $default = '' ) {
239
		if ( empty($args) || ! array_key_exists( $key, $args ) ) {
240
			return $default;
241
		}
242
243
		return $args[ $key ];
244
	}
245
}
246