GravityView  1.19.4
The best, easiest way to display Gravity Forms entries on your website.
class-gravityview-widget.php
Go to the documentation of this file.
1 <?php
2 
3 /**
4  * Main GravityView widget class
5  */
7 
8  /**
9  * Widget admin label
10  * @var string
11  */
12  protected $widget_label = '';
13 
14  /**
15  * Widget description, shown on the "+ Add Widget" picker
16  * @var string
17  */
18  protected $widget_description = '';
19 
20  /**
21  * Widget details, shown in the widget lightbox
22  * @since 1.8
23  * @var string
24  */
25  protected $widget_subtitle = '';
26 
27  /**
28  * Widget admin id
29  * @var string
30  */
31  protected $widget_id = '';
32 
33  /**
34  * default configuration for header and footer
35  * @var array
36  */
37  protected $defaults = array();
38 
39  /**
40  * Widget admin advanced settings
41  * @var array
42  */
43  protected $settings = array();
44 
45  /**
46  * allow class to automatically add widget_text filter for you in shortcode
47  * @var string
48  */
49  protected $shortcode_name;
50 
51  // hold widget View options
52  private $widget_options;
53 
54  function __construct( $widget_label , $widget_id , $defaults = array(), $settings = array() ) {
55 
56 
57  /**
58  * The shortcode name is set to the lowercase name of the widget class, unless overridden by the class specifying a different value for $shortcode_name
59  * @var string
60  */
61  $this->shortcode_name = !isset( $this->shortcode_name ) ? strtolower( get_class($this) ) : $this->shortcode_name;
62 
63  $this->widget_label = $widget_label;
64  $this->widget_id = $widget_id;
65  $this->defaults = array_merge( array( 'header' => 0, 'footer' => 0 ), $defaults );
66 
67  // Make sure every widget has a title, even if empty
68  $this->settings = $this->get_default_settings();
69  $this->settings = wp_parse_args( $settings, $this->settings );
70 
71  // register widgets to be listed in the View Configuration
72  add_filter( 'gravityview_register_directory_widgets', array( $this, 'register_widget') );
73 
74  // widget options
75  add_filter( 'gravityview_template_widget_options', array( $this, 'assign_widget_options' ), 10, 3 );
76 
77  // frontend logic
78  add_action( "gravityview_render_widget_{$widget_id}", array( $this, 'render_frontend' ), 10, 1 );
79 
80  // register shortcodes
81  add_action( 'wp', array( $this, 'add_shortcode') );
82 
83  // Use shortcodes in text widgets.
84  add_filter('widget_text', array( $this, 'maybe_do_shortcode' ) );
85  }
86 
87 
88  /**
89  * Define general widget settings
90  * @since 1.5.4
91  * @return array $settings Default settings
92  */
93  protected function get_default_settings() {
94 
95  $settings = array();
96 
97  /**
98  * @filter `gravityview/widget/enable_custom_class` Enable custom CSS class settings for widgets
99  * @param boolean $enable_custom_class False by default. Return true if you want to enable.
100  * @param GravityView_Widget $this Current instance of GravityView_Widget
101  */
102  $enable_custom_class = apply_filters('gravityview/widget/enable_custom_class', false, $this );
103 
104  if( $enable_custom_class ) {
105 
106  $settings['custom_class'] = array(
107  'type' => 'text',
108  'label' => __( 'Custom CSS Class:', 'gravityview' ),
109  'desc' => __( 'This class will be added to the widget container', 'gravityview'),
110  'value' => '',
111  'merge_tags' => true,
112  );
113 
114  }
115 
116  return $settings;
117  }
118 
119  /**
120  * @return string
121  */
122  public function get_widget_id() {
123  return $this->widget_id;
124  }
125 
126  /**
127  * Get the widget settings
128  * @return array|null Settings array; NULL if not set
129  */
130  public function get_settings() {
131  return !empty( $this->settings ) ? $this->settings : NULL;
132  }
133 
134  /**
135  * Get a setting by the setting key
136  * @param string $key Key for the setting
137  * @return mixed|null Value of the setting; NULL if not set
138  */
139  public function get_setting( $key ) {
140  $setting = NULL;
141 
142  if( isset( $this->settings ) && is_array( $this->settings ) ) {
143  $setting = isset( $this->settings[ $key ] ) ? $this->settings[ $key ] : NULL;
144  }
145 
146  return $setting;
147  }
148 
149  /**
150  * Do shortcode if the Widget's shortcode exists.
151  * @param string $text Widget text to check
152  * @param null|WP_Widget Empty if not called by WP_Widget, or a WP_Widget instance
153  * @return string Widget text
154  */
155  function maybe_do_shortcode( $text, $widget = NULL ) {
156 
157  if( !empty( $this->shortcode_name ) && has_shortcode( $text, $this->shortcode_name ) ) {
158  return do_shortcode( $text );
159  }
160 
161  return $text;
162  }
163 
164  function render_shortcode( $atts, $content = '', $context = '' ) {
165 
166  ob_start();
167 
168  $this->render_frontend( $atts, $content, $context );
169 
170  return ob_get_clean();
171  }
172 
173  /**
174  * Add $this->shortcode_name shortcode to output self::render_frontend()
175  */
176  function add_shortcode( $run_on_singular = true ) {
177  global $post;
178 
179  if( GravityView_Plugin::is_admin() ) { return; }
180 
181  if( empty( $this->shortcode_name ) ) { return; }
182 
183  // If the widget shouldn't output on single entries, don't show it
184  if( empty( $this->show_on_single ) && class_exists('GravityView_frontend') && GravityView_frontend::is_single_entry() ) {
185  do_action('gravityview_log_debug', sprintf( '%s[add_shortcode]: Skipping; set to not run on single entry.', get_class($this)) );
186 
187  add_shortcode( $this->shortcode_name, '__return_null' );
188  return;
189  }
190 
191 
192  if( !has_gravityview_shortcode( $post ) ) {
193 
194  do_action('gravityview_log_debug', sprintf( '%s[add_shortcode]: No shortcode present; not adding render_frontend shortcode.', get_class($this)) );
195 
196  add_shortcode( $this->shortcode_name, '__return_null' );
197  return;
198  }
199 
200  add_shortcode( $this->shortcode_name, array( $this, 'render_shortcode') );
201  }
202 
203  /**
204  * Register widget to become available in admin
205  * @param array $widgets
206  * @return array $widgets
207  */
208  function register_widget( $widgets ) {
209  $widgets[ $this->widget_id ] = array(
210  'label' => $this->widget_label ,
211  'description' => $this->widget_description,
212  'subtitle' => $this->widget_subtitle,
213  );
214  return $widgets;
215  }
216 
217  /**
218  * Assign template specific field options
219  *
220  * @access protected
221  * @param array $options (default: array())
222  * @param string $template (default: '')
223  * @return array
224  */
225  public function assign_widget_options( $options = array(), $template = '', $widget = '' ) {
226 
227  if( $this->widget_id === $widget ) {
228  $options = array_merge( $options, $this->settings );
229  }
230 
231  return $options;
232  }
233 
234 
235  /**
236  * Frontend logic
237  *
238  * @return void
239  */
240  public function render_frontend( $widget_args, $content = '', $context = '') {
241  // to be defined by child class
242  if( !$this->pre_render_frontend() ) {
243  return;
244  }
245  }
246 
247  /**
248  * General validations when rendering the widget
249  * @return boolean True: render frontend; False: don't render frontend
250  */
251  public function pre_render_frontend() {
253 
254  if( empty( $gravityview_view ) ) {
255  do_action('gravityview_log_debug', sprintf( '%s[render_frontend]: $gravityview_view not instantiated yet.', get_class($this)) );
256  return false;
257  }
258 
259  /**
260  * @filter `gravityview/widget/hide_until_searched` Modify whether to hide content until search
261  * @param boolean $hide_until_searched Hide until search?
262  * @param GravityView_Widget $this Widget instance
263  */
264  $hide_until_search = apply_filters( 'gravityview/widget/hide_until_searched', $gravityview_view->hide_until_searched, $this );
265 
266  if( $hide_until_search ) {
267  do_action('gravityview_log_debug', sprintf( '%s[render_frontend]: Hide View data until search is performed', get_class($this)) );
268  return false;
269  }
270 
271  return true;
272  }
273 
274 
275 } // GravityView_Widget
276 
assign_widget_options($options=array(), $template= '', $widget= '')
Assign template specific field options.
register_widget($widgets)
Register widget to become available in admin.
add_shortcode($run_on_singular=true)
Add $this->shortcode_name shortcode to output self::render_frontend()
Main GravityView widget class.
get_settings()
Get the widget settings.
get_setting($key)
Get a setting by the setting key.
get_default_settings()
Define general widget settings.
static is_single_entry()
Verify if user requested a single entry view.
render_shortcode($atts, $content= '', $context= '')
render_frontend($widget_args, $content= '', $context= '')
Frontend logic.
static is_admin()
Check if is_admin(), and make sure not DOING_AJAX.
pre_render_frontend()
General validations when rendering the widget.
global $post
has_gravityview_shortcode($post=NULL)
Check whether the post is GravityView.
static getInstance($passed_post=NULL)
maybe_do_shortcode($text, $widget=NULL)
Do shortcode if the Widget&#39;s shortcode exists.
if(empty($field_settings['content'])) $content
Definition: custom.php:37