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

add_extension_pickup_locations()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 32
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 2
dl 0
loc 32
rs 8.8571
c 0
b 0
f 0
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
		} else {
36
37
			//check if the gallery is password protected
38
			if ( post_password_required( $current_foogallery->_post ) ) {
39
				echo get_the_password_form( $current_foogallery->_post );
40
				return;
41
			}
42
43
			//find the gallery template we will use to render the gallery
44
			$current_foogallery_template = $this->get_arg( $args, 'template', $current_foogallery->gallery_template );
45
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
			//check if we have any attachments
52
			if ( ! $current_foogallery->has_attachments() ) {
53
				//no attachments!
54
				do_action( "foogallery_template_no_attachments-($current_foogallery_template)", $current_foogallery );
55
			} else {
56
57
				//create locator instance
58
                $loader = $this->create_locator_instance();
59
60
				if ( false !== ( $template_location = $loader->locate_file( "gallery-{$current_foogallery_template}.php" ) ) ) {
61
62
					//we have found a template!
63
					do_action( 'foogallery_located_template', $current_foogallery );
64
					do_action( "foogallery_located_template-{$current_foogallery_template}", $current_foogallery );
65
66
					//try to include some JS, but allow template to opt-out based on some condition
67
					if ( false !== apply_filters( "foogallery_template_load_js-{$current_foogallery_template}", true, $current_foogallery ) ) {
68
						if ( false !== ( $js_location = $loader->locate_file( "gallery-{$current_foogallery_template}.js" ) ) ) {
69
							$js_deps = apply_filters( "foogallery_template_js_deps-{$current_foogallery_template}", array(), $current_foogallery );
70
							$js_ver = apply_filters( "foogallery_template_js_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery );
71
							wp_enqueue_script( "foogallery-template-{$current_foogallery_template}", $js_location['url'], $js_deps, $js_ver );
72
							do_action( 'foogallery_template_enqueue_script', $current_foogallery_template, $js_location['url'] );
73
						}
74
					}
75
76
					//try to include some CSS, but allow template to opt-out based on some condition
77
					if ( false !== apply_filters( "foogallery_template_load_css-{$current_foogallery_template}", true, $current_foogallery ) ) {
78
						if ( false !== ( $css_location = $loader->locate_file( "gallery-{$current_foogallery_template}.css" ) ) ) {
79
							$css_deps = apply_filters( "foogallery_template_css_deps-{$current_foogallery_template}", array(), $current_foogallery );
80
							$css_ver = apply_filters( "foogallery_template_css_ver-{$current_foogallery_template}", FOOGALLERY_VERSION, $current_foogallery );
81
							foogallery_enqueue_style( "foogallery-template-{$current_foogallery_template}", $css_location['url'], $css_deps, $css_ver );
82
						}
83
					}
84
85
					//finally include the actual php template!
86
					if ( $template_location ) {
87
						$this->load_gallery_template( $current_foogallery, $template_location['path'] );
0 ignored issues
show
Bug introduced by
It seems like $current_foogallery defined by $this->find_gallery($args) on line 30 can also be of type boolean; however, FooGallery_Template_Load...load_gallery_template() does only seem to accept object<FooGallery>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88
					}
89
90
					//cater for lightbox extensions needing to add styles and javascript
91
					$lightbox = foogallery_gallery_template_setting( 'lightbox' );
92
					if ( !empty( $lightbox ) ) {
93
						do_action( "foogallery_template_lightbox-{$lightbox}", $current_foogallery );
94
					}
95
96
					//we have loaded all files, now let extensions do some stuff
97
					do_action( "foogallery_loaded_template", $current_foogallery );
98
					do_action( "foogallery_loaded_template-($current_foogallery_template)", $current_foogallery );
99
				} else {
100
					//we could not find a template!
101
					_e( 'No gallery template found!', 'foogallery' );
102
				}
103
			}
104
		}
105
	}
106
107
	/***
108
	 * Loads a gallery template location and wraps the calls so that it can be intercepted
109
	 *
110
	 * @param FooGallery $gallery
111
	 * @param string $template_location
112
	 */
113
	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...
114
115
		$override_load_template = apply_filters( 'foogallery_load_gallery_template', false, $gallery, $template_location );
116
117
		if ( $override_load_template ) {
118
			//if we have overridden the loading of the template, then we can exit without doing anything further
119
			return;
120
		}
121
122
		//if we get to this point, then we need to load the template as per normal
123
		load_template( $template_location, false );
124
	}
125
126
    /**
127
     * Creates a locator instance used for including template files
128
     *
129
     *
130
     */
131
    public function create_locator_instance() {
132
        $instance_name = FOOGALLERY_SLUG . '_gallery_templates';
133
        $loader        = new Foo_Plugin_File_Locator_v1( $instance_name, FOOGALLERY_FILE, 'templates', FOOGALLERY_SLUG );
134
135
        //allow extensions to very easily add pickup locations for their files
136
        $this->add_extension_pickup_locations( $loader, apply_filters( $instance_name . '_files', array() ) );
137
138
        return $loader;
139
    }
140
141
	/**
142
	 * Add pickup locations to the loader to make it easier for extensions
143
	 *
144
	 * @param $loader Foo_Plugin_File_Locator_v1
145
	 * @param $extension_files array
146
	 */
147
	function add_extension_pickup_locations( $loader, $extension_files ) {
148
		if ( count( $extension_files ) > 0 ) {
149
			$position = 120;
150
			foreach ( $extension_files as $file ) {
151
152
				//add pickup location for php template
153
				$loader->add_location( $position, array(
154
					'path' => trailingslashit( plugin_dir_path( $file ) ),
155
					'url'  => trailingslashit( plugin_dir_url( $file ) )
156
				) );
157
158
				$position++;
159
160
				//add pickup location for extensions js folder
161
				$loader->add_location( $position, array(
162
					'path' => trailingslashit( plugin_dir_path( $file ) . 'js' ),
163
					'url'  => trailingslashit( plugin_dir_url( $file ) . 'js' )
164
				) );
165
166
				$position++;
167
168
				//add pickup location for extension css folder
169
				$loader->add_location( $position, array(
170
					'path' => trailingslashit( plugin_dir_path( $file ) . 'css' ),
171
					'url'  => trailingslashit( plugin_dir_url( $file ) . 'css' )
172
				) );
173
174
				$position++;
175
176
			}
177
		}
178
	}
179
180
	/**
181
	 * load the gallery based on either the id or slug, passed in via arguments
182
	 *
183
	 * @param $args array       Arguments passed in from the shortcode
184
	 *
185
	 * @return bool|FooGallery  The gallery object we want to render
186
	 */
187
	function find_gallery( $args ) {
188
189
		$id = intval( $this->get_arg( $args, 'id' ), 0 );
190
191
		if ( $id > 0 ) {
192
193
			//load gallery by ID
194
			return FooGallery::get_by_id( $id );
195
196
		} else {
197
198
			//take into account the cases where id is passed in via the 'gallery' attribute
199
			$gallery = $this->get_arg( $args, 'gallery', 0 );
200
201
			if ( intval( $gallery ) > 0 ) {
202
				//we have an id, so load
203
				return FooGallery::get_by_id( intval( $gallery ) );
204
			}
205
206
			//we are dealing with a slug
207
			return FooGallery::get_by_slug( $gallery );
208
		}
209
	}
210
211
	/**
212
	 * Helper to get an argument value from an array arguments
213
	 *
214
	 * @param $args    Array    the array of arguments to search
215
	 * @param $key     string   the key of the argument you are looking for
216
	 * @param $default string   a default value if the argument is not found
217
	 *
218
	 * @return string
219
	 */
220
	function get_arg( $args, $key, $default = '' ) {
221
		if ( empty($args) || ! array_key_exists( $key, $args ) ) {
222
			return $default;
223
		}
224
225
		return $args[ $key ];
226
	}
227
}
228