class-subscribe-and-connect-widget.php
7.62 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
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly.
/**
* WooThemes Subscribe & Connect Widget
*
* A WooThemes standardized Subscribe & Connect widget.
*
* @package WordPress
* @category Widgets
* @author WooThemes
* @since 1.0.0
*
* TABLE OF CONTENTS
*
* protected $subscribe_and_connect_widget_cssclass
* protected $subscribe_and_connect_widget_description
* protected $subscribe_and_connect_widget_idbase
* protected $subscribe_and_connect_widget_title
*
* - __construct()
* - widget()
* - form()
*/
class Subscribe_And_Connect_Widget extends WP_Widget {
protected $subscribe_and_connect_widget_cssclass;
protected $subscribe_and_connect_widget_description;
protected $subscribe_and_connect_widget_idbase;
protected $subscribe_and_connect_widget_title;
/**
* Constructor function.
* @since 1.0.0
* @return void
*/
public function __construct() {
/* Widget variable settings. */
$this->subscribe_and_connect_widget_cssclass = 'widget_subscribe_and_connect_items';
$this->subscribe_and_connect_widget_description = __( 'Subscribe & Connect.', 'subscribe-and-connect' );
$this->subscribe_and_connect_widget_idbase = 'subscribe-and-connect';
$this->subscribe_and_connect_widget_title = __( 'Subscribe & Connect', 'subscribe-and-connect' );
// Cache
add_action( 'save_post', array( $this, 'flush_widget_cache' ) );
add_action( 'deleted_post', array( $this, 'flush_widget_cache' ) );
add_action( 'switch_theme', array( $this, 'flush_widget_cache' ) );
/* Widget settings. */
$widget_ops = array(
'classname' => $this->subscribe_and_connect_widget_cssclass,
'description' => $this->subscribe_and_connect_widget_description
);
/* Widget control settings. */
$control_ops = array(
'id_base' => $this->subscribe_and_connect_widget_idbase
);
/* Create the widget. */
parent::__construct( $this->subscribe_and_connect_widget_idbase, $this->subscribe_and_connect_widget_title, $widget_ops, $control_ops );
} // End __construct()
/**
* Display the widget on the frontend.
* @since 1.0.0
* @param array $args Widget arguments.
* @param array $instance Widget settings for this instance.
* @return void
*/
public function widget( $args, $instance ) {
$cache = wp_cache_get( 'widget_subscribe_and_connect_items', 'widget' );
if ( !is_array($cache) )
$cache = array();
if ( ! isset( $args['widget_id'] ) )
$args['widget_id'] = $this->id;
if ( isset( $cache[ $args['widget_id'] ] ) ) {
echo $cache[ $args['widget_id'] ];
return;
}
if ( isset( $instance['description'] ) ) {
$args['description'] = $instance['description'];
} else {
$args['description'] = $args['description'];
}
ob_start();
extract( $args, EXTR_SKIP );
/* Our variables from the widget settings. */
$title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
$description = $args['description'];
$args = array();
/* Display the widget title if one was input (before and after defined by themes). */
if ( $title ) { $args['title'] = $title; }
/* Widget content. */
// Add actions for plugins/themes to hook onto.
do_action( $this->subscribe_and_connect_widget_cssclass . '_top' );
if ( isset( $instance['social'] ) ) { $args['social'] = $instance['social']; }
if ( isset( $instance['subscribe'] ) ) { $args['subscribe'] = $instance['subscribe']; }
// Display S&C.
echo $before_widget;
if ( $title )
echo $before_title . $title . $after_title;
echo '<div class="subscribe-and-connect-connect">';
echo $description;
if ( true == $instance['subscribe'] ) {
subscribe_and_connect_subscribe();
}
if ( true == $instance['social'] ) {
subscribe_and_connect_connect();
}
echo '</div><!--/.subscribe-and-connect-connect-->';
echo $after_widget;
// Add actions for plugins/themes to hook onto.
do_action( $this->subscribe_and_connect_widget_cssclass . '_bottom' );
$cache[ $widget_id ] = ob_get_flush();
wp_cache_set( 'widget_subscribe_and_connect_items', $cache, 'widget' );
} // End widget()
/**
* Method to update the settings from the form() method.
* @since 1.0.0
* @param array $new_instance New settings.
* @param array $old_instance Previous settings.
* @return array Updated settings.
*/
public function update ( $new_instance, $old_instance ) {
$instance = $old_instance;
/* Strip tags for title and name to remove HTML (important for text inputs). */
$instance['title'] = strip_tags( $new_instance['title'] );
$instance['description'] = wp_kses( $new_instance['description'] );
/* Escape checkbox values */
$instance['social'] = esc_attr( $new_instance['social'] );
$instance['subscribe'] = esc_attr( $new_instance['subscribe'] );
/* Flush cache. */
$this->flush_widget_cache();
$alloptions = wp_cache_get( 'alloptions', 'options' );
if ( isset($alloptions['widget_woothemes_wc_items']) )
delete_option( 'widget_woothemes_wc_items' );
return $instance;
} // End update()
/**
* The form on the widget control in the widget administration area.
* Make use of the get_field_id() and get_field_name() function when creating your form elements. This handles the confusing stuff.
* @since 1.0.0
* @param array $instance The settings for this instance.
* @return void
*/
public function form( $instance ) {
global $subscribe_and_connect;
$settings = $subscribe_and_connect->get_settings();
/* Set up some default widget settings. */
/* Make sure all keys are added here, even with empty string values. */
$defaults = array(
'title' => __( 'Subscribe & Connect', 'subscribe-and-connect' ),
'description' => $settings['general']['text'],
'social' => 0,
'subscribe' => 0,
);
$instance = wp_parse_args( (array) $instance, $defaults );
?>
<!-- Widget Title: Text Input -->
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title (optional):', 'subscribe-and-connect' ); ?></label>
<input type="text" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'description' ); ?>"><?php _e( 'Description (optional):', 'subscribe-and-connect' ); ?></label>
<textarea type="text" name="<?php echo $this->get_field_name( 'description' ); ?>" rows="4" class="widefat" id="<?php echo $this->get_field_id( 'description' ); ?>"><?php echo $instance['description']; ?></textarea>
</p>
<!-- Widget Show Subscribe: Checkbox Input -->
<p>
<input id="<?php echo $this->get_field_id( 'subscribe' ); ?>" name="<?php echo $this->get_field_name( 'subscribe' ); ?>" type="checkbox" value="1" <?php checked( '1', $instance['subscribe'] ); ?> />
<label for="<?php echo $this->get_field_id( 'subscribe' ); ?>"><?php _e( 'Display subscribe form', 'subscribe-and-connect' ); ?></label>
</p>
<!-- Widget Show Social: Checkbox Input -->
<p>
<input id="<?php echo $this->get_field_id( 'social' ); ?>" name="<?php echo $this->get_field_name( 'social' ); ?>" type="checkbox" value="1" <?php checked( '1', $instance['social'] ); ?> />
<label for="<?php echo $this->get_field_id( 'social' ); ?>"><?php _e( 'Display social icons', 'subscribe-and-connect' ); ?></label>
</p>
<?php
} // End form()
/**
* Flush widget cache
* @since 1.0.0
* @return void
*/
public function flush_widget_cache() {
wp_cache_delete( 'widget_subscribe_and_connect_items', 'widget' );
}
} // End Class
/* Register the widget. */
add_action( 'widgets_init', create_function( '', 'return register_widget( "Subscribe_And_Connect_Widget" );' ), 1 );