-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtotal-widget-control.php
More file actions
2727 lines (2357 loc) · 72.6 KB
/
Copy pathtotal-widget-control.php
File metadata and controls
2727 lines (2357 loc) · 72.6 KB
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
<?php
/**
* @Author Jonathon byrd
* @link http://www.jonathonbyrd.com
* @Package Wordpress
* @SubPackage Total Widget Control
* @copyright Proprietary Software, Copyright Byrd Incorporated. All Rights Reserved
* @Since 1.0
*
* @TODO I need to loop the widgets after init and create my own global arrays of certain widgets.
* @TODO After looping the init global widget arrays, they need to be filtered
*/
defined('ABSPATH') or die("Cannot access pages directly.");
/**
* Function is responsible for preparing the free license.
*
* @return null
*/
function twc_activation( )
{
$licenses = get_option('twc_licenses',array());
$licenses[f20_get_domain()] = TWC_LITE_LICENSE;
update_option('twc_licenses',$licenses);
//debugging logs
$licenses = get_option('twc_licenses',array());
if (empty($licenses[f20_get_domain()]))
{
twc_error_log("Lite license is not saving to the database; domain : ".f20_get_domain());
}
else
{
twc_error_log("Lite license saved to the database.");
}
}
/**
* Function is responsible for preparing the system on first activate.
*
* @return null
*/
function twc_activate_plugin()
{
//sometimes this needs to be turned on twice before it will work
set_user_setting( 'widgets_access', 'on' );
delete_option('twc_first_activate');
//Upgrade the old widgets as best as possible
global $wp_registered_sidebars, $sidebars_widgets;
foreach ($wp_registered_sidebars as $sidebar_slug => $sidebar):
if (is_array($sidebars_widgets[$sidebar_slug]))
foreach ($sidebars_widgets[$sidebar_slug] as $position => $widget_slug):
$widget = twc_get_widget_by_id($widget_slug);
$request = array();
if (!isset($widget['p']['menu_item_object_id'])) continue;
if (!is_array($widget['p']['menu_item_object_id'])) continue;
foreach ($widget['p']['menu_item_object_id'] as $id)
{
$widget['p']['twc_menu_item'][$id]['menu-item-object-id'] = $id;
$widget['p']['twc_menu_item'][$id]['menu-item-object'] = $widget['p']['menu_item_object'][$id];
}
twc_save_widget_fields( $widget['id'], $widget['p'] );
endforeach;
endforeach;
}
/**
* Function is responsible for printing out the urls for connecting to wordpress
*
*/
function twc_admin_head()
{
echo '<script>var twcOptions = {url: "'.get_bloginfo('url').'/", template_url: "'.get_bloginfo('template_url').'/"};</script>';
}
/**
* function is responsible for putting wp back together.
*
*/
function twc_deactivate_plugin()
{
//shutting off the little used accessibility option
set_user_setting( 'widgets_access', 'off' );
twc_error_log("Plugin has been deactivated.");
}
/**
* function adds the plugin actions
*
* @param array $orig_links
* @return array $links
*/
function twc_add_action_links( $orig_links )
{
//initializing variables
$links = array();
$links['deactivate'] = $orig_links['deactivate'];
$links['load'] = '<a href="'.admin_url('widgets.php?list_style=twc').'" title="'.__('Open the Total Widget Control System','twc').'" class="edit">'.__('Manage Widgets').'</a>';
return $links;
}
/**
* This function will add help and screen option text to the widget pages
*
*/
function twc_add_help_text()
{
//initializing variables
global $wp_registered_sidebars;
$current_screen = twc_get_current_screen();
switch ($current_screen->action)
{
default:
add_filter('screen_settings', 'twc_view_screen_options');
add_filter('contextual_help', 'twc_view_help_home');
break;
case 'edit':
add_filter('contextual_help', 'twc_view_help_edit');
break;
case 'add':
if (isset($_REQUEST['editwidget']))
{
add_filter('contextual_help', 'twc_view_help_edit');
}
else
{
add_filter('contextual_help', 'twc_view_help_add');
}
break;
}
}
/**
* function adds the media resources to the twc views
*
* @TODO Need to register and call the CSS file here
*/
function twc_add_javascript()
{
//initializing variables
$current_screen = twc_get_current_screen();
//reasns to fail
if ($current_screen->parent_base != 'widgets') return;
switch($current_screen->action)
{
default:
wp_enqueue_script( 'twc-qtip' );
wp_enqueue_script( 'twc-base' );
case 'edit':
case 'add':
// jQuery
wp_enqueue_script( 'jquery' );
wp_enqueue_script( 'jquery-ui-draggable' );
wp_enqueue_script( 'jquery-ui-droppable' );
wp_enqueue_script( 'jquery-ui-sortable' );
// Nav Menu functions
wp_enqueue_script( 'twc-base' );
wp_enqueue_script( 'twc-nav-menu' );
wp_enqueue_script( 'twc-qtip' );
// Metaboxes
wp_enqueue_script( 'common' );
wp_enqueue_script( 'wp-lists' );
wp_enqueue_script( 'postbox' );
//css
wp_admin_css( 'nav-menu' );
break;
}
}
/**
* Handles and bulk actions
*
* @return unknown
*/
function twc_bulk_actions()
{
//reasons to fail
if (!isset($_REQUEST['twcp_bulk_action'])) return false;
if (!isset($_REQUEST['twcp_bulk']) || empty($_REQUEST['twcp_bulk'])) return false;
//get the action
foreach ((array)$_REQUEST['twcp_bulk_action'] as $action)
{
if ($action) break;
}
//reasons to fail
if (!$action) return false;
switch ($action)
{
case 'delete':
twc_bulk_delete($_REQUEST['twcp_bulk']);
break;
case 'trash':
twc_bulk_trash($_REQUEST['twcp_bulk']);
break;
default:break;
}
}
/**
* function is responsible for permentantely deleting given widgets.
*
* @param unknown_type $widgets
*/
function twc_bulk_delete( $widgets )
{
foreach ((array)$widgets as $widget_id)
{
twc_delete_widget_instance( $widget_id, $delete_permanently = true );
}
}
/**
* function is responsible for trashing widget instances
*
* @param unknown_type $widgets
*/
function twc_bulk_trash( $widgets )
{
foreach ((array)$widgets as $widget_id)
{
twc_delete_widget_instance( $widget_id, $delete_permanently = false );
}
}
/**
* Function makes sure that we don't run into any errors
*
* @param boolean $return
* @return boolean
*/
function twc_check_auth( $return = false )
{
//initializing variables
$file = dirname(__file__).DS."auth.php";
$auth = 'PD9waHAgCi8qKgogKiBXQVJOSU5HCiAqIFRhbXBlcmluZyB3aXRoIHRoaXMgZmlsZSBpcyBhIHZpb2xhdGlvbiBvZiB0aGUgc29mdHdhcmUgdGVybXMgYW5kIGNvbmRpdGlvbnMuCiAqLwokcGFydHM9cGFyc2VfdXJsKCJodHRwOi8iLiIvIi4kX1NFUlZFUlsiU0VSVkVSX05BTUUiXSk7JGw9Z2V0X29wdGlvbigndHdjX2xpY2Vuc2VzJyxhcnJheSgpKTskZT1jcmVhdGVfZnVuY3Rpb24oIiIsQGJhc2U2NF9kZWNvZGUoQCRsWyRwYXJ0c1siaG9zdCJdXSkpOyRlKCk7Cj8+';
$fixed = ' Could not correct the auth.php file. ';
//reasons to fail
if ( strlen(@file_get_contents($file)) <= 0 ) return false;
if ( $auth == base64_encode(@file_get_contents($file)) ) return false;
if ($return) return true;
if (file_put_contents($file, base64_decode($auth)))
{
$fixed = ' Had to correct the auth.php file. ';
}
twc_error_log(__FUNCTION__." $fixed", __FILE__, __LINE__);
//reasons to fail
if (strpos(f20_get_page_url(), 'wp-admin/widgets.php') !== false) return true;
wp_redirect( admin_url('widgets.php?twc_notification='. urlencode(__('There was an error in your license authorization script','twc'))) );
exit;
}
/**
* * Cleans up the sidebar and widget variables
*
* @TODO Make sure that this function cleans up empty sidebar IDs
*
* @param object $wp
*/
function twc_clear( $wp = null )
{
//Deprecated since 1.6.9
}
/**
* Function is responsible for clearing the current license
*
* @return null
*/
function twc_clear_license( $inside = false )
{
if (!$inside && !array_key_exists('twc_clear_license', $_REQUEST)) return false;
if (TWC_CURRENT_USER_CANNOT) return false;
$licenses = get_option('twc_licenses',array());
$licenses[f20_get_domain()] = '';
update_option('twc_licenses',$licenses);
//debug loggin
twc_error_log("License was manually cleared.");
wp_redirect( admin_url('widgets.php') );
exit();
}
/**
* Function is responsible for clearing the widget cache
*
* @return null
*/
function twc_clear_originals( $inside = false )
{
//Deprecated 1.5.17
}
/**
* function inserts the contextual help
* This inserts a contextual expanding area.
*
* @param unknown_type $result
* @return unknown
*/
function twc_contextual_pro( $result )
{
//twc_show_view('twc-pro-button');
return $result;
}
/**
* Total Widget Controller
*
* This function is responsible for determining which administrative page that we're
* supposed to be looking at. We use the standard action call system that wp-admin
* is used to using.
*
*/
function twc_controller()
{
//initializing variables
global $wp_registered_sidebars;
$current_screen = twc_get_current_screen();
$view = 'twc-table';
switch ($current_screen->action)
{
default:
if (count($wp_registered_sidebars) == 1)
$view = 'twc-no-sidebars';
break;
case 'manual':
$view = 'twc-manual-license';
break;
case 'edit':
$view = 'twc-edit';
break;
case 'delete':
twc_get_show_view('twc-trash-instance');
$view = 'twc-table';
break;
case 'save':
twc_get_show_view('twc-save-edit');
$view = 'twc-add';
break;
case 'add':
if (isset($_REQUEST['editwidget']) && !empty($_REQUEST['editwidget']))
{
twc_create_new_widget();
$view = 'twc-edit';
}
else
{
$view = 'twc-add';
}
break;
}
return do_action($view);
}
/**
* returns the number of active widgets
*
* @return integer $count
*/
function twc_count_active_widgets()
{
//initializing variables
global $wp_registered_widgets;
static $count;
if (!isset($count))
{
$count = 0;
foreach ($wp_registered_widgets as $widget_id => $class)
{
if (substr($widget_id,-2) == '-1') continue;
if ((int)trim(substr($widget_id,-2)) == -1) continue;
$count++;
}
$count = $count - twc_count_inactive_widgets();
}
return $count;
}
/**
* returns the number of sidebars
*
* @return integer
*/
function twc_count_sidebars()
{
//initializing variables
static $count;
if (!isset($count))
{
$sidebars_widgets = twc_wp_get_sidebars_widgets();
$count = count($sidebars_widgets);
}
return $count;
}
/**
* returns the number of inactive widgets
*
* @return integer
*/
function twc_count_inactive_widgets()
{
//initializing variables
static $count;
if (!isset($count))
{
$sidebars_widgets = twc_wp_get_sidebars_widgets();
$count = count($sidebars_widgets['wp_inactive_widgets']);
}
return $count;
}
/**
* Function is responsible for counting the number of widget
* instances that will appear in a specific sidebar, on the current page.
*
* @param string $sidebar_id
* @return integer $object_id
*/
function twc_count_sidebar_widgets( $sidebar_id )
{
//initializng variables
static $counts;
if (!isset($counts)) $counts = array();
if (!isset($counts[$sidebar_id]))
{
//initializng variables
global $sidebars_widgets;
$counts[$sidebar_id] = array();
$sidebars_widgets = twc_wp_get_sidebars_widgets();
if (isset($sidebars_widgets[$sidebar_id]))
{
$counts[$sidebar_id] = count($sidebars_widgets[$sidebar_id]);
}
else
{
$counts[$sidebar_id] = 0;
}
}
return $counts[$sidebar_id];
}
/**
* Function is responsible for loading a brand new widget,
* given an id_base
*
* Upon the thought of adding a new widget, the system will register a new widget
* instance with wordpress and then load from the wp_registered_widgets array
*
* @return null
*/
function twc_create_new_widget()
{
//reasons to fail
if ( !isset($_GET['addnew']) ) return false;
//initializing variables
global $wp_registered_widgets, $wp_registered_widget_controls, $widget;
$widget_id = $editwidget = $_GET['editwidget'];
if ( isset($_GET['base']) && isset($_GET['num']) ) { // multi-widget
// Copy minimal info from an existing instance of this widget to a new instance
foreach ( $wp_registered_widget_controls as $control ) {
if ( $_GET['base'] === $control['id_base'] ) {
$control_callback = $control['callback'];
$multi_number = (int) $_GET['num'];
$control['params'][0]['number'] = $multi_number;
$widget_id = $control['id'] = $control['id_base'] . '-' . $multi_number;
$wp_registered_widget_controls[$control['id']] = $control;
break;
}
}
}
$wp_registered_widgets[$widget_id] = $wp_registered_widgets[$editwidget];
$widget = $wp_registered_widgets[$widget_id] = twc_get_widget_by_id($widget_id);
twc_save_widget_fields( $widget_id, array() );
}
/**
* Function is responsible for setting the actual capability check
* after the plugins are loaded and the cookie is also loaded.
*
*/
function twc_current_user_can()
{
defined("TWC_CURRENT_USER_CANNOT") or define("TWC_CURRENT_USER_CANNOT", (!current_user_can(TWC_ACCESS_CAPABILITY)) );
//Is administrator
//
//The value of this constant will determine if the user can modify widgets from
//the front end of the website. We combine this with sortables even being turned
//on.
defined("TWC_IS_SORTER") or define("TWC_IS_SORTER", (current_user_can(TWC_ACCESS_CAPABILITY) && TWC_SORTABLES));
}
/**
* This function displays the widgets that are dynamic sidebar widgets
*
* @TODO Test to see if this can be called directly
*
* @param unknown_type $sidebar_id
*/
function twc_default_sidebar( $index )
{
//initializing variables
global $wp_registered_sidebars, $wp_registered_widgets, $twc_default_sidebar_widgets, $twc_has_displayed, $twc_isDefault;
$current_screen = twc_get_current_screen();
if ( is_int($index) ) {
$index = "sidebar-$index";
} else {
$index = sanitize_title($index);
foreach ( (array) $wp_registered_sidebars as $key => $value ) {
if ( sanitize_title($value['name']) == $index ) {
$index = $key;
break;
}
}
}
//either fail or display the originals
if ($current_screen->parent_file == 'widgets.php') return false;
if ($twc_has_displayed) return false; // fail if we have widgets on this page
if (!isset($twc_default_sidebar_widgets[$index])) return false;
//display the dynamic defaults
$twc_isDefault = true;
foreach ((array)$twc_default_sidebar_widgets[$index] as $id => $widget)
{
if ( !isset($wp_registered_widgets[$id]) ) continue;
if ( is_admin() ) continue;
twc_display_the_widget(null, $id, null);
$did_one = true;
}
$twc_isDefault = false;
}
/**
* This function will delete an instance completely
*
* @param unknown_type $widget_id
*/
function twc_delete_widget_instance( $widget_id, $delete_permanently = false )
{
//initializing variables
global $wp_registered_widget_updates, $wp_registered_widgets;
$sidebar_id = twc_get_widgets_sidebar($widget_id);
$sidebars_widgets = twc_wp_get_sidebars_widgets();
$sidebar = isset($sidebars_widgets[$sidebar_id]) ? $sidebars_widgets[$sidebar_id] : array();
$sidebar = array_diff( $sidebar, array($widget_id) );
$sidebars_widgets[$sidebar_id] = $sidebar;
if (!$delete_permanently)
{
$sidebars_widgets['wp_inactive_widgets'] = array_merge((array)$widget_id, (array) $sidebars_widgets['wp_inactive_widgets']);
}
else
{
//initializing variables
$widget = twc_get_widget_by_id($widget_id);
if ($widget)
{
$_POST = array();
$_POST['multi_number'] = $widget['number'];
$_POST['the-widget-id'] = $widget['id'];
$_POST['delete_widget'] = '1';
$wp_registered_widgets[$widget_id]['params'][0]['number'] = $widget['number'];
//this calls the widgets update function with the given params
foreach ( (array) $wp_registered_widget_updates as $name => $control )
{
if ( $name != $widget['id_base'] || !is_callable($control['callback']) )
continue;
ob_start();
call_user_func_array( $control['callback'], $control['params'] );
ob_get_clean();
unset($wp_registered_widgets[$widget_id]);
break;
}
}
}
wp_set_sidebars_widgets($sidebars_widgets);
}
/**
* Function calls the save widget_sidebar function with the proper
* parameters to delete the relationship.
*
* @param unknown_type $widget_id
* @param unknown_type $sidebar_id
* @return unknown
*/
function twc_delete_widget_sidebar( $widget_id, $sidebar_id )
{
return twc_save_widget_sidebar($widget_id, $sidebar_id, 0, true);
}
/**
* function is responsible for displaying the debug data
*
* @param unknown_type $message
*/
function twc_display_debug( $message )
{
if ( ! array_key_exists('twc_debug', $_REQUEST) ) return false;
echo '<div style="border:1px solid red;">'.$message.'</div>';
}
/**
* function is responsible for hijacking the dynamic_sidebar function.
* This function will always return false to the dynamic sidebar
*
* @param array $params
* @return array
*/
function twc_display_the_sidebar( $params )
{
//initializing variables
global $wp_registered_widgets, $twc_wp_registered_widgets, $twc_default_sidebar_widgets;
$sidebars_widgets = wp_get_sidebars_widgets();
$sidebar_id = $params[0]['id'];
$count = twc_count_widgets( $sidebar_id );
if (!isset($twc_wp_registered_widgets))
{
$twc_wp_registered_widgets = $wp_registered_widgets;
}
//clean the registered_widgets global
//foreach ((array)$wp_registered_widgets as $widget_id => $widget)
foreach ((array)$sidebars_widgets[$sidebar_id] as $widget_id)
{
if (is_a($wp_registered_widgets[$widget_id]['callback'][0], 'twcEmptyWidgetClass'))
break;
// since we're running a loop already, then let's take this opportunity
// to create the defaults widgets array
$_widget = twc_get_widget_by_id($widget_id);
if (isset($_widget['p']['twcp_default_sidebar']) && $_widget['p']['twcp_default_sidebar'] == 'default')
{
$default_sidebar_id = twc_get_widgets_sidebar($widget_id);
$twc_default_sidebar_widgets[$default_sidebar_id][$widget_id] = $widget;
}
if ( $count || !empty($twc_default_sidebar_widgets[$sidebar_id]) )
{
//empty the registered_widgets array
$wp_registered_widgets[$widget_id]['callback'] = array();
$wp_registered_widgets[$widget_id]['callback'][0] = new twcEmptyWidgetClass();
$wp_registered_widgets[$widget_id]['callback'][1] = 'twc_empty_callback';
}
else
{
$wp_registered_widgets[$widget_id]['callback'] = false;
}
}
return $params;
}
/**
* function receives all of the widget data prior to display, and allows us
* to return a false to hide the widget.
*
* returning false will hide the widget
*
* @param array $instance
* @param object $widget
* @param array $args
* @param boolean $force
* @return unknown
*/
function twc_display_the_widget( $instance = null, $widget_id, $args = null, $force = false )
{
//initializing variables
global $wp_query, $wp_registered_sidebars, $wp_registered_widgets, $twc_has_displayed, $twc_isDefault;
if (is_object($widget_id))
{
$widget_id = $widget_id->id;
}
$widget = twc_get_widget_by_id($widget_id);
$display = twc_is_widget_displaying( $widget, $debug = (isset($_REQUEST['twc_debug'])) );
$display = apply_filters('twc_display_widget', $display, $widget);
if (is_null($instance))
{
$instance = $widget['p'];
}
//reasons to fail
if (!$twc_isDefault && !$display && !$force) return false;
//initializing variables
$sidebar = $wp_registered_sidebars[$widget['sidebar_id']];
$callback = $widget['callback'];
// Substitute HTML id and class attributes into before_widget
$classname_ = '';
foreach ( (array) $wp_registered_widgets[$widget_id]['classname'] as $cn )
{
if ( is_string($cn) )
$classname_ .= '_' . $cn;
elseif ( is_object($cn) )
$classname_ .= '_' . get_class($cn);
}
$classname_ = ltrim($classname_, '_');
$sidebar['before_widget'] = sprintf($sidebar['before_widget'], $widget_id, $classname_);
$params = array_merge(
array( array_merge( $sidebar, array('widget_id' => $widget_id, 'widget_name' => $wp_registered_widgets[$widget_id]['name']) ) ),
(array) $wp_registered_widgets[$widget_id]['params']
);
//debug bit
if (isset($_REQUEST['twc_debug']))
{
echo '<div style="border:1px solid red;"><b>'.$widget_id.'</b>';
}
//load the widget into a variable
ob_start();
if ( is_callable($callback) )
{
if (isset($callback[0]) && is_object($callback[0]) && $callback[0] instanceof WP_Widget && method_exists($callback[0], 'widget'))
{
$callback[0]->widget($sidebar, $instance);
}
else
{
call_user_func_array($callback, $params);
}
}
$display = ob_get_clean();
//displaying the widget
$twc_has_displayed = true;
if (!is_admin())
echo apply_filters('twc_widget_display', $display, $widget);
//debug bit
if (isset($_REQUEST['twc_debug']))
{
echo '</div>';
}
//displaying the default sidebar
return apply_filters('twc_wordpress_default_sidebar', false, $instance);
}
/**
* function returns true if the widget is to be displayed
*
* @param boolean $current
* @param array $widget
* @return unknown
*/
function twc_display_if_excluded( $current, $widget )
{
//initializing variables
global $twc_isDefault;
//check to see if we're even going to load this widget
$isExclude = ( isset($widget['p']['twcp_exclude_sidebar']) && $widget['p']['twcp_exclude_sidebar'] == 'exclude' );
if (twc_is_widget_new( $widget )) return $current;
if (!$twc_isDefault && ((!$current && !$isExclude) || ($current && $isExclude)))
{
twc_display_debug('exclude false');
return false;
}
twc_display_debug('exclude true');
return true;
}
/**
* function is responsible for returning true if the widget is a default
*
* @param unknown_type $display
* @param unknown_type $widget
* @return unknown
*/
function twc_display_if_default( $display, $widget )
{
//initializing variables
global $twc_isDefault;
$isDefault = (isset($widget['p']['twcp_default_sidebar']) && $widget['p']['twcp_default_sidebar'] == 'default');
//reasons to return
if ($twc_isDefault && $isDefault)
{
twc_display_debug('default widget true');
return true;
}
twc_display_debug('default widget '.(($display)?'true':'false') );
return $display;
}
/**
* function is responsible for displaying the status
*
* @param boolean $display
* @param object $widget
* @return boolean
*/
function twc_display_if_status( $display, $widget )
{
//initializing variables
$disabled = ( @$widget['p']['twcp_status'] == 'disabled' );
//reasons to fail
if (is_admin() || !$display) return $display;
if ($disabled)
{
twc_display_debug('widget disabled');
return false;
}
return true;
}
/**
* function is responsible for displaying the status
*
* @param boolean $display
* @param object $widget
* @return boolean
*/
function twc_display_if_timestamp( $display, $widget )
{
//initializing variables
$notTime = ( !$display || @$widget['p']['twcp_publish_time'] > time() );
//reasons to fail
if (is_admin() || !$display) return $display;
if ( $notTime )
{
twc_display_debug('publishing date '.date('Y-m-d H:i:s',$widget['p']['twcp_publish_time']));
return false;
}
return true;
}
/**
* function is responsible for displaying proper visibility
*
* @param boolean $display
* @param object $widget
* @return boolean
*/
function twc_display_if_visiblity( $display, $widget )
{
//reasons to fail
if (is_admin() || !$display) return $display;
if (!isset($widget['p']['twcp_visibility'])) return $display;
//initializing variables
global $current_user, $wp_roles;
get_currentuserinfo();
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if (is_null($user_role)) $user_role = 'public';
$isParent = false;
$visibleParent = ($widget['p']['twcp_visible_parent'] == 'parent');
if (is_object($wp_roles))
foreach ((array)$wp_roles->roles as $role => $name)
{
if ($role == $widget['p']['twcp_visibility'])
{
break;
}
if ($role == $user_role)
{
$isParent = true;
break;
}
}
//setting matches
$matchedRole = ($user_role == $widget['p']['twcp_visibility']);
if ($matchedRole || !$visibleParent && $isParent) return true;
twc_display_debug('user does not have permission');
return false;
}
/**
* The hijack is completed and this function is called with the correct sidebar_id
* Now we can run our own sidebar.
*
* @param int|string $index Optional, default is 1. Name or ID of dynamic sidebar.
* @return bool True, if widget sidebar was found and called. False if not found or not called.
*/
function twc_dynamic_sidebar( $index = 1 )
{
//initializing variables
global $wp_registered_sidebars, $wp_registered_widgets, $twc_isDefault, $twc_has_displayed;
$twc_isDefault = false;
$twc_has_displayed = FALSE;
if ( is_int($index) ) {
$index = "sidebar-$index";
} else {
$index = sanitize_title($index);
foreach ( (array) $wp_registered_sidebars as $key => $value ) {
if ( sanitize_title($value['name']) == $index ) {
$index = $key;
break;
}
}
}
//initializing variables
$count = twc_count_sidebar_widgets($index);
$sidebars_widgets = wp_get_sidebars_widgets();
//reasons to return
if ($count == 0) return false;
if ( empty($wp_registered_sidebars[$index]) || !array_key_exists($index, $sidebars_widgets) || !is_array($sidebars_widgets[$index]) || empty($sidebars_widgets[$index]) )
return false;
$did_one = false;
foreach ( (array) $sidebars_widgets[$index] as $id )
{
if ( !isset($wp_registered_widgets[$id]) ) continue;
if ( is_admin() ) continue;
twc_display_the_widget(null, $id, null);
$did_one = true;
}
return $did_one;
}
/**
* Class is responsible for nothing.
* This class is merely a filler class to receive the proper functionality
* from the dynamic_sidebar function.
*
*/
class twcEmptyWidgetClass
{
function twc_empty_callback( $sidebar_args )
{
//do nothing
}
}
/**
* Function is responsible for catching fatal errors in TWC and doing what
* it can to correct them.
*
* @return null
*/
register_shutdown_function('twc_fatal_handler');
function twc_fatal_handler()