em-events.php
11.7 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
<?php
/**
* @author marcus
* Standard events list widget
*/
class EM_Widget extends WP_Widget {
var $defaults;
public static function init(){
return register_widget("EM_Widget");
}
/** constructor */
function __construct() {
$this->defaults = array(
'title' => __('Events','events-manager'),
'scope' => 'future',
'order' => 'ASC',
'limit' => 5,
'category' => 0,
'format_header' => '',
'format' => EM_Formats::dbem_block_event_list_item_format(''),
'format_footer' => '',
'nolistwrap' => false,
'orderby' => 'event_start_date,event_start_time,event_name',
'all_events' => 0,
'all_events_text' => __('all events', 'events-manager'),
'no_events_text' => '<div class="em-list-no-items">'.__('No events', 'events-manager').'</div>',
'v6' => false,
);
$this->em_orderby_options = apply_filters('em_settings_events_default_orderby_ddm', array(
'event_start_date,event_start_time,event_name' => __('start date, start time, event name','events-manager'),
'event_name,event_start_date,event_start_time' => __('name, start date, start time','events-manager'),
'event_name,event_end_date,event_end_time' => __('name, end date, end time','events-manager'),
'event_end_date,event_end_time,event_name' => __('end date, end time, event name','events-manager'),
));
$widget_ops = array('description' => __( "Display a list of events on Events Manager.", 'events-manager') );
parent::__construct(false, $name = 'Events', $widget_ops);
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
$instance = array_merge($this->defaults, $instance);
$instance = $this->fix_scope($instance); // depcreciate
echo $args['before_widget'];
if( !empty($instance['title']) ){
echo $args['before_title'];
echo apply_filters('widget_title',$instance['title'], $instance, $this->id_base);
echo $args['after_title'];
}
//remove owner searches
$instance['owner'] = false;
//legacy stuff - deal with unsaved pre-v6 items, v6 saved and preview modes
$v6 = EM_Options::get('v6', null);
//add li tags to old widgets that have no forced li wrappers
if( ($v6 && empty($instance['v6'])) || $v6 === 'p' || $v6 === 'p' ){
$instance = $this->get_v6_instance_options($instance);
}
//orderby fix for previous versions with old orderby values
if( !array_key_exists($instance['orderby'], $this->em_orderby_options) ){
//replace old values
$old_vals = array(
'name' => 'event_name',
'end_date' => 'event_end_date',
'start_date' => 'event_start_date',
'end_time' => 'event_end_time',
'start_time' => 'event_start_time'
);
foreach($old_vals as $old_val => $new_val){
$instance['orderby'] = str_replace($old_val, $new_val, $instance['orderby']);
}
}
//get events
$events = EM_Events::get(apply_filters('em_widget_events_get_args',$instance));
//output events
echo '<div class="'. implode(' ', em_get_template_classes('events-widget')) .'">';
echo $instance['format_header'];
if ( count($events) > 0 ){
foreach($events as $event){
echo $event->output( $instance['format'] );
}
}else{
echo $instance['no_events_text'];
}
if ( !empty($instance['all_events']) ){
$events_link = (!empty($instance['all_events_text'])) ? em_get_link($instance['all_events_text']) : em_get_link(__('all events','events-manager'));
echo '<li class="all-events-link">'.$events_link.'</li>';
}
echo $instance['format_footer'];
echo '</div>';
echo $args['after_widget'];
}
function get_v6_instance_options( $instance ){
$instance['format_header'] = '';
$instance['format'] = EM_Formats::block_event_list_item_format('');
$instance['format_footer'] = '';
return $instance;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
foreach($this->defaults as $key => $value){
if( !isset($new_instance[$key]) ){
$new_instance[$key] = $value;
}
//balance tags and sanitize output formats
if( in_array($key, array('format', 'no_events_text', 'all_events_text')) ){
if( is_multisite() && !em_wp_is_super_admin() ) $new_instance[$key] = wp_kses_post($new_instance[$key]); //for multisite
$new_instance[$key] = force_balance_tags($new_instance[$key]);
}
}
$new_instance['v6'] = EM_Options::get('v6', false);
return $new_instance;
}
/** @see WP_Widget::form */
function form($instance) {
$instance = array_merge($this->defaults, $instance);
// fix legacy stuff
$instance = $this->fix_scope($instance); // depcreciate
$v6 = EM_Options::get('v6', null);
if( ($v6 === true || $v6 === 'undo') && empty($instance['v6']) ){
$instance = $this->get_v6_instance_options($instance);
}elseif( empty($instance['v6']) && $instance['format'] !== $this->defaults['format'] ) {
// still unmigrated, not saved either during v6
$instance['format_header'] = '<ul>';
$instance['format_footer'] = '</ul>';
if ( !preg_match('/^<li/i', trim($instance['format'])) ) $instance['format'] = '<li>'. $instance['format'] .'</li>';
if (!preg_match('/^<li/i', trim($instance['no_events_text'])) ) $instance['no_events_text'] = '<li class="no-events">'.$instance['no_events_text'].'</li>';
}
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php esc_html_e('Title', 'events-manager'); ?>: </label>
<input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo esc_attr($instance['title']); ?>" class="widefat" />
</p>
<p>
<label for="<?php echo $this->get_field_id('limit'); ?>"><?php esc_html_e('Number of events','events-manager'); ?>: </label>
<input type="text" id="<?php echo $this->get_field_id('limit'); ?>" name="<?php echo $this->get_field_name('limit'); ?>" size="3" value="<?php echo esc_attr($instance['limit']); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('scope'); ?>"><?php esc_html_e('Scope','events-manager'); ?>: </label><br/>
<select id="<?php echo $this->get_field_id('scope'); ?>" name="<?php echo $this->get_field_name('scope'); ?>" class="widefat" >
<?php foreach( em_get_scopes() as $key => $value) : ?>
<option value='<?php echo esc_attr($key); ?>' <?php echo ($key == $instance['scope']) ? "selected='selected'" : ''; ?>>
<?php echo esc_html($value); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('order'); ?>"><?php esc_html_e('Order By','events-manager'); ?>: </label>
<select id="<?php echo $this->get_field_id('orderby'); ?>" name="<?php echo $this->get_field_name('orderby'); ?>" class="widefat">
<?php foreach($this->em_orderby_options as $key => $value) : ?>
<option value='<?php echo esc_attr($key); ?>' <?php echo ( !empty($instance['orderby']) && $key == $instance['orderby']) ? "selected='selected'" : ''; ?>>
<?php echo esc_html($value); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('order'); ?>"><?php esc_html_e('Order','events-manager'); ?>: </label>
<select id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" class="widefat">
<?php
$order_options = apply_filters('em_widget_order_ddm', array(
'ASC' => __('Ascending','events-manager'),
'DESC' => __('Descending','events-manager')
));
?>
<?php foreach( $order_options as $key => $value) : ?>
<option value='<?php echo esc_attr($key); ?>' <?php echo ($key == $instance['order']) ? "selected='selected'" : ''; ?>>
<?php echo esc_html($value); ?>
</option>
<?php endforeach; ?>
</select>
</p>
<p>
<label for="<?php echo $this->get_field_id('category'); ?>"><?php esc_html_e('Category IDs','events-manager'); ?>: </label>
<input type="text" id="<?php echo $this->get_field_id('category'); ?>" class="widefat" name="<?php echo $this->get_field_name('category'); ?>" size="3" value="<?php echo esc_attr($instance['category']); ?>" /><br />
<em><?php esc_html_e('1,2,3 or 2 (0 = all)','events-manager'); ?> </em>
</p>
<p>
<label for="<?php echo $this->get_field_id('all_events'); ?>"><?php esc_html_e('Show all events link at bottom?','events-manager'); ?>: </label>
<input type="checkbox" id="<?php echo $this->get_field_id('all_events'); ?>" name="<?php echo $this->get_field_name('all_events'); ?>" <?php echo (!empty($instance['all_events']) && $instance['all_events']) ? 'checked':''; ?> class="widefat">
</p>
<p id="<?php echo $this->get_field_id('all_events'); ?>-section">
<label for="<?php echo $this->get_field_id('all_events'); ?>"><?php esc_html_e('All events link text?','events-manager'); ?>: </label>
<input type="text" id="<?php echo $this->get_field_id('all_events_text'); ?>" name="<?php echo $this->get_field_name('all_events_text'); ?>" value="<?php echo esc_attr( $instance['all_events_text'] ); ?>" >
</p>
<script type="text/javascript">
jQuery('#<?php echo $this->get_field_id('all_events'); ?>').on('change', function(){
if( this.checked ){
jQuery(this).parent().next().show();
}else{
jQuery(this).parent().next().hide();
}
}).trigger('change');
</script>
<p>
<label for="<?php echo $this->get_field_id('format_header'); ?>"><?php esc_html_e('List item header format','events-manager'); ?>: </label>
<textarea rows="5" cols="24" id="<?php echo $this->get_field_id('format_header'); ?>" name="<?php echo $this->get_field_name('format_header'); ?>" class="widefat"><?php echo esc_textarea($instance['format_header'] ); ?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id('format'); ?>"><?php esc_html_e('List item format','events-manager'); ?>: </label>
<textarea rows="5" cols="24" id="<?php echo $this->get_field_id('format'); ?>" name="<?php echo $this->get_field_name('format'); ?>" class="widefat"><?php echo esc_textarea($instance['format']); ?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id('format_footer'); ?>"><?php esc_html_e('List item footer format','events-manager'); ?>: </label>
<textarea rows="5" cols="24" id="<?php echo $this->get_field_id('format_footer'); ?>" name="<?php echo $this->get_field_name('format_footer'); ?>" class="widefat"><?php echo esc_textarea($instance['format_footer']); ?></textarea>
</p>
<p>
<label for="<?php echo $this->get_field_id('no_events_text'); ?>"><?php _e('No events message','events-manager'); ?>: </label>
<input type="text" id="<?php echo $this->get_field_id('no_events_text'); ?>" name="<?php echo $this->get_field_name('no_events_text'); ?>" value="<?php echo esc_attr( $instance['no_events_text'] ); ?>" >
</p>
<?php
}
/**
* Backwards compatability for an old setting which is now just another scope.
* @param unknown_type $instance
* @return string
*/
function fix_scope($instance){
if( !empty($instance['time_limit']) && is_numeric($instance['time_limit']) && $instance['time_limit'] > 1 ){
$instance['scope'] = $instance['time_limit'].'-months';
}elseif( !empty($instance['time_limit']) && $instance['time_limit'] == 1){
$instance['scope'] = 'month';
}elseif( !empty($instance['time_limit']) && $instance['time_limit'] == 'no-limit'){
$instance['scope'] = 'all';
}
return $instance;
}
}
add_action('widgets_init', 'EM_Widget::init');
?>