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

GravityView_Widget::add_shortcode()   D

Complexity

Conditions 9
Paths 6

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 16
nc 6
nop 1
dl 0
loc 31
rs 4.909
c 0
b 0
f 0
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;
0 ignored issues
show
Unused Code introduced by
The property $widget_options is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
53
54
	function __construct( $widget_label , $widget_id , $defaults = array(), $settings = array() ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
introduced by
Expected 0 spaces between argument "$widget_label" and comma; 1 found
Loading history...
introduced by
Expected 0 spaces between argument "$widget_id" and comma; 1 found
Loading history...
55
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
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;
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
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') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
82
83
		// Use shortcodes in text widgets.
84
		add_filter('widget_text', array( $this, 'maybe_do_shortcode' ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
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 );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
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'),
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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;
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
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;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
141
142
		if( isset( $this->settings ) && is_array( $this->settings ) ) {
143
			$setting = isset( $this->settings[ $key ] ) ? $this->settings[ $key ] : NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $widget is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
156
157
		if( !empty( $this->shortcode_name ) && has_shortcode( $text, $this->shortcode_name ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
158
			return do_shortcode( $text );
159
		}
160
161
		return $text;
162
	}
163
164
	function render_shortcode( $atts, $content = '', $context = '' ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) {
0 ignored issues
show
Unused Code introduced by
The parameter $run_on_singular is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
177
		global $post;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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() ) {
0 ignored issues
show
Bug introduced by
The property show_on_single does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
190
			do_action('gravityview_log_debug', sprintf( '%s[add_shortcode]: Skipping; set to not run on single entry.', get_class($this)) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
191
192
			add_shortcode( $this->shortcode_name, '__return_null' );
193
			return;
194
		}
195
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
196
197
		if( !has_gravityview_shortcode( $post ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
198
199
			do_action('gravityview_log_debug', sprintf( '%s[add_shortcode]: No shortcode present; not adding render_frontend shortcode.', get_class($this)) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
200
201
			add_shortcode( $this->shortcode_name, '__return_null' );
202
			return;
203
		}
204
205
		add_shortcode( $this->shortcode_name, array( $this, 'render_shortcode') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
214
		$widgets[ $this->widget_id ] = array(
215
			'label' => $this->widget_label ,
0 ignored issues
show
introduced by
Expected 0 spaces between "widget_label" and comma; 1 found
Loading history...
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() ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
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)) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
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 );
0 ignored issues
show
Documentation introduced by
The property $hide_until_searched is declared protected in GravityView_View. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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)) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
273
			return false;
274
		}
275
276
		return true;
277
	}
278
279
280
} // GravityView_Widget
281
282