forked from scribu/wp-scb-framework
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Widget.php
124 lines (97 loc) · 2.61 KB
/
Widget.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
<?php
/**
* Adds compatibility methods between WP_Widget and scbForms.
*/
abstract class scbWidget extends WP_Widget {
/**
* Widget defaults.
* @var array
*/
protected $defaults = array();
/**
* Widgets to register.
* @var array
*/
private static $scb_widgets = array();
/**
* Initializes widget.
*
* @param string $class
* @param string $file (optional)
* @param string $base (optional)
*
* @return void
*/
public static function init( $class, $file = '', $base = '' ) {
self::$scb_widgets[] = $class;
add_action( 'widgets_init', array( __CLASS__, '_scb_register' ) );
// for auto-uninstall
if ( $file && $base && class_exists( 'scbOptions' ) ) {
new scbOptions( "widget_$base", $file );
}
}
/**
* Registers widgets.
*
* @return void
*/
public static function _scb_register() {
foreach ( self::$scb_widgets as $widget ) {
register_widget( $widget );
}
}
/**
* Displays widget content.
*
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
* @param array $instance The settings for the particular instance of the widget.
*
* @return void
*/
public function widget( $args, $instance ) {
$instance = wp_parse_args( $instance, $this->defaults );
extract( $args );
echo $before_widget;
$title = apply_filters( 'widget_title', isset( $instance['title'] ) ? $instance['title'] : '', $instance, $this->id_base );
if ( ! empty( $title ) ) {
echo $before_title . $title . $after_title;
}
$this->content( $instance );
echo $after_widget;
}
/**
* This is where the actual widget content goes.
*
* @param array $instance The settings for the particular instance of the widget.
*
* @return void
*/
protected function content( $instance ) { }
//_____HELPER METHODS_____
/**
* Generates a input form field.
*
* @param array $args
* @param array $formdata (optional)
*
* @return string
*/
protected function input( $args, $formdata = array() ) {
$prefix = array( 'widget-' . $this->id_base, $this->number );
$form = new scbForm( $formdata, $prefix );
// Add default class
if ( ! isset( $args['extra'] ) && 'text' == $args['type'] ) {
$args['extra'] = array( 'class' => 'widefat' );
}
// Add default label position
if ( ! in_array( $args['type'], array( 'checkbox', 'radio' ) ) && empty( $args['desc_pos'] ) ) {
$args['desc_pos'] = 'before';
}
$name = $args['name'];
if ( ! is_array( $name ) && '[]' == substr( $name, -2 ) ) {
$name = array( substr( $name, 0, -2 ), '' );
}
$args['name'] = $name;
return $form->input( $args );
}
}