/var/www/hkosl.com/aga/wp-content/plugins/siteorigin-panels/widgets/widgets.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
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
<?php
// Include all the basic widgets
include plugin_dir_path(__FILE__) . '/less/functions.php';

/**
 * Include all the widget files and register their widgets
 */
function origin_widgets_init(){
    foreach(
glob(plugin_dir_path(__FILE__).'/widgets/*/*.php') as $file) {
        include_once (
$file);

        
$p pathinfo($file);
        
$class $p['filename'];
        
$class str_replace('-'' '$class);
        
$class ucwords($class);
        
$class str_replace(' ''_'$class);

        
$class 'SiteOrigin_Panels_Widget_'.$class;
        if( 
class_exists($class) ) register_widget($class);
    }
}
add_action('widgets_init''origin_widgets_init');

function 
origin_widgets_enqueue($prefix){
    if(
$prefix == 'widgets.php'wp_enqueue_script('origin-widgets-admin-script'plugin_dir_url__FILE__ ).'js/admin.js', array('jquery'), SITEORIGIN_PANELS_VERSION);
}
add_action('admin_enqueue_scripts''origin_widgets_enqueue');

function 
origin_widgets_generate_css($class$style$preset$version null){
    
$widget = new $class();
    if( !
is_subclass_of($widget'SiteOrigin_Panels_Widget') ) return '';
    if(empty(
$version)) $version SITEORIGIN_PANELS_VERSION;

    
$id str_replace('_'''strtolower(str_replace('SiteOrigin_Panels_Widget_'''$class)));
    
$key strtolower($id.'-'.$style.'-'$preset.'-'.str_replace('.'''$version));

    
$css get_site_transient('origin_wcss:'.$key);
    if(
$css === false || ( defined('SITEORIGIN_PANELS_NOCACHE') && SITEORIGIN_PANELS_NOCACHE ) ) {

        
// Recreate the CSS
        
$css "/* Regenerate Cache */\n\n" ;
        
$css .= $widget->create_css($style$preset);
        
$css preg_replace('#/\*.*?\*/#s'''$css);
        
$css preg_replace('/\s*([{}|:;,])\s+/''$1'$css);
        
$css preg_replace('/\s\s+(.*)/''$1'$css);
        
$css str_replace(';}''}'$css);

        
set_site_transient('origin_wcss:'.$key$css86400);
    }

    return 
$css;
}

function 
origin_widgets_footer_css(){
    global 
$origin_widgets_generated_css;
    if( !empty( 
$origin_widgets_generated_css ) ) {
        echo 
'<style type="text/css">';
        foreach( 
$origin_widgets_generated_css as $id => $css ) {
            if( empty(
$css) ) continue;
            echo 
$css;
            
$origin_widgets_generated_css[$id] = '';
        }
        echo 
'</style>';
    }
}
add_action('wp_head''origin_widgets_footer_css');
add_action('wp_footer''origin_widgets_footer_css');

/**
 * Class SiteOrigin_Panels_Widget
 */
abstract class SiteOrigin_Panels_Widget extends WP_Widget{
    public 
$form_args;
    protected 
$demo;
    protected 
$origin_id;
    public 
$sub_widgets;

    private 
$styles;

    
/**
     * Create the widget
     *
     * @param string $name Name for the widget displayed on the configuration page.
     * @param array $widget_options Optional Passed to wp_register_sidebar_widget()
     *     - description: shown on the configuration page
     *     - classname
     * @param array $control_options Optional Passed to wp_register_widget_control()
     *     - width: required if more than 250px
     *     - height: currently not used but may be needed in the future
     * @param array $form Form arguments.
     * @param array $demo Values for the demo of the page builder widget.
     * @internal param string $id_base
     */
    
function __construct($name$widget_options = array(), $control_options = array(), $form = array(), $demo = array()){
        
$id_base str_replace('SiteOrigin_Panels_Widget_'''get_class($this));
        
$id_base strtolower(str_replace('_''-'$id_base));

        
parent::__construct('origin_'.$id_base$name$widget_options$control_options);
        
$this->origin_id $id_base;

        
$this->form_args $form;
        
$this->demo $demo;
        
$this->styles = array();
        
$this->sub_widgets = array();
    }

    
/**
     * Update the widget and save the new CSS.
     *
     * @param array $old
     * @param array $new
     * @return array
     */
    
function update($new$old) {

        
// We wont clear cache if this is a preview
        
if( !is_preview() ){
            
// Remove the old CSS file
            
if(!empty($old['origin_style'])) {
                list(
$style$preset) = explode(':'$old['origin_style']);
                
$this->clear_css_cache($style$preset);
            }

            
// Clear the cache for all sub widgets
            
if(!empty($this->sub_widgets)){
                global 
$wp_widget_factory;
                foreach(
$this->sub_widgets as $id => $sub) {
                    if(empty(
$old['origin_style_'.$id])) continue;
                    
$the_widget $wp_widget_factory->widgets[$sub[1]];
                    list(
$style$preset) = explode(':'$old['origin_style_'.$id]);

                    
$the_widget->clear_css_cache($style$preset);
                }
            }



        }

        foreach(
$this->form_args as $field_id => $field_args) {
            if(
$field_args['type'] == 'checkbox') {
                
$new[$field_id] = !empty($new[$field_id]);
            }
        }

        return 
$new;
    }

    
/**
     * Display the form for the widget. Auto generated from form array.
     *
     * @param array $instance
     * @return string|void
     */
    
public function form($instance){

        
?>
        <div style="margin-bottom: 20px;">
            <strong>
                <?php
                _e
'This is a legacy widget. ''siteorigin-panels' );
                
_e'Ideally you should move to using widgets from the SiteOrigin Widgets Bundle instead. ''siteorigin-panels' );
                
_e"It'll be moved to a separate plugin after Page Builder 2.6 is released. "'siteorigin-panels' );
                echo 
'<a href="https://siteorigin.com/widgets-bundle/" target="_blank" rel="noopener noreferrer">' __'Widgets Bundle''siteorigin-panels' ) . '</a>';
                
?>
            </strong>
        </div>
        <?php

        
foreach($this->form_args as $field_id => $field_args) {
            if(isset(
$field_args['default']) && !isset($instance[$field_id])) {
                
$instance[$field_id] = $field_args['default'];
            }
            if(!isset(
$instance[$field_id])) $instance[$field_id] = false;

            
?><p><label for="<?php echo $this->get_field_id$field_id ); ?>"><?php echo esc_html($field_args['label']) ?></label><?php

            
if($field_args['type'] != 'checkbox') echo '<br />';

            switch(
$field_args['type']) {
                case 
'text' :
                    
?><input type="text" class="widefat" id="<?php echo $this->get_field_id$field_id ); ?>" name="<?php echo $this->get_field_name$field_id ); ?>" value="<?php echo esc_attr($instance[$field_id]) ?>" /><?php
                    
break;
                case 
'textarea' :
                    if(empty(
$field_args['height'])) $field_args['height'] = 6;
                    
?><textarea class="widefat" id="<?php echo $this->get_field_id$field_id ); ?>" name="<?php echo $this->get_field_name$field_id ); ?>" rows="<?php echo intval($field_args['height']) ?>"><?php echo esc_textarea($instance[$field_id]) ?></textarea><?php
                    
break;
                case 
'number' :
                    
?><input type="number" class="small-text" id="<?php echo $this->get_field_id$field_id ); ?>" name="<?php echo $this->get_field_name$field_id ); ?>" value="<?php echo floatval($instance[$field_id]) ?>" /><?php
                    
break;
                case 
'checkbox' :
                    
?><input type="checkbox" class="small-text" id="<?php echo $this->get_field_id$field_id ); ?>" name="<?php echo $this->get_field_name$field_id ); ?><?php checked(!empty($instance[$field_id])) ?>/><?php
                    
break;
                case 
'select' :
                    
?>
                    <select id="<?php echo $this->get_field_id$field_id ); ?>" name="<?php echo $this->get_field_name$field_id ); ?>">
                        <?php foreach($field_args['options'] as $k => $v) : ?>
                            <option value="<?php echo esc_attr($k?><?php selected($instance[$field_id], $k?>><?php echo esc_html($v?></option>
                        <?php endforeach; ?>
                    </select>
                    <?php
                    
break;
            }
            if(!empty(
$field_args['description'])) echo '<small class="description">'.esc_html($field_args['description']).'</small>';

            
?></p><?php
        
}

        if(!isset(
$instance['origin_style'])) {
            
$instance['origin_style'] = !empty($this->widget_options['default_style']) ? $this->widget_options['default_style'] : false;
        }

        
do_action('siteorigin_panels_widget_before_styles'$this$instance);

        
// Now, lets add the style options.
        
$styles $this->get_styles();
        if( !empty( 
$styles ) ) {
            
?>
            <p>
                <label for="<?php echo $this->get_field_id('origin_style'?>"><?php _e('Style''siteorigin-panels'?></label>
                <select name="<?php echo $this->get_field_name('origin_style'?>" id="<?php echo $this->get_field_id('origin_style'?>">
                    <?php foreach($this->get_styles() as $style_id => $style_info) : $presets $this->get_style_presets($style_id); ?>
                        <?php if(!empty($presets)) : foreach($presets as $preset_id => $preset) : ?>
                            <option value="<?php echo esc_attr($style_id.':'.$preset_id?><?php selected($style_id.':'.$preset_id$instance['origin_style']) ?>>
                                <?php echo esc_html($style_info['Name'] . ' - ' ucwordsstr_replace'_'' '$preset_id ) ) ) ?>
                            </option>
                        <?php endforeach; endif; ?>
                    <?php endforeach ?>
                </select>
            </p>
            <?php
        
}

        
do_action('siteorigin_panels_widget_before_substyles'$this$instance);

        foreach(
$this->sub_widgets as $id => $sub) {
            global 
$wp_widget_factory;
            
$the_widget $wp_widget_factory->widgets[$sub[1]];

            if(!isset(
$instance['origin_style_'.$id])) $instance['origin_style_'.$id] = !empty($this->widget_options['default_style_'.$id]) ? $this->widget_options['default_style_'.$id] : false;

            
?>
            <p>
                <label for="<?php echo $this->get_field_id('origin_style_'.$id?>"><?php printf(__('%s Style''siteorigin-panels'), $sub[0]) ?></label>
                <select name="<?php echo $this->get_field_name('origin_style_'.$id?>" id="<?php echo $this->get_field_id('origin_style_'.$id?>">
                    <?php foreach($the_widget->get_styles() as $style_id => $style_info) : $presets $the_widget->get_style_presets($style_id); ?>
                        <?php if(!empty($presets)) : foreach($presets as $preset_id => $preset) : ?>
                            <option value="<?php echo esc_attr($style_id.':'.$preset_id?><?php selected($style_id.':'.$preset_id$instance['origin_style_'.$id]) ?>>
                                <?php echo esc_html($style_info['Name'].' - ' ucwordsstr_replace'_'' '$preset_id ) ) ) ?>
                            </option>
                        <?php endforeach; endif; ?>
                    <?php endforeach ?>
                </select>
            </p>
            <?php
        
}

        
do_action('siteorigin_panels_widget_after_styles'$this$instance);
    }

    
/**
     * Render the widget.
     *
     * @param array $args
     * @param array $instance
     * @return bool|void
     */
    
function widget($args$instance){

        
// Set up defaults for all the widget args
        
foreach($this->form_args as $field_id => $field_args) {
            if(isset(
$field_args['default']) && !isset($instance[$field_id])) {
                
$instance[$field_id] = $field_args['default'];
            }
            if(!isset(
$instance[$field_id])) $instance[$field_id] = false;
        }

        
// Filter the title
        
if(!empty($instance['title'])) {
            
$instance['title'] = apply_filters('widget_title'$instance['title'], $instance$this->id_base);
        }

        if(!empty(
$instance['origin_style'])) {
            list(
$style$preset) = explode(':'$instance['origin_style']);
            
$style sanitize_file_name($style);
            
$preset sanitize_file_name($preset);

            
$data $this->get_style_data($style);
            
$template $data['Template'];
        }
        else {
            
$style 'default';
            
$preset 'default';
        }

        if(empty(
$template)) $template 'default';

        
$template_file false;
        
$paths $this->get_widget_paths();

        foreach(
$paths as $path) {
            if(
file_exists($path.'/'.$this->origin_id.'/tpl/'.$template.'.php')) {
                
$template_file $path.'/'.$this->origin_id.'/tpl/'.$template.'.php';
                break;
            }
        }
        if(empty(
$template_file)) {
            echo 
$args['before_widget'];
            echo 
'Template not found';
            echo 
$args['after_widget'];
            return 
false;
        }

        
// Dynamically generate the CSS
        
global $origin_widgets_generated_css;
        if( empty(
$origin_widgets_generated_css) ) {
            
$origin_widgets_generated_css = array();
        }

        if(!empty(
$instance['origin_style'])) {
            
$filename $this->origin_id.'-'.$style.'-'.$preset;
            if( !isset(
$origin_widgets_generated_css[$filename]) ) {
                
$origin_widgets_generated_css[$filename] = origin_widgets_generate_css(get_class($this), $style$preset);
            }
        }

        if(
method_exists($this'enqueue_scripts')) {
            
$this->enqueue_scripts();
        }

        
$widget_classes apply_filters('siteorigin_widgets_classes', array(
            
'origin-widget',
            
'origin-widget-'.$this->origin_id,
            
'origin-widget-'.$this->origin_id.'-'$style .'-' $preset,
        ), 
$instance);

        if(
method_exists($this'widget_classes')) {
            
$widget_classes $this->widget_classes(array(
                
'origin-widget',
                
'origin-widget-'.$this->origin_id,
                
'origin-widget-'.$this->origin_id.'-'$style .'-' $preset,
            ), 
$instance);
        }

        echo 
$args['before_widget'];
        echo 
'<div class="'.esc_attr(implode(' '$widget_classes) ).'">';
        include 
$template_file;
        echo 
'</div>';
        echo 
$args['after_widget'];
    }

    
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    // Extra functions specific to a SiteOrigin widget.
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    /**
     * A sub widget is a widget that's style is required by this widget
     *
     * @param $id
     * @param $instance
     */
    
function sub_widget($id$instance){
        
$sub $this->sub_widgets[$id];
        global 
$wp_widget_factory;
        
$the_widget $wp_widget_factory->widgets[$sub[1]];
        
$the_widget->widget(array('before_widget' => '''after_widget' => ''), $instance);
    }

    
/**
     * Get the CSS for the given style and preset
     *
     * @param $style
     * @param $preset
     * @return string
     */
    
function create_css($style$preset) {
        
$paths $this->get_widget_paths();
        
$style_file false;

        
// Find the file - exit if it can't be found.
        
foreach($paths as $path) {
            if(
file_exists($path.'/'.$this->origin_id.'/styles/'.$style.'.less')) {
                
$style_file $path.'/'.$this->origin_id.'/styles/'.$style.'.less';
                break;
            }
        }
        if(empty(
$style_file)) return '';

        if( !
class_exists('lessc') ) include plugin_dir_path(__FILE__) . 'lib/lessc.inc.php';

        foreach(
$this->get_widget_folders() as $folder => $folder_url) {
            
$filename rtrim($folder'/') . '/' $this->origin_id.'/styles/'.$style.'.less';
            if(
file_exists($filename)) {
                
$less file_get_contents($filename);
                break;
            }
        }
        
// Add in the mixins
        
$less str_replace(
            
'@import "../../../less/mixins";',
            
"\n\n".file_get_contents(plugin_dir_path(__FILE__).'less/mixins.less'),
            
$less
        
);

        
// Apply the preset variables to the LESS file
        
$presets $this->get_style_presets($style);
        if(!empty(
$presets[$preset]) && is_array($presets[$preset])){
            foreach(
$presets[$preset] as $k => $v) {
                
$less preg_replace('/@'.preg_quote($k).':(.*);/''@'.$k.': '.$v.';'$less);
            }
        }

        
// Scope the CSS with the wrapper we'll be adding
        
$less '.origin-widget.origin-widget-'.$this->origin_id.'-'.$style.'-'.$preset.' {' $less '}';
        
$lc = new lessc();
        
$lc->setPreserveComments(false);

        
$lc->registerFunction('lumlighten''origin_widgets_less_lumlighten');
        
$lc->registerFunction('lumdarken''origin_widgets_less_lumdarken');
        
$lc->registerFunction('texture''origin_widgets_less_texture');
        
$lc->registerFunction('widgetimage''origin_widgets_less_widgetimage');

        
// Create the CSS
        
return $lc->compile($less);
    }

    
/**
     * Removes a CSS file
     *
     * @param $style
     * @param $preset
     */
    
function clear_css_cache($style$preset){
        
$filename $this->origin_id.'-'.$style.'-'.$preset;
        
delete_site_transient('origin_widgets_css_cache:'.$filename);
    }

    
/**
     * Get all the paths where we'll look for widgets.
     *
     * @return array
     */
    
function get_widget_paths(){
        static 
$paths = array();

        if(empty(
$paths)) {
            
$paths array_keys($this->get_widget_folders());
        }

        return 
$paths;
    }

    
/**
     * Get all the folders where we'll look for widgets
     *
     * @return mixed|void
     */
    
static function get_widget_folders(){
        static 
$folders = array();

        if(empty(
$folders)) {
            
$folders = array(
                
get_stylesheet_directory().'/widgets' => get_stylesheet_directory_uri().'/widgets/widgets',
                
get_template_directory().'/widgets' => get_template_directory_uri().'/widgets',
                
plugin_dir_path__FILE__ ) . 'widgets' => plugin_dir_url__FILE__ ).'widgets',
            );
            
$folders apply_filters('siteorigin_widget_folders'$folders);
        }

        return 
$folders;
    }

    
/**
     * Get all the folders where we'll look for widget images
     *
     * @return mixed|void
     */
    
static function get_image_folders(){
        static 
$folders = array();
        if(empty(
$folders)) {
            
$folders = array(
                
get_stylesheet_directory().'/widgets/img' => get_stylesheet_directory_uri().'/widgets/img',
                
get_template_directory().'/widgets/img' => get_template_directory_uri().'/widgets/img',
                
plugin_dir_path__FILE__ ) . 'img' => plugin_dir_url__FILE__ ) . 'img',
            );
            
$folders apply_filters('siteorigin_widget_image_folders'$folders);
        }

        return 
$folders;
    }

    
/**
     * Get all the styles for this widget.
     *
     * @return array
     */
    
public function get_styles(){
        if( empty( 
$this->styles ) ) {
            
// We can add extra paths here
            
foreach($this->get_widget_paths() as $path) {
                if(!
is_dir($path)) continue;

                
$files glob($path.'/'.$this->origin_id.'/styles/*.less');
                if(!empty(
$files)) {
                    foreach(
glob($path.'/'.$this->origin_id.'/styles/*.less') as $file) {
                        
$p pathinfo($file);
                        
$this->styles[$p['filename']] = $this->get_style_data($p['filename']);
                    }
                }
            }
        }

        return 
$this->styles;
    }

    
/**
     * Get the presets for a given style
     *
     * @param $style_id
     * @return mixed|void
     */
    
public function get_style_presets($style_id) {

        
$presets = array();

        foreach(
$this->get_widget_folders() as $folder => $folder_uri) {
            
$filename rtrim($folder'/') . '/' $this->origin_id.'/presets/'.sanitize_file_name($style_id).'.php';

            if(
file_exists($filename)) {
                
// This file should register a filter that adds the presets
                
$new_presets = include($filename);
                
$presets array_merge($presets$new_presets);
            }
        }


        return 
apply_filters('origin_widget_presets_'.$this->origin_id.'_'.$style_id$presets);
    }

    
/**
     * Get data for the style.
     *
     * @param $name
     * @return array
     */
    
public function get_style_data($name) {
        
$paths $this->get_widget_paths();

        foreach(
$paths as $path) {
            
$filename $path.'/'.$this->origin_id.'/styles/'.sanitize_file_name($name).'.less';
            if(!
file_exists($filename)) continue;

            
$data get_file_data($filename, array(
                
'Name' => 'Name',
                
'Template' => 'Template',
                
'Author' => 'Author',
                
'Author URI' => 'Author URI',
            ), 
'origin_widget');
            return 
$data;
        }
        return 
false;
    }

    
/**
     * Render a demo of the widget.
     *
     * @param array $args
     */
    
function render_demo($args = array()){
        
$this->widget($args$this->demo);
    }

    
/**
     * Register a widget that we'll be using inside this widget.
     *
     * @param $id
     * @param $name
     * @param $class
     */
    
function add_sub_widget($id$name$class){
        
$this->sub_widgets[$id] = array($name$class);
    }

    
/**
     * Add the fields required to query the posts.
     */
    
function add_post_query_fields(){
        
// Add the posts type field
        
$post_types get_post_types(array('public' => true));
        
$post_types array_values($post_types);
        
$this->form_args['query_post_type'] = array(
            
'type' => 'select',
            
'options' => $post_types,
            
'label' => __('Post Type''siteorigin-panels')
        );

        
// Add the posts per page field
        
$this->form_args['query_posts_per_page'] = array(
            
'type' => 'number',
            
'default' => 10,
            
'label' => __('Posts Per Page''siteorigin-panels'),
        );

        
$this->form_args['query_orderby'] = array(
            
'type' => 'select',
            
'label' => __('Order By''siteorigin-panels'),
            
'options' => array(
                
'none'  => __('None''siteorigin-panels'),
                
'ID'  => __('Post ID''siteorigin-panels'),
                
'author'  => __('Author''siteorigin-panels'),
                
'name'  => __('Name''siteorigin-panels'),
                
'name'  => __('Name''siteorigin-panels'),
                
'date'  => __('Date''siteorigin-panels'),
                
'modified'  => __('Modified''siteorigin-panels'),
                
'parent'  => __('Parent''siteorigin-panels'),
                
'rand'  => __('Random''siteorigin-panels'),
                
'comment_count'  => __('Comment Count''siteorigin-panels'),
                
'menu_order'  => __('Menu Order''siteorigin-panels'),
            )
        );

        
$this->form_args['query_order'] = array(
            
'type' => 'select',
            
'label' => __('Order''siteorigin-panels'),
            
'options' => array(
                
'ASC'  => __('Ascending''siteorigin-panels'),
                
'DESC'  => __('Descending''siteorigin-panels'),
            )
        );

        
$this->form_args['query_sticky'] = array(
            
'type' => 'select',
            
'label' => __('Sticky Posts''siteorigin-panels'),
            
'options' => array(
                
''  => __('Default''siteorigin-panels'),
                
'ignore'  => __('Ignore Sticky''siteorigin-panels'),
                
'exclude'  => __('Exclude Sticky''siteorigin-panels'),
                
'only'  => __('Only Sticky''siteorigin-panels'),
            )
        );

        
$this->form_args['query_additional'] = array(
            
'type' => 'text',
            
'label' => __('Additional Arguments''siteorigin-panels'),
            
'description' => preg_replace(
                
'/1\{ *(.*?) *\}/',
                
'<a href="http://codex.wordpress.org/Function_Reference/query_posts">$1</a>',
                
__('Additional query arguments. See 1{query_posts}.''siteorigin-panels')
            )
        );
    }

    
/**
     * Get all the posts for the current query
     *
     * @param $instance
     * @return WP_Query
     */
    
static function get_query_posts($instance) {
        
$query_args = array();
        foreach(
$instance as $k => $v){
            if(
strpos($k'query_') === 0) {
                
$query_args[preg_replace('/query_/'''$k1)] = $v;
            }
        }
        
$query $query_args;
        unset(
$query['additional']);
        unset(
$query['sticky']);

        
// Add the additional arguments
        
$query wp_parse_args($query_args['additional'], $query);

        
// Add the sticky posts if required
        
switch($query_args['sticky']){
            case 
'ignore' :
                
$query['ignore_sticky_posts'] = 1;
                break;
            case 
'only' :
                
$query['post__in'] = get_option'sticky_posts' );
                break;
            case 
'exclude' :
                
$query['post__not_in'] = get_option'sticky_posts' );
                break;
        }

        
// Add the current page
        
global $wp_query;
        
$query['paged'] = $wp_query->get('paged');

        return new 
WP_Query($query);
    }
}

// All the standard bundled widgets

/**
 * A gallery widget
 *
 * Class SiteOrigin_Panels_Widgets_Gallery
 */
class SiteOrigin_Panels_Widgets_Gallery extends WP_Widget {
    function 
__construct() {
        
parent::__construct(
            
'siteorigin-panels-gallery',
            
__'Gallery (PB)''siteorigin-panels' ),
            array(
                
'description' => __'Displays a gallery.''siteorigin-panels' ),
            )
        );
    }

    function 
widget$args$instance ) {
        echo 
$args['before_widget'];

        
$shortcode_attr = array();
        foreach(
$instance as $k => $v){
            if(empty(
$v)) continue;
            
$shortcode_attr[] = $k.'="'.esc_attr($v).'"';
        }

        echo 
do_shortcode('[gallery '.implode(' '$shortcode_attr).']');

        echo 
$args['after_widget'];
    }

    function 
update$new$old ) {
        return 
$new;
    }

    function 
form$instance ) {
        global 
$_wp_additional_image_sizes;

        
$types apply_filters('siteorigin_panels_gallery_types', array());

        
$instance wp_parse_args($instance, array(
            
'ids' => '',
            
'size' => apply_filters('siteorigin_panels_gallery_default_size'''),
            
'type' => apply_filters('siteorigin_panels_gallery_default_type'''),
            
'columns' => 3,
            
'link' => '',

        ));

        
?>
        <p>
            <label for="<?php echo $this->get_field_id'ids' ?>"><?php _e'Gallery Images''siteorigin-panels' ?></label>
            <a href="#" onclick="return false;" class="so-gallery-widget-select-attachments hidden"><?php _e('edit gallery''siteorigin-panels'?></a>
            <input type="text" class="widefat" value="<?php echo esc_attr($instance['ids']) ?>" name="<?php echo $this->get_field_name('ids'?>" />
        </p>
        <p class="description">
            <?php _e("Comma separated attachment IDs. Defaults to all current page's attachments."'siteorigin-panels'?>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id'size' ?>"><?php _e'Image Size''siteorigin-panels' ?></label>
            <select name="<?php echo $this->get_field_name'size' ?>" id="<?php echo $this->get_field_id'size' ?>">
                <option value="" <?php selected(empty($instance['size'])) ?>><?php esc_html_e('Default''siteorigin-panels'?></option>
                <option value="large" <?php selected('large'$instance['size']) ?>><?php esc_html_e'Large''siteorigin-panels' ?></option>
                <option value="medium" <?php selected('medium'$instance['size']) ?>><?php esc_html_e'Medium''siteorigin-panels' ?></option>
                <option value="thumbnail" <?php selected('thumbnail'$instance['size']) ?>><?php esc_html_e'Thumbnail''siteorigin-panels' ?></option>
                <option value="full" <?php selected('full'$instance['size']) ?>><?php esc_html_e'Full''siteorigin-panels' ?></option>
                <?php if(!empty($_wp_additional_image_sizes)) : foreach ( $_wp_additional_image_sizes as $name => $info ) : ?>
                    <option value="<?php echo esc_attr$name ?><?php selected($name$instance['size']) ?>><?php echo esc_html$name ?></option>
                <?php endforeach; endif; ?>
            </select>
        </p>

        <p>
            <label for="<?php echo $this->get_field_id'type' ?>"><?php _e'Gallery Type''siteorigin-panels' ?></label>
            <input type="text" class="regular" value="<?php echo esc_attr($instance['type']) ?>" name="<?php echo $this->get_field_name('type'?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id'columns' ?>"><?php _e'Columns''siteorigin-panels' ?></label>
            <input type="text" class="regular" value="<?php echo esc_attr($instance['columns']) ?>" name="<?php echo $this->get_field_name('columns'?>" />
        </p>

        <p>
            <label for="<?php echo $this->get_field_id'link' ?>"><?php _e'Link To''siteorigin-panels' ?></label>
            <select name="<?php echo $this->get_field_name'link' ?>" id="<?php echo $this->get_field_id'link' ?>">
                <option value="" <?php selected(''$instance['link']) ?>><?php esc_html_e('Attachment Page''siteorigin-panels'?></option>
                <option value="file" <?php selected('file'$instance['link']) ?>><?php esc_html_e('File''siteorigin-panels'?></option>
                <option value="none" <?php selected('none'$instance['link']) ?>><?php esc_html_e('None''siteorigin-panels'?></option>
            </select>
        </p>

    <?php
    
}
}

/**
 * An image widget
 *
 * Class SiteOrigin_Panels_Widgets_Image
 */
class SiteOrigin_Panels_Widgets_Image extends WP_Widget {
    function 
__construct() {
        
parent::__construct(
            
'siteorigin-panels-image',
            
__'Image (PB)''siteorigin-panels' ),
            array(
                
'description' => __'Displays a simple image.''siteorigin-panels' ),
            )
        );
    }

    
/**
     * @param array $args
     * @param array $instance
     */
    
function widget$args$instance ) {
        echo 
$args['before_widget'];
        if(!empty(
$instance['href'])) echo '<a href="' $instance['href'] . '">';
        echo 
'<img src="'.esc_url($instance['src']).'" />';
        if(!empty(
$instance['href'])) echo '</a>';
        echo 
$args['after_widget'];
    }

    function 
update($new$old){
        
$new wp_parse_args($new, array(
            
'src' => '',
            
'href' => '',
        ));
        return 
$new;
    }

    function 
form$instance ) {
        
$instance wp_parse_args($instance, array(
            
'src' => '',
            
'href' => '',
        ));

        
?>
        <p>
            <label for="<?php echo $this->get_field_id'src' ?>"><?php _e'Image URL''siteorigin-panels' ?></label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id'src' ?>" name="<?php echo $this->get_field_name'src' ?>" value="<?php echo esc_attr($instance['src']) ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id'href' ?>"><?php _e'Destination URL''siteorigin-panels' ?></label>
            <input type="text" class="widefat" id="<?php echo $this->get_field_id'href' ?>" name="<?php echo $this->get_field_name'href' ?>" value="<?php echo esc_attr($instance['href']) ?>" />
        </p>
    <?php
    
}
}

/**
 * A widget that lets you embed video.
 */
class SiteOrigin_Panels_Widgets_EmbeddedVideo extends WP_Widget {
    function 
__construct() {
        
parent::__construct(
            
'siteorigin-panels-embedded-video',
            
__'Embedded Video (PB)''siteorigin-panels' ),
            array(
                
'description' => __'Embeds a video.''siteorigin-panels' ),
            )
        );
    }

    
/**
     * Display the video using
     *
     * @param array $args
     * @param array $instance
     */
    
function widget$args$instance ) {
        
$embed = new WP_Embed();

        if(!
wp_script_is('fitvids'))
            
wp_enqueue_script('fitvids'plugin_dir_url__FILE__ ) . 'js/jquery.fitvids.js', array('jquery'), SITEORIGIN_PANELS_VERSION);

        if(!
wp_script_is('siteorigin-panels-embedded-video'))
            
wp_enqueue_script('siteorigin-panels-embedded-video'plugin_dir_url__FILE__ ).'js/embedded-video.js', array('jquery''fitvids'), SITEORIGIN_PANELS_VERSION);

        echo 
$args['before_widget'];
        
?><div class="siteorigin-fitvids"><?php echo $embed->run_shortcode'[embed]' $instance['video'] . '[/embed]' ?></div><?php
        
echo $args['after_widget'];
    }

    
/**
     * Display the embedded video form.
     *
     * @param array $instance
     * @return string|void
     */
    
function form$instance ) {
        
$instance wp_parse_args$instance, array(
            
'video' => '',
        ) );

        
?>
        <p>
            <label for="<?php echo $this->get_field_id'video' ?>"><?php _e'Video''siteorigin-panels' ?></label>
            <input type="text" class="widefat" name="<?php echo $this->get_field_name'video' ?>" id="<?php echo $this->get_field_id'video' ?>" value="<?php echo esc_attr$instance['video'] ) ?>" />
        </p>
        <?php
    
}

    function 
update$new$old ) {
        
$new['video'] = str_replace'https://''http://'$new['video'] );
        return 
$new;
    }
}

class 
SiteOrigin_Panels_Widgets_Video extends WP_Widget {
    function 
__construct() {
        
parent::__construct(
            
'siteorigin-panels-video',
            
__'Self Hosted Video (PB)''siteorigin-panels' ),
            array(
                
'description' => __'A self hosted video player.''siteorigin-panels' ),
            )
        );
    }

    function 
widget$args$instance ) {
        if ( empty(
$instance['url']) ) return;
        if ( !
function_exists('wp_video_shortcode') ) return;

        
$instance wp_parse_args($instance, array(
            
'url' => '',
            
'poster' => '',
            
'autoplay' => false,
        ));

        echo 
$args['before_widget'];
        echo 
wp_video_shortcode( array(
            
'src' => $instance['url'],
            
'poster' => $instance['poster'],
            
'autoplay' => $instance['autoplay'],
        ) );
        echo 
$args['after_widget'];
    }

    function 
update$new$old ) {
        
$new['url'] = esc_url_raw$new['url'] );
        
$new['poster'] = esc_url_raw$new['poster'] );
        
$new['autoplay'] = !empty($new['autoplay']) ? 0;
        return 
$new;
    }

    function 
form$instance ) {
        
$instance wp_parse_args($instance, array(
            
'url' => '',
            
'poster' => '',
            
'skin' => 'siteorigin',
            
'ratio' => 1.777,
            
'autoplay' => false,
        ));

        
?>
        <p>
            <label for="<?php echo $this->get_field_id('url'?>"><?php _e('Video URL''siteorigin-panels'?></label>
            <input id="<?php echo $this->get_field_id('url'?>" name="<?php echo $this->get_field_name('url'?>" type="text" class="widefat" value="<?php echo esc_attr($instance['url']) ?>" />
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('poster'?>"><?php _e('Poster URL''siteorigin-panels'?></label>
            <input id="<?php echo $this->get_field_id('poster'?>" name="<?php echo $this->get_field_name('poster'?>" type="text" class="widefat" value="<?php echo esc_attr($instance['poster']) ?>" />
            <small class="description"><?php _e('An image that displays before the video starts playing.''siteorigin-panels'?></small>
        </p>
        <p>
            <label for="<?php echo $this->get_field_id('autoplay'?>">
                <input id="<?php echo $this->get_field_id('autoplay'?>" name="<?php echo $this->get_field_name('autoplay'?>" type="checkbox" value="1" <?php checked($instance['autoplay']) ?> />
                <?php _e('Auto Play Video''siteorigin-panels'?>
            </label>
        </p>
    <?php
    
}
}

/**
 * A shortcode for self hosted video.
 *
 * @param array $atts
 * @return string
 */
function siteorigin_panels_video_shortcode($atts){
    
/**
     * @var string $url
     * @var string $poster
     * @var string $skin
     */
    
$instance shortcode_atts( array(
        
'url' => '',
        
'src' => '',
        
'poster' => '',
        
'skin' => 'siteorigin',
        
'ratio' => 1.777,
        
'autoplay' => 0,
    ), 
$atts );

    if(!empty(
$instance['src'])) $instance['url'] = $instance['src'];
    if(empty(
$instance['url'])) return;

    
ob_start();
    
the_widget('SiteOrigin_Panels_Widgets_Video'$instance);
    return 
ob_get_clean();

}
add_shortcode('self_video''siteorigin_panels_video_shortcode');

/**
 * Register the widgets.
 */
function siteorigin_panels_widgets_init(){
    
register_widget('SiteOrigin_Panels_Widgets_Gallery');
    
register_widget('SiteOrigin_Panels_Widgets_Image');
    
register_widget('SiteOrigin_Panels_Widgets_EmbeddedVideo');
    
register_widget('SiteOrigin_Panels_Widgets_Video');
}
add_action('widgets_init''siteorigin_panels_widgets_init');