Completed
Push — master ( 2a60ef...645aad )
by Zack
53:50 queued 48:17
created

GravityView_Admin_Views   F

Complexity

Total Complexity 145

Size/Duplication

Total Lines 1267
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 8

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 1267
ccs 0
cts 463
cp 0
rs 0.8
c 0
b 0
f 0
wmc 145
lcom 1
cbo 8

27 Methods

Rating   Name   Duplication   Size   Complexity  
A render_setting_row() 0 4 1
A __construct() 0 41 1
A default_field_blacklist() 0 13 2
A render_label() 0 3 1
A suggest_support_articles() 0 19 2
B filter_pre_get_posts_by_gravityview_form_id() 0 22 7
A add_view_dropdown() 0 19 4
A render_field_option() 0 4 1
B gform_toolbar_menu() 0 79 8
B tooltips() 0 60 10
B add_custom_column_content() 0 52 8
B get_connected_form_links() 0 56 8
A add_post_type_columns() 0 28 2
F save_postdata() 0 92 22
B render_available_fields() 0 47 8
A render_additional_fields() 0 44 4
B get_entry_default_fields() 0 80 3
A get_available_fields() 0 34 3
A render_available_widgets() 0 14 3
A get_registered_widgets() 0 3 1
F render_active_areas() 0 112 25
B render_widgets_active_areas() 0 67 3
A render_field_pickers() 0 26 2
B render_directory_active_areas() 0 41 5
B add_scripts_and_styles() 0 50 6
A enqueue_gravity_forms_scripts() 0 17 2
A register_no_conflict() 0 40 3

How to fix   Complexity   

Complex Class

Complex classes like GravityView_Admin_Views often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use GravityView_Admin_Views, and based on these observations, apply Extract Interface, too.

1
<?php
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
		add_action( 'save_post', array( $this, 'save_postdata' ) );
25
26
		// set the blacklist field types across the entire plugin
27
		add_filter( 'gravityview_blacklist_field_types', array( $this, 'default_field_blacklist' ), 10, 2 );
28
29
		// Tooltips
30
		add_filter( 'gform_tooltips', array( $this, 'tooltips') );
31
32
		// adding styles and scripts
33
		add_action( 'admin_enqueue_scripts', array( 'GravityView_Admin_Views', 'add_scripts_and_styles'), 999 );
34
		add_filter( 'gform_noconflict_styles', array( $this, 'register_no_conflict') );
35
		add_filter( 'gform_noconflict_scripts', array( $this, 'register_no_conflict') );
36
		add_filter( 'gravityview_noconflict_styles', array( $this, 'register_no_conflict') );
37
		add_filter( 'gravityview_noconflict_scripts', array( $this, 'register_no_conflict') );
38
39
		add_action( 'gravityview_render_directory_active_areas', array( $this, 'render_directory_active_areas'), 10, 4 );
40
		add_action( 'gravityview_render_widgets_active_areas', array( $this, 'render_widgets_active_areas'), 10, 3 );
41
		add_action( 'gravityview_render_field_pickers', array( $this, 'render_field_pickers') );
42
		add_action( 'gravityview_render_available_fields', array( $this, 'render_available_fields'), 10, 2 );
43
		add_action( 'gravityview_render_available_widgets', array( $this, 'render_available_widgets') );
44
		add_action( 'gravityview_render_active_areas', array( $this, 'render_active_areas'), 10, 5 );
45
46
		// @todo check if this hook is needed..
47
		//add_action( 'gravityview_render_field_options', array( $this, 'render_field_options'), 10, 9 );
48
49
		// Add Connected Form column
50
		add_filter('manage_gravityview_posts_columns' , array( $this, 'add_post_type_columns' ) );
51
52
		add_filter( 'gform_toolbar_menu', array( 'GravityView_Admin_Views', 'gform_toolbar_menu' ), 10, 2 );
53
		add_action( 'gform_form_actions', array( 'GravityView_Admin_Views', 'gform_toolbar_menu' ), 10, 2 );
54
55
		add_action( 'manage_gravityview_posts_custom_column', array( $this, 'add_custom_column_content'), 10, 2 );
56
57
		add_action( 'restrict_manage_posts', array( $this, 'add_view_dropdown' ) );
58
59
		add_action( 'pre_get_posts', array( $this, 'filter_pre_get_posts_by_gravityview_form_id' ) );
60
61
		add_filter( 'gravityview/support_port/localization_data', array( $this, 'suggest_support_articles' ) );
62
63
	}
64
65
	/**
66
     * When on the Add/Edit View screen, suggest most popular articles related to that
67
     *
68
	 * @param array $localization_data Data to be passed to the Support Port JS
69
	 *
70
	 * @return array
71
	 */
72
	function suggest_support_articles( $localization_data = 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...
73
74
	    if( ! gravityview()->request->is_view() ) {
75
	        return $localization_data;
76
        }
77
78
		$localization_data['suggest'] = array(
79
            '57ef23539033602e61d4a560',
80
            '54c67bb9e4b0512429885513',
81
            '54c67bb9e4b0512429885512',
82
            '54c67bbbe4b07997ea3f3f6b',
83
            '54d1a33ae4b086c0c0964ce9',
84
            '57ef253c9033602e61d4a563',
85
            '552355bfe4b0221aadf2572b',
86
            '54c67bcde4b051242988553e',
87
        );
88
89
		return $localization_data;
90
	}
91
92
	/**
93
	 * @since 1.15
94
	 * @param WP_Query $query
95
	 */
96
	public function filter_pre_get_posts_by_gravityview_form_id( &$query ) {
97
		global $pagenow;
98
99
		if ( !is_admin() ) {
100
			return;
101
		}
102
103
		$form_id = isset( $_GET['gravityview_form_id'] ) ? (int) $_GET['gravityview_form_id'] : false;
104
105
		if( 'edit.php' !== $pagenow || ! $form_id || ! isset( $query->query_vars[ 'post_type' ] ) ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $form_id of type integer|false is loosely compared to false; this is ambiguous if the integer can be zero. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
106
			return;
107
		}
108
109
		if ( $query->query_vars[ 'post_type' ] == 'gravityview' ) {
110
			$query->set( 'meta_query', array(
111
				array(
112
					'key' => '_gravityview_form_id',
113
					'value' => $form_id,
114
				)
115
			) );
116
		}
117
	}
118
119
	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...
120
		$current_screen = get_current_screen();
121
122
		if( 'gravityview' !== $current_screen->post_type ) {
123
			return;
124
		}
125
126
		$forms = gravityview_get_forms();
127
		$current_form = \GV\Utils::_GET( 'gravityview_form_id' );
128
		// If there are no forms to select, show no forms.
129
		if( !empty( $forms ) ) { ?>
130
			<select name="gravityview_form_id" id="gravityview_form_id">
131
				<option value="" <?php selected( '', $current_form, true ); ?>><?php esc_html_e( 'All forms', 'gravityview' ); ?></option>
132
				<?php foreach( $forms as $form ) { ?>
133
					<option value="<?php echo $form['id']; ?>" <?php selected( $form['id'], $current_form, true ); ?>><?php echo esc_html( $form['title'] ); ?></option>
134
				<?php } ?>
135
			</select>
136
		<?php }
137
	}
138
139
140
	/**
141
	 * @deprecated since 1.2
142
	 * Start using GravityView_Render_Settings::render_setting_row
143
	 */
144
	public static function render_setting_row( $key = '', $current_settings = array(), $override_input = null, $name = 'template_settings[%s]', $id = 'gravityview_se_%s' ) {
145
		_deprecated_function( 'GravityView_Admin_Views::render_setting_row', '1.1.7', 'GravityView_Render_Settings::render_setting_row' );
146
		GravityView_Render_Settings::render_setting_row( $key, $current_settings, $override_input, $name , $id );
147
	}
148
149
	/**
150
	 * @deprecated since 1.2
151
	 * Start using GravityView_Render_Settings::render_field_option
152
	 */
153
	public static function render_field_option( $name = '', $option, $curr_value = NULL ) {
154
		_deprecated_function( 'GravityView_Admin_Views::render_field_option', '1.1.7', 'GravityView_Render_Settings::render_field_option' );
155
		return GravityView_Render_Settings::render_field_option( $name, $option, $curr_value );
156
	}
157
158
159
	/**
160
	 * Add a GravityView menu to the Form Toolbar with connected views
161
	 * @param  array  $menu_items Menu items, as set in GFForms::top_toolbar()
162
	 * @param  int $id         ID of the current Gravity form
163
	 * @return array            Modified array
164
	 */
165
	public static function gform_toolbar_menu( $menu_items = array(), $id = NULL ) {
166
167
		// Don't show on Trashed forms
168
		if( 'trash' === rgget( 'filter') ) {
169
			return $menu_items;
170
		}
171
172
		$connected_views = gravityview_get_connected_views( $id, array( 'post_status' => 'any' ) );
173
174
		$priority = 0;
175
176
		if( 'form_list' === GFForms::get_page() ) {
177
			$priority = 790;
178
        }
179
180
		if( empty( $connected_views ) ) {
181
182
		    $menu_items['gravityview'] = array(
183
				'label'          => esc_attr__( 'Create a View', 'gravityview' ),
184
				'icon'           => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>',
185
				'title'          => esc_attr__( 'Create a View using this form as a data source', 'gravityview' ),
186
				'url'            => admin_url( 'post-new.php?post_type=gravityview&form_id=' . $id ),
187
				'menu_class'     => 'gv_connected_forms gf_form_toolbar_settings',
188
				'priority'       => $priority,
189
				'capabilities'   => array( 'edit_gravityviews' ),
190
			);
191
192
			return $menu_items;
193
		}
194
195
		$sub_menu_items = array();
196
		foreach ( (array)$connected_views as $view ) {
197
198
			if( ! GVCommon::has_cap( 'edit_gravityview', $view->ID ) ) {
199
				continue;
200
			}
201
202
			$label = empty( $view->post_title ) ? sprintf( __('No Title (View #%d)', 'gravityview' ), $view->ID ) : $view->post_title;
203
204
			$sub_menu_items[] = array(
205
				'label' => esc_attr( $label ),
206
				'url' => admin_url( 'post.php?action=edit&post='.$view->ID ),
207
			);
208
		}
209
210
		// If there were no items added, then let's create the parent menu
211
		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...
212
213
		    $sub_menu_items[] = array(
214
			    'label' => esc_attr__( 'Create a View', 'gravityview' ),
215
                'link_class' => 'gv-create-view',
216
			    'title' => esc_attr__( 'Create a View using this form as a data source', 'gravityview' ),
217
			    'url'   => admin_url( 'post-new.php?post_type=gravityview&form_id=' . $id ),
218
			    'capabilities'   => array( 'edit_gravityviews' ),
219
            );
220
221
			// Make sure Gravity Forms uses the submenu; if there's only one item, it uses a link instead of a dropdown
222
			$sub_menu_items[] = array(
223
				'url' => '#',
224
				'label' => '',
225
				'menu_class' => 'hidden',
226
				'capabilities' => '',
227
			);
228
229
			$menu_items['gravityview'] = array(
230
				'label'          => __( 'Connected Views', 'gravityview' ),
231
				'icon'           => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>',
232
				'title'          => __( 'GravityView Views using this form as a data source', 'gravityview' ),
233
				'url'            => '#',
234
				'onclick'        => 'return false;',
235
				'menu_class'     => 'gv_connected_forms gf_form_toolbar_settings',
236
				'sub_menu_items' => $sub_menu_items,
237
				'priority'       => $priority,
238
				'capabilities'   => array( 'edit_gravityviews' ),
239
			);
240
		}
241
242
		return $menu_items;
243
	}
244
245
	/**
246
	 * List the field types without presentation properties (on a View context)
247
	 *
248
	 * @param array $array Existing field types to add to a blacklist
249
	 * @param string|null $context Context for the blacklist. Default: NULL.
250
	 * @access public
251
	 * @return array Default blacklist fields merged with existing blacklist fields
252
	 */
253
	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...
254
255
		$add = array( 'captcha', 'page' );
256
257
		// Don't allowing editing the following values:
258
		if( $context === 'edit' ) {
259
			$add[] = 'post_id';
260
		}
261
262
		$return = array_merge( $array, $add );
263
264
		return $return;
265
	}
266
267
	/**
268
	 * Add tooltip text for use throughout the UI
269
	 * @param  array       $tooltips Array of Gravity Forms tooltips
270
	 * @return array                Modified tooltips array
271
	 */
272
	public function tooltips( $tooltips = array() ) {
273
274
		$gv_tooltips = array();
275
276
		// Generate tooltips for View settings
277
		$default_args = \GV\View_Settings::defaults( true );
278
279
		foreach ( $default_args as $key => $arg ) {
280
281
			// If an arg has `tooltip` defined, but it's false, don't display a tooltip
282
			if( isset( $arg['tooltip'] ) && empty( $arg['tooltip'] ) ) { continue; }
283
284
			// By default, use `tooltip` if defined.
285
			$tooltip = empty( $arg['tooltip'] ) ? NULL : $arg['tooltip'];
286
287
			// Otherwise, use the description as a tooltip.
288
			if( empty( $tooltip ) && !empty( $arg['desc'] ) ) {
289
				$tooltip = $arg['desc'];
290
			}
291
292
			// If there's no tooltip set, continue
293
			if( empty( $tooltip ) ) {
294
				continue;
295
			}
296
297
			// Add the tooltip
298
			$gv_tooltips[ 'gv_'.$key ] = array(
299
				'title'	=> $arg['label'],
300
				'value'	=> $tooltip,
301
			);
302
303
		}
304
305
		$gv_tooltips['gv_css_merge_tags'] = array(
306
			'title' => __('CSS Merge Tags', 'gravityview'),
307
			'value' => sprintf( __( 'Developers: The CSS classes will be sanitized using the %ssanitize_title_with_dashes()%s function.', 'gravityview'), '<code>', '</code>' )
308
		);
309
310
		/**
311
		 * @filter `gravityview_tooltips` The tooltips GravityView adds to the Gravity Forms tooltip array
312
		 * @param array $gv_tooltips Associative array with unique keys containing array of `title` and `value` keys, as expected by `gform_tooltips` filter
313
		 * @deprecated Renamed to `gravityview/metaboxes/tooltips`
314
		 */
315
		$gv_tooltips = apply_filters( 'gravityview_tooltips', $gv_tooltips );
316
317
		/**
318
		 * @filter `gravityview/metaboxes/tooltips` The tooltips GravityView adds to the Gravity Forms tooltip array
319
		 * @param array $gv_tooltips Associative array with unique keys containing array of `title` and `value` keys, as expected by `gform_tooltips` filter
320
		 */
321
		$gv_tooltips = apply_filters( 'gravityview/metaboxes/tooltips', $gv_tooltips );
322
323
		foreach ( $gv_tooltips as $key => $tooltip ) {
324
325
			$title = empty( $tooltip['title'] ) ? '' : '<h6>'.esc_html( $tooltip['title'] ) .'</h6>';
326
327
			$tooltips[ $key ] = $title . wpautop( esc_html( $tooltip['value'] ) );
328
		}
329
330
		return $tooltips;
331
	}
332
333
	/**
334
	 * Add the Data Source information
335
	 *
336
	 * @param null $column_name
337
	 * @param $post_id
338
	 *
339
	 * @return void
340
	 */
341
	public function add_custom_column_content( $column_name = NULL, $post_id )	{
342
343
		$output = '';
344
345
		switch ( $column_name ) {
346
			case 'gv_template':
347
348
				$template_id = gravityview_get_template_id( $post_id );
349
350
				// All Views should have a connected form. If it doesn't, that's not right.
351
				if ( empty( $template_id ) ) {
352
					gravityview()->log->error( 'View ID {view_id} does not have a connected template.', array( 'view_id' => $post_id ) );
353
					break;
354
				}
355
356
				$templates = gravityview_get_registered_templates();
357
358
				$template = isset( $templates[ $template_id ] ) ? $templates[ $template_id ] : false;
359
360
				// Generate backup if label doesn't exist: `example_name` => `Example Name`
361
				$template_id_pretty = ucwords( implode( ' ', explode( '_', $template_id ) ) );
362
363
				$output = $template ? $template['label'] : $template_id_pretty;
364
365
				break;
366
367
			case 'gv_connected_form':
368
369
				$form_id = gravityview_get_form_id( $post_id );
370
371
				// All Views should have a connected form. If it doesn't, that's not right.
372
				if ( empty( $form_id ) ) {
373
					gravityview()->log->error( 'View ID {view_id} does not have a connected GF form.', array( 'view_id' => $post_id ) );
374
					$output = __( 'Not connected.', 'gravityview' );
375
					break;
376
				}
377
378
				$form = gravityview_get_form( $form_id );
379
380
				if ( ! $form ) {
381
					gravityview()->log->error( 'Connected form not found: Form #{form_id}', array( 'form_id' => $form_id ) );
382
383
					$output = __( 'The connected form can not be found; it may no longer exist.', 'gravityview' );
384
				} else {
385
					$output = self::get_connected_form_links( $form );
386
				}
387
388
				break;
389
		}
390
391
		echo $output;
392
	}
393
394
395
	/**
396
	 * Get HTML links relating to a connected form, like Edit, Entries, Settings, Preview
397
	 * @param  array|int $form Gravity Forms forms array, or the form ID
398
	 * @param  boolean $include_form_link Whether to include the bold name of the form in the output
399
	 * @return string          HTML links
400
	 */
401
	static public function get_connected_form_links( $form, $include_form_link = true ) {
402
403
		// Either the form is empty or the form ID is 0, not yet set.
404
		if( empty( $form ) ) {
405
			return '';
406
		}
407
408
		// The $form is passed as the form ID
409
		if( !is_array( $form ) ) {
410
			$form = gravityview_get_form( $form );
411
		}
412
413
		$form_id = $form['id'];
414
		$links = array();
415
416
		if( GVCommon::has_cap( 'gravityforms_edit_forms' ) ) {
417
			$form_url = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;id=%d', $form_id ) );
418
			$form_link = '<strong class="gv-form-title">'.gravityview_get_link( $form_url, $form['title'], 'class=row-title' ).'</strong>';
419
			$links[] = '<span>'.gravityview_get_link( $form_url, __('Edit Form', 'gravityview') ).'</span>';
420
		} else {
421
			$form_link = '<strong class="gv-form-title">'. esc_html( $form['title'] ). '</strong>';
422
		}
423
424
		if( GVCommon::has_cap( 'gravityforms_view_entries' ) ) {
425
			$entries_url = admin_url( sprintf( 'admin.php?page=gf_entries&amp;id=%d', $form_id ) );
426
			$links[] = '<span>'.gravityview_get_link( $entries_url, __('Entries', 'gravityview') ).'</span>';
427
		}
428
429
		if( GVCommon::has_cap( array( 'gravityforms_edit_settings', 'gravityview_view_settings' ) ) ) {
430
			$settings_url = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;view=settings&amp;id=%d', $form_id ) );
431
			$links[] = '<span>'.gravityview_get_link( $settings_url, __('Settings', 'gravityview'), 'title='.__('Edit settings for this form', 'gravityview') ).'</span>';
432
		}
433
434
		if( GVCommon::has_cap( array("gravityforms_edit_forms", "gravityforms_create_form", "gravityforms_preview_forms") ) ) {
435
			$preview_url = site_url( sprintf( '?gf_page=preview&amp;id=%d', $form_id ) );
436
			$links[] = '<span>'.gravityview_get_link( $preview_url, __('Preview Form', 'gravityview'), 'title='.__('Preview this form', 'gravityview') ).'</span>';
437
		}
438
439
		$output = '';
440
441
		if( !empty( $include_form_link ) ) {
442
			$output .= $form_link;
443
		}
444
445
		/**
446
		 * @filter `gravityview_connected_form_links` Modify the links shown in the Connected Form links
447
		 * @since 1.6
448
		 * @param array $links Links to show
449
		 * @param array $form Gravity Forms form array
450
		 */
451
		$links = apply_filters( 'gravityview_connected_form_links', $links, $form );
452
453
		$output .= '<div class="row-actions">'. implode( ' | ', $links ) .'</div>';
454
455
		return $output;
456
	}
457
458
	/**
459
	 * Add the Data Source column to the Views page
460
	 * @param  array      $columns Columns array
461
	 */
462
	public function add_post_type_columns( $columns ) {
463
464
		// Get the date column and save it for later to add back in.
465
		// This adds it after the Data Source column.
466
		// This way, we don't need to do array_slice, array_merge, etc.
467
		$date = $columns['date'];
468
		unset( $columns['date'] );
469
470
		$data_source_required_caps = array(
471
			'gravityforms_edit_forms',
472
			'gravityforms_view_entries',
473
			'gravityforms_edit_settings',
474
			'gravityview_view_settings',
475
			'gravityforms_create_form',
476
			'gravityforms_preview_forms',
477
		);
478
479
		if( GVCommon::has_cap( $data_source_required_caps ) ) {
480
			$columns['gv_connected_form'] = __( 'Data Source', 'gravityview' );
481
		}
482
483
		$columns['gv_template'] = _x( 'Template', 'Column title that shows what template is being used for Views', 'gravityview' );
484
485
		// Add the date back in.
486
		$columns['date'] = $date;
487
488
		return $columns;
489
	}
490
491
	/**
492
	 * Save View configuration
493
	 *
494
	 * @access public
495
	 * @param int $post_id Currently saved Post ID
496
	 * @return void
497
	 */
498
	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...
499
500
		if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
501
			return;
502
		}
503
504
		// validate post_type
505
		if ( ! isset( $_POST['post_type'] ) || 'gravityview' != $_POST['post_type'] ) {
506
			return;
507
		}
508
509
		// validate user can edit and save View
510
		if ( ! GVCommon::has_cap( 'edit_gravityview', $post_id ) ) {
511
			gravityview()->log->error( 'Current user does not have the capability to edit View {view_id}', array( 'view_id' => $post_id, 'data' => wp_get_current_user() ) );
512
			return;
513
		}
514
515
		gravityview()->log->debug( '[save_postdata] Saving View post type.', array( 'data' => $_POST ) );
516
517
		$statii = array();
518
519
		// check if this is a start fresh View
520
		if ( isset( $_POST['gravityview_select_form_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_form_nonce'], 'gravityview_select_form' ) ) {
521
522
			$form_id = !empty( $_POST['gravityview_form_id'] ) ? $_POST['gravityview_form_id'] : '';
523
			// save form id
524
			$statii['form_id'] = update_post_meta( $post_id, '_gravityview_form_id', $form_id );
525
526
		}
527
528
		if( false === GVCommon::has_cap( 'gravityforms_create_form' ) && empty( $statii['form_id'] ) ) {
529
			gravityview()->log->error( 'Current user does not have the capability to create a new Form.', array( 'data' => wp_get_current_user() ) );
530
			return;
531
		}
532
533
		// Was this a start fresh?
534
		if ( ! empty( $_POST['gravityview_form_id_start_fresh'] ) ) {
535
			$statii['start_fresh'] = add_post_meta( $post_id, '_gravityview_start_fresh', 1 );
536
		} else {
537
			$statii['start_fresh'] = delete_post_meta( $post_id, '_gravityview_start_fresh' );
538
		}
539
540
		// Check if we have a template id
541
		if ( isset( $_POST['gravityview_select_template_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_template_nonce'], 'gravityview_select_template' ) ) {
542
543
			$template_id = !empty( $_POST['gravityview_directory_template'] ) ? $_POST['gravityview_directory_template'] : '';
544
545
			// now save template id
546
			$statii['directory_template'] = update_post_meta( $post_id, '_gravityview_directory_template', $template_id );
547
		}
548
549
550
		// save View Configuration metabox
551
		if ( isset( $_POST['gravityview_view_configuration_nonce'] ) && wp_verify_nonce( $_POST['gravityview_view_configuration_nonce'], 'gravityview_view_configuration' ) ) {
552
553
			// template settings
554
			if( empty( $_POST['template_settings'] ) ) {
555
				$_POST['template_settings'] = array();
556
			}
557
			$statii['template_settings'] = update_post_meta( $post_id, '_gravityview_template_settings', $_POST['template_settings'] );
558
559
			// guard against unloaded View configuration page
560
			if ( isset( $_POST['gv_fields'] ) && isset( $_POST['gv_fields_done'] ) ) {
561
				$fields = array();
562
563
				if ( ! empty( $_POST['gv_fields'] ) ) {
564
					$fields = _gravityview_process_posted_fields();
565
				}
566
567
				$fields = wp_slash( $fields );
568
569
				$statii['directory_fields'] = update_post_meta( $post_id, '_gravityview_directory_fields', $fields );
570
			}
571
572
			// Directory Visible Widgets
573
			if( empty( $_POST['widgets'] ) ) {
574
				$_POST['widgets'] = array();
575
			}
576
			$statii['directory_widgets'] = gravityview_set_directory_widgets( $post_id, $_POST['widgets'] );
577
578
		} // end save view configuration
579
580
		/**
581
		 * @action `gravityview_view_saved` After a View has been saved in the admin
582
		 * @param int $post_id ID of the View that has been saved
583
		 * @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.
584
		 * @since 1.17.2
585
		 */
586
		do_action('gravityview_view_saved', $post_id, $statii );
587
588
		gravityview()->log->debug( '[save_postdata] Update Post Meta Statuses (also returns false if nothing changed)', array( 'data' => array_map( 'intval', $statii ) ) );
589
	}
590
591
	/**
592
	 * @deprecated 1.1.6
593
	 */
594
	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...
595
		_deprecated_function( 'GravityView_Admin_Views::render_label()', '1.1.6', 'Use the GravityView_Admin_View_Field class instead.' );
596
	}
597
598
	/**
599
	 * Render html for displaying available fields based on a Form ID
600
	 * $blacklist_field_types - contains the field types which are not proper to be shown in a directory.
601
	 *
602
     * @see GravityView_Ajax::get_available_fields_html() Triggers `gravityview_render_available_fields` action
603
	 * @access public
604
     *
605
	 * @param int $form Gravity Forms Form ID (default: '')
606
	 * @param string $context (default: 'single')
607
     *
608
	 * @return void
609
	 */
610
	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...
611
612
		/**
613
		 * @filter  `gravityview_blacklist_field_types` Modify the types of fields that shouldn't be shown in a View.
614
		 * @param[in,out] array $blacklist_field_types Array of field types to block for this context.
615
		 * @param[in] string $context View context ('single', 'directory', or 'edit')
616
		 */
617
		$blacklist_field_types = apply_filters( 'gravityview_blacklist_field_types', array(), $context );
618
619
		if ( ! is_array( $blacklist_field_types ) ) {
620
621
		    gravityview()->log->error( '$blacklist_field_types is not an array', array( 'data' => print_r( $blacklist_field_types, true ) ) );
622
623
			$blacklist_field_types = array();
624
		}
625
626
		$fields = $this->get_available_fields( $form, $context );
627
628
		$output = '';
629
630
		if( !empty( $fields ) ) {
631
632
			foreach( $fields as $id => $details ) {
633
634
				if( in_array( $details['type'], (array) $blacklist_field_types ) ) {
635
					continue;
636
				}
637
638
				// Edit mode only allows editing the parent fields, not single inputs.
639
				if( $context === 'edit' && !empty( $details['parent'] ) ) {
640
					continue;
641
				}
642
643
				$output .= new GravityView_Admin_View_Field( $details['label'], $id, $details, $settings = array(), $form );
644
645
			} // End foreach
646
		}
647
648
		echo $output;
649
650
		// For the EDIT view we only want to allow the form fields.
651
		if( $context === 'edit' ) {
652
			return;
653
		}
654
655
		$this->render_additional_fields( $form, $context );
656
	}
657
658
	/**
659
	 * Render html for displaying additional fields based on a Form ID
660
	 *
661
	 * @access public
662
	 * @param int $form Gravity Forms Form ID (default: '')
663
	 * @param string $context (default: 'single')
664
	 * @return void
665
	 */
666
	public function render_additional_fields( $form = 0, $context = 'single' ) {
0 ignored issues
show
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...
667
668
		/**
669
		 * @filter `gravityview_additional_fields` non-standard Fields to show at the bottom of the field picker
670
		 * @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
671
		 */
672
		$additional_fields = apply_filters( 'gravityview_additional_fields', array(
673
			array(
674
				'label_text' => __( '+ Add All Fields', 'gravityview' ),
675
				'desc' => __('Add all the available fields at once.', 'gravityview'),
676
				'field_id' => 'all-fields',
677
				'label_type' => 'field',
678
				'input_type' => NULL,
679
				'field_options' => NULL,
680
				'settings_html'	=> NULL,
681
			)
682
		));
683
684
		if( !empty( $additional_fields )) {
685
			foreach ( (array)$additional_fields as $item ) {
686
687
				// Prevent items from not having index set
688
				$item = wp_parse_args( $item, array(
689
					'label_text' => NULL,
690
					'field_id' => NULL,
691
					'label_type' => NULL,
692
					'input_type' => NULL,
693
					'field_options' => NULL,
694
					'settings_html'	=> NULL,
695
				));
696
697
				// Backward compat.
698
				if( !empty( $item['field_options'] ) ) {
699
					// Use settings_html from now on.
700
					$item['settings_html'] = $item['field_options'];
701
				}
702
703
				// Render a label for each of them
704
				echo new GravityView_Admin_View_Field( $item['label_text'], $item['field_id'], $item, $settings = array(), $form );
705
706
			}
707
		}
708
709
	}
710
711
	/**
712
	 * Retrieve the default fields id, label and type
713
	 * @param  string|array $form form_ID or form object
714
	 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
715
	 * @return array
716
	 */
717
	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...
718
719
		$entry_default_fields = array();
720
721
		if( in_array( $zone, array( 'directory', 'single' ) ) ) {
722
723
			$entry_default_fields = array(
724
				'id' => array(
725
					'label' => __('Entry ID', 'gravityview'),
726
					'type' => 'id',
727
					'desc'	=> __('The unique ID of the entry.', 'gravityview'),
728
				),
729
				'date_created' => array(
730
					'label' => __('Entry Date', 'gravityview'),
731
					'desc'	=> __('The date the entry was created.', 'gravityview'),
732
					'type' => 'date_created',
733
				),
734
				'source_url' => array(
735
					'label' => __('Source URL', 'gravityview'),
736
					'type' => 'source_url',
737
					'desc'	=> __('The URL of the page where the form was submitted.', 'gravityview'),
738
				),
739
				'ip' => array(
740
					'label' => __('User IP', 'gravityview'),
741
					'type' => 'ip',
742
					'desc'	=> __('The IP Address of the user who created the entry.', 'gravityview'),
743
				),
744
				'created_by' => array(
745
					'label' => __('User', 'gravityview'),
746
					'type' => 'created_by',
747
					'desc'	=> __('Details of the logged-in user who created the entry (if any).', 'gravityview'),
748
				),
749
750
				/**
751
				 * @since 1.7.2
752
				 */
753
			    'other_entries' => array(
754
				    'label'	=> __('Other Entries', 'gravityview'),
755
				    'type'	=> 'other_entries',
756
				    'desc'	=> __('Display other entries created by the entry creator.', 'gravityview'),
757
			    ),
758
	        );
759
760
			if( 'single' !== $zone) {
761
762
				$entry_default_fields['entry_link'] = array(
763
					'label' => __('Link to Entry', 'gravityview'),
764
					'desc'	=> __('A dedicated link to the single entry with customizable text.', 'gravityview'),
765
					'type' => 'entry_link',
766
				);
767
			}
768
769
		} // if not zone directory or single
770
771
		/**
772
		 * @since  1.2
773
		 */
774
		$entry_default_fields['custom']	= array(
775
			'label'	=> __('Custom Content', 'gravityview'),
776
			'type'	=> 'custom',
777
			'desc'	=> __('Insert custom text or HTML.', 'gravityview'),
778
		);
779
780
		/**
781
		 * @since develop
782
		 */
783
		$entry_default_fields['sequence'] = array(
784
			'label'	=> __('Result Number', 'gravityview'),
785
			'type'	=> 'sequence',
786
			'desc'	=> __('Display a sequential result number for each entry.', 'gravityview'),
787
		);
788
789
		/**
790
		 * @filter `gravityview_entry_default_fields` Modify the default fields for each zone and context
791
		 * @param array $entry_default_fields Array of fields shown by default
792
		 * @param  string|array $form form_ID or form object
793
		 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
794
		 */
795
		return apply_filters( 'gravityview_entry_default_fields', $entry_default_fields, $form, $zone);
796
	}
797
798
	/**
799
	 * Calculate the available fields
800
	 * @param  string|array $form form_ID or form object
801
	 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
802
	 * @return array         fields
803
	 */
804
	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...
805
806
		if( empty( $form ) ) {
807
			gravityview()->log->error( '$form is empty' );
808
			return array();
809
		}
810
811
		// get form fields
812
		$fields = gravityview_get_form_fields( $form, true );
813
814
		// get meta fields ( only if form was already created )
815
		if( !is_array( $form ) ) {
816
			$meta_fields = gravityview_get_entry_meta( $form );
817
		} else {
818
			$meta_fields = array();
819
		}
820
821
		// get default fields
822
		$default_fields = $this->get_entry_default_fields( $form, $zone );
823
824
		//merge without loosing the keys
825
		$fields = $fields + $meta_fields + $default_fields;
826
827
		// Move Custom Content to top
828
		$fields = array( 'custom' => $fields['custom'] ) + $fields;
829
830
		/**
831
		 * @filter `gravityview/admin/available_fields` Modify the available fields that can be used in a View.
832
		 * @param[in,out] array $fields The fields.
833
		 * @param  string|array $form form_ID or form object
834
		 * @param  string $zone Either 'single', 'directory', 'header', 'footer'
835
		 */
836
		return apply_filters( 'gravityview/admin/available_fields', $fields, $form, $zone );
837
	}
838
839
840
	/**
841
	 * Render html for displaying available widgets
842
	 * @return string html
843
	 */
844
	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...
845
846
		$widgets = $this->get_registered_widgets();
0 ignored issues
show
Deprecated Code introduced by
The method GravityView_Admin_Views::get_registered_widgets() has been deprecated with message: Use \GV\Widget::registered()

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
847
848
		if( !empty( $widgets ) ) {
849
850
			foreach( $widgets as $id => $details ) {
851
852
				echo new GravityView_Admin_View_Widget( $details['label'], $id, $details );
853
854
			}
855
		}
856
857
	}
858
859
	/**
860
	 * Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object
861
	 * @deprecated Use \GV\Widget::registered()
862
	 * @since 1.13.1
863
	 * @return array
864
	 */
865
	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...
866
		return \GV\Widget::registered();
867
	}
868
869
	/**
870
	 * Generic function to render rows and columns of active areas for widgets & fields
871
	 * @param  string $template_id The current slug of the selected View template
872
	 * @param  string $type   Either 'widget' or 'field'
873
	 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
874
	 * @param  array $rows    The layout structure: rows, columns and areas
875
	 * @param  array $values  Saved objects
876
	 * @return void
877
	 */
878
	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...
879
		global $post;
880
881
		if( $type === 'widget' ) {
882
			$button_label = __( 'Add Widget', 'gravityview' );
883
		} else {
884
			$button_label = __( 'Add Field', 'gravityview' );
885
		}
886
887
		$available_items = array();
888
889
		$view = \GV\View::from_post( $post );
890
		$form_id = null;
891
892
		// if saved values, get available fields to label everyone
893
		if( !empty( $values ) && ( !empty( $post->ID ) || !empty( $_POST['template_id'] ) ) ) {
894
895
			if( !empty( $_POST['template_id'] ) ) {
896
				$form = GravityView_Ajax::pre_get_form_fields( $_POST['template_id'] );
897
			} else {
898
				$form_id = $form = gravityview_get_form_id( $post->ID );
899
			}
900
901
			if ( 'field' === $type ) {
902
				$available_items[ $form ] = $this->get_available_fields( $form, $zone );
903
904
				$joined_forms = gravityview_get_joined_forms( $post->ID );
905
906
                foreach ( $joined_forms as $form ) {
907
                    $available_items[ $form->ID ] = $this->get_available_fields( $form->ID, $zone );
908
                }
909
			} else {
910
				$available_items[ $form ] = \GV\Widget::registered();
911
			}
912
		}
913
914
		foreach( $rows as $row ) :
915
			foreach( $row as $col => $areas ) :
916
				$column = ($col == '2-2') ? '1-2' : $col; ?>
917
918
				<div class="gv-grid-col-<?php echo esc_attr( $column ); ?>">
919
920
					<?php foreach( $areas as $area ) : 	?>
921
922
						<div class="gv-droppable-area" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>" data-context="<?php echo esc_attr( $zone ); ?>">
923
							<div class="active-drop active-drop-<?php echo esc_attr( $type ); ?>" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>">
924
925
								<?php // render saved fields
926
927
								if( ! empty( $values[ $zone .'_'. $area['areaid'] ] ) ) {
928
929
									foreach( $values[ $zone .'_'. $area['areaid'] ] as $uniqid => $field ) {
930
931
										// Maybe has a form ID
932
										$form_id = empty( $field['form_id'] ) ? $form_id : $field['form_id'];
933
934
										$input_type = NULL;
935
936
										if ( $form_id ) {
937
											$original_item = isset( $available_items[ $form_id ] [ $field['id'] ] ) ? $available_items[ $form_id ] [ $field['id'] ] : false ;
938
                                        } else {
939
											$original_item = isset( $available_items[ $field['id'] ] ) ? $available_items[ $field['id'] ] : false ;
940
                                        }
941
942
										if ( !$original_item ) {
943
											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(' data' => array('available_items' => $available_items, 'field' => $field ) ) );
944
945
											$original_item = $field;
946
										} else {
947
											$input_type = isset( $original_item['type'] ) ? $original_item['type'] : NULL;
948
										}
949
950
										// Field options dialog box
951
										$field_options = GravityView_Render_Settings::render_field_options( $form_id, $type, $template_id, $field['id'], $original_item['label'], $zone .'_'. $area['areaid'], $input_type, $uniqid, $field, $zone, $original_item );
952
953
										$item = array(
954
											'input_type' => $input_type,
955
											'settings_html' => $field_options,
956
											'label_type' => $type
957
										);
958
959
										// Merge the values with the current item to pass things like widget descriptions and original field names
960
										if ( $original_item ) {
961
											$item = wp_parse_args( $item, $original_item );
962
										}
963
964
										switch( $type ) {
965
											case 'widget':
966
												echo new GravityView_Admin_View_Widget( $item['label'], $field['id'], $item, $field );
967
												break;
968
											default:
969
												echo new GravityView_Admin_View_Field( $field['label'], $field['id'], $item, $field, $form_id );
970
										}
971
									}
972
973
								} // End if zone is not empty ?>
974
975
								<span class="drop-message"><?php echo sprintf(esc_attr__('"+ %s" or drag existing %ss here.', 'gravityview'), $button_label, $type ); ?></span>
976
							</div>
977
							<div class="gv-droppable-area-action">
978
								<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 ); ?>" data-formid="<?php echo $view ? esc_attr( $view->form ? $view->form->ID : '' ) : ''; ?>"><?php echo '+ '.esc_html( $button_label ); ?></a>
979
980
								<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>
981
							</div>
982
						</div>
983
984
					<?php endforeach; ?>
985
986
				</div>
987
			<?php endforeach;
988
		endforeach;
989
	}
990
991
	/**
992
	 * Render the widget active areas
993
     * @param  string $template_id The current slug of the selected View template
994
	 * @param  string $zone    Either 'header' or 'footer'
995
	 * @param  string $post_id Current Post ID (view)
996
	 * @return string          html
997
	 */
998
	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...
999
1000
		$default_widget_areas = \GV\Widget::get_default_widget_areas();
1001
1002
		$widgets = array();
1003
		if ( ! empty( $post_id ) ) {
1004
			if ( 'auto-draft' === get_post_status( $post_id ) ) {
1005
				// This is a new View, prefill the widgets
1006
				$widgets = array(
1007
					'header_top' => array(
1008
						substr( md5( microtime( true ) ), 0, 13 ) => array (
1009
							'id' => 'search_bar',
1010
							'label' => __( 'Search Bar', 'gravityview' ),
1011
							'search_layout' => 'horizontal',
1012
							'search_clear' => '0',
1013
							'search_fields' => '[{"field":"search_all","input":"input_text"}]',
1014
							'search_mode' => 'any',
1015
						),
1016
					),
1017
					'header_left' => array(
1018
						substr( md5( microtime( true ) ), 0, 13 ) => array(
1019
							'id' => 'page_info',
1020
							'label' => __( 'Show Pagination Info', 'gravityview' ),
1021
						),
1022
					),
1023
					'header_right' => array(
1024
						substr( md5( microtime( true ) ), 0, 13 ) => array(
1025
							'id' => 'page_links',
1026
							'label' => __( 'Page Links', 'gravityview' ),
1027
							'show_all' => '0',
1028
						),
1029
					),
1030
					'footer_right' => array(
1031
						substr( md5( microtime( true ) ), 0, 13 ) => array(
1032
							'id' => 'page_links',
1033
							'label' => __( 'Page Links', 'gravityview' ),
1034
							'show_all' => '0',
1035
						),
1036
					),
1037
				);
1038
1039
				/**
1040
				 * @filter `gravityview/view/widgets/default` Modify the default widgets for new Views
1041
				 * @param[in,out] array $widgets A Widget configuration array
1042
				 * @param string $zone The widget zone that's being requested
1043
				 * @param int $post_id The auto-draft post ID
1044
				 */
1045
				$widgets = apply_filters( 'gravityview/view/widgets/default', $widgets, $template_id, $zone, $post_id );
1046
			} else {
1047
				$widgets = gravityview_get_directory_widgets( $post_id );
1048
			}
1049
		}
1050
1051
		ob_start();
1052
		?>
1053
1054
		<div class="gv-grid gv-grid-pad gv-grid-border" id="directory-<?php echo $zone; ?>-widgets">
1055
			<?php $this->render_active_areas( $template_id, 'widget', $zone, $default_widget_areas, $widgets ); ?>
1056
		</div>
1057
1058
		<?php
1059
		$output = ob_get_clean();
1060
1061
		echo $output;
1062
1063
		return $output;
1064
	}
1065
1066
	/**
1067
     * Renders "Add Field" tooltips
1068
     *
1069
     * @since 2.0.11
1070
     *
1071
	 * @param string $context "directory", "single", or "edit"
1072
     *
1073
     * @return void
1074
	 */
1075
	function render_field_pickers( $context = 'directory' ) {
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...
1076
1077
		// list of available fields to be shown in the popup
1078
		$forms = gravityview_get_forms( 'any' );
1079
1080
		$form_ids = array_map( function ($form) { return $form['id']; }, $forms);
1081
1082
		foreach ( $form_ids as $form_id ) {
1083
			$filter_field_id = sprintf( 'gv-field-filter-%s-%d', $context, $form_id );
1084
			?>
1085
            <div id="<?php echo esc_html( $context ); ?>-available-fields-<?php echo esc_attr( $form_id ); ?>" class="hide-if-js gv-tooltip">
1086
                <span class="close"><i class="dashicons dashicons-dismiss"></i></span>
1087
                <div class="gv-field-filter-form">
1088
                    <label class="screen-reader-text" for="<?php echo esc_html( $filter_field_id ); ?>"><?php esc_html_e( 'Filter Fields:', 'gravityview' ); ?></label>
1089
                    <input type="search" class="widefat gv-field-filter" aria-controls="<?php echo $filter_field_id; ?>" id="<?php echo esc_html( $filter_field_id ); ?>" placeholder="<?php esc_html_e( 'Filter fields by name or label', 'gravityview' ); ?>" />
1090
                </div>
1091
1092
                <div id="available-fields-<?php echo $filter_field_id; ?>" aria-live="polite" role="listbox">
1093
                <?php do_action('gravityview_render_available_fields', $form_id, $context ); ?>
1094
                </div>
1095
1096
                <div class="gv-no-results hidden description"><?php esc_html_e( 'No fields were found matching the search.', 'gravityview' ); ?></div>
1097
            </div>
1098
			<?php
1099
		}
1100
    }
1101
1102
	/**
1103
	 * Render the Template Active Areas and configured active fields for a given template id and post id
1104
	 *
1105
	 * @access public
1106
	 * @param string $template_id (default: '') Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()}
1107
	 * @param string $post_id (default: '')
1108
	 * @param string $context (default: 'single')
1109
	 * @return string HTML of the active areas
1110
	 */
1111
	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...
1112
		if( empty( $template_id ) ) {
1113
			gravityview()->log->debug( '[render_directory_active_areas] {template_id} is empty', array( 'template_id' => $template_id ) );
1114
			return '';
1115
		}
1116
1117
		/**
1118
		 * @filter `gravityview_template_active_areas`
1119
		 * @see GravityView_Template::assign_active_areas()
1120
		 * @param array $template_areas Empty array, to be filled in by the template class
1121
		 * @param string $template_id Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()}
1122
		 * @param string $context Current View context: `directory`, `single`, or `edit` (default: 'single')
1123
		 */
1124
		$template_areas = apply_filters( 'gravityview_template_active_areas', array(), $template_id, $context );
1125
1126
		if( empty( $template_areas ) ) {
1127
1128
			gravityview()->log->debug( '[render_directory_active_areas] No areas defined. Maybe template {template_id} is disabled.', array( 'data' => $template_id ) );
1129
			$output = '<div>';
1130
			$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>';
1131
			$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>';
1132
			$output .= '</div>';
1133
		} else {
1134
1135
			$fields = '';
1136
			if ( ! empty( $post_id ) ) {
1137
				$fields = gravityview_get_directory_fields( $post_id );
1138
			}
1139
1140
			ob_start();
1141
			$this->render_active_areas( $template_id, 'field', $context, $template_areas, $fields );
1142
			$output = ob_get_clean();
1143
1144
		}
1145
1146
		if( $echo ) {
1147
			echo $output;
1148
		}
1149
1150
		return $output;
1151
	}
1152
1153
	/**
1154
	 * Enqueue scripts and styles at Views editor
1155
	 *
1156
	 * @access public
1157
	 * @param mixed $hook
1158
	 * @return void
1159
	 */
1160
	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...
1161
		global $plugin_page, $pagenow;
1162
1163
		$is_widgets_page = ( $pagenow === 'widgets.php' );
1164
1165
		// Add the GV font (with the Astronaut)
1166
        wp_enqueue_style( 'gravityview_global', plugins_url('assets/css/admin-global.css', GRAVITYVIEW_FILE), array(), \GV\Plugin::$version );
1167
		wp_register_style( 'gravityview_views_styles', plugins_url( 'assets/css/admin-views.css', GRAVITYVIEW_FILE ), array( 'dashicons', 'wp-jquery-ui-dialog' ), \GV\Plugin::$version );
1168
1169
		wp_register_script( 'gravityview-jquery-cookie', plugins_url('assets/lib/jquery.cookie/jquery.cookie.min.js', GRAVITYVIEW_FILE), array( 'jquery' ), \GV\Plugin::$version, true );
1170
1171
		if( GFForms::get_page() === 'form_list' ) {
1172
			wp_enqueue_style( 'gravityview_views_styles' );
1173
			return;
1174
        }
1175
1176
		// Don't process any scripts below here if it's not a GravityView page.
1177
		if( ! gravityview()->request->is_admin( $hook, 'single' ) && ! $is_widgets_page ) {
0 ignored issues
show
Unused Code introduced by
The call to Frontend_Request::is_admin() has too many arguments starting with $hook.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
Unused Code introduced by
The call to Request::is_admin() has too many arguments starting with $hook.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
1178
		    return;
1179
		}
1180
1181
        wp_enqueue_script( 'jquery-ui-datepicker' );
1182
        wp_enqueue_style( 'gravityview_views_datepicker', plugins_url('assets/css/admin-datepicker.css', GRAVITYVIEW_FILE), \GV\Plugin::$version );
1183
1184
        $script_debug = (defined('SCRIPT_DEBUG') && SCRIPT_DEBUG) ? '' : '.min';
1185
1186
        //enqueue scripts
1187
        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' ), \GV\Plugin::$version );
1188
1189
        wp_localize_script('gravityview_views_scripts', 'gvGlobals', array(
1190
            'cookiepath' => COOKIEPATH,
1191
            'passed_form_id' => (bool) \GV\Utils::_GET( 'form_id' ),
1192
            'nonce' => wp_create_nonce( 'gravityview_ajaxviews' ),
1193
            'label_viewname' => __( 'Enter View name here', 'gravityview' ),
1194
            'label_close' => __( 'Close', 'gravityview' ),
1195
            'label_cancel' => __( 'Cancel', 'gravityview' ),
1196
            'label_continue' => __( 'Continue', 'gravityview' ),
1197
            'label_ok' => __( 'Ok', 'gravityview' ),
1198
            'label_publisherror' => __( 'Error while creating the View for you. Check the settings or contact GravityView support.', 'gravityview' ),
1199
            'loading_text' => esc_html__( 'Loading&hellip;', 'gravityview' ),
1200
            'loading_error' => esc_html__( 'There was an error loading dynamic content.', 'gravityview' ),
1201
            'field_loaderror' => __( 'Error while adding the field. Please try again or contact GravityView support.', 'gravityview' ),
1202
            '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' ),
1203
        ));
1204
1205
		wp_enqueue_style( 'gravityview_views_styles' );
1206
1207
        // Enqueue scripts needed for merge tags
1208
        self::enqueue_gravity_forms_scripts();
1209
	}
1210
1211
	/**
1212
	 * Enqueue Gravity Forms scripts, needed for Merge Tags
1213
     *
1214
     * @since 1.0.5-beta
1215
     *
1216
     * @return void
1217
	 */
1218
	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...
1219
		GFForms::register_scripts();
1220
1221
		$scripts = array(
1222
		    'sack',
1223
		    'gform_gravityforms',
1224
		    'gform_forms',
1225
		    'gform_form_admin',
1226
		    'jquery-ui-autocomplete'
1227
		);
1228
1229
		if ( wp_is_mobile() ) {
1230
		    $scripts[] = 'jquery-touch-punch';
1231
		}
1232
1233
		wp_enqueue_script( $scripts );
1234
	}
1235
1236
	/**
1237
	 * Add GravityView scripts and styles to Gravity Forms and GravityView No-Conflict modes
1238
	 *
1239
	 * @param array $registered Existing scripts or styles that have been registered (array of the handles)
1240
	 *
1241
	 * @return array
1242
	 */
1243
	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...
1244
1245
		$allowed_dependencies = array();
1246
1247
		$filter = current_filter();
1248
1249
		if ( preg_match( '/script/ism', $filter ) ) {
1250
1251
			$allowed_dependencies = array(
1252
				'jquery-ui-core',
1253
				'jquery-ui-dialog',
1254
				'jquery-ui-tabs',
1255
				'jquery-ui-draggable',
1256
				'jquery-ui-droppable',
1257
				'jquery-ui-sortable',
1258
				'jquery-ui-tooltip',
1259
				'gravityview_views_scripts',
1260
				'gravityview-support',
1261
				'gravityview-jquery-cookie',
1262
				'gravityview_views_datepicker',
1263
				'sack',
1264
				'gform_gravityforms',
1265
				'gform_forms',
1266
				'gform_form_admin',
1267
				'jquery-ui-autocomplete'
1268
			);
1269
1270
		} elseif ( preg_match( '/style/ism', $filter ) ) {
1271
1272
			$allowed_dependencies = array(
1273
				'dashicons',
1274
				'wp-jquery-ui-dialog',
1275
				'gravityview_views_styles',
1276
				'gravityview_global',
1277
				'gravityview_views_datepicker'
1278
			);
1279
		}
1280
1281
		return array_merge( $registered, $allowed_dependencies );
1282
	}
1283
1284
1285
}
1286
1287
new GravityView_Admin_Views;
1288