/var/www/hkosl.com/aga/wp-content/plugins/foogallery/includes/extensions/class-extensions-api.php


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
<?php
/**
 * @TODO
 */

if ( ! class_exists'FooGallery_Extensions_API' ) ) {

    
define'FOOGALLERY_EXTENSIONS_ENDPOINT''https://raw.githubusercontent.com/fooplugins/foogallery-extensions/master/extensions.json' );
    
define'FOOGALLERY_EXTENSIONS_FUTURE_ENDPOINT''https://raw.githubusercontent.com/fooplugins/foogallery-extensions/future/extensions.json' );
    
//define( 'FOOGALLERY_EXTENSIONS_ENDPOINT', FOOGALLERY_URL . 'extensions/extensions.json.js' );
    
define'FOOGALLERY_EXTENSIONS_LOADING_ERRORS''foogallery_extensions_loading_errors' );
    
define'FOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE''foogallery_extensions_loading_errors_response' );
    
define'FOOGALLERY_EXTENSIONS_AVAILABLE_TRANSIENT_KEY''foogallery_extensions_available' );
    
define'FOOGALLERY_EXTENSIONS_MESSAGE_TRANSIENT_KEY''foogallery_extensions_message' );
    
define'FOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY''foogallery_extensions_activated' );
    
define'FOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY''foogallery_extensions_errors' );
    
define'FOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY''foogallery_extensions_slugs' );
    
define'FOOGALLERY_EXTENSIONS_AUTO_ACTIVATED_OPTIONS_KEY''foogallery_extensions_auto_activated' );

    
/**
     * Foogalolery Extensions Manager Class
     * Class FooGallery_Extensions_API
     */
    
class FooGallery_Extensions_API {

        
/**
         * Internal list of all extensions
         * @var array
         */
        
private $extensions false;

        
/**
         * Internal list of all extension slugs
         * @var array
         */
        
private $extension_slugs false;

        
/**
         * Extension API constructor
         * @param bool $load
         */
        
function __construct$load false ) {
            if ( 
$load ) {
                
$this->load_available_extensions();
            }
        }

        
/**
         * Returns true if there were errors loading extensions
         * @return bool
         */
        
public function has_extension_loading_errors() {
            return 
get_optionFOOGALLERY_EXTENSIONS_LOADING_ERRORS );
        }

        
/**
         * Returns the actual reposnse when there were errors trying to fetch all extensions
         * @return mixed
         */
        
public function get_extension_loading_errors_response() {
            return 
get_optionFOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
        }

        
/**
         * Get back the extension endpoint based on a setting
         */
        
public function get_extensions_endpoint() {
            if ( 
'on' === foogallery_get_setting'use_future_endpoint' ) ) {
                
$extension_url FOOGALLERY_EXTENSIONS_FUTURE_ENDPOINT;
            } else {
                
$extension_url FOOGALLERY_EXTENSIONS_ENDPOINT;
            }
            
//make sure we always get the latest version!
            
$extension_url .= '?v=' wp_generate_password();

            return 
apply_filters('foogallery_extension_api_endpoint'$extension_url );
        }

        
/**
         * Reset all previous errors
         */
        
public function reset_errors() {
            
delete_optionFOOGALLERY_EXTENSIONS_LOADING_ERRORS );
            
delete_optionFOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE );
        }

        
/**
         * Load all available extensions from the public endpoint and store in a transient for later use
         */
        
private function load_available_extensions() {
            if ( 
false === ( $this->extensions get_transientFOOGALLERY_EXTENSIONS_AVAILABLE_TRANSIENT_KEY ) ) ) {

                
//clear any previous state
                
$this->reset_errors();
                
$this->extensions null;
                
$expires 60 60 24//1 day

                
$extension_url $this->get_extensions_endpoint();

                
//fetch the data from our public list of extensions hosted on github
                
$response wp_remote_get$extension_url, array( 'sslverify' => false ) );

                if( ! 
is_wp_error$response ) ) {

                    if ( 
$response['response']['code'] == 200 ) {
                        
$this->extensions = @json_decode$response['body'], true );

                        
//if we got a valid list of extensions then calculate which are new and cache the result
                        
if ( is_array$this->extensions ) ) {
                            
$this->determine_new_extensions( );
                            
$this->save_slugs_for_new_calculations();
                        }
                    }
                }

                if ( ! 
is_array$this->extensions ) ) {
                    
//there was some problem getting a list of extensions. Could be a network error, or the extension json was malformed
                    
update_optionFOOGALLERY_EXTENSIONS_LOADING_ERRORStrue );
                    
update_optionFOOGALLERY_EXTENSIONS_LOADING_ERRORS_RESPONSE$response );
                    
$this->extensions $this->default_extenions_in_case_of_emergency();
                    
$expires 60//Only cache for 5 minutes if there are errors.
                
}

                
//Cache the result
                
set_transientFOOGALLERY_EXTENSIONS_AVAILABLE_TRANSIENT_KEY$this->extensions$expires );
            }
        }

        
/**
         * Get an array of default extensions.
         * 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
         *
         * @return array
         */
        
private function default_extenions_in_case_of_emergency() {
            
$extensions = array();

            
$extensions[] =    array(
                
'slug' => 'albums',
                
'class' => 'FooGallery_Albums_Extension',
                
'title' => 'Albums',
                
'categories' =>    array( 'Featured''Free' ),
                
'description' => 'Group your galleries into albums. Boom!',
                
'html' => 'Group your galleries into albums. Boom!',
                
'author' => 'FooPlugins',
                
'author_url' => 'http://fooplugins.com',
                
'thumbnail' => '/extensions/albums/foogallery-albums.png',
                
'tags' => array( 'functionality' ),
                
'source' => 'bundled'
            
);

            
//FooBox premium
            
$extensions[] = array(
                
'slug' => 'foobox',
                
'class' => 'FooGallery_FooBox_Extension',
                
'categories' => array( 'Featured''Premium' ),
                
'file' => 'foobox-free.php',
                
'title' => 'FooBox PRO',
                
'description' => 'The best lightbox for WordPress just got even better!',
                
'price' => '$27',
                
'author' => 'FooPlugins',
                
'author_url' => 'http://fooplugins.com',
                
'thumbnail' => '/assets/extension_bg.png',
                
'tags' => array( 'premium''lightbox', ),
                
'source' => 'fooplugins',
                
'download_button' =>
                    array(
                        
'text' => 'Buy - $27',
                        
'target' => '_blank',
                        
'href' => 'http://fooplugins.com/plugins/foobox',
                        
'confirm' => false,
                    ),
                
'activated_by_default' => true,
                
'minimum_version' => '2.3.2',
                
'remove_if_active' => array('foobox-image-lightbox')
            );

            
//FooBox lightbox
            
$extensions[] = array (
                
'slug' => 'foobox-image-lightbox',
                
'class' => 'FooGallery_FooBox_Free_Extension',
                
'categories' => array( 'Featured''Free', ),
                
'file' => 'foobox-free.php',
                
'title' => 'FooBox FREE',
                
'description' => 'The best lightbox for WordPress. Free',
                
'author' => 'FooPlugins',
                
'author_url' => 'http://fooplugins.com',
                
'thumbnail' => '/assets/extension_bg.png',
                
'tags' => array( 'lightbox' ),
                
'source' => 'repo',
                
'activated_by_default' => true,
                
'minimum_version' => '1.0.2.1',
            );

            
//The NextGen importer
            
$extensions[] = array(
                
'slug' => 'nextgen',
                
'class' => 'FooGallery_Nextgen_Gallery_Importer_Extension',
                
'categories' => array( 'Free' ),
                
'title' => 'NextGen Importer',
                
'description' => 'Imports all your existing NextGen galleries',
                
'author' => 'FooPlugins',
                
'author_url' => 'http://fooplugins.com',
                
'thumbnail' => '/assets/extension_bg.png',
                
'tags' => array( 'tools' ),
                
'source' => 'bundled',
            );

            return 
$extensions;
        }

        
/**
         * @TODO
         */
        
private function determine_new_extensions() {
            
$previous_slugs get_optionFOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY );
            if ( 
$previous_slugs ) {
                
//only do something if we have a previously saved array
                
foreach ( $this->extensions as &$extension ) {
                    if ( ! 
in_array$extension['slug'], $previous_slugs ) ) {
                        if ( ! isset( 
$extension['tags'] ) ) {
                            
$extension['tags'] = array();
                        }
                        
array_unshift$extension['tags'] , __'new''foogallery' ) );
                    }
                }
            }
        }

        
/**
         * @TODO
         */
        
private function save_slugs_for_new_calculations() {
            if ( 
is_array$this->extensions ) ) {
                
$slugs = array();
                foreach ( 
$this->extensions as $extension ) {
                    
$slugs[] = $extension['slug'];
                }
                if ( 
count$slugs ) > ) {
                    
update_optionFOOGALLERY_EXTENSIONS_SLUGS_OPTIONS_KEY$slugs );
                }
            }
        }

        
/**
         * Clears the cached list of extensions
         */
        
public function clear_cached_extensions() {
            
delete_transientFOOGALLERY_EXTENSIONS_AVAILABLE_TRANSIENT_KEY );
        }

        
/**
         * Reload the extensions from the public endpoint
         */
        
public function reload() {
            
$this->clear_cached_extensions();
            
$this->load_available_extensions();
        }

        
/**
         * Get all loaded extensions
         * @return array
         */
        
function get_all() {

            
//check if we need to load
            
if ( false === $this->extensions ) {
                
$this->load_available_extensions();
            }

            
//get any extra extensions from plugins
            
$extra_extensions apply_filters'foogallery_available_extensions', array() );

            if ( 
count$extra_extensions ) > ) {
                
//get a list of slugs so we can determine duplicates!
                
$this->extension_slugs = array();
                foreach ( 
$this->extensions as $extension ) {
                    
$this->extension_slugs[] = $extension['slug'];
                }

                
//only add if not a duplicate
                
foreach ( $extra_extensions as $extension ) {
                    if ( ! 
in_array$extension['slug'], $this->extension_slugs ) ) {
                        
$this->extensions[] = $extension;
                    }
                }
            }

            return 
$this->extensions;
        }


        
/**
         * return a list of all extensions for the extension view.
         * This list could be changed based on other plugin
         */
        
function get_all_for_view() {
            
$all_extensions $this->get_all();
            
$extensions = array();
            
$active_extensions = array();

            
//add all extensions to an array using the slug as the array key
            
foreach ( $all_extensions as &$extension ) {
                
$active $this->is_active$extension['slug'], true );
                
$extension['downloaded'] = $active || $this->is_downloaded$extension );
                
$extension['is_active'] = $active;
                
$extension['has_errors'] = $this->has_errors$extension['slug'] );

                
//build up a list of active extensions
                
if ( $active ) {
                    
$active_extensions[$extension['slug']] = $extension;
                }

                
//remove any bundled extensions that are activated_by_default = true
                
if ( isset( $extension['activated_by_default'] ) &&
                    
true === $extension['activated_by_default'] &&
                    isset( 
$extension['source'] ) &&
                    
'bundled' === $extension['source']) {
                    
//do not include a bundled extension that is activated by default
                
} else {
                    
$extensions$extension['slug'] ] = $extension;
                }
            }

            
//loop through all active extensions and remove any other extensions if required based on the 'remove_if_active' property
            
foreach ( $active_extensions as $active_extension_slug => $active_extension ) {
                
//check if we need to remove any other extensions from the list
                
if ( isset( $active_extension['remove_if_active'] ) ) {

                    foreach ( 
$active_extension['remove_if_active'] as $extension_slug_to_remove ) {
                        if ( 
array_key_exists$extension_slug_to_remove$extensions ) ) {
                            unset( 
$extensions$extension_slug_to_remove ] );
                        }
                    }
                }
            }

            
$extensions apply_filters'foogallery_extensions_for_view'$extensions );

            return 
$extensions;
        }
        
/**
         * Get all loaded extensions slugs
         * @return array
         */
        
function get_all_slugs() {
            
//load all extensions first!
            
$this->get_all();

            return 
$this->extension_slugs;
        }

        
/**
         * Returns a distinct array of categories that are used in the extensions
         * @return mixed
         */
        
function get_all_categories() {
            
$categories['all'] = array(
                
'name' => __'All''foogallery' ),
            );
            
$categories['activated'] = array(
                
'name' => __'Active''foogallery' ),
            );
            
$active 0;
            foreach ( 
$this->get_all() as $extension ) {
                if ( 
$this->is_active$extension['slug'] ) ) {
                    
$active++;
                }
                
$category_names $extension['categories'];
                foreach ( 
$category_names as $category_name ) {
                    
$category_slug foo_convert_to_key$category_name );

                    if ( ! 
array_key_exists$category_slug$categories ) ) {
                        
$categories$category_slug ] = array(
                            
'name'  => $category_name,
                        );
                    }
                }
            }
            
$categories['build_your_own'] = array(
                
'name' => __'Build Your Own''foogallery' )
            );
            return 
apply_filters'foogallery_extension_categories'$categories );
        }

        
/**
         * @TODO
         * @param $slug
         *
         * @return bool
         */
        
public function get_extension$slug ) {
            foreach ( 
$this->get_all() as $extension ) {
                if ( 
$extension['slug'] === $slug ) {
                    return 
$extension;
                }
            }
            return 
false;
        }

        
/**
         * @TODO
         * @param $file
         *
         * @return bool
         */
        
public function get_extension_by_file$file ) {
            
$file basename$file ); //normalize to just the filename

            
foreach ( $this->get_all() as $extension ) {
                if ( 
foo_safe_get$extension'file' ) === $file ) {
                    return 
$extension;
                }
            }
            return 
false;
        }

        
/**
         * @TODO
         * @param      $slug
         *
         * @return bool
         */
        
public function is_active$slug$perform_active_check false ) {
            
$active_extensions $this->get_active_extensions();
            if ( 
array_key_exists$slug$active_extensions ) ) {
                
//it has been previously activated through the extensions page
                
return true;
            }

            if ( 
$perform_active_check ) {
                
$extension $this->get_extension$slug );

                
//if we have an 'plugin_active_class' attribute and that class exists, it means our plugin must be active
                
if ( isset( $extension['plugin_active_class'] ) ) {
                    if ( 
class_exists$extension['plugin_active_class'] ) ) {
                        return 
true;
                    }
                }

                
//if we cannot find the extension class in memory, then check to see if the extension plugin is activated
                
if ( isset( $extension['perform_plugin_active_check'] ) && true === $extension['perform_plugin_active_check'] &&
                    isset( 
$extension['file'] ) ) {
                    
$plugin $this->find_active_wordpress_plugin$extension );

                    return 
$plugin !== false;
                }
            }

            return 
false;

//            global $foogallery_extensions;
//
//            //first check if the extension class was loaded into memory
//            if ( $foogallery_extensions ) {
//                if ( array_key_exists( $slug, $foogallery_extensions ) ) {
//                    return true;
//                }
//            }
//
//            //if we cannot find the extension class in memory, then check to see if the extension plugin is activated
//            $extension = $this->get_extension( $slug );
//            $plugin = $this->find_active_wordpress_plugin( $extension );
//            return $plugin;
        
}

        
/**
         * @TODO
         *
         * @param bool $extension
         *
         * @param bool $slug
         *
         * @return bool
         */
        
public function is_downloaded$extension false$slug false ) {
            
//allow you to pass in a slug rather
            
if ( ! $extension && $slug !== false ) {
                
$extension $this->get_extension$slug );
            }
            if ( 
$extension ) {
                
//first check if the class exists
                
if ( class_exists$extension['class'] ) ) {
                    return 
true;
                }

                
//next fallback to see if a plugin exists that has the same file name
                
$plugin $this->find_wordpress_plugin$extension );
                return 
false !== $plugin;
            }
            return 
false;
        }

        
/**
         * @TODO
         * @param $slug
         *
         * @return bool
         */
        
public function has_errors$slug ) {
            
$error_extensions $this->get_error_extensions();

            if ( 
$error_extensions ) {
                return 
array_key_exists$slug$error_extensions );
            }
            return 
false;
        }

        
/**
         * @TODO
         * @param $plugin
         */
        
public function handle_wordpress_plugin_deactivation$plugin ) {
            
$extension $this->get_extension_by_file$plugin );
            if ( 
$extension ) {
                
//we have found a matching extension
                
$this->deactivate$extension['slug'], false );
            }
        }

        
/**
         * @TODO
         * @param $plugin
         */
        
public function handle_wordpress_plugin_activation$plugin ) {
            
$extension $this->get_extension_by_file$plugin );
            if ( 
$extension ) {
                
//we have found a matching extension
                
$this->activate$extension['slug'], false );
            }
        }

        
/**
         * @TODO
         * @param      $slug
         * @param bool $deactivate_wordpress_plugin
         * @param bool $error_loading
         *
         * @return array|mixed|void
         */
        
public function deactivate$slug$deactivate_wordpress_plugin true$error_loading false ) {
            
$extension $this->get_extension$slug );
            if ( 
$extension ) {
                if ( 
$deactivate_wordpress_plugin && 'bundled' !== foo_safe_get$extension'source'false ) ) {
                    
$plugin $this->find_wordpress_plugin$extension );
                    if ( 
$plugin ) {
                        
$failure deactivate_plugins$plugin['file'], truefalse );
                        if ( 
null !== $failure ) {
                            return array(
                                
'message' => sprintf__'The extension %s could NOT be deactivated!''foogallery' ), "<strong>{$extension['title']}</strong>" ),
                                
'type' => 'error'
                            
);
                        }
                    }
                }

                
$active_extensions $this->get_active_extensions();
                if ( 
array_key_exists$slug$active_extensions ) ) {
                    unset( 
$active_extensions$slug ] );
                    if ( empty(
$active_extensions) ) {
                        
delete_optionFOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY );
                    } else {
                        
update_optionFOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY$active_extensions );
                    }
                }

                if ( 
$error_loading ) {
                    
$this->add_to_error_extensions$slug );
                }

                
//we are done, allow for extensions to do something after an extension is activated
                
do_action'foogallery_extension_deactivated-' $slug );

                return 
apply_filters'foogallery_extensions_deactivated_message-' $slug, array(
                    
'message' => sprintf__'The extension %s was successfully deactivated''foogallery' ), "<strong>{$extension['title']}</strong>" ),
                    
'type' => 'success',
                ) );
            }
            return array(
                
'message' => sprintf__'Unknown extension : %s''foogallery' ), $slug ),
                
'type' => 'error',
            );
        }

        
/**
         * @TODO
         *
         * @param      $slug
         * @param bool $activate_wordpress_plugin
         *
         * @return array|mixed|void
         */
        
public function activate$slug$activate_wordpress_plugin true ) {
            
$extension $this->get_extension$slug );
            if ( 
$extension ) {
                
//first remove it from our error list (if it was there before)
                
$this->remove_from_error_extensions$slug );

                if ( 
$activate_wordpress_plugin && 'bundled' !== foo_safe_get$extension'source'false ) ) {
                    
//activate the plugin, WordPress style!
                    
$plugin $this->find_wordpress_plugin$extension );

                    if ( 
$plugin ) {

                        
//check min version
                        
$minimum_version foo_safe_get$extension'minimum_version' );
                        if ( !empty(
$minimum_version) ) {
                            
$actual_version $plugin['plugin']['Version'];
                            if ( 
version_compare$actual_version$minimum_version ) < ) {
                                
$this->add_to_error_extensions$slugsprintf__'Requires %s version %s','foogallery' ), $extension['title'], $minimum_version ) );
                                return array(
                                    
'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 ),
                                    
'type' => 'error',
                                );
                            }
                        }

                        
//try to activate the plugin
                        
$failure activate_plugin$plugin['file'], ''falsefalse );
                        if ( 
null !== $failure ) {
                            return array(
                                
'message' => sprintf__'The extension %s could NOT be activated!''foogallery' ), "<strong>{$extension['title']}</strong>" ),
                                
'type' => 'error',
                            );
                        }
                    }
                }
                
//load an instance of the extension class into memory
                
$loader = new FooGallery_Extensions_Loader();
                
$loader->load_extension$slugfoo_safe_get$extension'class'false ) );

                
//then add the extension to our saved option so that it can be loaded on startup
                
$this->add_to_activated_extensions$extension );

                
//we are done, allow for extensions to do something after an extension is activated
                
do_action'foogallery_extension_activated-' $slug );

                
//return our result
                
return apply_filters'foogallery_extension_activated_message-' $slug, array(
                    
'message' => sprintf__'The extension %s was successfully activated''foogallery' ), "<strong>{$extension['title']}</strong>" ),
                    
'type' => 'success',
                ) );
            }
            return array(
                
'message' => sprintf__'Unknown extension : %s''foogallery' ), $slug ),
                
'type' => 'error',
            );
        }

        
/**
         * @TODO
         * @param boolean $extension
         *
         * @return array|bool
         */
        
private function find_wordpress_plugin$extension ) {
            
$plugins get_plugins();
            foreach ( 
$plugins as $plugin_file => $plugin ) {
                if ( isset(
$extension['file']) && foo_ends_with$plugin_file$extension['file'] ) ) {
                    return array(
                        
'file' => $plugin_file,
                        
'plugin' => $plugin,
                        
'active' => is_plugin_active$plugin_file ),
                    );
                }
            }
            return 
false;
        }

        
/**
         * @TODO
         * @param boolean $extension
         *
         * @return array|bool
         */
        
private function find_active_wordpress_plugin$extension ) {
            
$plugins get_plugins();
            foreach ( 
$plugins as $plugin_file => $plugin ) {
                if ( 
is_plugin_active$plugin_file ) && isset($extension['file']) && foo_ends_with$plugin_file$extension['file'] ) ) {
                    return array(
                        
'file' => $plugin_file,
                        
'plugin' => $plugin
                    
);
                }
            }
            return 
false;
        }

        
/**
         * @TODO
         * @param $slug
         *
         * @return array|mixed|void
         */
        
public function download$slug ) {
            
$extension $this->get_extension$slug );
            if ( 
$extension ) {

                
//we need some files!
                
require_once ABSPATH 'wp-admin/includes/plugin-install.php'// plugins_api calls
                
require_once ABSPATH 'wp-admin/includes/class-wp-upgrader.php'// Plugin_Upgrader class
                
require_once FOOGALLERY_PATH 'includes/admin/class-silent-installer-skin.php'//our silent installer skin

                
$download_link = isset( $extension['download_link'] ) ? $extension['download_link'] : false;

                if ( 
'repo' === $extension['source'] ) {
                    
$plugins_api plugins_api'plugin_information', array( 'slug' => $slug'fields' => array( 'sections' => false ) ) );

                    if ( 
is_wp_error$plugins_api ) ) {
                        return array(
                            
'message' => sprintf__'Unable to connect to the WordPress.org plugin API to download %s. Full error log: %s''foogallery' ), $slug,  '<br />' var_export$plugins_apitrue ) ),
                            
'type' => 'error',
                        );
                    }

                    
//get the download link from the API call
                    
if ( isset( $plugins_api->download_link ) ) {
                        
$download_link $plugins_api->download_link;
                    }
                }

                
//check we have something to download
                
if ( empty( $download_link ) ) {
                    return array(
                        
'message' => sprintf__'The extension %s has no download link!''foogallery' ), $slug ),
                        
'type' => 'error',
                    );
                }

                
$skin = new FooGallery_Silent_Installer_Skin();

                
//instantiate Plugin_Upgrader
                
$upgrader = new Plugin_Upgrader$skin );

                
$upgrader->install$download_link );

                if ( 
'process_failed' === $skin->feedback ) {
                    
$error_message is_wp_error$skin->result ) ? $skin->result->get_error_message() : __'Unknown!''foogallery' );

                    
//save the error message for the extension
                    
$this->add_to_error_extensions$slugsprintf__('Could not be downloaded! Error : %s''foogallery' ), $error_message ) );

                    
//we had an error along the way
                    
return apply_filters'foogallery_extensions_download_failure-' $slug, array(
                        
'message' => sprintf__'The extension %s could NOT be downloaded! Error : %s''foogallery' ), "<strong>{$extension['title']}</strong>"$error_message ),
                        
'type' => 'error'
                    
) );
                }

                
//return our result
                
return apply_filters'foogallery_extensions_download_success-' $slug, array(
                    
'message' => sprintf__'The extension %s was successfully downloaded and can now be activated. %s''foogallery' ),
                        
"<strong>{$extension['title']}</strong>",
                        
'<a href="' esc_urladd_query_arg( array(
                                
'action' => 'activate',
                                
'extension' => $slug, ) ) ) . '">' __'Activate immediately''foogallery' ) . '</a>'
                    
),
                    
'type' => 'success',
                ) );
            }
            return array(
                
'message' => sprintf__'Unknown extension : %s''foogallery' ), $slug ),
                
'type' => 'error',
            );
        }

        
/**
         * @TODO
         * @return mixed|void
         */
        
public function get_active_extensions() {
            
//should we not rather get back all plugins that are active?
            
return get_optionFOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY, array() );
        }

        
/**
         * @TODO
         * @return mixed|void
         */
        
public function get_error_extensions() {
            return 
get_optionFOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY, array() );
        }

        public function 
get_error_message$slug ) {
            
$error_extensions $this->get_error_extensions();
            if ( 
array_key_exists$slug$error_extensions ) ) {
                return 
$error_extensions$slug ];
            }
            return 
'';
        }

        
/**
         * @TODO
         * @param $extension
         */
        
private function add_to_activated_extensions$extension ) {
            
$slug $extension['slug'];
            
$active_extensions $this->get_active_extensions();
            if ( !
array_key_exists$slug$active_extensions ) ) {
                
$active_extensions$slug ] = $extension['class'];
                
update_optionFOOGALLERY_EXTENSIONS_ACTIVATED_OPTIONS_KEY$active_extensions );
            }
        }

        
/**
         * @TODO
         * @param $slug
         */
        
public function add_to_error_extensions$slug$error_message '' ) {
            
$error_extensions $this->get_error_extensions();

            if ( empty(
$error_message) ) {
                
$error_message __'Error loading extension!''foogallery' );
            }

            if ( 
array_key_exists$slug$error_extensions ) &&
                
$error_message === $error_extensions[$slug]) {
                
//do nothing!
            
} else {
                
$error_extensions[$slug] = $error_message;
                
update_optionFOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY$error_extensions );
            }
        }

        
/**
         * @TODO
         * @param $slug
         */
        
private function remove_from_error_extensions$slug ) {
            
$error_extensions $this->get_error_extensions();
            if ( 
array_key_exists$slug$error_extensions ) ) {
                unset( 
$error_extensions$slug ] );
                
update_optionFOOGALLERY_EXTENSIONS_ERRORS_OPTIONS_KEY$error_extensions );
            }
        }

        
/**
         * @TODO
         */
        
public function auto_activate_extensions() {
            foreach ( 
$this->get_all() as $extension ) {
                if ( 
true === foo_safe_get$extension'activated_by_default' ) ) {
                    
//check to see if the extension is downloaded
                    
if ( $this->is_downloaded$extension ) ) {
                        
$this->add_to_activated_extensions$extension );
                    }
                }
            }
        }
    }
}