Completed
Push — develop ( 507546...65eb42 )
by Zack
05:37
created

includes/widgets/class-gravityview-widget.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Main GravityView widget class
5
 */
6
class GravityView_Widget {
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 ( function_exists( 'gravityview' ) && gravityview()->request->is_admin() ) {
180
			return;
181
			/** Deprecated in favor of gravityview()->request->is_admin(). */
182
		} else if ( GravityView_Plugin::is_admin() ) {
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_Plugin::is_admin() has been deprecated.

This method has been deprecated.

Loading history...
183
			return;
184
		}
185
186
		if( empty( $this->shortcode_name ) ) { return; }
187
188
		// If the widget shouldn't output on single entries, don't show it
189
		if( empty( $this->show_on_single ) && class_exists('GravityView_frontend') && GravityView_frontend::is_single_entry() ) {
190
			do_action('gravityview_log_debug', sprintf( '%s[add_shortcode]: Skipping; set to not run on single entry.', get_class($this)) );
191
192
			add_shortcode( $this->shortcode_name, '__return_null' );
193
			return;
194
		}
195
196
197
		if( !has_gravityview_shortcode( $post ) ) {
198
199
			do_action('gravityview_log_debug', sprintf( '%s[add_shortcode]: No shortcode present; not adding render_frontend shortcode.', get_class($this)) );
200
201
			add_shortcode( $this->shortcode_name, '__return_null' );
202
			return;
203
		}
204
205
		add_shortcode( $this->shortcode_name, array( $this, 'render_shortcode') );
206
	}
207
208
	/**
209
	 * Register widget to become available in admin
210
	 * @param  array $widgets
211
	 * @return array $widgets
212
	 */
213
	function register_widget( $widgets ) {
214
		$widgets[ $this->widget_id ] = array(
215
			'label' => $this->widget_label ,
216
			'description' => $this->widget_description,
217
			'subtitle' => $this->widget_subtitle,
218
		);
219
		return $widgets;
220
	}
221
222
	/**
223
	 * Assign template specific field options
224
	 *
225
	 * @access protected
226
	 * @param array $options (default: array())
227
	 * @param string $template (default: '')
228
	 * @return array
229
	 */
230
	public function assign_widget_options( $options = array(), $template = '', $widget = '' ) {
231
232
		if( $this->widget_id === $widget ) {
233
			$options = array_merge( $options, $this->settings );
234
		}
235
236
		return $options;
237
	}
238
239
240
	/**
241
	 * Frontend logic
242
	 *
243
	 * @return void
244
	 */
245
	public function render_frontend( $widget_args, $content = '', $context = '') {
246
		// to be defined by child class
247
		if( !$this->pre_render_frontend() ) {
248
			return;
249
		}
250
	}
251
252
	/**
253
	 * General validations when rendering the widget
254
	 * @return boolean True: render frontend; False: don't render frontend
255
	 */
256
	public function pre_render_frontend() {
257
		$gravityview_view = GravityView_View::getInstance();
258
259
		if( empty( $gravityview_view ) ) {
260
			do_action('gravityview_log_debug', sprintf( '%s[render_frontend]: $gravityview_view not instantiated yet.', get_class($this)) );
261
			return false;
262
		}
263
264
		/**
265
		 * @filter `gravityview/widget/hide_until_searched` Modify whether to hide content until search
266
		 * @param boolean $hide_until_searched Hide until search?
267
		 * @param GravityView_Widget $this Widget instance
268
		 */
269
		$hide_until_search = apply_filters( 'gravityview/widget/hide_until_searched', $gravityview_view->hide_until_searched, $this );
270
271
		if( $hide_until_search ) {
272
			do_action('gravityview_log_debug', sprintf( '%s[render_frontend]: Hide View data until search is performed', get_class($this)) );
273
			return false;
274
		}
275
276
		return true;
277
	}
278
279
280
} // GravityView_Widget
281
282