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 ( fc9531...c44ae7 )
by Brad
02:36
created

FooGallery_Extensions_API   D

Complexity

Total Complexity 100

Size/Duplication

Total Lines 724
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2
Metric Value
wmc 100
lcom 1
cbo 2
dl 0
loc 724
rs 4.4444

32 Methods

Rating   Name   Duplication   Size   Complexity  
A has_extension_loading_errors() 0 3 1
A get_extension_loading_errors_response() 0 3 1
A reset_errors() 0 4 1
A save_slugs_for_new_calculations() 0 11 4
A clear_cached_extensions() 0 3 1
A reload() 0 4 1
A find_wordpress_plugin() 0 13 4
A get_active_extensions() 0 3 1
A get_error_extensions() 0 3 1
A auto_activate_extensions() 0 10 4
A __construct() 0 5 2
A get_extensions_endpoint() 0 8 2
B load_available_extensions() 0 38 6
B default_extenions_in_case_of_emergency() 0 90 1
B determine_new_extensions() 0 14 5
B get_all() 0 27 6
A get_all_slugs() 0 6 1
B get_all_categories() 0 28 5
A get_extension() 0 8 3
A get_extension_by_file() 0 10 3
A is_active() 0 8 2
B is_downloaded() 0 17 5
A has_errors() 0 8 2
A handle_wordpress_plugin_deactivation() 0 7 2
A handle_wordpress_plugin_activation() 0 7 2
D deactivate() 0 43 9
B activate() 0 56 8
C download() 0 66 8
A get_error_message() 0 7 2
A add_to_activated_extensions() 0 8 2
A add_to_error_extensions() 0 10 3
A remove_from_error_extensions() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like FooGallery_Extensions_API often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use FooGallery_Extensions_API, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * @TODO
4
 */
5
6
if ( ! class_exists( 'FooGallery_Extensions_API' ) ) {
7
8
	define( 'FOOGALLERY_EXTENSIONS_ENDPOINT', 'https://raw.githubusercontent.com/fooplugins/foogallery-extensions/master/extensions.json' );
9
	define( 'FOOGALLERY_EXTENSIONS_FUTURE_ENDPOINT', 'https://raw.githubusercontent.com/fooplugins/foogallery-extensions/future/extensions.json' );
10
	//define( 'FOOGALLERY_EXTENSIONS_ENDPOINT', FOOGALLERY_URL . 'extensions/extensions.json.js' );
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% 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...
11
	define( 'FOOGALLERY_EXTENSIONS_LOADING_ERRORS', 'foogallery_extensions_loading_errors' );
12
	define( 'FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE', 'foogallery_extensions_loading_errors_response' );
13
	define( 'FOOGALLERY_EXTENSIONS_AVAILABLE_TRANSIENT_KEY', 'foogallery_extensions_available' );
14
	define( 'FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY', 'foogallery_extensions_message' );
15
	define( 'FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY', 'foogallery_extensions_activated' );
16
	define( 'FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY', 'foogallery_extensions_errors' );
17
	define( 'FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY', 'foogallery_extensions_slugs' );
18
	define( 'FOOGALLERY_EXTENSIONS_AUTO_ACTIVATED_OPTIONS_KEY', 'foogallery_extensions_auto_activated' );
19
20
	/**
21
	 * Foogalolery Extensions Manager Class
22
	 * Class FooGallery_Extensions_API
23
	 */
24
	class FooGallery_Extensions_API {
25
26
		/**
27
		 * Internal list of all extensions
28
		 * @var array
29
		 */
30
		private $extensions = false;
31
32
		/**
33
		 * Internal list of all extension slugs
34
		 * @var array
35
		 */
36
		private $extension_slugs = false;
37
38
		/**
39
		 * Extension API constructor
40
		 * @param bool $load
41
		 */
42
		function __construct( $load = false ) {
43
			if ( $load ) {
44
				$this->load_available_extensions();
45
			}
46
		}
47
48
		/**
49
		 * Returns true if there were errors loading extensions
50
		 * @return bool
51
		 */
52
		public function has_extension_loading_errors() {
53
			return get_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS );
54
		}
55
56
		/**
57
		 * Returns the actual reposnse when there were errors trying to fetch all extensions
58
		 * @return mixed
59
		 */
60
		public function get_extension_loading_errors_response() {
61
			return get_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
62
		}
63
64
		/**
65
		 * Get back the extension endpoint based on a setting
66
		 */
67
		public function get_extensions_endpoint() {
68
			if ( 'on' === foogallery_get_setting( 'use_future_endpoint' ) ) {
69
				$extension_url = FOOGALLERY_EXTENSIONS_FUTURE_ENDPOINT;
70
			} else {
71
				$extension_url = FOOGALLERY_EXTENSIONS_ENDPOINT;
72
			}
73
			return apply_filters('foogallery_extension_api_endpoint', $extension_url );
74
		}
75
76
		/**
77
		 * Reset all previous errors
78
		 */
79
		public function reset_errors() {
80
			delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS );
81
			delete_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
82
		}
83
84
		/**
85
		 * Load all available extensions from the public endpoint and store in a transient for later use
86
		 */
87
		private function load_available_extensions() {
88
			if ( false === ( $this->extensions = get_transient( FOOGALLERY_EXTENSIONS_AVAILABLE_TRANSIENT_KEY ) ) ) {
89
90
				//clear any previous state
91
				$this->reset_errors();
92
				$this->extensions = null;
93
				$expires = 60 * 60 * 24; //1 day
94
95
				$extension_url = $this->get_extensions_endpoint();
96
97
				//fetch the data from our public list of extensions hosted on github
98
				$response = wp_remote_get( $extension_url, array( 'sslverify' => false ) );
99
100
				if( ! is_wp_error( $response ) ) {
101
102
					if ( $response['response']['code'] == 200 ) {
103
						$this->extensions = @json_decode( $response['body'], true );
104
105
						//if we got a valid list of extensions then calculate which are new and cache the result
106
						if ( is_array( $this->extensions ) ) {
107
							$this->determine_new_extensions( );
108
							$this->save_slugs_for_new_calculations();
109
						}
110
					}
111
				}
112
113
				if ( ! is_array( $this->extensions ) ) {
114
					//there was some problem getting a list of extensions. Could be a network error, or the extension json was malformed
115
					update_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS, true );
116
					update_option( FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE, $response );
117
					$this->extensions = $this->default_extenions_in_case_of_emergency();
118
					$expires = 5 * 60; //Only cache for 5 minutes if there are errors.
119
				}
120
121
				//Cache the result
122
				set_transient( FOOGALLERY_EXTENSIONS_AVAILABLE_TRANSIENT_KEY, $this->extensions, $expires );
123
			}
124
		}
125
126
		/**
127
		 * Get an array of default extensions.
128
		 * If for some reason, the extension list cannot be fetched from our public listing, we need to return the defaults so that the plugin can function offline
129
		 *
130
		 * @return array
131
		 */
132
		private function default_extenions_in_case_of_emergency() {
133
			$extensions = array();
134
135
			//Our default gallery templates
136
			$extensions[] = array(
137
				'slug'        => 'default_templates',
138
				'class'       => 'FooGallery_Default_Templates_Extension',
139
				'categories'  => array( 'Featured', 'Free', ),
140
				'title'       => 'Default Templates',
141
				'description' => 'The bundled gallery templates.',
142
				'author'      => 'FooPlugins',
143
				'author_url'  => 'http://fooplugins.com',
144
				'thumbnail'   => '/assets/extension_bg.png',
145
				'tags'        => array( 'template', ),
146
				'source'      => 'bundled',
147
				'activated_by_default' => true,
148
			);
149
150
			$extensions[] =	array(
151
				'slug' => 'albums',
152
				'class' => 'FooGallery_Albums_Extension',
153
				'title' => 'Albums',
154
				'categories' =>	array( 'Featured', 'Free' ),
155
				'description' => 'Group your galleries into albums. Boom!',
156
				'html' => 'Group your galleries into albums. Boom!',
157
				'author' => 'FooPlugins',
158
				'author_url' => 'http://fooplugins.com',
159
				'thumbnail' => '/extensions/albums/foogallery-albums.png',
160
				'tags' => array( 'functionality' ),
161
				'source' => 'bundled'
162
			);
163
164
			//FooBox lightbox
165
			$extensions[] = array (
166
				'slug' => 'foobox-image-lightbox',
167
				'class' => 'FooGallery_FooBox_Free_Extension',
168
				'categories' => array( 'Featured', 'Free', ),
169
				'file' => 'foobox-free.php',
170
				'title' => 'FooBox FREE',
171
				'description' => 'The best lightbox for WordPress. Free',
172
				'author' => 'FooPlugins',
173
				'author_url' => 'http://fooplugins.com',
174
				'thumbnail' => '/assets/extension_bg.png',
175
				'tags' => array( 'lightbox' ),
176
				'source' => 'repo',
177
				'activated_by_default' => true,
178
				'minimum_version' => '1.0.2.1',
179
			);
180
181
			//FooBox premium
182
			$extensions[] = array(
183
				'slug' => 'foobox',
184
				'class' => 'FooGallery_FooBox_Extension',
185
				'categories' => array( 'Featured', 'Premium' ),
186
				'file' => 'foobox.php',
187
				'title' => 'FooBox PRO',
188
				'description' => 'The best lightbox for WordPress just got even better!',
189
				'price' => '$27',
190
				'author' => 'FooPlugins',
191
				'author_url' => 'http://fooplugins.com',
192
				'thumbnail' => '/assets/extension_bg.png',
193
				'tags' => array( 'premium', 'lightbox', ),
194
				'source' => 'fooplugins',
195
				'download_button' =>
196
					array(
197
						'text' => 'Buy - $27',
198
						'target' => '_blank',
199
						'href' => 'http://fooplugins.com/plugins/foobox',
200
						'confirm' => false,
201
					),
202
				'activated_by_default' => true,
203
				'minimum_version' => '2.3.2',
204
			);
205
206
			//The NextGen importer
207
			$extensions[] = array(
208
				'slug' => 'nextgen',
209
				'class' => 'FooGallery_Nextgen_Gallery_Importer_Extension',
210
				'categories' => array( 'Free' ),
211
				'title' => 'NextGen Importer',
212
				'description' => 'Imports all your existing NextGen galleries',
213
				'author' => 'FooPlugins',
214
				'author_url' => 'http://fooplugins.com',
215
				'thumbnail' => '/assets/extension_bg.png',
216
				'tags' => array( 'tools' ),
217
				'source' => 'bundled',
218
			);
219
220
			return $extensions;
221
		}
222
223
		/**
224
		 * @TODO
225
		 */
226
		private function determine_new_extensions() {
227
			$previous_slugs = get_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
228
			if ( $previous_slugs ) {
229
				//only do something if we have a previously saved array
230
				foreach ( $this->extensions as &$extension ) {
231
					if ( ! in_array( $extension['slug'], $previous_slugs ) ) {
232
						if ( ! isset( $extension['tags'] ) ) {
233
							$extension['tags'] = array();
234
						}
235
						array_unshift( $extension['tags'] , __( 'new', 'foogallery' ) );
236
					}
237
				}
238
			}
239
		}
240
241
		/**
242
		 * @TODO
243
		 */
244
		private function save_slugs_for_new_calculations() {
245
			if ( is_array( $this->extensions ) ) {
246
				$slugs = array();
247
				foreach ( $this->extensions as $extension ) {
248
					$slugs[] = $extension['slug'];
249
				}
250
				if ( count( $slugs ) > 0 ) {
251
					update_option( FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY, $slugs );
252
				}
253
			}
254
		}
255
256
		/**
257
		 * Clears the cached list of extensions
258
		 */
259
		public function clear_cached_extensions() {
260
			delete_transient( FOOGALLERY_EXTENSIONS_AVAILABLE_TRANSIENT_KEY );
261
		}
262
263
		/**
264
		 * Reload the extensions from the public endpoint
265
		 */
266
		public function reload() {
267
			$this->clear_cached_extensions();
268
			$this->load_available_extensions();
269
		}
270
271
		/**
272
		 * Get all loaded extensions
273
		 * @return array
274
		 */
275
		function get_all() {
276
277
			//check if we need to load
278
			if ( false === $this->extensions ) {
279
				$this->load_available_extensions();
280
			}
281
282
			//get any extra extensions from plugins
283
			$extra_extensions = apply_filters( 'foogallery_available_extensions', array() );
284
285
			if ( count( $extra_extensions ) > 0 ) {
286
				//get a list of slugs so we can determine duplicates!
287
				$this->extension_slugs = array();
288
				foreach ( $this->extensions as $extension ) {
289
					$this->extension_slugs[] = $extension['slug'];
290
				}
291
292
				//only add if not a duplicate
293
				foreach ( $extra_extensions as $extension ) {
294
					if ( ! in_array( $extension['slug'], $this->extension_slugs ) ) {
295
						$this->extensions[] = $extension;
296
					}
297
				}
298
			}
299
300
			return $this->extensions;
301
		}
302
303
		/**
304
		 * Get all loaded extensions slugs
305
		 * @return array
306
		 */
307
		function get_all_slugs() {
308
			//load all extensions first!
309
			$this->get_all();
310
311
			return $this->extension_slugs;
312
		}
313
314
		/**
315
		 * Returns a distinct array of categories that are used in the extensions
316
		 * @return mixed
317
		 */
318
		function get_all_categories() {
319
			$categories['all'] = array(
320
				'name' => __( 'All', 'foogallery' ),
321
			);
322
			$categories['activated'] = array(
323
				'name' => __( 'Active', 'foogallery' ),
324
			);
325
			$active = 0;
326
			foreach ( $this->get_all() as $extension ) {
327
				if ( $this->is_active( $extension['slug'] ) ) {
328
					$active++;
329
				}
330
				$category_names = $extension['categories'];
331
				foreach ( $category_names as $category_name ) {
332
					$category_slug = foo_convert_to_key( $category_name );
333
334
					if ( ! array_key_exists( $category_slug, $categories ) ) {
335
						$categories[ $category_slug ] = array(
336
							'name'  => $category_name,
337
						);
338
					}
339
				}
340
			}
341
			$categories['build_your_own'] = array(
342
				'name' => __( 'Build Your Own', 'foogallery' )
343
			);
344
			return apply_filters( 'foogallery_extension_categories', $categories );
345
		}
346
347
		/**
348
		 * @TODO
349
		 * @param $slug
350
		 *
351
		 * @return bool
352
		 */
353
		public function get_extension( $slug ) {
354
			foreach ( $this->get_all() as $extension ) {
355
				if ( $extension['slug'] === $slug ) {
356
					return $extension;
357
				}
358
			}
359
			return false;
360
		}
361
362
		/**
363
		 * @TODO
364
		 * @param $file
365
		 *
366
		 * @return bool
367
		 */
368
		public function get_extension_by_file( $file ) {
369
			$file = basename( $file ); //normalize to just the filename
370
371
			foreach ( $this->get_all() as $extension ) {
372
				if ( foo_safe_get( $extension, 'file' ) === $file ) {
373
					return $extension;
374
				}
375
			}
376
			return false;
377
		}
378
379
		/**
380
		 * @TODO
381
		 * @param      $slug
382
		 *
383
		 * @return bool
384
		 */
385
		public function is_active( $slug ) {
386
			$active_extensions = $this->get_active_extensions();
387
388
			if ( $active_extensions ) {
389
				return array_key_exists( $slug, $active_extensions );
390
			}
391
			return false;
392
		}
393
394
		/**
395
		 * @TODO
396
		 *
397
		 * @param bool $extension
398
		 *
399
		 * @param bool $slug
400
		 *
401
		 * @return bool
402
		 */
403
		public function is_downloaded( $extension = false, $slug = false ) {
404
			//allow you to pass in a slug rather
405
			if ( ! $extension && $slug !== false ) {
406
				$extension = $this->get_extension( $slug );
407
			}
408
			if ( $extension ) {
409
				//first check if the class exists
410
				if ( class_exists( $extension['class'] ) ) {
411
					return true;
412
				}
413
414
				//next fallback to see if a plugin exists that has the same file name
415
				$plugin = $this->find_wordpress_plugin( $extension );
416
				return false !== $plugin;
417
			}
418
			return false;
419
		}
420
421
		/**
422
		 * @TODO
423
		 * @param $slug
424
		 *
425
		 * @return bool
426
		 */
427
		public function has_errors( $slug ) {
428
			$error_extensions = $this->get_error_extensions();
429
430
			if ( $error_extensions ) {
431
				return array_key_exists( $slug, $error_extensions );
432
			}
433
			return false;
434
		}
435
436
		/**
437
		 * @TODO
438
		 * @param $plugin
439
		 */
440
		public function handle_wordpress_plugin_deactivation( $plugin ) {
441
			$extension = $this->get_extension_by_file( $plugin );
442
			if ( $extension ) {
443
				//we have found a matching extension
444
				$this->deactivate( $extension['slug'], false );
445
			}
446
		}
447
448
		/**
449
		 * @TODO
450
		 * @param $plugin
451
		 */
452
		public function handle_wordpress_plugin_activation( $plugin ) {
453
			$extension = $this->get_extension_by_file( $plugin );
454
			if ( $extension ) {
455
				//we have found a matching extension
456
				$this->activate( $extension['slug'], false );
457
			}
458
		}
459
460
		/**
461
		 * @TODO
462
		 * @param      $slug
463
		 * @param bool $deactivate_wordpress_plugin
464
		 * @param bool $error_loading
465
		 *
466
		 * @return array|mixed|void
467
		 */
468
		public function deactivate( $slug, $deactivate_wordpress_plugin = true, $error_loading = false ) {
469
			$extension = $this->get_extension( $slug );
470
			if ( $extension ) {
471
				if ( $deactivate_wordpress_plugin && 'bundled' === foo_safe_get( $extension, 'source', false ) ) {
472
					$plugin = $this->find_wordpress_plugin( $extension );
473
					if ( $plugin ) {
474
						$failure = deactivate_plugins( $plugin['file'], true, false );
475
						if ( null !== $failure ) {
476
							return array(
477
								'message' => sprintf( __( 'The extension %s could NOT be deactivated!', 'foogallery' ), "<strong>{$extension['title']}</strong>" ),
478
								'type' => 'error'
479
							);
480
						}
481
					}
482
				}
483
484
				$active_extensions = $this->get_active_extensions();
485
				if ( array_key_exists( $slug, $active_extensions ) ) {
486
					unset( $active_extensions[ $slug ] );
487
					if ( empty($active_extensions) ) {
488
						delete_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY );
489
					} else {
490
						update_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY, $active_extensions );
491
					}
492
				}
493
494
				if ( $error_loading ) {
495
					$this->add_to_error_extensions( $slug );
496
				}
497
498
				//we are done, allow for extensions to do something after an extension is activated
499
				do_action( 'foogallery_extension_deactivated-' . $slug );
500
501
				return apply_filters( 'foogallery_extensions_deactivated_message-' . $slug, array(
502
					'message' => sprintf( __( 'The extension %s was successfully deactivated', 'foogallery' ), "<strong>{$extension['title']}</strong>" ),
503
					'type' => 'success',
504
				) );
505
			}
506
			return array(
507
				'message' => sprintf( __( 'Unknown extension : %s', 'foogallery' ), $slug ),
508
				'type' => 'error',
509
			);
510
		}
511
512
		/**
513
		 * @TODO
514
		 *
515
		 * @param      $slug
516
		 * @param bool $activate_wordpress_plugin
517
		 *
518
		 * @return array|mixed|void
519
		 */
520
		public function activate( $slug, $activate_wordpress_plugin = true ) {
521
			$extension = $this->get_extension( $slug );
522
			if ( $extension ) {
523
				//first remove it from our error list (if it was there before)
524
				$this->remove_from_error_extensions( $slug );
525
526
				if ( $activate_wordpress_plugin && 'bundled' !== foo_safe_get( $extension, 'source', false ) ) {
527
					//activate the plugin, WordPress style!
528
					$plugin = $this->find_wordpress_plugin( $extension );
529
530
					if ( $plugin ) {
531
532
						//check min version
533
						$minimum_version = foo_safe_get( $extension, 'minimum_version' );
534
						if ( !empty($minimum_version) ) {
535
							$actual_version = $plugin['plugin']['Version'];
536
							if ( version_compare( $actual_version, $minimum_version ) < 0 ) {
537
								$this->add_to_error_extensions( $slug, sprintf( __( 'Requires %s version %s','foogallery' ), $extension['title'], $minimum_version ) );
538
								return array(
539
									'message' => sprintf( __( 'The extension %s could not be activated, because you are using an outdated version! Please update %s to at least version %s.', 'foogallery' ), $extension['title'], $extension['title'], $minimum_version ),
540
									'type' => 'error',
541
								);
542
							}
543
						}
544
545
						//try to activate the plugin
546
						$failure = activate_plugin( $plugin['file'], '', false, false );
547
						if ( null !== $failure ) {
548
							return array(
549
								'message' => sprintf( __( 'The extension %s could NOT be activated!', 'foogallery' ), "<strong>{$extension['title']}</strong>" ),
550
								'type' => 'error',
551
							);
552
						}
553
					}
554
				}
555
				//load an instance of the extension class into memory
556
				$loader = new FooGallery_Extensions_Loader();
557
				$loader->load_extension( $slug, foo_safe_get( $extension, 'class', false ) );
558
559
				//then add the extension to our saved option so that it can be loaded on startup
560
				$this->add_to_activated_extensions( $extension );
561
562
				//we are done, allow for extensions to do something after an extension is activated
563
				do_action( 'foogallery_extension_activated-' . $slug );
564
565
				//return our result
566
				return apply_filters( 'foogallery_extension_activated_message-' . $slug, array(
567
					'message' => sprintf( __( 'The extension %s was successfully activated', 'foogallery' ), "<strong>{$extension['title']}</strong>" ),
568
					'type' => 'success',
569
				) );
570
			}
571
			return array(
572
				'message' => sprintf( __( 'Unknown extension : %s', 'foogallery' ), $slug ),
573
				'type' => 'error',
574
			);
575
		}
576
577
		/**
578
		 * @TODO
579
		 * @param boolean $extension
580
		 *
581
		 * @return array|bool
582
		 */
583
		private function find_wordpress_plugin( $extension ) {
584
			$plugins = get_plugins();
585
			foreach ( $plugins as $plugin_file => $plugin ) {
586
				if ( isset($extension['file']) && foo_ends_with( $plugin_file, $extension['file'] ) ) {
587
					return array(
588
						'file' => $plugin_file,
589
						'plugin' => $plugin,
590
						'active' => is_plugin_active( $plugin_file ),
591
					);
592
				}
593
			}
594
			return false;
595
		}
596
597
		/**
598
		 * @TODO
599
		 * @param $slug
600
		 *
601
		 * @return array|mixed|void
602
		 */
603
		public function download( $slug ) {
604
			$extension = $this->get_extension( $slug );
605
			if ( $extension ) {
606
607
				//we need some files!
608
				require_once ABSPATH . 'wp-admin/includes/plugin-install.php'; // plugins_api calls
609
				require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; // Plugin_Upgrader class
610
				require_once FOOGALLERY_PATH . 'includes/admin/class-silent-installer-skin.php'; //our silent installer skin
611
612
				$download_link = isset( $extension['download_link'] ) ? $extension['download_link'] : false;
613
614
				if ( 'repo' === $extension['source'] ) {
615
					$plugins_api = plugins_api( 'plugin_information', array( 'slug' => $slug, 'fields' => array( 'sections' => false ) ) );
616
617
					if ( is_wp_error( $plugins_api ) ) {
618
						return array(
619
							'message' => sprintf( __( 'Unable to connect to the WordPress.org plugin API to download %s. Full error log: %s', 'foogallery' ), $slug,  '<br />' . var_export( $plugins_api, true ) ),
620
							'type' => 'error',
621
						);
622
					}
623
624
					//get the download link from the API call
625
					if ( isset( $plugins_api->download_link ) ) {
626
						$download_link = $plugins_api->download_link;
627
					}
628
				}
629
630
				//check we have something to download
631
				if ( empty( $download_link ) ) {
632
					return array(
633
						'message' => sprintf( __( 'The extension %s has no download link!', 'foogallery' ), $slug ),
634
						'type' => 'error',
635
					);
636
				}
637
638
				$skin = new FooGallery_Silent_Installer_Skin();
639
640
				//instantiate Plugin_Upgrader
641
				$upgrader = new Plugin_Upgrader( $skin );
642
643
				$upgrader->install( $download_link );
644
645
				if ( 'process_failed' === $skin->feedback ) {
646
					//we had an error along the way
647
					return apply_filters( 'foogallery_extensions_download_failure-' . $slug, array(
648
						'message' => sprintf( __( 'The extension %s could NOT be downloaded!', 'foogallery' ), "<strong>{$extension['title']}</strong>" ),
649
						'type' => 'error'
650
					) );
651
				}
652
653
				//return our result
654
				return apply_filters( 'foogallery_extensions_download_success-' . $slug, array(
655
					'message' => sprintf( __( 'The extension %s was successfully downloaded and can now be activated. %s', 'foogallery' ),
656
						"<strong>{$extension['title']}</strong>",
657
						'<a href="' . esc_url( add_query_arg( array(
658
								'action' => 'activate',
659
								'extension' => $slug, ) ) ) . '">' . __( 'Activate immediately', 'foogallery' ) . '</a>'
660
					),
661
					'type' => 'success',
662
				) );
663
			}
664
			return array(
665
				'message' => sprintf( __( 'Unknown extension : %s', 'foogallery' ), $slug ),
666
				'type' => 'error',
667
			);
668
		}
669
670
		/**
671
		 * @TODO
672
		 * @return mixed|void
673
		 */
674
		public function get_active_extensions() {
675
			return get_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY, array() );
676
		}
677
678
		/**
679
		 * @TODO
680
		 * @return mixed|void
681
		 */
682
		public function get_error_extensions() {
683
			return get_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY, array() );
684
		}
685
686
		public function get_error_message( $slug ) {
687
			$error_extensions = $this->get_error_extensions();
688
			if ( array_key_exists( $slug, $error_extensions ) ) {
689
				return $error_extensions[ $slug ];
690
			}
691
			return '';
692
		}
693
694
		/**
695
		 * @TODO
696
		 * @param $extension
697
		 */
698
		private function add_to_activated_extensions( $extension ) {
699
			$slug = $extension['slug'];
700
			$active_extensions = $this->get_active_extensions();
701
			if ( !array_key_exists( $slug, $active_extensions ) ) {
702
				$active_extensions[ $slug ] = $extension['class'];
703
				update_option( FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY, $active_extensions );
704
			}
705
		}
706
707
		/**
708
		 * @TODO
709
		 * @param $slug
710
		 */
711
		public function add_to_error_extensions( $slug, $error_message = '' ) {
712
			$error_extensions = $this->get_error_extensions();
713
			if ( ! array_key_exists( $slug, $error_extensions ) ) {
714
				if ( empty($error_message) ) {
715
					$error_message = __( 'Error loading extension!', 'foogallery' );
716
				}
717
				$error_extensions[$slug] = $error_message;
718
				update_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY, $error_extensions );
719
			}
720
		}
721
722
		/**
723
		 * @TODO
724
		 * @param $slug
725
		 */
726
		private function remove_from_error_extensions( $slug ) {
727
			$error_extensions = $this->get_error_extensions();
728
			if ( array_key_exists( $slug, $error_extensions ) ) {
729
				unset( $error_extensions[ $slug ] );
730
				update_option( FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY, $error_extensions );
731
			}
732
		}
733
734
		/**
735
		 * @TODO
736
		 */
737
		public function auto_activate_extensions() {
738
			foreach ( $this->get_all() as $extension ) {
739
				if ( true === foo_safe_get( $extension, 'activated_by_default' ) ) {
740
					//check to see if the extension is downloaded
741
					if ( $this->is_downloaded( $extension ) ) {
742
						$this->add_to_activated_extensions( $extension );
743
					}
744
				}
745
			}
746
		}
747
	}
748
}
749