Completed
Push — master ( 79386d...2cfa6f )
by Zack
18:13 queued 12:52
created

render_widgets_active_areas()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 15
c 1
b 0
f 0
nc 2
nop 3
dl 0
loc 23
rs 9.0856
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 19 and the first side effect is on line 16.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Renders all the metaboxes on Add New / Edit View post type.
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    Katz Web Services, Inc.
8
 * @link      http://gravityview.co
9
 * @copyright Copyright 2014, Katz Web Services, Inc.
10
 *
11
 * @since 1.0.0
12
 */
13
14
/** If this file is called directly, abort. */
15
if ( ! defined( 'ABSPATH' ) ) {
16
	die;
17
}
18
19
class GravityView_Admin_Views {
20
21
22
23
	function __construct() {
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...
24
25
		add_action( 'save_post', array( $this, 'save_postdata' ) );
26
27
		// set the blacklist field types across the entire plugin
28
		add_filter( 'gravityview_blacklist_field_types', array( $this, 'default_field_blacklist' ), 10, 2 );
29
30
		// Tooltips
31
		add_filter( 'gform_tooltips', array( $this, 'tooltips') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
32
33
		// adding styles and scripts
34
		add_action( 'admin_enqueue_scripts', array( 'GravityView_Admin_Views', 'add_scripts_and_styles'), 999 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
35
		add_filter( 'gform_noconflict_styles', array( $this, 'register_no_conflict') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
36
		add_filter( 'gform_noconflict_scripts', array( $this, 'register_no_conflict') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
37
		add_filter( 'gravityview_noconflict_styles', array( $this, 'register_no_conflict') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
38
		add_filter( 'gravityview_noconflict_scripts', array( $this, 'register_no_conflict') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
39
40
		add_action( 'gravityview_render_directory_active_areas', array( $this, 'render_directory_active_areas'), 10, 4 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
41
		add_action( 'gravityview_render_widgets_active_areas', array( $this, 'render_widgets_active_areas'), 10, 3 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
42
		add_action( 'gravityview_render_available_fields', array( $this, 'render_available_fields'), 10, 2 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
43
		add_action( 'gravityview_render_available_widgets', array( $this, 'render_available_widgets') );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
44
		add_action( 'gravityview_render_active_areas', array( $this, 'render_active_areas'), 10, 5 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
45
46
		// @todo check if this hook is needed..
47
		//add_action( 'gravityview_render_field_options', array( $this, 'render_field_options'), 10, 9 );
0 ignored issues
show
Unused Code Comprehensibility introduced by
66% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
48
49
		// Add Connected Form column
50
		add_filter('manage_gravityview_posts_columns' , array( $this, 'add_post_type_columns' ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
51
52
		add_filter( 'gform_toolbar_menu', array( 'GravityView_Admin_Views', 'gform_toolbar_menu' ), 10, 2 );
53
54
		add_action( 'manage_gravityview_posts_custom_column', array( $this, 'add_custom_column_content'), 10, 2 );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
55
56
		add_action( 'restrict_manage_posts', array( $this, 'add_view_dropdown' ) );
57
58
		add_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts_by_gravityview_form_id' ) );
59
60
	}
61
62
	/**
63
	 * @since 1.15
64
	 * @param WP_Query $query
65
	 */
66
	public function filter_pre_get_posts_by_gravityview_form_id( &$query ) {
67
		global $pagenow;
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...
68
69
		if ( !is_admin() ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
70
			return;
71
		}
72
73
		if( 'edit.php' !== $pagenow || ! rgget( 'gravityview_form_id' ) || ! isset( $query->query_vars[ 'post_type' ] ) ) {
0 ignored issues
show
introduced by
Array keys should NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
74
			return;
75
		}
76
77
		if ( $query->query_vars[ 'post_type' ] == 'gravityview' ) {
0 ignored issues
show
introduced by
Found "== '". Use Yoda Condition checks, you must
Loading history...
introduced by
Array keys should NOT be surrounded by spaces if they only contain a string or an integer.
Loading history...
78
			$query->set( 'meta_query', array(
79
				array(
80
					'key' => '_gravityview_form_id',
81
					'value' => rgget( 'gravityview_form_id' ),
82
				)
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
83
			) );
84
		}
85
	}
86
87
	function add_view_dropdown() {
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...
88
		$current_screen = get_current_screen();
89
90
		if( 'gravityview' !== $current_screen->post_type ) {
91
			return;
92
		}
93
94
		$forms = gravityview_get_forms();
95
		$current_form = rgget( 'gravityview_form_id' );
96
		// If there are no forms to select, show no forms.
97
		if( !empty( $forms ) ) { ?>
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
98
			<select name="gravityview_form_id" id="gravityview_form_id">
99
				<option value="" <?php selected( '', $current_form, true ); ?>><?php esc_html_e( 'All forms', 'gravityview' ); ?></option>
100
				<?php foreach( $forms as $form ) { ?>
101
					<option value="<?php echo $form['id']; ?>" <?php selected( $form['id'], $current_form, true ); ?>><?php echo esc_html( $form['title'] ); ?></option>
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$form'
Loading history...
102
				<?php } ?>
103
			</select>
104
		<?php }
105
	}
106
107
108
	/**
109
	 * @deprecated since 1.2
110
	 * Start using GravityView_Render_Settings::render_setting_row
111
	 */
112
	public static function render_setting_row( $key = '', $current_settings = array(), $override_input = null, $name = 'template_settings[%s]', $id = 'gravityview_se_%s' ) {
113
		_deprecated_function( 'GravityView_Admin_Views::render_setting_row', '1.1.7', 'GravityView_Render_Settings::render_setting_row' );
114
		GravityView_Render_Settings::render_setting_row( $key, $current_settings, $override_input, $name , $id );
115
	}
116
117
	/**
118
	 * @deprecated since 1.2
119
	 * Start using GravityView_Render_Settings::render_field_option
120
	 */
121
	public static function render_field_option( $name = '', $option, $curr_value = NULL ) {
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
122
		_deprecated_function( 'GravityView_Admin_Views::render_field_option', '1.1.7', 'GravityView_Render_Settings::render_field_option' );
123
		return GravityView_Render_Settings::render_field_option( $name, $option, $curr_value );
124
	}
125
126
127
	/**
128
	 * Add a GravityView menu to the Form Toolbar with connected views
129
	 * @param  array  $menu_items Menu items, as set in GFForms::top_toolbar()
130
	 * @param  int $id         ID of the current Gravity form
131
	 * @return array            Modified array
132
	 */
133
	static function gform_toolbar_menu( $menu_items = array(), $id = NULL ) {
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...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
134
135
		$connected_views = gravityview_get_connected_views( $id );
136
137
		if( empty( $connected_views ) ) {
138
			return $menu_items;
139
		}
140
141
		$sub_menu_items = array();
142
		foreach ( (array)$connected_views as $view ) {
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
143
144
			if( ! GVCommon::has_cap( 'edit_gravityview', $view->ID ) ) {
145
				continue;
146
			}
147
148
			$label = empty( $view->post_title ) ? sprintf( __('No Title (View #%d)', 'gravityview' ), $view->ID ) : $view->post_title;
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
149
150
			$sub_menu_items[] = array(
151
				'label' => esc_attr( $label ),
152
				'url' => admin_url( 'post.php?action=edit&post='.$view->ID ),
153
			);
154
		}
155
156
		// If there were no items added, then let's create the parent menu
157
		if( $sub_menu_items ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $sub_menu_items of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
158
159
			// Make sure Gravity Forms uses the submenu; if there's only one item, it uses a link instead of a dropdown
160
			$sub_menu_items[] = array(
161
				'url' => '#',
162
				'label' => '',
163
				'menu_class' => 'hidden',
164
				'capabilities' => '',
165
			);
166
167
			$menu_items['gravityview'] = array(
168
				'label'          => __( 'Connected Views', 'gravityview' ),
169
				'icon'           => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>',
170
				'title'          => __( 'GravityView Views using this form as a data source', 'gravityview' ),
171
				'url'            => '#',
172
				'onclick'        => 'return false;',
173
				'menu_class'     => 'gv_connected_forms gf_form_toolbar_settings',
174
				'link_class'     => ( 1 === 1 ? '' : 'gf_toolbar_disabled' ),
175
				'sub_menu_items' => $sub_menu_items,
176
				'priority'       => 0,
177
				'capabilities'   => array( 'edit_gravityviews' ),
178
			);
179
		}
180
181
		return $menu_items;
182
	}
183
184
	/**
185
	 * List the field types without presentation properties (on a View context)
186
	 *
187
	 * @param array $array Existing field types to add to a blacklist
188
	 * @param string|null $context Context for the blacklist. Default: NULL.
189
	 * @access public
190
	 * @return array Default blacklist fields merged with existing blacklist fields
191
	 */
192
	function default_field_blacklist( $array = array(), $context = NULL ) {
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...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
193
194
		$add = array( 'captcha', 'page' );
195
196
		// Don't allowing editing the following values:
197
		if( $context === 'edit' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
198
			$add[] = 'post_id';
199
		}
200
201
		$return = array_merge( $array, $add );
202
203
		return $return;
204
	}
205
206
	/**
207
	 * Add tooltip text for use throughout the UI
208
	 * @param  array       $tooltips Array of Gravity Forms tooltips
209
	 * @return array                Modified tooltips array
210
	 */
211
	public function tooltips( $tooltips = array() ) {
212
213
		$gv_tooltips = array();
214
215
		// Generate tooltips for View settings
216
		$default_args = GravityView_View_Data::get_default_args( true );
217
218
		foreach ( $default_args as $key => $arg ) {
219
220
			// If an arg has `tooltip` defined, but it's false, don't display a tooltip
221
			if( isset( $arg['tooltip'] ) && empty( $arg['tooltip'] ) ) { continue; }
222
223
			// By default, use `tooltip` if defined.
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
224
			$tooltip = empty( $arg['tooltip'] ) ? NULL : $arg['tooltip'];
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
225
226
			// Otherwise, use the description as a tooltip.
227
			if( empty( $tooltip ) && !empty( $arg['desc'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
228
				$tooltip = $arg['desc'];
229
			}
230
231
			// If there's no tooltip set, continue
232
			if( empty( $tooltip ) ) {
233
				continue;
234
			}
235
236
			// Add the tooltip
237
			$gv_tooltips[ 'gv_'.$key ] = array(
238
				'title'	=> $arg['label'],
239
				'value'	=> $tooltip,
240
			);
241
242
		}
243
244
		$gv_tooltips['gv_css_merge_tags'] = array(
245
			'title' => __('CSS Merge Tags', 'gravityview'),
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...
246
			'value' => sprintf( __( 'Developers: The CSS classes will be sanitized using the %ssanitize_title_with_dashes()%s function.', 'gravityview'), '<code>', '</code>' )
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
247
		);
248
249
		/**
250
		 * @filter `gravityview_tooltips` The tooltips GravityView adds to the Gravity Forms tooltip array
251
		 * @param array $gv_tooltips Associative array with unique keys containing array of `title` and `value` keys, as expected by `gform_tooltips` filter
252
		 */
253
		$gv_tooltips = apply_filters( 'gravityview_tooltips', $gv_tooltips );
254
255
		foreach ( $gv_tooltips as $key => $tooltip ) {
256
257
			$title = empty( $tooltip['title'] ) ? '' : '<h6>'.esc_html( $tooltip['title'] ) .'</h6>';
258
259
			$tooltips[ $key ] = $title . wpautop( esc_html( $tooltip['value'] ) );
260
		}
261
262
		return $tooltips;
263
	}
264
265
	/**
266
	 * Add the Data Source information
267
	 *
268
	 * @param null $column_name
269
	 * @param $post_id
270
	 *
271
	 * @return void
272
	 */
273
	public function add_custom_column_content( $column_name = NULL, $post_id )	{
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
274
275
		$output = '';
276
277
		switch ( $column_name ) {
278
			case 'gv_template':
279
280
				$template_id = gravityview_get_template_id( $post_id );
281
282
				// All Views should have a connected form. If it doesn't, that's not right.
283
				if ( empty( $template_id ) ) {
284
					do_action( 'gravityview_log_error', sprintf( __METHOD__ . ' View ID %s does not have a connected template.', $post_id ) );
285
					break;
286
				}
287
288
				$templates = gravityview_get_registered_templates();
289
290
				$template = isset( $templates[ $template_id ] ) ? $templates[ $template_id ] : false;
291
292
				// Generate backup if label doesn't exist: `example_name` => `Example Name`
293
				$template_id_pretty = ucwords( implode( ' ', explode( '_', $template_id ) ) );
294
295
				$output = $template ? $template['label'] : $template_id_pretty;
296
297
				break;
298
299
			case 'gv_connected_form':
300
301
				$form_id = gravityview_get_form_id( $post_id );
302
303
				// All Views should have a connected form. If it doesn't, that's not right.
304
				if ( empty( $form_id ) ) {
305
					do_action( 'gravityview_log_error', sprintf( '[add_data_source_column_content] View ID %s does not have a connected GF form.', $post_id ) );
306
					$output = __( 'Not connected.', 'gravityview' );
307
					break;
308
				}
309
310
				$form = gravityview_get_form( $form_id );
311
312
				if ( ! $form ) {
313
					do_action( 'gravityview_log_error', sprintf( '[add_data_source_column_content] Connected form not found: Form #%d', $form_id ) );
314
315
					$output = __( 'The connected form can not be found; it may no longer exist.', 'gravityview' );
316
				} else {
317
					$output = self::get_connected_form_links( $form );
318
				}
319
320
				break;
321
		}
322
323
		echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
324
	}
325
326
327
	/**
328
	 * Get HTML links relating to a connected form, like Edit, Entries, Settings, Preview
329
	 * @param  array|int $form Gravity Forms forms array, or the form ID
330
	 * @param  boolean $include_form_link Whether to include the bold name of the form in the output
331
	 * @return string          HTML links
332
	 */
333
	static public function get_connected_form_links( $form, $include_form_link = true ) {
0 ignored issues
show
Coding Style introduced by
As per PSR2, the static declaration should come after the visibility declaration.
Loading history...
334
335
		// Either the form is empty or the form ID is 0, not yet set.
336
		if( empty( $form ) ) {
337
			return '';
338
		}
339
340
		// The $form is passed as the form ID
341
		if( !is_array( $form ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
342
			$form = gravityview_get_form( $form );
343
		}
344
345
		$form_id = $form['id'];
346
		$links = array();
347
348
		if( GVCommon::has_cap( 'gravityforms_edit_forms' ) ) {
349
			$form_url = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;id=%d', $form_id ) );
350
			$form_link = '<strong class="gv-form-title">'.gravityview_get_link( $form_url, $form['title'], 'class=row-title' ).'</strong>';
351
			$links[] = '<span>'.gravityview_get_link( $form_url, __('Edit Form', 'gravityview') ).'</span>';
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...
352
		} else {
353
			$form_link = '<strong class="gv-form-title">'. esc_html( $form['title'] ). '</strong>';
354
		}
355
356
		if( GVCommon::has_cap( 'gravityforms_view_entries' ) ) {
357
			$entries_url = admin_url( sprintf( 'admin.php?page=gf_entries&amp;id=%d', $form_id ) );
358
			$links[] = '<span>'.gravityview_get_link( $entries_url, __('Entries', 'gravityview') ).'</span>';
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...
359
		}
360
361
		if( GVCommon::has_cap( array( 'gravityforms_edit_settings', 'gravityview_view_settings' ) ) ) {
362
			$settings_url = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;view=settings&amp;id=%d', $form_id ) );
363
			$links[] = '<span>'.gravityview_get_link( $settings_url, __('Settings', 'gravityview'), 'title='.__('Edit settings for this form', 'gravityview') ).'</span>';
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...
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
364
		}
365
366
		if( GVCommon::has_cap( array("gravityforms_edit_forms", "gravityforms_create_form", "gravityforms_preview_forms") ) ) {
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
Coding Style Comprehensibility introduced by
The string literal gravityforms_edit_forms does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal gravityforms_create_form does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
Coding Style Comprehensibility introduced by
The string literal gravityforms_preview_forms does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
introduced by
No space before closing parenthesis of array is bad style
Loading history...
367
			$preview_url = site_url( sprintf( '?gf_page=preview&amp;id=%d', $form_id ) );
368
			$links[] = '<span>'.gravityview_get_link( $preview_url, __('Preview Form', 'gravityview'), 'title='.__('Preview this form', 'gravityview') ).'</span>';
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...
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw '__'
Loading history...
369
		}
370
371
		$output = '';
372
373
		if( !empty( $include_form_link ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
374
			$output .= $form_link;
375
		}
376
377
		/**
378
		 * @filter `gravityview_connected_form_links` Modify the links shown in the Connected Form links
379
		 * @since 1.6
380
		 * @param array $links Links to show
381
		 * @param array $form Gravity Forms form array
382
		 */
383
		$links = apply_filters( 'gravityview_connected_form_links', $links, $form );
384
385
		$output .= '<div class="row-actions">'. implode( ' | ', $links ) .'</div>';
386
387
		return $output;
388
	}
389
390
	/**
391
	 * Add the Data Source column to the Views page
392
	 * @param  array      $columns Columns array
393
	 */
394
	public function add_post_type_columns( $columns ) {
395
396
		// Get the date column and save it for later to add back in.
397
		// This adds it after the Data Source column.
398
		// This way, we don't need to do array_slice, array_merge, etc.
399
		$date = $columns['date'];
400
		unset( $columns['date'] );
401
402
		$data_source_required_caps = array(
403
			'gravityforms_edit_forms',
404
			'gravityforms_view_entries',
405
			'gravityforms_edit_settings',
406
			'gravityview_view_settings',
407
			'gravityforms_create_form',
408
			'gravityforms_preview_forms',
409
		);
410
411
		if( GVCommon::has_cap( $data_source_required_caps ) ) {
412
			$columns['gv_connected_form'] = __( 'Data Source', 'gravityview' );
413
		}
414
415
		$columns['gv_template'] = _x( 'Template', 'Column title that shows what template is being used for Views', 'gravityview' );
416
417
		// Add the date back in.
418
		$columns['date'] = $date;
419
420
		return $columns;
421
	}
422
423
	/**
424
	 * Save View configuration
425
	 *
426
	 * @access public
427
	 * @param int $post_id Currently saved Post ID
428
	 * @return void
429
	 */
430
	function save_postdata( $post_id ) {
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...
431
432
		if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
433
			return;
434
		}
435
436
		// validate post_type
437
		if ( ! isset( $_POST['post_type'] ) || 'gravityview' != $_POST['post_type'] ) {
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
438
			return;
439
		}
440
441
		// validate user can edit and save View
442
		if ( ! GVCommon::has_cap( 'edit_gravityview', $post_id ) ) {
443
			do_action( 'gravityview_log_error', __METHOD__ . ' - Current user does not have the capability to edit View #' . $post_id, wp_get_current_user() );
444
			return;
445
		}
446
447
		do_action( 'gravityview_log_debug', '[save_postdata] Saving View post type.', $_POST );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
448
449
		$statii = array();
450
451
		// check if this is a start fresh View
452
		if ( isset( $_POST['gravityview_select_form_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_form_nonce'], 'gravityview_select_form' ) ) {
453
454
			$form_id = !empty( $_POST['gravityview_form_id'] ) ? $_POST['gravityview_form_id'] : '';
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
455
			// save form id
456
			$statii['form_id'] = update_post_meta( $post_id, '_gravityview_form_id', $form_id );
457
458
		}
459
460
		if( false === GVCommon::has_cap( 'gravityforms_create_form' ) && empty( $statii['form_id'] ) ) {
461
			do_action( 'gravityview_log_error', __METHOD__ . ' - Current user does not have the capability to create a new Form.', wp_get_current_user() );
462
			return;
463
		}
464
465
		// Was this a start fresh?
466
		if ( ! empty( $_POST['gravityview_form_id_start_fresh'] ) ) {
467
			$statii['start_fresh'] = add_post_meta( $post_id, '_gravityview_start_fresh', 1 );
468
		} else {
469
			$statii['start_fresh'] = delete_post_meta( $post_id, '_gravityview_start_fresh' );
470
		}
471
472
		// Check if we have a template id
473
		if ( isset( $_POST['gravityview_select_template_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_template_nonce'], 'gravityview_select_template' ) ) {
474
475
			$template_id = !empty( $_POST['gravityview_directory_template'] ) ? $_POST['gravityview_directory_template'] : '';
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
476
477
			// now save template id
478
			$statii['directory_template'] = update_post_meta( $post_id, '_gravityview_directory_template', $template_id );
479
		}
480
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
481
482
		// save View Configuration metabox
483
		if ( isset( $_POST['gravityview_view_configuration_nonce'] ) && wp_verify_nonce( $_POST['gravityview_view_configuration_nonce'], 'gravityview_view_configuration' ) ) {
484
485
			// template settings
486
			if( empty( $_POST['template_settings'] ) ) {
487
				$_POST['template_settings'] = array();
488
			}
489
			$statii['template_settings'] = update_post_meta( $post_id, '_gravityview_template_settings', $_POST['template_settings'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
490
491
			$fields = array();
492
493
			// Directory&single Visible Fields
494
			if( !empty( $preset_fields ) ) {
0 ignored issues
show
Bug introduced by
The variable $preset_fields does not exist. Did you mean $fields?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
495
496
				$fields = $preset_fields;
497
498
			} elseif( !empty( $_POST['gv_fields'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
499
				$fields = _gravityview_process_posted_fields();
500
			}
501
502
			$statii['directory_fields'] = update_post_meta( $post_id, '_gravityview_directory_fields', $fields );
503
504
			// Directory Visible Widgets
505
			if( empty( $_POST['widgets'] ) ) {
506
				$_POST['widgets'] = array();
507
			}
508
			$statii['directory_widgets'] = gravityview_set_directory_widgets( $post_id, $_POST['widgets'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
509
510
		} // end save view configuration
511
512
		/**
513
		 * @action `gravityview_view_saved` After a View has been saved in the admin
514
		 * @param int $post_id ID of the View that has been saved
515
		 * @param array $statii Array of statuses of the post meta saving processes. If saving worked, each key should be mapped to a value of the post ID (`directory_widgets` => `124`). If failed (or didn't change), the value will be false.
516
		 * @since 1.17.2
517
		 */
518
		do_action('gravityview_view_saved', $post_id, $statii );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
519
520
		do_action('gravityview_log_debug', '[save_postdata] Update Post Meta Statuses (also returns false if nothing changed)', array_map( 'intval', $statii ) );
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after opening bracket; 0 found
Loading history...
521
	}
522
523
	/**
524
	 * @deprecated 1.1.6
525
	 */
526
	function render_label() {
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...
527
		_deprecated_function( 'GravityView_Admin_Views::render_label()', '1.1.6', 'Use the GravityView_Admin_View_Field class instead.' );
528
	}
529
530
	/**
531
	 * Render html for displaying available fields based on a Form ID
532
	 * $blacklist_field_types - contains the field types which are not proper to be shown in a directory.
533
	 *
534
	 * @access public
535
	 * @param int $form Gravity Forms Form ID (default: '')
536
	 * @param string $context (default: 'single')
537
	 * @return void
538
	 */
539
	function render_available_fields( $form = 0, $context = 'single' ) {
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...
540
541
		/**
542
		 * @filter  `gravityview_blacklist_field_types` Modify the types of fields that shouldn't be shown in a View.
543
		 * @param[in,out] array $blacklist_field_types Array of field types to block for this context.
544
		 * @param[in] string $context View context ('single', 'directory', or 'edit')
545
		 */
546
		$blacklist_field_types = apply_filters( 'gravityview_blacklist_field_types', array(), $context );
547
548
		$fields = $this->get_available_fields( $form, $context );
549
550
		$output = '';
551
552
		if( !empty( $fields ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
553
554
			foreach( $fields as $id => $details ) {
555
556
				if( in_array( $details['type'], $blacklist_field_types ) ) {
557
					continue;
558
				}
559
560
				// Edit mode only allows editing the parent fields, not single inputs.
561
				if( $context === 'edit' && !empty( $details['parent'] ) ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
introduced by
Expected 1 space after "!"; 0 found
Loading history...
562
					continue;
563
				}
564
565
				$output .= new GravityView_Admin_View_Field( $details['label'], $id, $details );
566
567
			} // End foreach
568
		}
569
570
		echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
571
572
		// For the EDIT view we only want to allow the form fields.
573
		if( $context === 'edit' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
574
			return;
575
		}
576
577
		$this->render_additional_fields( $form, $context );
578
	}
579
580
	/**
581
	 * Render html for displaying additional fields based on a Form ID
582
	 *
583
	 * @access public
584
	 * @param int $form Gravity Forms Form ID (default: '')
585
	 * @param string $context (default: 'single')
586
	 * @return void
587
	 */
588
	public function render_additional_fields( $form = 0, $context = 'single' ) {
0 ignored issues
show
Unused Code introduced by
The parameter $form 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...
Unused Code introduced by
The parameter $context 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...
589
590
		/**
591
		 * @filter `gravityview_additional_fields` non-standard Fields to show at the bottom of the field picker
592
		 * @param array $additional_fields Associative array of field arrays, with `label_text`, `desc`, `field_id`, `label_type`, `input_type`, `field_options`, and `settings_html` keys
593
		 */
594
		$additional_fields = apply_filters( 'gravityview_additional_fields', array(
595
			array(
596
				'label_text' => __( '+ Add All Fields', 'gravityview' ),
597
				'desc' => __('Add all the available fields at once.', 'gravityview'),
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...
598
				'field_id' => 'all-fields',
599
				'label_type' => 'field',
600
				'input_type' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
601
				'field_options' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
602
				'settings_html'	=> NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
603
			)
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
604
		));
605
606
		if( !empty( $additional_fields )) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
607
			foreach ( (array)$additional_fields as $item ) {
0 ignored issues
show
introduced by
No space after closing casting parenthesis is prohibited
Loading history...
608
609
				// Prevent items from not having index set
610
				$item = wp_parse_args( $item, array(
611
					'label_text' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
612
					'field_id' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
613
					'label_type' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
614
					'input_type' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
615
					'field_options' => NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
616
					'settings_html'	=> NULL,
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
617
				));
618
619
				// Backward compat.
620
				if( !empty( $item['field_options'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
621
					// Use settings_html from now on.
622
					$item['settings_html'] = $item['field_options'];
623
				}
624
625
				// Render a label for each of them
626
				echo new GravityView_Admin_View_Field( $item['label_text'], $item['field_id'], $item );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'new'
Loading history...
627
628
			}
629
		}
630
631
	}
632
633
	/**
634
	 * Retrieve the default fields id, label and type
635
	 * @param  string|array $form form_ID or form object
636
	 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
637
	 * @return array
638
	 */
639
	function get_entry_default_fields($form, $zone) {
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...
640
641
		$entry_default_fields = array();
642
643
		if( in_array( $zone, array( 'directory', 'single' ) ) ) {
644
645
			$entry_default_fields = array(
646
				'id' => array(
647
					'label' => __('Entry ID', 'gravityview'),
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...
648
					'type' => 'id',
649
					'desc'	=> __('The unique ID of the entry.', 'gravityview'),
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...
650
				),
651
				'date_created' => array(
652
					'label' => __('Entry Date', 'gravityview'),
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...
653
					'desc'	=> __('The date the entry was created.', 'gravityview'),
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...
654
					'type' => 'date_created',
655
				),
656
				'source_url' => array(
657
					'label' => __('Source URL', 'gravityview'),
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...
658
					'type' => 'source_url',
659
					'desc'	=> __('The URL of the page where the form was submitted.', 'gravityview'),
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...
660
				),
661
				'ip' => array(
662
					'label' => __('User IP', 'gravityview'),
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...
663
					'type' => 'ip',
664
					'desc'	=> __('The IP Address of the user who created the entry.', 'gravityview'),
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...
665
				),
666
				'created_by' => array(
667
					'label' => __('User', 'gravityview'),
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...
668
					'type' => 'created_by',
669
					'desc'	=> __('Details of the logged-in user who created the entry (if any).', 'gravityview'),
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...
670
				),
671
672
				/**
673
				 * @since  1.2
674
				 */
675
				'custom'	=> array(
676
					'label'	=> __('Custom Content', 'gravityview'),
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...
677
					'type'	=> 'custom',
678
					'desc'	=> __('Insert custom text or HTML.', 'gravityview'),
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...
679
				),
680
681
				/**
682
				 * @since 1.7.2
683
				 */
684
			    'other_entries' => array(
685
				    'label'	=> __('Other Entries', 'gravityview'),
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...
686
				    'type'	=> 'other_entries',
687
				    'desc'	=> __('Display other entries created by the entry creator.', 'gravityview'),
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...
688
			    ),
689
	        );
690
691
			if( 'single' !== $zone) {
0 ignored issues
show
introduced by
No space before closing parenthesis is prohibited
Loading history...
692
693
				$entry_default_fields['entry_link'] = array(
694
					'label' => __('Link to Entry', 'gravityview'),
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...
695
					'desc'	=> __('A dedicated link to the single entry with customizable text.', 'gravityview'),
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...
696
					'type' => 'entry_link',
697
				);
698
			}
699
700
		} // if not zone directory or single
701
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
702
703
		/**
704
		 * @filter `gravityview_entry_default_fields` Modify the default fields for each zone and context
705
		 * @param array $entry_default_fields Array of fields shown by default
706
		 * @param  string|array $form form_ID or form object
707
		 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
708
		 */
709
		return apply_filters( 'gravityview_entry_default_fields', $entry_default_fields, $form, $zone);
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
710
	}
711
712
	/**
713
	 * Calculate the available fields
714
	 * @param  string|array $form form_ID or form object
715
	 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
716
	 * @return array         fields
717
	 */
718
	function get_available_fields( $form = '', $zone = NULL ) {
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...
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
719
720
		if( empty( $form ) ) {
721
			do_action( 'gravityview_log_error', '[get_available_fields] $form is empty' );
722
			return array();
723
		}
724
725
		// get form fields
726
		$fields = gravityview_get_form_fields( $form, true );
727
728
		// get meta fields ( only if form was already created )
729
		if( !is_array( $form ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
730
			$meta_fields = gravityview_get_entry_meta( $form );
731
		} else {
732
			$meta_fields = array();
733
		}
734
735
		// get default fields
736
		$default_fields = $this->get_entry_default_fields( $form, $zone );
737
738
		//merge without loosing the keys
739
		$fields = $fields + $meta_fields + $default_fields;
740
741
		return $fields;
742
	}
743
744
745
	/**
746
	 * Render html for displaying available widgets
747
	 * @return string html
748
	 */
749
	function render_available_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...
750
751
		$widgets = $this->get_registered_widgets();
752
753
		if( !empty( $widgets ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
754
755
			foreach( $widgets as $id => $details ) {
756
757
				echo new GravityView_Admin_View_Widget( $details['label'], $id, $details );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'new'
Loading history...
758
759
			}
760
		}
761
762
	}
763
764
	/**
765
	 * Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object
766
	 * @since 1.13.1
767
	 * @return array
768
	 */
769
	function get_registered_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...
770
		/**
771
		 * @filter `gravityview_register_directory_widgets` Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object
772
		 * @param array $registered_widgets Empty array
773
		 */
774
		$registered_widgets = apply_filters( 'gravityview_register_directory_widgets', array() );
775
776
		return $registered_widgets;
777
	}
778
779
	/**
780
	 * Generic function to render rows and columns of active areas for widgets & fields
781
	 * @param  string $template_id The current slug of the selected View template
782
	 * @param  string $type   Either 'widget' or 'field'
783
	 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
784
	 * @param  array $rows    The layout structure: rows, columns and areas
785
	 * @param  array $values  Saved objects
786
	 * @return void
787
	 */
788
	function render_active_areas( $template_id, $type, $zone, $rows, $values ) {
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...
789
		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...
790
791
		if( $type === 'widget' ) {
0 ignored issues
show
introduced by
Found "=== '". Use Yoda Condition checks, you must
Loading history...
792
			$button_label = __( 'Add Widget', 'gravityview' );
793
		} else {
794
			$button_label = __( 'Add Field', 'gravityview' );
795
		}
796
797
		$available_items = array();
798
799
		// if saved values, get available fields to label everyone
800
		if( !empty( $values ) && ( !empty( $post->ID ) || !empty( $_POST['template_id'] ) ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
801
802
			if( !empty( $_POST['template_id'] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
803
				$form = GravityView_Ajax::pre_get_form_fields( $_POST['template_id'] );
0 ignored issues
show
introduced by
Detected access of super global var $_POST, probably need manual inspection.
Loading history...
introduced by
Detected usage of a non-sanitized input variable: $_POST
Loading history...
804
			} else {
805
				$form = gravityview_get_form_id( $post->ID );
806
			}
807
808
			if( 'field' === $type ) {
809
				$available_items = $this->get_available_fields( $form, $zone );
810
			} else {
811
				$available_items = $this->get_registered_widgets();
812
			}
813
814
		}
815
816
		foreach( $rows as $row ) :
817
			foreach( $row as $col => $areas ) :
818
				$column = ($col == '2-2') ? '1-2' : $col; ?>
819
820
				<div class="gv-grid-col-<?php echo esc_attr( $column ); ?>">
821
822
					<?php foreach( $areas as $area ) : 	?>
823
824
						<div class="gv-droppable-area">
825
							<div class="active-drop active-drop-<?php echo esc_attr( $type ); ?>" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>">
826
827
								<?php // render saved fields
828
829
								if( !empty( $values[ $zone .'_'. $area['areaid'] ] ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
830
831
									foreach( $values[ $zone .'_'. $area['areaid'] ] as $uniqid => $field ) {
832
833
										$input_type = NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
834
										$original_item = isset( $available_items[ $field['id'] ] ) ? $available_items[ $field['id'] ] : false ;
835
836
										if( !$original_item ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
837
838
											do_action('gravityview_log_error', 'An item was not available when rendering the output; maybe it was added by a plugin that is now de-activated.', array('available_items' => $available_items, 'field' => $field ));
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...
introduced by
No space after opening parenthesis of array is bad style
Loading history...
839
840
											$original_item = $field;
841
										} else {
842
843
											$input_type = isset( $original_item['type'] ) ? $original_item['type'] : NULL;
0 ignored issues
show
Coding Style introduced by
TRUE, FALSE and NULL must be lowercase; expected null, but found NULL.
Loading history...
844
845
										}
846
847
										// Field options dialog box
848
										$field_options = GravityView_Render_Settings::render_field_options( $type, $template_id, $field['id'], $original_item['label'], $zone .'_'. $area['areaid'], $input_type, $uniqid, $field, $zone, $original_item );
849
850
										$item = array(
851
											'input_type' => $input_type,
852
											'settings_html' => $field_options,
853
											'label_type' => $type
0 ignored issues
show
introduced by
Each line in an array declaration must end in a comma
Loading history...
854
										);
855
856
										// Merge the values with the current item to pass things like widget descriptions and original field names
857
										if( $original_item ) {
858
											$item = wp_parse_args( $item, $original_item );
859
										}
860
861
										switch( $type ) {
862
											case 'widget':
863
												echo new GravityView_Admin_View_Widget( $item['label'], $field['id'], $item, $field );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'new'
Loading history...
864
												break;
865
											default:
866
												echo new GravityView_Admin_View_Field( $item['label'], $field['id'], $item, $field );
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not 'new'
Loading history...
867
										}
868
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
869
870
										//endif;
871
872
									}
873
874
								} // End if zone is not empty ?>
875
876
								<span class="drop-message"><?php echo sprintf(esc_attr__('"+ %s" or drag existing %ss here.', 'gravityview'), $button_label, $type ); ?></span>
0 ignored issues
show
introduced by
Expected a sanitizing function (see Codex for 'Data Validation'), but instead saw 'sprintf'
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...
877
							</div>
878
							<div class="gv-droppable-area-action">
879
								<a href="#" class="gv-add-field button-secondary" title="" data-objecttype="<?php echo esc_attr( $type ); ?>" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>" data-context="<?php echo esc_attr( $zone ); ?>"><?php echo '+ '.esc_html( $button_label ); ?></a>
880
								<p class="gv-droppable-area-title"><strong><?php echo esc_html( $area['title'] ); ?></strong><?php if( !empty( $area['subtitle'] ) ) { ?><span class="gv-droppable-area-subtitle"> &ndash; <?php echo esc_html( $area['subtitle'] ); ?></span><?php } ?></p>
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
881
							</div>
882
						</div>
883
884
					<?php endforeach; ?>
885
886
				</div>
887
			<?php endforeach;
888
		endforeach;
889
	}
890
891
	/**
892
	 * Render the widget active areas
893
	 * @param  string $zone    Either 'header' or 'footer'
894
	 * @param  string $post_id Current Post ID (view)
895
	 * @return string          html
896
	 */
897
	function render_widgets_active_areas( $template_id = '', $zone, $post_id = '' ) {
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...
898
899
		$default_widget_areas = GravityView_Plugin::get_default_widget_areas();
900
901
		$widgets = array();
902
		if( !empty( $post_id ) ) {
0 ignored issues
show
introduced by
Expected 1 space after "!"; 0 found
Loading history...
903
			$widgets = gravityview_get_directory_widgets( $post_id );
904
		}
905
906
		ob_start();
907
		?>
908
909
		<div class="gv-grid gv-grid-pad gv-grid-border" id="directory-<?php echo $zone; ?>-widgets">
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$zone'
Loading history...
910
			<?php $this->render_active_areas( $template_id, 'widget', $zone, $default_widget_areas, $widgets ); ?>
911
		</div>
912
913
		<?php
914
		$output = ob_get_clean();
915
916
		echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
917
918
		return $output;
919
	}
920
921
	/**
922
	 * Render the Template Active Areas and configured active fields for a given template id and post id
923
	 *
924
	 * @access public
925
	 * @param string $template_id (default: '') Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()}
926
	 * @param string $post_id (default: '')
927
	 * @param string $context (default: 'single')
928
	 * @return string HTML of the active areas
929
	 */
930
	function render_directory_active_areas( $template_id = '', $context = 'single', $post_id = '', $echo = false ) {
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...
931
932
		if( empty( $template_id ) ) {
933
			do_action( 'gravityview_log_debug', '[render_directory_active_areas] $template_id is empty' );
934
			return '';
935
		}
936
937
		/**
938
		 * @filter `gravityview_template_active_areas` 
939
		 * @see GravityView_Template::assign_active_areas()
940
		 * @param array $template_areas Empty array, to be filled in by the template class
941
		 * @param string $template_id Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()}
942
		 * @param string $context Current View context: `directory`, `single`, or `edit` (default: 'single')
943
		 */
944
		$template_areas = apply_filters( 'gravityview_template_active_areas', array(), $template_id, $context );
945
946
		if( empty( $template_areas ) ) {
947
948
			do_action( 'gravityview_log_debug', '[render_directory_active_areas] No areas defined. Maybe template %s is disabled.', $template_id );
949
			$output = '<div>';
950
			$output .= '<h2 class="description" style="font-size: 16px; margin:0">'. sprintf( esc_html__( 'This View is configured using the %s View type, which is disabled.', 'gravityview' ), '<em>'.$template_id.'</em>' ) .'</h2>';
951
			$output .= '<p class="description" style="font-size: 14px; margin:0 0 1em 0;padding:0">'.esc_html__('The data is not lost; re-activate the associated plugin and the configuration will re-appear.', 'gravityview').'</p>';
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...
952
			$output .= '</div>';
953
		} else {
954
955
			$fields = '';
956
			if ( ! empty( $post_id ) ) {
957
				$fields = gravityview_get_directory_fields( $post_id );
958
			}
959
960
			ob_start();
961
			$this->render_active_areas( $template_id, 'field', $context, $template_areas, $fields );
962
			$output = ob_get_clean();
963
964
		}
965
966
		if( $echo ) {
967
			echo $output;
0 ignored issues
show
introduced by
Expected next thing to be a escaping function, not '$output'
Loading history...
968
		}
969
970
		return $output;
971
	}
972
973
	/**
974
	 * Enqueue scripts and styles at Views editor
975
	 *
976
	 * @access public
977
	 * @param mixed $hook
978
	 * @return void
979
	 */
980
	static function add_scripts_and_styles( $hook ) {
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...
981
		global $plugin_page, $pagenow;
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...
982
983
		$is_widgets_page = ( $pagenow === 'widgets.php' );
984
985
		// Add the GV font (with the Astronaut)
986
		wp_enqueue_style( 'gravityview_global', plugins_url('assets/css/admin-global.css', GRAVITYVIEW_FILE), array(), GravityView_Plugin::version );
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...
987
988
		wp_register_script( 'gravityview-jquery-cookie', plugins_url('assets/lib/jquery.cookie/jquery.cookie.min.js', GRAVITYVIEW_FILE), array( 'jquery' ), GravityView_Plugin::version, true );
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...
989
990
		// Don't process any scripts below here if it's not a GravityView page.
991
		if( !gravityview_is_admin_page($hook) && !$is_widgets_page ) { return; }
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...
992
993
		// Only enqueue the following on single pages
994
		if( gravityview_is_admin_page($hook, 'single') || $is_widgets_page ) {
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...
995
996
			wp_enqueue_script( 'jquery-ui-datepicker' );
997
			wp_enqueue_style( 'gravityview_views_datepicker', plugins_url('assets/css/admin-datepicker.css', GRAVITYVIEW_FILE), GravityView_Plugin::version );
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...
998
999
			$script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
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...
1000
1001
			//enqueue scripts
1002
			wp_enqueue_script( 'gravityview_views_scripts', plugins_url( 'assets/js/admin-views' . $script_debug . '.js', GRAVITYVIEW_FILE ), array( 'jquery-ui-tabs', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-ui-tooltip', 'jquery-ui-dialog', 'gravityview-jquery-cookie', 'jquery-ui-datepicker', 'underscore' ), GravityView_Plugin::version );
1003
1004
			wp_localize_script('gravityview_views_scripts', 'gvGlobals', array(
1005
				'cookiepath' => COOKIEPATH,
1006
				'nonce' => wp_create_nonce( 'gravityview_ajaxviews' ),
1007
				'label_viewname' => __( 'Enter View name here', 'gravityview' ),
1008
				'label_close' => __( 'Close', 'gravityview' ),
1009
				'label_cancel' => __( 'Cancel', 'gravityview' ),
1010
				'label_continue' => __( 'Continue', 'gravityview' ),
1011
				'label_ok' => __( 'Ok', 'gravityview' ),
1012
				'label_publisherror' => __( 'Error while creating the View for you. Check the settings or contact GravityView support.', 'gravityview' ),
1013
				'loading_text' => esc_html__( 'Loading&hellip;', 'gravityview' ),
1014
				'loading_error' => esc_html__( 'There was an error loading dynamic content.', 'gravityview' ),
1015
				'field_loaderror' => __( 'Error while adding the field. Please try again or contact GravityView support.', 'gravityview' ),
1016
				'remove_all_fields' => __( 'Would you like to remove all fields in this zone? (You are seeing this message because you were holding down the ALT key)', 'gravityview' ),
1017
			));
1018
1019
			wp_enqueue_style( 'gravityview_views_styles', plugins_url( 'assets/css/admin-views.css', GRAVITYVIEW_FILE ), array('dashicons', 'wp-jquery-ui-dialog' ), GravityView_Plugin::version );
0 ignored issues
show
introduced by
No space after opening parenthesis of array is bad style
Loading history...
1020
1021
			self::enqueue_gravity_forms_scripts();
1022
1023
		} // End single page
1024
	}
1025
1026
	static function enqueue_gravity_forms_scripts() {
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...
1027
		GFForms::register_scripts();
1028
1029
		$scripts = array(
1030
		    'sack',
1031
		    'gform_gravityforms',
1032
		    'gform_forms',
1033
		    'gform_form_admin',
1034
		    'jquery-ui-autocomplete'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
1035
		);
1036
1037
		if ( wp_is_mobile() ) {
1038
		    $scripts[] = 'jquery-touch-punch';
1039
		}
1040
1041
		foreach ($scripts as $script) {
0 ignored issues
show
introduced by
No space after opening parenthesis is prohibited
Loading history...
introduced by
No space before closing parenthesis is prohibited
Loading history...
1042
			wp_enqueue_script( $script );
1043
		}
1044
	}
1045
1046
	/**
1047
	 * Add GravityView scripts and styles to Gravity Forms and GravityView No-Conflict modes
1048
	 *
1049
	 * @param array $registered Existing scripts or styles that have been registered (array of the handles)
1050
	 *
1051
	 * @return array
1052
	 */
1053
	function register_no_conflict( $registered ) {
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...
1054
1055
		$allowed_dependencies = array();
1056
1057
		$filter = current_filter();
1058
1059
		if ( preg_match( '/script/ism', $filter ) ) {
1060
1061
			$allowed_dependencies = array(
1062
				'jquery-ui-core',
1063
				'jquery-ui-dialog',
1064
				'jquery-ui-tabs',
1065
				'jquery-ui-draggable',
1066
				'jquery-ui-droppable',
1067
				'jquery-ui-sortable',
1068
				'jquery-ui-tooltip',
1069
				'gravityview_views_scripts',
1070
				'gravityview-support',
1071
				'gravityview-jquery-cookie',
1072
				'gravityview_views_datepicker',
1073
				'sack',
1074
				'gform_gravityforms',
1075
				'gform_forms',
1076
				'gform_form_admin',
1077
				'jquery-ui-autocomplete'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
1078
			);
1079
			
1080
		} elseif ( preg_match( '/style/ism', $filter ) ) {
1081
1082
			$allowed_dependencies = array(
1083
				'dashicons',
1084
				'wp-jquery-ui-dialog',
1085
				'gravityview_views_styles',
1086
				'gravityview_global',
1087
				'gravityview_views_datepicker'
0 ignored issues
show
introduced by
Comma required after last value in array declaration
Loading history...
1088
			);
1089
		}
1090
1091
		return array_merge( $registered, $allowed_dependencies );
1092
	}
1093
1094
1095
}
1096
1097
new GravityView_Admin_Views;
1098