PluginProbe
Leyka / 3.5
Leyka v3.5
3.32.3 2.3.2 2.3.3 2.3.4 2.3.5 2.3.6 2.3.6.1 2.3.7 2.3.8 2.3.9 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1 3.10 3.11 3.11.1 3.12 3.13 3.14 3.15 3.16 3.17 All 89 releases
leyka / inc / leyka-widgets.php

leyka-widgets.php in Leyka 3.5, at inc/leyka-widgets.php

497 lines 19.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php if( !defined('WPINC') ) die;
2 /**
3 * Leyka widgets
4 **/
5
6 add_action('widgets_init', 'leyka_custom_widgets', 15);
7 function leyka_custom_widgets(){
8
9 register_widget('Leyka_Campaign_Card_Widget');
10 register_widget('Leyka_Campaigns_List_Widget');
11 register_widget('Leyka_Donations_List_Widget');
12
13 }
14
15 class Leyka_Campaign_Card_Widget extends WP_Widget {
16
17 public function __construct() {
18
19 $widget_ops = array(
20 'classname' => 'leyka_campaign_card',
21 'description' => __('Campaign informer with configurable elements', 'leyka')
22 );
23
24 parent::__construct('leyka_campaign_card', __('Leyka: Campaign Card', 'leyka'), $widget_ops);
25
26 }
27
28 public function widget($args, $instance) {
29
30 extract($args, EXTR_SKIP);
31
32 $title = apply_filters('widget_title', $instance['title']);
33
34 if( !empty($instance['campaign_id']) && $instance['campaign_id'] === '-' ) { // Last campaign
35
36 $query = get_posts(array('post_type' => Leyka_Campaign_Management::$post_type, 'posts_per_page' => 1,));
37 if( !$query ) {
38 return;
39 }
40
41 $campaign_id = reset($query)->ID;
42
43 } else if((int)$instance['campaign_id'] === 0) { // Take campaign from context
44 $campaign_id = null;
45 } else { // Campaign is set explicitly
46 $campaign_id = (int)$instance['campaign_id'];
47 }
48
49 $args = array(
50 'show_title' => !!$instance['show_title'],
51 'show_thumb' => !!$instance['show_thumb'],
52 'show_excerpt' => !!$instance['show_excerpt'],
53 'show_scale' => !!$instance['show_scale'],
54 'show_button' => !!$instance['show_button'],
55 );
56
57 $css_id = 'leyka_campaign_card_widget-'.uniqid();
58 $html = leyka_get_campaign_card($campaign_id, $args);
59 if( !$html ) {
60 return;
61 }
62 $campaign = new Leyka_Campaign($campaign_id);
63 if( !leyka_form_is_screening(false) ) { // Don't increase campaign views counter if we're on a page with this campaign's donation form
64 $campaign->increase_views_counter();
65 }
66
67 /** @var $before_widget */
68 echo $before_widget;
69 if($title) {
70 /**
71 * @var $before_title
72 * @var $after_title
73 */
74 echo $before_title.$title.$after_title;
75 }
76
77 echo '<div id="'.esc_attr($css_id).'">'.$html."</div>";
78
79 /** @var $after_widget */
80 echo $after_widget;
81
82 }
83
84 public function update($new_instance, $old_instance) {
85
86 $instance = $old_instance;
87
88 $instance['title'] = sanitize_text_field($new_instance['title']);
89 $instance['campaign_id'] = sanitize_text_field($new_instance['campaign_id']);
90
91 $instance['show_title'] = !empty($new_instance['show_title']);
92 $instance['show_thumb'] = !empty($new_instance['show_thumb']);
93 $instance['show_excerpt'] = !empty($new_instance['show_excerpt']);
94 $instance['show_scale'] = !empty($new_instance['show_scale']);
95 $instance['show_button'] = !empty($new_instance['show_button']);
96
97 return $instance;
98
99 }
100
101 public function form($instance) {
102
103 $defaults = array(
104 'title' => '',
105 'campaign_id' => '',
106 'show_title' => 1,
107 'show_thumb' => 1,
108 'show_excerpt' => 1,
109 'show_scale' => 1,
110 'show_button' => 1,
111 );
112
113 $instance = wp_parse_args((array)$instance, $defaults);?>
114
115 <p>
116 <label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
117 <?php esc_html_e('Title', 'leyka');?>:
118 </label>
119 <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($instance['title']);?>">
120 </p>
121
122 <p>
123 <label for="<?php echo esc_attr($this->get_field_id('campaign_id'));?>">
124 <?php esc_html_e('Campaign ID', 'leyka');?>:
125 </label>
126
127 <?php $current_value = $instance['campaign_id'];?>
128 <select id="<?php echo $this->get_field_id('campaign_id');?>" name="<?php echo $this->get_field_name( 'campaign_id');?>" class="widefat">
129 <option value="-" <?php echo $current_value == '-' ? 'selected="selected"' : '';?>>
130 <?php esc_html_e('The most recent campaign', 'leyka');?>
131 </option>
132 <option value="0" <?php echo $current_value == '0' ? 'selected="selected"' : '';?>>
133 <?php esc_html_e('Campaign based on a context', 'leyka');?>
134 </option>
135
136 <?php foreach(get_posts(array('post_type' => Leyka_Campaign_Management::$post_type, 'nopaging' => true)) as $campaign) {?>
137 <option value="<?php echo $campaign->ID;?>" <?php echo $current_value == $campaign->ID ? 'selected="selected"' : '';?>>
138 <?php echo $campaign->post_title;?>
139 </option>
140 <?php }?>
141
142 </select>
143 <br>
144 <small class="help"><?php _e('Copy-paste ID of the campaign to output, state "0" to detect it from context or leave the field blank to display the most recent campaign', 'leyka');?></small>
145 </p>
146
147 <p>
148 <label for="<?php echo esc_attr($this->get_field_id('show_title'));?>">
149 <input id="<?php echo $this->get_field_id('show_title');?>" name="<?php echo $this->get_field_name('show_title');?>" value="1" type="checkbox" <?php checked( !!$instance['show_title'], 1 );?>>
150 <?php esc_html_e('Show title', 'leyka');?>
151 </label>
152 <br>
153 <label for="<?php echo esc_attr($this->get_field_id('show_thumb')); ?>">
154 <input id="<?php echo $this->get_field_id( 'show_thumb' ); ?>" name="<?php echo $this->get_field_name('show_thumb');?>" value="1" type="checkbox" <?php checked( !!$instance['show_thumb'], 1 );?>>
155 <?php esc_html_e('Show thumbnail', 'leyka');?>
156 </label>
157 <br>
158 <label for="<?php echo esc_attr($this->get_field_id('show_excerpt'));?>">
159 <input id="<?php echo $this->get_field_id( 'show_excerpt' ); ?>" name="<?php echo $this->get_field_name( 'show_excerpt' ); ?>" value="1" type="checkbox" <?php checked( !!$instance['show_excerpt'], 1 );?>>
160 <?php esc_html_e('Show excerpt', 'leyka');?>
161 </label>
162 <br>
163 <label for="<?php echo esc_attr($this->get_field_id('show_scale'));?>">
164 <input id="<?php echo $this->get_field_id( 'show_scale' ); ?>" name="<?php echo $this->get_field_name( 'show_scale' ); ?>" value="1" type="checkbox" <?php checked( !!$instance['show_scale'], 1 );?>>
165 <?php esc_html_e('Show scale', 'leyka');?>
166 </label>
167 <br>
168 <label for="<?php echo esc_attr($this->get_field_id('show_button')); ?>">
169 <input id="<?php echo $this->get_field_id( 'show_button' ); ?>" name="<?php echo $this->get_field_name( 'show_button' ); ?>" value="1" type="checkbox" <?php checked( !!$instance['show_button'], 1 );?>>
170 <?php esc_html_e('Show «support» button', 'leyka');?>
171 </label>
172 </p>
173
174 <?php }
175
176 }
177
178 class Leyka_Campaigns_List_Widget extends WP_Widget {
179
180 public function __construct() {
181
182 $widget_ops = array(
183 'classname' => 'leyka_campaigns_list',
184 'description' => esc_html__('List of recent campaigns with configurable attributes', 'leyka'),
185 );
186
187 parent::__construct('leyka_campaigns_list', esc_html__('Leyka: Campaigns List', 'leyka'), $widget_ops);
188
189 }
190
191 public function widget($args, $instance) {
192
193 extract($args, EXTR_SKIP);
194
195 $title = apply_filters('widget_title', $instance['title']);
196
197 $q_args = array(
198 'post_type' => Leyka_Campaign_Management::$post_type,
199 'posts_per_page' => empty($instance['limit']) ? 3 : (int)$instance['limit'],
200 'post_status' => 'publish',
201 );
202
203 if( !empty($instance['include']) ) {
204 $q_args['post__in'] = array_map('intval', explode(',', $instance['include']));
205 }
206
207 if( !empty($instance['exclude']) ) {
208 $q_args['post__not_in'] = array_map('intval', explode(',', $instance['exclude']));
209 }
210
211 if( !empty($instance['exclude_finished']) ) {
212 $q_args['meta_query'] = array(
213 array(
214 'key' => 'is_finished',
215 'value' => 1,
216 'compare' => '!=',
217 'type' => 'NUMERIC',
218 ),
219 );
220 }
221
222 $campaigns = get_posts(apply_filters('leyka_campaigns_list_widget_query_args', $q_args, $instance));
223 if( !$campaigns ) {
224 return;
225 }
226
227 $args = array(
228 'show_title' => !empty($instance['show_title']),
229 'show_thumb' => !empty($instance['show_thumb']),
230 'show_excerpt' => !empty($instance['show_excerpt']),
231 'show_scale' => !empty($instance['show_scale']),
232 'show_button' => !empty($instance['show_button']),
233 'exclude_finished' => !empty($instance['exclude_finished']),
234 );
235
236 /** @var $before_widget */
237 /** @var $before_title */
238 /** @var $after_widget */
239 /** @var $after_title */
240 echo $before_widget;
241 if($title) {
242 echo $before_title.$title.$after_title;
243 }
244
245 $css_id = 'leyka_campaign_list_widget-'.uniqid();
246 echo "<div id='".esc_attr($css_id)."' class='leyka-campaigns-list'>";
247
248 add_filter('leyka_campaign_card_thumbnail_size', array($this, '_campaign_thumb_size'));
249 add_filter('leyka_campaign_card_class', array($this, '_campaign_css'));
250
251 foreach($campaigns as $campaign) {
252 echo leyka_get_campaign_card($campaign->ID, $args);
253 }
254 remove_filter('leyka_campaign_card_thumbnail_size', array($this, '_campaign_thumb_size'));
255 remove_filter('leyka_campaign_card_class', array($this, '_campaign_css'));
256
257 echo "</div>";
258 echo $after_widget;
259 }
260
261 public function _campaign_thumb_size($size){
262 return 'thumbnail';
263 }
264
265 public function _campaign_css($css) {
266 return 'leyka-campaign-list-item';
267 }
268
269 public function update($new_instance, $old_instance) {
270
271 $instance = $old_instance;
272
273 $instance['title'] = sanitize_text_field($new_instance['title']);
274 $instance['limit'] = empty($new_instance['limit']) ? 3 : (int)$new_instance['limit'];
275
276 $instance['show_title'] = !empty($new_instance['show_title']);
277 $instance['show_thumb'] = !empty($new_instance['show_thumb']);
278 $instance['show_excerpt'] = !empty($new_instance['show_excerpt']);
279 $instance['show_scale'] = !empty($new_instance['show_scale']);
280 $instance['show_button'] = !empty($new_instance['show_button']);
281
282 $instance['include'] = sanitize_text_field($new_instance['include']);
283 $instance['exclude'] = sanitize_text_field($new_instance['exclude']);
284
285 $instance['exclude_finished'] = !empty($new_instance['exclude_finished']);
286
287 return $instance;
288
289 }
290
291 public function form($instance) {
292
293 $defaults = array(
294 'title' => '',
295 'limit' => 3,
296 'show_title' => true,
297 'show_thumb' => true,
298 'show_excerpt' => true,
299 'show_scale' => true,
300 'show_button' => true,
301 'include' => '',
302 'exclude' => '',
303 'exclude_finished' => false,
304 );
305
306 $instance = wp_parse_args((array)$instance, $defaults);
307
308 $limit = (int)$instance['limit'];
309
310 $show_title = (int)$instance['show_title'];
311 $show_thumb = (int)$instance['show_thumb'];
312 $show_excerpt = !empty($instance['show_excerpt']);
313 $show_scale = !empty($instance['show_scale']);
314 $show_button = !empty($instance['show_button']);
315
316 $exclude_finished = !empty($instance['exclude_finished']);?>
317
318 <p>
319 <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php _e('Title', 'leyka');?>:</label>
320 <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($instance['title']);?>" />
321 </p>
322
323 <p>
324 <label for="<?php echo esc_attr($this->get_field_id('limit'));?>"><?php _e('Number', 'leyka');?>:</label>
325 <select class="widefat" name="<?php echo $this->get_field_name('limit');?>" id="<?php echo $this->get_field_id('limit');?>">
326 <?php for($i=1; $i<=10; $i++) { ?>
327 <option <?php selected($limit, $i);?> value="<?php echo $i;?>"><?php echo $i;?></option>
328 <?php }?>
329 </select>
330 </p>
331
332 <p>
333 <label for="<?php echo esc_attr($this->get_field_id('show_title'));?>">
334 <input id="<?php echo $this->get_field_id('show_title');?>" name="<?php echo $this->get_field_name('show_title'); ?>" value="1" type="checkbox" <?php checked($show_title, 1);?>>
335 <?php _e('Show title', 'leyka');?></label>
336 <br>
337 <label for="<?php echo esc_attr($this->get_field_id('show_thumb'));?>">
338 <input id="<?php echo $this->get_field_id('show_thumb');?>" name="<?php echo $this->get_field_name('show_thumb'); ?>" value="1" type="checkbox" <?php checked($show_thumb, 1);?>>
339 <?php _e('Show thumbnail', 'leyka');?></label>
340 <br>
341 <label for="<?php echo esc_attr($this->get_field_id('show_excerpt')); ?>">
342 <input id="<?php echo $this->get_field_id('show_excerpt');?>" name="<?php echo $this->get_field_name('show_excerpt'); ?>" value="1" type="checkbox" <?php checked($show_excerpt, 1);?>>
343 <?php _e('Show excerpt', 'leyka');?></label>
344 <br>
345 <label for="<?php echo esc_attr($this->get_field_id('show_scale')); ?>">
346 <input id="<?php echo $this->get_field_id('show_scale');?>" name="<?php echo $this->get_field_name('show_scale'); ?>" value="1" type="checkbox" <?php checked($show_scale, 1);?>>
347 <?php _e('Show scale', 'leyka');?></label>
348 <br>
349 <label for="<?php echo esc_attr($this->get_field_id('show_button')); ?>">
350 <input id="<?php echo $this->get_field_id('show_button');?>" name="<?php echo $this->get_field_name('show_button'); ?>" value="1" type="checkbox" <?php checked($show_button, 1);?>>
351 <?php _e('Show «support» button', 'leyka');?></label>
352 </p>
353
354 <p>
355 <label for="<?php echo esc_attr($this->get_field_id('include'));?>"><?php _e('Include campaigns', 'leyka');?>:</label>
356 <input class="widefat" id="<?php echo esc_attr($this->get_field_id('include'));?>" name="<?php echo esc_attr($this->get_field_name('include'));?>" type="text" value="<?php echo esc_attr($instance['include']);?>">
357 <small class="help"><?php _e('Comma-separated list of IDs', 'leyka');?></small>
358 </p>
359
360 <p>
361 <label for="<?php echo esc_attr($this->get_field_id('exclude'));?>"><?php _e('Exclude campaigns', 'leyka');?>:</label>
362 <input class="widefat" id="<?php echo esc_attr($this->get_field_id('exclude'));?>" name="<?php echo esc_attr($this->get_field_name('exclude'));?>" type="text" value="<?php echo esc_attr($instance['exclude']);?>">
363 <small class="help"><?php _e('Comma-separated list of IDs', 'leyka');?></small>
364 </p>
365
366 <p>
367 <label for="<?php echo esc_attr($this->get_field_id('exclude_finished'));?>">
368 <input id="<?php echo $this->get_field_id('exclude_finished');?>" name="<?php echo $this->get_field_name('exclude_finished');?>" value="1" type="checkbox" <?php checked($exclude_finished, 1);?>>
369 <?php _e('Exclude finished campaigns', 'leyka');?>
370 </label>
371 </p>
372
373 <?php }
374
375 }
376
377 class Leyka_Donations_List_Widget extends WP_Widget {
378
379 public function __construct() {
380
381 $widget_ops = array(
382 'classname' => 'leyka_donations_list',
383 'description' => esc_html__('Recent donations list, optionally filtered by campaign', 'leyka')
384 );
385 parent::__construct('leyka_donations_list', esc_html__('Leyka: Donations List', 'leyka'), $widget_ops);
386
387 }
388
389 public function widget($args, $instance) {
390
391 extract($args, EXTR_SKIP);
392
393 $title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title']);
394
395 $campaign_id = empty($instance['campaign_id']) ? 'all' : (int)$instance['campaign_id'];
396
397 $args = array(
398 'num' => empty($instance['limit']) ? 5 : (int)$instance['limit'],
399 'show_purpose' => !empty($instance['show_purpose']),
400 'show_name' => !empty($instance['show_name']),
401 'show_date' => !empty($instance['show_date']),
402 );
403
404 $html = leyka_get_donors_list($campaign_id, $args);
405 if( !$html ) {
406 return;
407 }
408
409 /**
410 * @var $before_widget
411 * @var $before_title
412 * @var $after_title
413 * @var $after_widget
414 */
415 echo $before_widget.($title ? $before_title.$title.$after_title : '').$html.$after_widget;
416
417 }
418
419 public function update($new_instance, $old_instance) {
420
421 $instance = $old_instance;
422
423 $instance['title'] = sanitize_text_field($new_instance['title']);
424 $instance['limit'] = empty($new_instance['limit']) ? 5 : (int)$new_instance['limit'];
425 $instance['campaign_id'] = sanitize_text_field($new_instance['campaign_id']);
426 $instance['show_purpose'] = !empty($new_instance['show_purpose']);
427 $instance['show_name'] = !empty($new_instance['show_name']);
428 $instance['show_date'] = !empty($new_instance['show_date']);
429
430 return $instance;
431
432 }
433
434 public function form($instance) {
435
436 $defaults = array(
437 'title' => '',
438 'limit' => 5,
439 'campaign_id' => '',
440 'show_purpose' => 1,
441 'show_name' => 1,
442 'show_date' => 1,
443 );
444
445 $instance = wp_parse_args((array)$instance, $defaults);?>
446
447 <p>
448 <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title', 'leyka');?>:</label>
449 <input class="widefat" id="<?php echo esc_attr($this->get_field_id('title'));?>" name="<?php echo esc_attr($this->get_field_name('title'));?>" type="text" value="<?php echo esc_attr($instance['title']);?>">
450 </p>
451
452 <p>
453 <label for="<?php echo esc_attr($this->get_field_id('limit'));?>"><?php _e('Donations number', 'leyka');?>:</label>
454 <select class="widefat" name="<?php echo $this->get_field_name('limit');?>" id="<?php echo $this->get_field_id('limit');?>">
455 <?php for($i = 5; $i <= 25; $i += 5) {?>
456 <option <?php selected((int)$instance['limit'], $i) ?> value="<?php echo esc_attr($i); ?>"><?php echo $i;?></option>
457 <?php }?>
458 </select>
459 </p>
460
461 <p>
462
463 <label for="<?php echo esc_attr($this->get_field_id('campaign_id'));?>">
464 <?php esc_html_e('Campaign ID', 'leyka');?>:
465 </label>
466 <input id="<?php echo esc_attr($this->get_field_id('campaign_id'));?>" name="<?php echo esc_attr($this->get_field_name('campaign_id'));?>" value="<?php echo esc_attr($instance['campaign_id']);?>" type="text"><br>
467
468 <small class="help">
469 <?php esc_html_e('Copy-paste ID of the campaign to filter donations in the list, state "0" to detect it from context or leave the field blank to display recent entries', 'leyka');?>
470 </small>
471
472 </p>
473
474 <h4><?php esc_html_e('Donation item settings', 'leyka');?></h4>
475
476 <p>
477
478 <label for="<?php echo esc_attr($this->get_field_id('show_purpose'));?>">
479 <input id="<?php echo esc_attr($this->get_field_id('show_purpose'));?>" name="<?php echo esc_attr($this->get_field_name('show_purpose'));?>" value="1" type="checkbox" <?php checked( !!$instance['show_purpose'], 1 );?>>
480 <?php esc_html_e('Show donation purpose', 'leyka');?>
481 </label>
482 <br>
483 <label for="<?php echo esc_attr($this->get_field_id('show_name'));?>">
484 <input id="<?php echo esc_attr($this->get_field_id('show_name'));?>" name="<?php echo esc_attr($this->get_field_name('show_name'));?>" value="1" type="checkbox" <?php checked( !!$instance['show_name'], 1 );?>>
485 <?php esc_html_e("Show donor's name", 'leyka');?>
486 </label>
487 <br>
488 <label for="<?php echo esc_attr($this->get_field_id('show_date'));?>">
489 <input id="<?php echo esc_attr($this->get_field_id('show_date'));?>" name="<?php echo esc_attr($this->get_field_name('show_date'));?>" value="1" type="checkbox" <?php checked( !!$instance['show_date'], 1 );?>>
490 <?php esc_html_e('Show donation date', 'leyka');?>
491 </label>
492
493 </p>
494
495 <?php }
496
497 }