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
|
<?php
namespace TotalPoll\Widgets; ! defined( 'ABSPATH' ) && exit();
use TotalPollVendors\TotalCore\Widgets\Widget;
/** * Poll Widget. * @package TotalPoll\Widgets */ class Poll extends Widget { /** * Poll constructor. */ public function __construct() { $widgetOptions = array( 'classname' => 'totalpoll-widget-poll', 'description' => esc_html__( 'TotalPoll poll widget', 'totalpoll' ), ); parent::__construct( 'totalpoll_poll', esc_html__( '[TotalPoll] Poll', 'totalpoll' ), $widgetOptions ); }
/** * Widget content. * * @param $args * @param $instance */ public function content( $args, $instance ) { if ( ! empty( $instance['poll'] ) ): $screen = $instance['screen'] ?: 'vote'; $poll = TotalPoll( 'polls.repository' )->getById( $instance['poll'] )->setScreen( $screen );
echo $poll->render(); endif; }
/** * Widget form. * * @param array $instance * * @return string|void */ public function form( $instance ) { $instance = wp_parse_args( $instance, [ 'poll' => null, 'title' => null, 'screen' => null ] ); parent::form( $instance ); }
/** * Widget form fields. * * @param $fields * @param $instance * * @return mixed */ public function fields( $fields, $instance ) { // Poll field $polls = []; $pollsPosts = (array) get_posts( [ 'post_type' => TP_POLL_CPT_NAME, 'posts_per_page' => - 1 ] );
foreach ( $pollsPosts as $poll ): $polls[ $poll->ID ] = $poll->post_title; endforeach;
$pollsListFieldOptions = [ 'class' => 'widefat', 'name' => esc_attr( $this->get_field_name( 'poll' ) ), 'label' => __( 'Poll:', 'totalpoll' ), 'options' => $polls, ];
$fields['poll'] = TotalPoll( 'form.field.select' )->setOptions( $pollsListFieldOptions )->setValue( $instance['poll'] ?: '' );
// Screen $screen = $instance['screen'] ?: 'vote'; $pollScreenFieldOption = [ 'class' => 'widefat totalpoll-page-selector', 'name' => esc_attr( $this->get_field_name( 'screen' ) ), 'label' => __( 'Screen:', 'totalpoll' ), 'options' => [ 'vote' => __( 'Vote', 'totalpoll' ), 'results' => __( 'Results', 'totalpoll' ), ], ]; $fields['screen'] = TotalPoll( 'form.field.select' )->setOptions( $pollScreenFieldOption )->setValue( $screen );
return $fields; } }
|