Completed
Push — develop ( 77088b...ed69dc )
by Zack
19:18
created

GravityView_Admin_Views::gform_toolbar_menu()   B

Complexity

Conditions 8
Paths 19

Size

Total Lines 80

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
nc 19
nop 2
dl 0
loc 80
ccs 0
cts 30
cp 0
crap 72
rs 7.1919
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Renders all the metaboxes on Add New / Edit View post type.
4
 *
5
 * @package   GravityView
6
 * @license   GPL2+
7
 * @author    GravityView <[email protected]>
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'), 10, 2 );
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' ) );
60
61
		add_filter( 'gravityview/support_port/localization_data', array( $this, 'suggest_support_articles' ) );
62
	}
63
64
	/**
65
     * When on the Add/Edit View screen, suggest most popular articles related to that
66
     *
67
	 * @param array $localization_data Data to be passed to the Support Port JS
68
	 *
69
	 * @return array
70
	 */
71
	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...
72
73
	    if( ! gravityview()->request->is_view() ) {
74
	        return $localization_data;
75
        }
76
77
		$localization_data['suggest'] = array(
78
            '57ef23539033602e61d4a560',
79
            '54c67bb9e4b0512429885513',
80
            '54c67bb9e4b0512429885512',
81
            '54c67bbbe4b07997ea3f3f6b',
82
            '54d1a33ae4b086c0c0964ce9',
83
            '57ef253c9033602e61d4a563',
84
            '552355bfe4b0221aadf2572b',
85
            '54c67bcde4b051242988553e',
86
        );
87
88
		return $localization_data;
89
	}
90
91
	/**
92
	 * @since 1.15
93
	 * @param WP_Query $query
94
	 */
95
	public function filter_pre_get_posts( &$query ) {
96
		global $pagenow;
97
98
		if ( ! is_admin() ) {
99
			return;
100
		}
101
102
		if ( 'edit.php' !== $pagenow ) {
103
			return;
104
		}
105
106
		if ( ! isset( $query->query_vars['post_type'] ) ) {
107
			return;
108
		}
109
110
		if ( 'gravityview' !== $query->query_vars['post_type'] ) {
111
			return;
112
		}
113
114
		$form_id = (int) \GV\Utils::_GET( 'gravityview_form_id' );
115
116
		$meta_query = array();
117
118
		if ( $form_id ) {
119
			$meta_query[] = array(
120
					'key'   => '_gravityview_form_id',
121
					'value' => $form_id,
122
			);
123
		}
124
125
		$layout_id = \GV\Utils::_GET( 'gravityview_layout' );
126
127
		if ( $layout_id ) {
128
			$meta_query[] = array(
129
					'key'   => '_gravityview_directory_template',
130
					'value' => esc_attr( $layout_id ),
131
			);
132
		}
133
134
		$query->set( 'meta_query', $meta_query );
135
	}
136
137
	/**
138
	 * Adds dropdown selects to filter Views by connected form and layout
139
	 *
140
	 * @return void
141
	 */
142
	public function add_view_dropdown() {
143
		$current_screen = get_current_screen();
144
145
		if( 'gravityview' !== $current_screen->post_type ) {
146
			return;
147
		}
148
149
		$forms = gravityview_get_forms();
150
		$current_form = \GV\Utils::_GET( 'gravityview_form_id' );
151
152
		// If there are no forms to select, show no forms.
153
		if( ! empty( $forms ) ) { ?>
154
			<label for="gravityview_form_id" class="screen-reader-text"><?php esc_html_e( 'Filter Views by form', 'gravityview' ); ?></label>
155
			<select name="gravityview_form_id" id="gravityview_form_id">
156
				<option value="" <?php selected( '', $current_form, true ); ?>><?php esc_html_e( 'All forms', 'gravityview' ); ?></option>
157
				<?php foreach( $forms as $form ) { ?>
158
					<option value="<?php echo esc_attr( $form['id'] ); ?>" <?php selected( $form['id'], $current_form, true ); ?>><?php echo esc_html( $form['title'] ); ?></option>
159
				<?php } ?>
160
			</select>
161
		<?php }
162
163
		$layouts = gravityview_get_registered_templates();
164
		$current_layout = \GV\Utils::_GET( 'gravityview_layout' );
165
166
		// If there are no forms to select, show no forms.
167
		if( ! empty( $layouts ) ) { ?>
168
			<label for="gravityview_layout_name" class="screen-reader-text"><?php esc_html_e( 'Filter Views by layout', 'gravityview' ); ?></label>
169
			<select name="gravityview_layout" id="gravityview_layout_name">
170
				<option value="" <?php selected( '', $current_layout, true ); ?>><?php esc_html_e( 'All layouts', 'gravityview' ); ?></option>
171
				<optgroup label="<?php esc_html_e( 'Layouts', 'gravityview' ); ?>">
172
				<?php foreach( $layouts as $layout_id => $layout ) {
173
					if ( in_array( $layout['type'], array( 'preset', 'internal' ), true ) ) {
174
						continue;
175
					}
176
					?>
177
					<option value="<?php echo esc_attr( $layout_id ); ?>" <?php selected( $layout_id, $current_layout, true ); ?>><?php echo esc_html( $layout['label'] ); ?></option>
178
				<?php } ?>
179
				</optgroup>
180
				<optgroup label="<?php esc_html_e( 'Form Presets', 'gravityview' ); ?>">
181
				<?php foreach( $layouts as $layout_id => $layout ) {
182
					if ( ! in_array( $layout['type'], array( 'preset' ), true ) ) {
183
						continue;
184
					}
185
					?>
186
					<option value="<?php echo esc_attr( $layout_id ); ?>" <?php selected( $layout_id, $current_layout, true ); ?>><?php echo esc_html( $layout['label'] ); ?></option>
187
				<?php } ?>
188
				</optgroup>
189
			</select>
190
		<?php }
191
	}
192
193
194
	/**
195
	 * @deprecated since 1.2
196
	 * Start using GravityView_Render_Settings::render_setting_row
197
	 */
198
	public static function render_setting_row( $key = '', $current_settings = array(), $override_input = null, $name = 'template_settings[%s]', $id = 'gravityview_se_%s' ) {
199
		_deprecated_function( 'GravityView_Admin_Views::render_setting_row', '1.1.7', 'GravityView_Render_Settings::render_setting_row' );
200
		GravityView_Render_Settings::render_setting_row( $key, $current_settings, $override_input, $name , $id );
201
	}
202
203
	/**
204
	 * @deprecated since 1.2
205
	 * Start using GravityView_Render_Settings::render_field_option
206
	 */
207
	public static function render_field_option( $name = '', $option, $curr_value = NULL ) {
208
		_deprecated_function( 'GravityView_Admin_Views::render_field_option', '1.1.7', 'GravityView_Render_Settings::render_field_option' );
209
		return GravityView_Render_Settings::render_field_option( $name, $option, $curr_value );
210
	}
211
212
213
	/**
214
	 * Add a GravityView menu to the Form Toolbar with connected views
215
	 * @param  array  $menu_items Menu items, as set in GFForms::top_toolbar()
216
	 * @param  int $id         ID of the current Gravity form
217
	 * @return array            Modified array
218
	 */
219
	public static function gform_toolbar_menu( $menu_items = array(), $id = NULL ) {
220
221
		// Don't show on Trashed forms
222
		if ( 'trash' === rgget( 'filter' ) ) {
223
			return $menu_items;
224
		}
225
226
		$connected_views = gravityview_get_connected_views( $id, array( 'post_status' => 'any' ) );
227
228
		$priority = 0;
229
230
		if( 'form_list' === GFForms::get_page() ) {
231
			$priority = 790;
232
        }
233
234
		if( empty( $connected_views ) ) {
235
236
		    $menu_items['gravityview'] = array(
237
				'label'          => esc_attr__( 'Create a View', 'gravityview' ),
238
				'icon'           => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>', // Only appears in GF pre-2.5
239
				'title'          => esc_attr__( 'Create a View using this form as a data source', 'gravityview' ),
240
				'url'            => admin_url( 'post-new.php?post_type=gravityview&form_id=' . $id ),
241
				'menu_class'     => 'gv_connected_forms gf_form_toolbar_settings',
242
				'priority'       => $priority,
243
				'capabilities'   => array( 'edit_gravityviews' ),
244
			);
245
246
			return $menu_items;
247
		}
248
249
		$sub_menu_items = array();
250
		foreach ( (array)$connected_views as $view ) {
251
252
			if( ! GVCommon::has_cap( 'edit_gravityview', $view->ID ) ) {
253
				continue;
254
			}
255
256
			$label = empty( $view->post_title ) ? sprintf( __('No Title (View #%d)', 'gravityview' ), $view->ID ) : $view->post_title;
257
258
			$sub_menu_items[] = array(
259
				'label' => esc_attr( $label ),
260
				'url' => admin_url( 'post.php?action=edit&post='.$view->ID ),
261
				'icon' => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>',
262
			);
263
		}
264
265
		// If there were no items added, then let's create the parent menu
266
		if( $sub_menu_items ) {
267
268
		    $sub_menu_items[] = array(
269
			    'label' => esc_attr__( 'Create a View', 'gravityview' ),
270
			    'icon' => '<span class="dashicons dashicons-plus"></span>',
271
			    'title' => esc_attr__( 'Create a View using this form as a data source', 'gravityview' ),
272
			    'url'   => admin_url( 'post-new.php?post_type=gravityview&form_id=' . $id ),
273
			    'capabilities'   => array( 'edit_gravityviews' ),
274
            );
275
276
			// Make sure Gravity Forms uses the submenu; if there's only one item, it uses a link instead of a dropdown
277
			$sub_menu_items[] = array(
278
				'url' => '#',
279
				'label' => '',
280
				'menu_class' => 'hidden',
281
				'capabilities' => '',
282
			);
283
284
			$menu_items['gravityview'] = array(
285
				'label'          => __( 'Connected Views', 'gravityview' ),
286
				'icon'           => '<i class="fa fa-lg gv-icon-astronaut-head gv-icon"></i>',
287
				'title'          => __( 'GravityView Views using this form as a data source', 'gravityview' ),
288
				'url'            => '#',
289
				'onclick'        => 'return false;',
290
				'menu_class'     => 'gv_connected_forms gf_form_toolbar_settings',
291
				'sub_menu_items' => $sub_menu_items,
292
				'priority'       => $priority,
293
				'capabilities'   => array( 'edit_gravityviews' ),
294
			);
295
		}
296
297
		return $menu_items;
298
	}
299
300
	/**
301
	 * List the field types without presentation properties (on a View context)
302
	 *
303
	 * @param array $array Existing field types to add to a blacklist
304
	 * @param string|null $context Context for the blacklist. Default: NULL.
305
	 * @return array Default blacklist fields merged with existing blacklist fields
306
	 */
307
	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...
308
309
		$add = array( 'captcha', 'page' );
310
311
		// Don't allowing editing the following values:
312
		if( $context === 'edit' ) {
313
			$add[] = 'post_id';
314
		}
315
316
		$return = array_merge( $array, $add );
317
318
		return $return;
319
	}
320
321
	/**
322
	 * Add tooltip text for use throughout the UI
323
	 * @param  array       $tooltips Array of Gravity Forms tooltips
324
	 * @return array                Modified tooltips array
325
	 */
326
	public function tooltips( $tooltips = array() ) {
327
328
		$gv_tooltips = array();
329
330
		// Generate tooltips for View settings
331
		$default_args = \GV\View_Settings::defaults( true );
332
333
		foreach ( $default_args as $key => $arg ) {
334
335
			// If an arg has `tooltip` defined, but it's false, don't display a tooltip
336
			if( isset( $arg['tooltip'] ) && empty( $arg['tooltip'] ) ) { continue; }
337
338
			// By default, use `tooltip` if defined.
339
			$tooltip = empty( $arg['tooltip'] ) ? NULL : $arg['tooltip'];
340
341
			// If there's no tooltip set, continue
342
			if( empty( $tooltip ) ) {
343
				continue;
344
			}
345
346
			// Add the tooltip
347
			$gv_tooltips[ 'gv_'.$key ] = array(
348
				'title'	=> $arg['label'],
349
				'value'	=> $tooltip,
350
			);
351
352
		}
353
354
		$gv_tooltips['gv_css_merge_tags'] = array(
355
			'title' => __('CSS Merge Tags', 'gravityview'),
356
			'value' => sprintf( __( 'Developers: The CSS classes will be sanitized using the %ssanitize_title_with_dashes()%s function.', 'gravityview'), '<code>', '</code>' )
357
		);
358
359
		/**
360
		 * @filter `gravityview_tooltips` The tooltips GravityView adds to the Gravity Forms tooltip array
361
		 * @param array $gv_tooltips Associative array with unique keys containing array of `title` and `value` keys, as expected by `gform_tooltips` filter
362
		 * @deprecated Renamed to `gravityview/metaboxes/tooltips`
363
		 */
364
		$gv_tooltips = apply_filters( 'gravityview_tooltips', $gv_tooltips );
365
366
		/**
367
		 * @filter `gravityview/metaboxes/tooltips` The tooltips GravityView adds to the Gravity Forms tooltip array
368
		 * @param array $gv_tooltips Associative array with unique keys containing array of `title` and `value` keys, as expected by `gform_tooltips` filter
369
		 */
370
		$gv_tooltips = apply_filters( 'gravityview/metaboxes/tooltips', $gv_tooltips );
371
372
		foreach ( $gv_tooltips as $key => $tooltip ) {
373
374
			$title = empty( $tooltip['title'] ) ? '' : '<h6>'.esc_html( $tooltip['title'] ) .'</h6>';
375
376
			$tooltips[ $key ] = $title . wpautop( esc_html( $tooltip['value'] ) );
377
		}
378
379
		return $tooltips;
380
	}
381
382
	/**
383
	 * Add the Data Source information
384
	 *
385
	 * @param null $column_name
386
	 * @param $post_id
387
	 *
388
	 * @return void
389
	 */
390
	public function add_custom_column_content( $column_name = NULL, $post_id )	{
391
392
		$output = '';
393
394
		switch ( $column_name ) {
395
			case 'gv_template':
396
397
				$template_id = gravityview_get_template_id( $post_id );
398
399
				// All Views should have a connected form. If it doesn't, that's not right.
400
				if ( empty( $template_id ) ) {
401
					gravityview()->log->error( 'View ID {view_id} does not have a connected template.', array( 'view_id' => $post_id ) );
402
					break;
403
				}
404
405
				$templates = gravityview_get_registered_templates();
406
407
				$template = isset( $templates[ $template_id ] ) ? $templates[ $template_id ] : false;
408
409
				// Generate backup if label doesn't exist: `example_name` => `Example Name`
410
				$template_id_pretty = ucwords( implode( ' ', explode( '_', $template_id ) ) );
411
412
				$output = $template ? $template['label'] : $template_id_pretty;
413
414
				break;
415
416
			case 'gv_connected_form':
417
418
				$form_id = gravityview_get_form_id( $post_id );
419
420
				// All Views should have a connected form. If it doesn't, that's not right.
421
				if ( empty( $form_id ) ) {
422
					gravityview()->log->error( 'View ID {view_id} does not have a connected GF form.', array( 'view_id' => $post_id ) );
423
					$output = __( 'Not connected.', 'gravityview' );
424
					break;
425
				}
426
427
				$form = gravityview_get_form( $form_id );
428
429
				if ( ! $form ) {
430
					gravityview()->log->error( 'Connected form not found: Form #{form_id}', array( 'form_id' => $form_id ) );
431
432
					$output = __( 'The connected form can not be found; it may no longer exist.', 'gravityview' );
433
				} else {
434
					$output = self::get_connected_form_links( $form );
435
				}
436
437
				break;
438
		}
439
440
		echo $output;
441
	}
442
443
444
	/**
445
	 * Get HTML links relating to a connected form, like Edit, Entries, Settings, Preview
446
	 * @param  array|int $form Gravity Forms forms array, or the form ID
447
	 * @param  boolean $include_form_link Whether to include the bold name of the form in the output
448
	 * @return string          HTML links
449
	 */
450
	static public function get_connected_form_links( $form, $include_form_link = true ) {
451
452
		// Either the form is empty or the form ID is 0, not yet set.
453
		if( empty( $form ) ) {
454
			return '';
455
		}
456
457
		// The $form is passed as the form ID
458
		if( !is_array( $form ) ) {
459
			$form = gravityview_get_form( $form );
460
		}
461
462
		$form_id = $form['id'];
463
		$links = array();
464
465
		if( GVCommon::has_cap( 'gravityforms_edit_forms' ) ) {
466
			$form_url = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;id=%d', $form_id ) );
467
			$form_link = '<strong class="gv-form-title">'.gravityview_get_link( $form_url, $form['title'], 'class=row-title' ).'</strong>';
468
			$links[] = '<span>'.gravityview_get_link( $form_url, __('Edit Form', 'gravityview') ).'</span>';
469
		} else {
470
			$form_link = '<strong class="gv-form-title">'. esc_html( $form['title'] ). '</strong>';
471
		}
472
473
		if( GVCommon::has_cap( 'gravityforms_view_entries' ) ) {
474
			$entries_url = admin_url( sprintf( 'admin.php?page=gf_entries&amp;id=%d', $form_id ) );
475
			$links[] = '<span>'.gravityview_get_link( $entries_url, __('Entries', 'gravityview') ).'</span>';
476
		}
477
478
		if( GVCommon::has_cap( array( 'gravityforms_edit_settings', 'gravityview_view_settings' ) ) ) {
479
			$settings_url = admin_url( sprintf( 'admin.php?page=gf_edit_forms&amp;view=settings&amp;id=%d', $form_id ) );
480
			$links[] = '<span>'.gravityview_get_link( $settings_url, __('Settings', 'gravityview'), 'title='.__('Edit settings for this form', 'gravityview') ).'</span>';
481
		}
482
483
		if( GVCommon::has_cap( array("gravityforms_edit_forms", "gravityforms_create_form", "gravityforms_preview_forms") ) ) {
484
			$preview_url = site_url( sprintf( '?gf_page=preview&amp;id=%d', $form_id ) );
485
			$links[] = '<span>'.gravityview_get_link( $preview_url, __('Preview Form', 'gravityview'), 'title='.__('Preview this form', 'gravityview') ).'</span>';
486
		}
487
488
		$output = '';
489
490
		if( !empty( $include_form_link ) ) {
491
			$output .= $form_link;
492
		}
493
494
		/**
495
		 * @filter `gravityview_connected_form_links` Modify the links shown in the Connected Form links
496
		 * @since 1.6
497
		 * @param array $links Links to show
498
		 * @param array $form Gravity Forms form array
499
		 */
500
		$links = apply_filters( 'gravityview_connected_form_links', $links, $form );
501
502
		$css_class = 'row-actions';
503
504
		// Is Screen Options > View mode set to "Extended view"? If so, keep actions visible.
505
		if( 'excerpt' === get_user_setting( 'posts_list_mode', 'list' ) ) {
506
			$css_class = 'row-actions visible';
507
		}
508
509
		$output .= '<div class="' . $css_class . '">'. implode( ' | ', $links ) .'</div>';
510
511
		return $output;
512
	}
513
514
	/**
515
	 * Add the Data Source column to the Views page
516
	 * @param  array      $columns Columns array
517
	 */
518
	public function add_post_type_columns( $columns ) {
519
520
		// Get the date column and save it for later to add back in.
521
		// This adds it after the Data Source column.
522
		// This way, we don't need to do array_slice, array_merge, etc.
523
		$date = $columns['date'];
524
		unset( $columns['date'] );
525
526
		$data_source_required_caps = array(
527
			'gravityforms_edit_forms',
528
			'gravityforms_view_entries',
529
			'gravityforms_edit_settings',
530
			'gravityview_view_settings',
531
			'gravityforms_create_form',
532
			'gravityforms_preview_forms',
533
		);
534
535
		if( GVCommon::has_cap( $data_source_required_caps ) ) {
536
			$columns['gv_connected_form'] = __( 'Data Source', 'gravityview' );
537
		}
538
539
		$columns['gv_template'] = _x( 'Template', 'Column title that shows what template is being used for Views', 'gravityview' );
540
541
		// Add the date back in.
542
		$columns['date'] = $date;
543
544
		return $columns;
545
	}
546
547
	/**
548
	 * Save View configuration
549
	 *
550
	 * @param int $post_id Currently saved Post ID
551
	 * @return void
552
	 */
553
	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...
554
555
		if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ){
556
			return;
557
		}
558
559
		// validate post_type
560
		if ( ! isset( $_POST['post_type'] ) || 'gravityview' != $_POST['post_type'] ) {
561
			return;
562
		}
563
564
		// validate user can edit and save View
565
		if ( ! GVCommon::has_cap( 'edit_gravityview', $post_id ) ) {
566
			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() ) );
567
			return;
568
		}
569
570
		gravityview()->log->debug( '[save_postdata] Saving View post type.', array( 'data' => $_POST ) );
571
572
		$statii = array();
573
574
		// check if this is a start fresh View
575
		if ( isset( $_POST['gravityview_select_form_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_form_nonce'], 'gravityview_select_form' ) ) {
576
577
			$form_id = !empty( $_POST['gravityview_form_id'] ) ? $_POST['gravityview_form_id'] : '';
578
			// save form id
579
			$statii['form_id'] = update_post_meta( $post_id, '_gravityview_form_id', $form_id );
580
581
		}
582
583
		if( false === GVCommon::has_cap( 'gravityforms_create_form' ) && empty( $statii['form_id'] ) ) {
584
			gravityview()->log->error( 'Current user does not have the capability to create a new Form.', array( 'data' => wp_get_current_user() ) );
585
			return;
586
		}
587
588
		// Was this a start fresh?
589
		if ( ! empty( $_POST['gravityview_form_id_start_fresh'] ) ) {
590
			$statii['start_fresh'] = add_post_meta( $post_id, '_gravityview_start_fresh', 1 );
591
		} else {
592
			$statii['start_fresh'] = delete_post_meta( $post_id, '_gravityview_start_fresh' );
593
		}
594
595
		// Check if we have a template id
596
		if ( isset( $_POST['gravityview_select_template_nonce'] ) && wp_verify_nonce( $_POST['gravityview_select_template_nonce'], 'gravityview_select_template' ) ) {
597
598
			$template_id = !empty( $_POST['gravityview_directory_template'] ) ? $_POST['gravityview_directory_template'] : '';
599
600
			// now save template id
601
			$statii['directory_template'] = update_post_meta( $post_id, '_gravityview_directory_template', $template_id );
602
		}
603
604
605
		// save View Configuration metabox
606
		if ( isset( $_POST['gravityview_view_configuration_nonce'] ) && wp_verify_nonce( $_POST['gravityview_view_configuration_nonce'], 'gravityview_view_configuration' ) ) {
607
608
			// template settings
609
			if( empty( $_POST['template_settings'] ) ) {
610
				$_POST['template_settings'] = array();
611
			}
612
			$statii['template_settings'] = update_post_meta( $post_id, '_gravityview_template_settings', $_POST['template_settings'] );
613
614
			// guard against unloaded View configuration page
615
			if ( isset( $_POST['gv_fields'] ) && isset( $_POST['gv_fields_done'] ) ) {
616
				$fields = array();
617
618
				if ( ! empty( $_POST['gv_fields'] ) ) {
619
					$fields = _gravityview_process_posted_fields();
620
				}
621
622
				$fields = wp_slash( $fields );
623
624
				$statii['directory_fields'] = update_post_meta( $post_id, '_gravityview_directory_fields', $fields );
625
			}
626
627
			// Directory Visible Widgets
628
			if( empty( $_POST['widgets'] ) ) {
629
				$_POST['widgets'] = array();
630
			}
631
			$statii['directory_widgets'] = gravityview_set_directory_widgets( $post_id, $_POST['widgets'] );
632
633
		} // end save view configuration
634
635
		/**
636
		 * @action `gravityview_view_saved` After a View has been saved in the admin
637
		 * @param int $post_id ID of the View that has been saved
638
		 * @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.
639
		 * @since 1.17.2
640
		 */
641
		do_action('gravityview_view_saved', $post_id, $statii );
642
643
		gravityview()->log->debug( '[save_postdata] Update Post Meta Statuses (also returns false if nothing changed)', array( 'data' => array_map( 'intval', $statii ) ) );
644
	}
645
646
	/**
647
	 * @deprecated 1.1.6
648
	 */
649
	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...
650
		_deprecated_function( 'GravityView_Admin_Views::render_label()', '1.1.6', 'Use the GravityView_Admin_View_Field class instead.' );
651
	}
652
653
	/**
654
	 * Render html for displaying available fields based on a Form ID
655
	 *
656
     * @see GravityView_Ajax::get_available_fields_html() Triggers `gravityview_render_available_fields` action
657
	 *
658
	 * @param int $form Gravity Forms Form ID (default: '')
659
	 * @param string $context (default: 'single')
660
     *
661
	 * @return void
662
	 */
663
	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...
664
665
	    // Determine if form is a preset and convert it to an array with fields
666
		$form = ( is_string( $form ) && preg_match( '/^preset_/', $form ) ) ? GravityView_Ajax::pre_get_form_fields( $form ) : $form;
667
668
		/**
669
		 * @filter  `gravityview_blacklist_field_types` Modify the types of fields that shouldn't be shown in a View.
670
		 * @param[in,out] array $blacklist_field_types Array of field types to block for this context.
671
		 * @param[in] string $context View context ('single', 'directory', or 'edit')
672
		 */
673
		$blacklist_field_types = apply_filters( 'gravityview_blacklist_field_types', array(), $context );
674
675
		if ( ! is_array( $blacklist_field_types ) ) {
676
677
		    gravityview()->log->error( '$blacklist_field_types is not an array', array( 'data' => print_r( $blacklist_field_types, true ) ) );
678
679
			$blacklist_field_types = array();
680
		}
681
682
		$fields = $this->get_available_fields( $form, $context );
683
684
		$output = '';
685
686
		if( !empty( $fields ) ) {
687
688
			foreach( $fields as $id => $details ) {
689
690
				if( in_array( $details['type'], (array) $blacklist_field_types ) ) {
691
					continue;
692
				}
693
694
				// Edit mode only allows editing the parent fields, not single inputs.
695
				if( $context === 'edit' && ! empty( $details['parent'] ) ) {
696
					continue;
697
				}
698
699
				$output .= new GravityView_Admin_View_Field( $details['label'], $id, $details, array(), $form );
700
701
			} // End foreach
702
		}
703
704
		echo $output;
705
706
		// For the EDIT view we only want to allow the form fields.
707
		if( $context === 'edit' ) {
708
			return;
709
		}
710
711
		$this->render_additional_fields( $form, $context );
712
	}
713
714
	/**
715
	 * Render html for displaying additional fields based on a Form ID
716
	 *
717
	 * @param int $form Gravity Forms Form ID (default: '')
718
	 * @param string $context (default: 'single')
719
	 * @return void
720
	 */
721
	public function render_additional_fields( $form = 0, $context = 'single' ) {
722
723
		$additional_fields = array(
724
			array(
725
				'label_text' => __( 'Add All Form Fields', 'gravityview' ),
726
				'desc' => __('Insert all the form fields at once.', 'gravityview'),
727
				'field_id' => 'all-fields',
728
				'label_type' => 'field',
729
				'input_type' => null,
730
				'field_options' => null,
731
				'settings_html'	=> null,
732
				'icon' => 'dashicons-plus-alt',
733
			)
734
		);
735
736
		/**
737
		 * @filter `gravityview_additional_fields` non-standard Fields to show at the bottom of the field picker
738
		 * @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
739
		 */
740
		$additional_fields = apply_filters( 'gravityview_additional_fields', $additional_fields );
741
742
		foreach ( (array) $additional_fields as $item ) {
743
744
			// Prevent items from not having index set
745
			$item = wp_parse_args( $item, array(
746
				'label_text' => null,
747
				'field_id' => null,
748
				'label_type' => null,
749
				'input_type' => null,
750
				'field_options' => null,
751
				'settings_html'	=> null,
752
				'icon' => null,
753
			));
754
755
			// Backward compat.
756
			if( !empty( $item['field_options'] ) ) {
757
				// Use settings_html from now on.
758
				$item['settings_html'] = $item['field_options'];
759
			}
760
761
			// Render a label for each of them
762
			echo new GravityView_Admin_View_Field( $item['label_text'], $item['field_id'], $item, $settings = array(), $form );
763
764
		}
765
766
	}
767
768
	/**
769
	 * Retrieve the default fields id, label and type
770
	 * @param  string|array $form form_ID or form object
771
	 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
772
	 * @return array
773
	 */
774
	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...
775
776
		$entry_default_fields = array();
777
778
		// if in zone directory or single
779
		if( in_array( $zone, array( 'directory', 'single' ), true ) ) {
780
781
			$meta_fields = GravityView_Fields::get_all( array( 'meta', 'gravityview', 'add-ons' ) );
782
783
			$entry_default_fields = array();
784
785
			foreach ( $meta_fields as $meta_field ) {
786
				$entry_default_fields += $meta_field->as_array();
787
			}
788
		}
789
790
		/**
791
		 * @filter `gravityview_entry_default_fields` Modify the default fields for each zone and context
792
		 * @param array $entry_default_fields Array of fields shown by default
793
		 * @param  string|array $form form_ID or form object
794
		 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
795
		 */
796
		return apply_filters( 'gravityview_entry_default_fields', $entry_default_fields, $form, $zone);
797
	}
798
799
	/**
800
	 * Calculate the available fields
801
	 * @param  string|array $form form_ID or form object
802
	 * @param  string $zone   Either 'single', 'directory', 'header', 'footer'
803
	 * @return array         fields
804
	 */
805
	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...
806
807
		if( empty( $form ) ) {
808
			gravityview()->log->error( '$form is empty' );
809
			return array();
810
		}
811
812
		// get form fields
813
		$fields = gravityview_get_form_fields( $form, true );
814
815
		// get meta fields ( only if form was already created )
816
		if( !is_array( $form ) ) {
817
			$meta_fields = gravityview_get_entry_meta( $form );
818
		} else {
819
			$meta_fields = array();
820
		}
821
822
		// get default fields
823
		$default_fields = $this->get_entry_default_fields( $form, $zone );
824
825
		//merge without loosing the keys
826
		$fields = $fields + $meta_fields + $default_fields;
827
828
		// Move Custom Content to top
829
		if ( isset( $fields['custom'] ) ) {
830
			$fields = array( 'custom' => $fields['custom'] ) + $fields;
831
		}
832
833
		$gv_fields = GravityView_Fields::get_all();
834
835
		foreach ( $fields as &$field ) {
836
			foreach ( $gv_fields as $gv_field ) {
837
				if ( \GV\Utils::get( $field, 'type' ) === $gv_field->name ) {
838
					$field['icon'] = \GV\Utils::get( $gv_field, 'icon' );
839
				}
840
			}
841
		}
842
843
		/**
844
		 * @filter `gravityview/admin/available_fields` Modify the available fields that can be used in a View.
845
		 * @param[in,out] array $fields The fields.
846
		 * @param  string|array $form form_ID or form object
847
		 * @param  string $zone Either 'single', 'directory', 'header', 'footer'
848
		 */
849
		return apply_filters( 'gravityview/admin/available_fields', $fields, $form, $zone );
850
	}
851
852
853
	/**
854
	 * Render html for displaying available widgets
855
	 * @return string html
856
	 */
857
	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...
858
859
		$widgets = \GV\Widget::registered();
860
861
		if ( empty( $widgets ) ) {
862
			return;
863
		}
864
865
		foreach ( $widgets as $id => $details ) {
866
			echo new GravityView_Admin_View_Widget( $details['label'], $id, $details );
867
		}
868
869
	}
870
871
	/**
872
	 * Get the list of registered widgets. Each item is used to instantiate a GravityView_Admin_View_Widget object
873
	 * @deprecated Use \GV\Widget::registered()
874
	 * @since 1.13.1
875
	 * @return array
876
	 */
877
	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...
878
879
		_deprecated_function( __METHOD__, '2.0', '\GV\Widget::registered()' );
880
881
		return \GV\Widget::registered();
882
	}
883
884
	/**
885
	 * Generic function to render rows and columns of active areas for widgets & fields
886
	 * @param  string $template_id The current slug of the selected View template
887
	 * @param  string $type   Either 'widget' or 'field'
888
	 * @param  string $zone   Either 'single', 'directory', 'edit', 'header', 'footer'
889
	 * @param  array $rows    The layout structure: rows, columns and areas
890
	 * @param  array $values  Saved objects
891
	 * @return void
892
	 */
893
	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...
894
		global $post;
895
896
		if( $type === 'widget' ) {
897
			$button_label = __( 'Add Widget', 'gravityview' );
898
		} else {
899
			$button_label = __( 'Add Field', 'gravityview' );
900
		}
901
902
		/**
903
		 * @internal Don't rely on this filter! This is for internal use and may change.
904
		 *
905
		 * @since 2.8.1
906
		 *
907
		 * @param string $button_label Text for button: "Add Widget" or "Add Field"
908
		 * @param array $atts {
909
		 *   @type string $type 'widget' or 'field'
910
		 *   @type string $template_id The current slug of the selected View template
911
		 *   @type string $zone Where is this button being shown? Either 'single', 'directory', 'edit', 'header', 'footer'
912
		 * }
913
		 */
914
		$button_label = apply_filters( 'gravityview/admin/add_button_label', $button_label, array( 'type' => $type, 'template_id' => $template_id, 'zone' => $zone ) );
915
916
		$available_items = array();
917
918
		$view = \GV\View::from_post( $post );
919
		$form_id = null;
920
921
		// if saved values, get available fields to label everyone
922
		if( !empty( $values ) && ( !empty( $post->ID ) || !empty( $_POST['template_id'] ) ) ) {
923
924
			if( !empty( $_POST['template_id'] ) ) {
925
				$form = GravityView_Ajax::pre_get_form_fields( $_POST['template_id'] );
926
			} else {
927
				$form_id = $form = gravityview_get_form_id( $post->ID );
928
			}
929
930
			if ( 'field' === $type ) {
931
				$available_items[ $form ] = $this->get_available_fields( $form, $zone );
932
933
				$joined_forms = gravityview_get_joined_forms( $post->ID );
934
935
                foreach ( $joined_forms as $form ) {
936
                    $available_items[ $form->ID ] = $this->get_available_fields( $form->ID, $zone );
937
                }
938
			} else {
939
				$available_items[ $form ] = \GV\Widget::registered();
940
			}
941
		}
942
943
		foreach( $rows as $row ) :
944
			foreach( $row as $col => $areas ) :
945
				$column = ($col == '2-2') ? '1-2' : $col; ?>
946
947
				<div class="gv-grid-col-<?php echo esc_attr( $column ); ?>">
948
949
					<?php foreach( $areas as $area ) : 	?>
950
951
						<div class="gv-droppable-area" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>" data-context="<?php echo esc_attr( $zone ); ?>">
952
                            <p class="gv-droppable-area-title" <?php if ( 'widget' === $type && empty( $area['subtitle'] ) ) { echo ' style="margin: 0; padding: 0;"'; } ?>>
953
								<strong <?php if ( 'widget' === $type ) { echo 'class="screen-reader-text"'; } ?>><?php echo esc_html( $area['title'] ); ?></strong>
954
955
								<?php if ( 'widget' !== $type ) { ?>
956
									<a class="clear-all-fields alignright" role="button" href="#" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>"><?php esc_html_e( 'Clear all fields', 'gravityview' ); ?></a>
957
								<?php } ?>
958
959
                                <?php if ( ! empty( $area['subtitle'] ) ) { ?>
960
									<span class="gv-droppable-area-subtitle"><span class="gf_tooltip gv_tooltip tooltip" title="<?php echo esc_attr( $area['subtitle'] ); ?>"></span></span>
961
								<?php } ?>
962
							</p>
963
							<div class="active-drop-container active-drop-container-<?php echo esc_attr( $type ); ?>">
964
							<div class="active-drop active-drop-<?php echo esc_attr( $type ); ?>" data-areaid="<?php echo esc_attr( $zone .'_'. $area['areaid'] ); ?>"><?php // render saved fields
965
								if( ! empty( $values[ $zone .'_'. $area['areaid'] ] ) ) {
966
967
									foreach( $values[ $zone .'_'. $area['areaid'] ] as $uniqid => $field ) {
968
969
										// Maybe has a form ID
970
										$form_id = empty( $field['form_id'] ) ? $form_id : $field['form_id'];
971
972
										$input_type = NULL;
973
974
										if ( $form_id ) {
975
											$original_item = isset( $available_items[ $form_id ] [ $field['id'] ] ) ? $available_items[ $form_id ] [ $field['id'] ] : false ;
976
                                        } else {
977
											$original_item = isset( $available_items[ $field['id'] ] ) ? $available_items[ $field['id'] ] : false ;
978
                                        }
979
980
										if ( !$original_item ) {
981
											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 ) ) );
982
983
											$original_item = $field;
984
										} else {
985
											$input_type = isset( $original_item['type'] ) ? $original_item['type'] : NULL;
986
										}
987
988
										// Field options dialog box
989
										$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 );
990
991
										$item = array(
992
											'input_type' => $input_type,
993
											'settings_html' => $field_options,
994
											'label_type' => $type,
995
										);
996
997
										// Merge the values with the current item to pass things like widget descriptions and original field names
998
										if ( $original_item ) {
999
											$item = wp_parse_args( $item, $original_item );
1000
										}
1001
1002
										switch( $type ) {
1003
											case 'widget':
1004
												echo new GravityView_Admin_View_Widget( $item['label'], $field['id'], $item, $field );
1005
												break;
1006
											default:
1007
												echo new GravityView_Admin_View_Field( $field['label'], $field['id'], $item, $field, $form_id );
1008
										}
1009
									}
1010
1011
								} // End if zone is not empty ?></div>
1012
								<div class="gv-droppable-area-action">
1013
									<a href="#" class="gv-add-field button button-link button-hero" title=""
1014
									   data-objecttype="<?php echo esc_attr( $type ); ?>"
1015
									   data-areaid="<?php echo esc_attr( $zone . '_' . $area['areaid'] ); ?>"
1016
									   data-context="<?php echo esc_attr( $zone ); ?>"
1017
									   data-formid="<?php echo $view ? esc_attr( $view->form ? $view->form->ID : '' ) : ''; ?>"><?php echo '<span class="dashicons dashicons-plus-alt"></span>' . esc_html( $button_label ); ?></a>
1018
								</div>
1019
							</div>
1020
						</div>
1021
1022
					<?php endforeach; ?>
1023
1024
				</div>
1025
			<?php endforeach;
1026
		endforeach;
1027
	}
1028
1029
	/**
1030
	 * Render the widget active areas
1031
	 *
1032
	 * @param  string $template_id The current slug of the selected View template
1033
	 * @param  string $zone    Either 'header' or 'footer'
1034
	 * @param  string $post_id Current Post ID (view)
1035
	 *
1036
	 * @return string          html
1037
	 */
1038
	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...
1039
1040
		$default_widget_areas = \GV\Widget::get_default_widget_areas();
1041
1042
		$widgets = array();
1043
		if ( ! empty( $post_id ) ) {
1044
			if ( 'auto-draft' === get_post_status( $post_id ) ) {
1045
				// This is a new View, prefill the widgets
1046
				$widgets = array(
1047
					'header_top' => array(
1048
						substr( md5( microtime( true ) ), 0, 13 ) => array (
1049
							'id' => 'search_bar',
1050
							'label' => __( 'Search Bar', 'gravityview' ),
1051
							'search_layout' => 'horizontal',
1052
							'search_clear' => '0',
1053
							'search_fields' => '[{"field":"search_all","input":"input_text"}]',
1054
							'search_mode' => 'any',
1055
						),
1056
					),
1057
					'header_left' => array(
1058
						substr( md5( microtime( true ) ), 0, 13 ) => array(
1059
							'id' => 'page_info',
1060
							'label' => __( 'Show Pagination Info', 'gravityview' ),
1061
						),
1062
					),
1063
					'header_right' => array(
1064
						substr( md5( microtime( true ) ), 0, 13 ) => array(
1065
							'id' => 'page_links',
1066
							'label' => __( 'Page Links', 'gravityview' ),
1067
							'show_all' => '0',
1068
						),
1069
					),
1070
					'footer_right' => array(
1071
						substr( md5( microtime( true ) ), 0, 13 ) => array(
1072
							'id' => 'page_links',
1073
							'label' => __( 'Page Links', 'gravityview' ),
1074
							'show_all' => '0',
1075
						),
1076
					),
1077
				);
1078
1079
				/**
1080
				 * @filter `gravityview/view/widgets/default` Modify the default widgets for new Views
1081
				 * @param[in,out] array $widgets A Widget configuration array
1082
				 * @param string $zone The widget zone that's being requested
1083
				 * @param int $post_id The auto-draft post ID
1084
				 */
1085
				$widgets = apply_filters( 'gravityview/view/widgets/default', $widgets, $template_id, $zone, $post_id );
1086
			} else {
1087
				$widgets = gravityview_get_directory_widgets( $post_id );
1088
			}
1089
		}
1090
1091
		ob_start();
1092
		?>
1093
1094
        <div class="gv-grid gv-grid-pad gv-grid-border" id="directory-<?php echo $zone; ?>-widgets">
1095
			<?php $this->render_active_areas( $template_id, 'widget', $zone, $default_widget_areas, $widgets ); ?>
1096
		</div>
1097
1098
		<?php
1099
		$output = ob_get_clean();
1100
1101
		echo $output;
1102
1103
		return $output;
1104
	}
1105
1106
	/**
1107
	 * Renders "Add Field" tooltips
1108
	 *
1109
	 * @since 2.0.11
1110
	 *
1111
	 * @param string $context  "directory", "single", or "edit"
1112
	 * @param array  $form_ids (default: array) Array of form IDs
1113
	 *
1114
	 * @return void
1115
	 */
1116
	function render_field_pickers( $context = 'directory', $form_ids = 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...
1117
1118
		global $post;
1119
1120
		if ( $post ) {
1121
			$source_form_id = gravityview_get_form_id( $post->ID );
1122
			if ( $source_form_id ) {
1123
				$form_ids[] = $source_form_id;
1124
			}
1125
1126
			$joined_forms = \GV\View::get_joined_forms( $post->ID );
1127
			foreach ( $joined_forms as $joined_form ) {
1128
				$form_ids[] = $joined_form->ID;
1129
			}
1130
		}
1131
		foreach ( array_unique( $form_ids ) as $form_id ) {
1132
			$filter_field_id = sprintf( 'gv-field-filter-%s-%s', $context, $form_id );
1133
1134
			?>
1135
            <div id="<?php echo esc_html( $context ); ?>-available-fields-<?php echo esc_attr( $form_id ); ?>" class="hide-if-js gv-tooltip">
1136
                <button class="close" role="button" aria-label="<?php esc_html_e( 'Close', 'gravityview' ); ?>"><i class="dashicons dashicons-dismiss"></i></button>
1137
1138
                <div class="gv-field-filter-form">
1139
                    <label class="screen-reader-text" for="<?php echo esc_html( $filter_field_id ); ?>"><?php esc_html_e( 'Filter Fields:', 'gravityview' ); ?></label>
1140
                    <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' ); ?>" />
1141
					<div class="button-group">
1142
						<span role="button" class="button button-large gv-items-picker gv-items-picker--grid" data-value="grid"><i class="dashicons dashicons-grid-view "></i></span>
1143
						<span role="button" class="button button-large gv-items-picker gv-items-picker--list active" data-value="list"><i class="dashicons dashicons-list-view"></i></span>
1144
					</div>
1145
                </div>
1146
1147
                <div id="available-fields-<?php echo $filter_field_id; ?>" aria-live="polite" role="listbox" class="gv-items-picker-container">
1148
					<?php do_action( 'gravityview_render_available_fields', $form_id, $context ); ?>
1149
                </div>
1150
1151
                <div class="gv-no-results hidden description"><?php esc_html_e( 'No fields were found matching the search.', 'gravityview' ); ?></div>
1152
            </div>
1153
			<?php
1154
		}
1155
	}
1156
1157
	/**
1158
	 * Render the Template Active Areas and configured active fields for a given template id and post id
1159
	 *
1160
	 * @param string $template_id (default: '') Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()}
1161
	 * @param string $post_id (default: '')
1162
	 * @param string $context (default: 'single')
1163
	 * @return string HTML of the active areas
1164
	 */
1165
	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...
1166
		if( empty( $template_id ) ) {
1167
			gravityview()->log->debug( '[render_directory_active_areas] {template_id} is empty', array( 'template_id' => $template_id ) );
1168
			return '';
1169
		}
1170
1171
		/**
1172
		 * @filter `gravityview_template_active_areas`
1173
		 * @see GravityView_Template::assign_active_areas()
1174
		 * @param array $template_areas Empty array, to be filled in by the template class
1175
		 * @param string $template_id Template ID, like `default_list`, `default_table`, `preset_business_data`, etc. {@see GravityView_Template::__construct()}
1176
		 * @param string $context Current View context: `directory`, `single`, or `edit` (default: 'single')
1177
		 */
1178
		$template_areas = apply_filters( 'gravityview_template_active_areas', array(), $template_id, $context );
1179
1180
		if( empty( $template_areas ) ) {
1181
1182
			gravityview()->log->debug( '[render_directory_active_areas] No areas defined. Maybe template {template_id} is disabled.', array( 'data' => $template_id ) );
1183
			$output = '<div>';
1184
			$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>';
1185
			$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>';
1186
			$output .= '</div>';
1187
		} else {
1188
1189
			$fields = '';
1190
			if ( ! empty( $post_id ) ) {
1191
				$fields = gravityview_get_directory_fields( $post_id );
1192
			}
1193
1194
			ob_start();
1195
			$this->render_active_areas( $template_id, 'field', $context, $template_areas, $fields );
1196
			$output = ob_get_clean();
1197
1198
		}
1199
1200
		if( $echo ) {
1201
			echo $output;
1202
		}
1203
1204
		return $output;
1205
	}
1206
1207
	/**
1208
	 * Enqueue scripts and styles at Views editor
1209
	 *
1210
	 * @param mixed $hook
1211
	 * @return void
1212
	 */
1213
	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...
1214
		global $plugin_page, $pagenow;
1215
1216
		$script_debug    = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
1217
		$is_widgets_page = ( $pagenow === 'widgets.php' );
1218
1219
		// Add legacy (2.4 and older) Gravity Forms tooltip script/style
1220
		if ( gravityview()->plugin->is_GF_25() && gravityview()->request->is_admin( '', 'single' ) ) {
1221
			wp_dequeue_script( 'gform_tooltip_init' );
1222
			wp_dequeue_style( 'gform_tooltip' );
1223
			wp_enqueue_style( 'gravityview_gf_tooltip', plugins_url( 'assets/css/gf_tooltip.css', GRAVITYVIEW_FILE ), array(), \GV\Plugin::$version );
1224
			wp_enqueue_script( 'gravityview_gf_tooltip', plugins_url( 'assets/js/gf_tooltip' . $script_debug . '.js', GRAVITYVIEW_FILE ), array(), \GV\Plugin::$version );
1225
		}
1226
1227
		// Add the GV font (with the Astronaut)
1228
        wp_enqueue_style( 'gravityview_global', plugins_url('assets/css/admin-global.css', GRAVITYVIEW_FILE), array(), \GV\Plugin::$version );
1229
		wp_register_style( 'gravityview_views_styles', plugins_url( 'assets/css/admin-views.css', GRAVITYVIEW_FILE ), array( 'dashicons', 'wp-jquery-ui-dialog' ), \GV\Plugin::$version );
1230
1231
		wp_register_script( 'gravityview-jquery-cookie', plugins_url('assets/lib/jquery.cookie/jquery.cookie.min.js', GRAVITYVIEW_FILE), array( 'jquery' ), \GV\Plugin::$version, true );
1232
1233
		if( GFForms::get_page() === 'form_list' ) {
1234
			wp_enqueue_style( 'gravityview_views_styles' );
1235
			return;
1236
        }
1237
1238
		// Don't process any scripts below here if it's not a GravityView page.
1239
		if( ! gravityview()->request->is_admin( $hook, 'single' ) && ! $is_widgets_page ) {
1240
		    return;
1241
		}
1242
1243
		wp_enqueue_code_editor( array( 'type' => 'text/html' ) )
1244
1245
        wp_enqueue_script( 'jquery-ui-datepicker' );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_STRING
Loading history...
1246
        wp_enqueue_style( 'gravityview_views_datepicker', plugins_url('assets/css/admin-datepicker.css', GRAVITYVIEW_FILE), \GV\Plugin::$version );
1247
1248
        // Enqueue scripts
1249
        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 );
1250
1251
        wp_localize_script('gravityview_views_scripts', 'gvGlobals', array(
1252
            'cookiepath' => COOKIEPATH,
1253
            'admin_cookiepath' => ADMIN_COOKIE_PATH,
1254
            'passed_form_id' => (bool) \GV\Utils::_GET( 'form_id' ),
1255
            'nonce' => wp_create_nonce( 'gravityview_ajaxviews' ),
1256
            'label_viewname' => __( 'Enter View name here', 'gravityview' ),
1257
            'label_reorder_search_fields' => __( 'Reorder Search Fields', 'gravityview' ),
1258
            'label_add_search_field' => __( 'Add Search Field', 'gravityview' ),
1259
            'label_remove_search_field' => __( 'Remove Search Field', 'gravityview' ),
1260
            'label_close' => __( 'Close', 'gravityview' ),
1261
            'label_cancel' => __( 'Cancel', 'gravityview' ),
1262
            'label_continue' => __( 'Continue', 'gravityview' ),
1263
            'label_ok' => __( 'Ok', 'gravityview' ),
1264
            'label_publisherror' => __( 'Error while creating the View for you. Check the settings or contact GravityView support.', 'gravityview' ),
1265
            'loading_text' => esc_html__( 'Loading&hellip;', 'gravityview' ),
1266
            'loading_error' => esc_html__( 'There was an error loading dynamic content.', 'gravityview' ),
1267
            'field_loaderror' => __( 'Error while adding the field. Please try again or contact GravityView support.', 'gravityview' ),
1268
            'remove_all_fields' => __( 'Would you like to remove all fields in this zone?', 'gravityview' ),
1269
        ));
1270
1271
		wp_enqueue_style( 'gravityview_views_styles' );
1272
1273
        // Enqueue scripts needed for merge tags
1274
        self::enqueue_gravity_forms_scripts();
1275
1276
		// 2.5 changed how Merge Tags are enqueued
1277
		if ( is_callable( array( 'GFCommon', 'output_hooks_javascript') ) ) {
1278
			GFCommon::output_hooks_javascript();
1279
		}
1280
	}
1281
1282
	/**
1283
	 * Enqueue Gravity Forms scripts, needed for Merge Tags
1284
     *
1285
     * @since 1.0.5-beta
1286
     *
1287
     * @return void
1288
	 */
1289
	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...
1290
		GFForms::register_scripts();
1291
1292
		$scripts = array(
1293
		    'sack',
1294
		    'gform_gravityforms',
1295
		    'gform_forms',
1296
		    'gform_form_admin',
1297
		    'jquery-ui-autocomplete'
1298
		);
1299
1300
		if ( wp_is_mobile() ) {
1301
		    $scripts[] = 'jquery-touch-punch';
1302
		}
1303
1304
		wp_enqueue_script( $scripts );
1305
	}
1306
1307
	/**
1308
	 * Add GravityView scripts and styles to Gravity Forms and GravityView No-Conflict modes
1309
	 *
1310
	 * @param array $registered Existing scripts or styles that have been registered (array of the handles)
1311
	 *
1312
	 * @return array
1313
	 */
1314
	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...
1315
1316
		$allowed_dependencies = array();
1317
1318
		$filter = current_filter();
1319
1320
		if ( preg_match( '/script/ism', $filter ) ) {
1321
1322
			$allowed_dependencies = array(
1323
				'jquery-ui-core',
1324
				'jquery-ui-dialog',
1325
				'jquery-ui-tabs',
1326
				'jquery-ui-draggable',
1327
				'jquery-ui-droppable',
1328
				'jquery-ui-sortable',
1329
				'jquery-ui-tooltip',
1330
				'gravityview_views_scripts',
1331
				'gravityview-support',
1332
				'gravityview-jquery-cookie',
1333
				'gravityview_views_datepicker',
1334
				'gravityview_gf_tooltip',
1335
				'sack',
1336
				'gform_gravityforms',
1337
				'gform_forms',
1338
				'gform_form_admin',
1339
				'jquery-ui-autocomplete',
1340
			);
1341
1342
		} elseif ( preg_match( '/style/ism', $filter ) ) {
1343
1344
			$allowed_dependencies = array(
1345
				'dashicons',
1346
				'wp-jquery-ui-dialog',
1347
				'gravityview_views_styles',
1348
				'gravityview_global',
1349
				'gravityview_views_datepicker',
1350
				'gravityview_gf_tooltip',
1351
			);
1352
		}
1353
1354
		return array_merge( $registered, $allowed_dependencies );
1355
	}
1356
1357
1358
}
1359
1360
new GravityView_Admin_Views;
1361