GravityView  2.17
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_id = 'poll';
25  $this->widget_description = __('Displays the results of Poll Fields that exist in the form.', 'gk-gravityview' );
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', 'gk-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', 'gk-gravityview'),
36  'type' => 'checkbox',
37  'value' => true,
38  'tooltip' => __( 'Display results percentages as part of results? Supported values are: true, false. Defaults to "true".', 'gk-gravityview' ),
39  ),
40  'counts' => array(
41  'label' => __('Display Counts', 'gk-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".', 'gk-gravityview' ),
45  ),
46  'style' => array(
47  'type' => 'select',
48  'label' => __('Style', 'gk-gravityview'),
49  'tooltip' => __( 'The Polls Add-On currently supports 4 built in styles: red, green, orange, blue. Defaults to "green".', 'gk-gravityview' ),
50  'value' => 'green',
51  'choices' => array(
52  'green' => __('Green', 'gk-gravityview'),
53  'blue' => __('Blue', 'gk-gravityview'),
54  'red' => __('Red', 'gk-gravityview'),
55  'orange' => __('Orange', 'gk-gravityview'),
56  ),
57  ),
58  );
59 
60  if ( ! $this->is_registered() ) {
61  // frontend - add template path
62  add_filter( 'gravityview_template_paths', array( $this, 'add_template_path' ) );
63  }
64 
65  parent::__construct( __( 'Poll Results', 'gk-gravityview' ) , null, $default_values, $settings );
66  }
67 
68  /**
69  * Include this extension templates path
70  * @since 1.8
71  * @param array $file_paths List of template paths ordered
72  */
73  function add_template_path( $file_paths ) {
74 
75  $index = 126;
76 
77  // Index 100 is the default GravityView template path.
78  $file_paths[ $index ] = plugin_dir_path( __FILE__ ) . 'templates/';
79 
80  return $file_paths;
81  }
82 
83  /**
84  * Load the scripts and styles needed for the display of the poll widget
85  *
86  * @since 1.8
87  */
88  private function enqueue_scripts_and_styles() {
89 
90  $GFPolls = GFPolls::get_instance();
91 
92  wp_enqueue_script('gpoll_js', $GFPolls->get_base_url() . '/js/gpoll.js', array('jquery'), $GFPolls->_version);
93 
94  $GFPolls->localize_scripts();
95 
96  wp_enqueue_style('gpoll_css', $GFPolls->get_base_url() . '/css/gpoll.css', null, $GFPolls->_version);
97  }
98 
99  /**
100  * @inheritDoc
101  *
102  * @since 1.8
103  */
104  public function pre_render_frontend() {
105 
106  if( ! class_exists('GFPolls') ) {
107 
108  gravityview()->log->error( 'Poll Widget not displayed; the Poll Addon is not loaded' );
109 
110  return false;
111  }
112 
113  $view = gravityview()->views->get();
114 
115  $poll_fields = array( $view->form->form['id'] => GFCommon::get_fields_by_type( $view->form, array( 'poll' ) ) );
116 
117  foreach ( $view->joins as $join ) {
118  $poll_fields[ $join->join_on->form['id'] ] = GFCommon::get_fields_by_type( $join->join_on->form, array( 'poll' ) );
119  }
120 
121  $poll_fields = array_filter( $poll_fields );
122 
123  if ( empty ( $poll_fields ) ) {
124  gravityview()->log->error( 'Poll Widget not displayed; there are no poll fields for the form' );
125  return false;
126  }
127 
128  $this->poll_fields = $poll_fields;
129 
130  return parent::pre_render_frontend();
131  }
132 
133  /**
134  * Get the display settings for the Poll widget
135  *
136  * @param array $widget_settings Settings for the Poll widget
137  *
138  * @return array Final poll widget settings
139  */
140  function get_frontend_settings( $widget_settings ) {
141 
142  $default_settings = array(
143  'field' => 0,
144  'style' => 'green',
145  'percentages' => true,
146  'counts' => true,
147  );
148 
149  $settings = wp_parse_args( $widget_settings, $default_settings );
150 
151  /**
152  * @filter `gravityview/widget/poll/settings` Modify display settings for the poll widget
153  * @since 1.8
154  * @param array $settings Settings with `field`, `style`, `percentages` and `counts` keys
155  */
156  $settings = apply_filters( 'gravityview/widget/poll/settings', $settings );
157 
158  return $settings;
159  }
160 
161  /**
162  * Render the widget
163  *
164  * @see https://www.gravityhelp.com/documentation/article/polls-add-on/
165  *
166  * @since 1.8
167  */
168  public function render_frontend( $widget_args, $content = '', $context = '' ) {
169 
170  if( ! $this->pre_render_frontend() ) {
171  return;
172  }
173 
174  // Make sure the class is loaded in DataTables
175  if( !class_exists( 'GFFormDisplay' ) ) {
176  include_once( GFCommon::get_base_path() . '/form_display.php' );
177  }
178 
180 
181  $settings = $this->get_frontend_settings( $widget_args );
182 
183  $percentages = empty( $settings['percentages'] ) ? 'false' : 'true';
184 
185  $counts = empty( $settings['counts'] ) ? 'false' : 'true';
186 
187  if( !empty( $settings['field'] ) ) {
188  $merge_tag = sprintf( '{gpoll: field="%d" style="%s" percentages="%s" counts="%s"}', $settings['field'], $settings['style'], $percentages, $counts );
189  } else {
190  $merge_tag = sprintf( '{all_poll_results: style="%s" percentages="%s" counts="%s"}', $settings['style'], $percentages, $counts );
191  }
192 
194 
195  $gravityview_view->poll_merge_tag = $merge_tag;
196 
197  $gravityview_view->poll_settings = $settings;
198  $gravityview_view->poll_fields = $this->poll_fields;
199 
200  $gravityview_view->render('widget', 'poll', false );
201 
202  unset( $gravityview_view->poll_merge_tag, $gravityview_view->poll_settings, $gravityview_view->poll_form, $gravityview_view->poll_fields );
203  }
204 
205 }
206 
get_frontend_settings( $widget_settings)
Get the display settings for the Poll widget.
static getInstance( $passed_post=NULL)
Main GravityView widget class.
enqueue_scripts_and_styles()
Load the scripts and styles needed for the display of the poll widget.
if(empty( $field_settings['content'])) $content
Definition: custom.php:37
add_template_path( $file_paths)
Include this extension templates path.
render_frontend( $widget_args, $content='', $context='')
Render the widget.
is_registered()
Whether this Widget&#39;s been registered already or not.
Widget to add custom content.
gravityview()
The main GravityView wrapper function.