GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-widget-poll.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Widget to add custom content
5  *
6  * @since 1.8
7  *
8  * @extends GravityView_Widget
9  */
11 
12  /**
13  * Does this get displayed on a single entry?
14  * @var boolean
15  */
16  protected $show_on_single = false;
17 
18  /**
19  * @todo add support for specifying poll field to display (via AJAX in field settings)
20  * @since 1.8
21  */
22  function __construct() {
23 
24  $this->widget_description = __('Displays the results of Poll Fields that exist in the form.', 'gravityview' );
25 
26  $this->widget_subtitle = sprintf( _x('Note: this will display poll results for %sall form entries%s, not only the entries displayed in the View.', 'The string placeholders are for emphasis HTML', 'gravityview' ), '<em>', '</em>' );
27 
28  $default_values = array(
29  'header' => 1,
30  'footer' => 1,
31  );
32 
33  $settings = array(
34  'percentages' => array(
35  'label' => __('Display Percentages', 'gravityview'),
36  'type' => 'checkbox',
37  'value' => true,
38  'tooltip' => __( 'Display results percentages as part of results? Supported values are: true, false. Defaults to "true".', 'gravityview' ),
39  ),
40  'counts' => array(
41  'label' => __('Display Counts', 'gravityview'),
42  'type' => 'checkbox',
43  'value' => true,
44  'tooltip' => __( 'Display number of times each choice has been selected when displaying results? Supported values are: true, false. Defaults to "true".', 'gravityview' ),
45  ),
46  'style' => array(
47  'type' => 'select',
48  'label' => __('Style', 'gravityview'),
49  'tooltip' => __( 'The Polls Add-On currently supports 4 built in styles: red, green, orange, blue. Defaults to "green".', 'gravityview' ),
50  'value' => 'green',
51  'choices' => array(
52  'green' => __('Green', 'gravityview'),
53  'blue' => __('Blue', 'gravityview'),
54  'red' => __('Red', 'gravityview'),
55  'orange' => __('Orange', 'gravityview'),
56  )
57  )
58  );
59 
60  parent::__construct( __( 'Poll Results', 'gravityview' ) , 'poll', $default_values, $settings );
61 
62  // frontend - add template path
63  add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
64 
65  }
66 
67  /**
68  * Include this extension templates path
69  * @since 1.8
70  * @param array $file_paths List of template paths ordered
71  */
72  function add_template_path( $file_paths ) {
73 
74  $index = 126;
75 
76  // Index 100 is the default GravityView template path.
77  $file_paths[ $index ] = plugin_dir_path( __FILE__ ) . 'templates/';
78 
79  return $file_paths;
80  }
81 
82  /**
83  * Load the scripts and styles needed for the display of the poll widget
84  *
85  * @since 1.8
86  */
87  private function enqueue_scripts_and_styles() {
88 
89  $GFPolls = GFPolls::get_instance();
90 
91  wp_enqueue_script('gpoll_js', $GFPolls->get_base_url() . '/js/gpoll.js', array('jquery'), $GFPolls->_version);
92 
93  $GFPolls->localize_scripts();
94 
95  wp_enqueue_style('gpoll_css', $GFPolls->get_base_url() . '/css/gpoll.css', null, $GFPolls->_version);
96  }
97 
98  /**
99  * @inheritDoc
100  *
101  * @since 1.8
102  */
103  public function pre_render_frontend() {
104 
105  if( !class_exists('GFPolls') ) {
106 
107  $return = false;
108 
109  do_action( 'gravityview_log_error', 'Poll Widget not displayed; the Poll Addon is not loaded' );
110 
111  } else {
112 
113  $return = parent::pre_render_frontend();
114 
115  $poll_fields = GFCommon::get_fields_by_type( GravityView_View::getInstance()->getForm(), array( 'poll' ) );
116 
117  if ( empty ( $poll_fields ) ) {
118  do_action( 'gravityview_log_error', 'Poll Widget not displayed; there are no poll fields for the form' );
119  $return = false;
120  }
121  }
122 
123  return $return;
124  }
125 
126  /**
127  * Get the display settings for the Poll widget
128  *
129  * @param array $widget_settings Settings for the Poll widget
130  *
131  * @return array Final poll widget settings
132  */
133  function get_frontend_settings( $widget_settings ) {
134 
135  $default_settings = array(
136  'field' => 0,
137  'style' => 'green',
138  'percentages' => true,
139  'counts' => true,
140  );
141 
142  $settings = wp_parse_args( $widget_settings, $default_settings );
143 
144  /**
145  * @filter `gravityview/widget/poll/settings` Modify display settings for the poll widget
146  * @since 1.8
147  * @param array $settings Settings with `field`, `style`, `percentages` and `counts` keys
148  */
149  $settings = apply_filters( 'gravityview/widget/poll/settings', $settings );
150 
151  return $settings;
152  }
153 
154  /**
155  * Render the widget
156  *
157  * @see https://www.gravityhelp.com/documentation/article/polls-add-on/
158  *
159  * @since 1.8
160  */
161  public function render_frontend( $widget_args, $content = '', $context = '') {
162 
163  if( !$this->pre_render_frontend() ) {
164  return;
165  }
166 
167  // Make sure the class is loaded in DataTables
168  if( !class_exists( 'GFFormDisplay' ) ) {
169  include_once( GFCommon::get_base_path() . '/form_display.php' );
170  }
171 
173 
174  $settings = $this->get_frontend_settings( $widget_args );
175 
176  $percentages = empty( $settings['percentages'] ) ? 'false' : 'true';
177 
178  $counts = empty( $settings['counts'] ) ? 'false' : 'true';
179 
180  if( !empty( $settings['field'] ) ) {
181  $merge_tag = sprintf( '{gpoll: field="%d" style="%s" percentages="%s" counts="%s"}', $settings['field'], $settings['style'], $percentages, $counts );
182  } else {
183  $merge_tag = sprintf( '{all_poll_results: style="%s" percentages="%s" counts="%s"}', $settings['style'], $percentages, $counts );
184  }
185 
187 
188  $gravityview_view->poll_merge_tag = $merge_tag;
189 
190  $gravityview_view->poll_settings = $settings;
191 
192  $gravityview_view->render('widget', 'poll', false );
193 
194  }
195 
196 }
197 
Main GravityView widget class.
render_frontend($widget_args, $content= '', $context= '')
Render the widget.
add_template_path($file_paths)
Include this extension templates path.
$merge_tag
Merge tag is already generated by the class.
Definition: widget-poll.php:14
enqueue_scripts_and_styles()
Load the scripts and styles needed for the display of the poll widget.
Widget to add custom content.
static getInstance($passed_post=NULL)
get_frontend_settings($widget_settings)
Get the display settings for the Poll widget.
if(empty($field_settings['content'])) $content
Definition: custom.php:37