Completed
Push — develop ( 4ab178...4b70e0 )
by Gennady
24:10 queued 09:04
created

GravityView_Render_Settings::render_field_option()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 6
nop 3
dl 0
loc 42
ccs 0
cts 14
cp 0
crap 30
rs 8.9368
c 0
b 0
f 0
1
<?php
2
/**
3
 * Renders field/widget options and view settings
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.2
12
 */
13
14
class GravityView_Render_Settings {
15
16
	/**
17
	 * Get the default options for a standard field.
18
	 *
19
	 * @param  string      $field_type  Type of field options to render (`field` or `widget`)
20
	 * @param  string      $template_id Layout slug (`default_table`, `default_list`, `datatables_table`, etc.
21
	 * @param  float       $field_id    GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
22
	 * @param  string      $context     What context are we in? Example: `single` or `directory`
23
	 * @param  string      $input_type  (textarea, list, select, etc.)
24
	 * @param  int         $form_id     The form ID. @since develop
25
	 * @return array       Array of field options with `label`, `value`, `type`, `default` keys
26
	 */
27 2
	public static function get_default_field_options( $field_type, $template_id, $field_id, $context, $input_type, $form_id ) {
28
29 2
		$field_options = array();
30
31 2
		$is_table_layout = preg_match( '/table/ism', $template_id );
32
33 2
		if( 'field' === $field_type ) {
34
35
			// Default options - fields
36
			$field_options = array(
37 2
				'show_label' => array(
38 2
					'type' => 'checkbox',
39 2
					'label' => __( 'Show Label', 'gravityview' ),
40 2
					'value' => ! empty ( $is_table_layout ),
41
				),
42
				'custom_label' => array(
43 2
					'type' => 'text',
44 2
					'label' => __( 'Custom Label:', 'gravityview' ),
45 2
					'value' => '',
46 2
					'class'      => 'widefat',
47
					'merge_tags' => true,
48
				),
49
				'custom_class' => array(
50 2
					'type' => 'text',
51 2
					'label' => __( 'Custom CSS Class:', 'gravityview' ),
52 2
					'desc' => __( 'This class will be added to the field container', 'gravityview'),
53 2
					'value' => '',
54
					'merge_tags' => true,
55 2
					'tooltip' => 'gv_css_merge_tags',
56 2
					'class'      => 'widefat code',
57
				),
58
				'only_loggedin' => array(
59 2
					'type' => 'checkbox',
60 2
					'label' => __( 'Make visible only to logged-in users?', 'gravityview' ),
61 2
					'value' => ''
62
				),
63
				'only_loggedin_cap' => array(
64 2
					'type' => 'select',
65 2
					'label' => __( 'Make visible for:', 'gravityview' ),
66 2
					'options' => self::get_cap_choices( $template_id, $field_id, $context, $input_type ),
67 2
					'class' => 'widefat',
68 2
					'value' => 'read',
69
				),
70
			);
71
72
			// Match Table as well as DataTables
73 2
			if( $is_table_layout && 'directory' === $context ) {
74 1
				$field_options['width'] = array(
75 1
					'type' => 'number',
76 1
					'label' => __('Percent Width', 'gravityview'),
77 1
					'desc' => __( 'Leave blank for column width to be based on the field content.', 'gravityview'),
78 1
					'class' => 'code widefat',
79 1
					'value' => '',
80
				);
81
			}
82
83
		}
84
85
		/**
86
		 * @filter `gravityview_template_{$field_type}_options` Filter the field options by field type. Filter names: `gravityview_template_field_options` and `gravityview_template_widget_options`
87
		 * @param[in,out] array    Array of field options with `label`, `value`, `type`, `default` keys
88
		 * @param[in]  string      $template_id Table slug
89
		 * @param[in]  float       $field_id    GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
90
		 * @param[in]  string      $context     What context are we in? Example: `single` or `directory`
91
		 * @param[in]  string      $input_type  (textarea, list, select, etc.)
92
		 * @param[in]  int         $form_id     The form ID. @since develop
93
		 */
94 2
		$field_options = apply_filters( "gravityview_template_{$field_type}_options", $field_options, $template_id, $field_id, $context, $input_type, $form_id );
95
96
		/**
97
		 * @filter `gravityview_template_{$input_type}_options` Filter the field options by input type (`$input_type` examples: `textarea`, `list`, `select`, etc.)
98
		 * @param[in,out] array    Array of field options with `label`, `value`, `type`, `default` keys
99
		 * @param[in]  string      $template_id Table slug
100
		 * @param[in]  float       $field_id    GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
101
		 * @param[in]  string      $context     What context are we in? Example: `single` or `directory`
102
		 * @param[in]  string      $input_type  (textarea, list, select, etc.)
103
		 * @param[in]  int         $form_id     The form ID. @since develop
104
		 */
105 2
		$field_options = apply_filters( "gravityview_template_{$input_type}_options", $field_options, $template_id, $field_id, $context, $input_type, $form_id );
106
107 2
		return $field_options;
108
	}
109
110
	/**
111
	 * Get capabilities options for GravityView
112
	 *
113
	 * Parameters are only to pass to the filter.
114
	 *
115
	 * @param  string $template_id Optional. View slug
116
	 * @param  string $field_id    Optional. GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
117
	 * @param  string $context     Optional. What context are we in? Example: `single` or `directory`
118
	 * @param  string $input_type  Optional. (textarea, list, select, etc.)
119
	 * @return array Associative array, with the key being the capability and the value being the label shown.
120
	 */
121 1
	static public function get_cap_choices( $template_id = '', $field_id = '', $context = '', $input_type = '' ) {
122
123
		$select_cap_choices = array(
124 1
			'read' => __( 'Any Logged-In User', 'gravityview' ),
125 1
			'publish_posts' => __( 'Author Or Higher', 'gravityview' ),
126 1
			'gravityforms_view_entries' => __( 'Can View Gravity Forms Entries', 'gravityview' ),
127 1
			'delete_others_posts' => __( 'Editor Or Higher', 'gravityview' ),
128 1
			'gravityforms_edit_entries' => __( 'Can Edit Gravity Forms Entries', 'gravityview' ),
129 1
			'manage_options' => __( 'Administrator', 'gravityview' ),
130
		);
131
132 1
		if( is_multisite() ) {
133
			$select_cap_choices['manage_network'] = __('Multisite Super Admin', 'gravityview' );
134
		}
135
136
		/**
137
		 * @filter `gravityview_field_visibility_caps` Modify the capabilities shown in the field dropdown
138
		 * @see https://docs.gravityview.co/article/96-how-to-modify-capabilities-shown-in-the-field-only-visible-to-dropdown
139
		 * @since  1.0.1
140
		 * @param  array $select_cap_choices Associative rray of role slugs with labels ( `manage_options` => `Administrator` )
141
		 * @param  string $template_id Optional. View slug
142
		 * @param  string $field_id    Optional. GF Field ID - Example: `3`, `5.2`, `entry_link`, `created_by`
143
		 * @param  string $context     Optional. What context are we in? Example: `single` or `directory`
144
		 * @param  string $input_type  Optional. (textarea, list, select, etc.)
145
		 */
146 1
		$select_cap_choices = apply_filters('gravityview_field_visibility_caps', $select_cap_choices, $template_id, $field_id, $context, $input_type );
147
148 1
		return $select_cap_choices;
149
	}
150
151
152
	/**
153
	 * Render Field Options html (shown through a dialog box)
154
	 *
155
	 * @see GravityView_Ajax::get_field_options
156
	 * @see GravityView_Admin_Views::render_active_areas
157
	 *
158
	 * @access public
159
	 * @param string $form_id
160
	 * @param string $field_type field / widget
161
	 * @param string $template_id
162
	 * @param string $field_id
163
	 * @param string $field_label
164
	 * @param string $area
165
	 * @param string $uniqid (default: '')
166
	 * @param string $current (default: '')
167
	 * @param string $context (default: 'single')
168
	 * @param array $item Field or widget array that's being rendered
169
	 *
170
	 * @return string HTML of dialog box
171
	 */
172 1
	public static function render_field_options( $form_id, $field_type, $template_id, $field_id, $field_label, $area, $input_type = NULL, $uniqid = '', $current = '', $context = 'single', $item = array() ) {
173
174 1
		if( empty( $uniqid ) ) {
175
			//generate a unique field id
176
			$uniqid = uniqid('', false);
177
		}
178
179
		// get field/widget options
180 1
		$options = self::get_default_field_options( $field_type, $template_id, $field_id, $context, $input_type, $form_id );
181
182
		// two different post arrays, depending of the field type
183 1
		$name_prefix = $field_type .'s' .'['. $area .']['. $uniqid .']';
184
185
		// build output
186 1
		$output = '';
187 1
		$output .= '<input type="hidden" class="field-key" name="'. $name_prefix .'[id]" value="'. esc_attr( $field_id ) .'">';
188 1
		$output .= '<input type="hidden" class="field-label" name="'. $name_prefix .'[label]" value="'. esc_attr( $field_label ) .'">';
189 1
		if ( $form_id ) {
190 1
			$output .= '<input type="hidden" class="field-form-id" name="'. $name_prefix .'[form_id]" value="'. esc_attr( $form_id ) .'">';
191
		}
192
193
		// If there are no options, return what we got.
194 1
		if(empty($options)) {
195
196
			// This is here for checking if the output is empty in render_label()
197
			$output .= '<!-- No Options -->';
198
199
			return $output;
200
		}
201
202 1
		$output .= '<div class="gv-dialog-options" title="'. esc_attr( sprintf( __( 'Options: %s', 'gravityview' ) , strip_tags( html_entity_decode( $field_label ) ) ) ) .'">';
203
204
		/**
205
		 * @since 1.8
206
		 */
207 1
		if( !empty( $item['subtitle'] ) ) {
208
			$output .= '<div class="subtitle">' . $item['subtitle'] . '</div>';
209
		}
210
211 1
		foreach( $options as $key => $option ) {
212
213 1
			$value = isset( $current[ $key ] ) ? $current[ $key ] : NULL;
214
215 1
			$field_output = self::render_field_option( $name_prefix . '['. $key .']' , $option, $value);
216
217
			// The setting is empty
218 1
			if( empty( $field_output ) ) {
219
				continue;
220
			}
221
222 1
			switch( $option['type'] ) {
223
				// Hide hidden fields
224 1
				case 'hidden':
225
					$output .= '<div class="gv-setting-container gv-setting-container-'. esc_attr( $key ) . ' screen-reader-text">'. $field_output . '</div>';
226
					break;
227
				default:
228 1
					$output .= '<div class="gv-setting-container gv-setting-container-'. esc_attr( $key ) . '">'. $field_output .'</div>';
229
			}
230
		}
231
232
		// close options window
233 1
		$output .= '</div>';
234
235 1
		return $output;
236
237
	}
238
239
240
241
	/**
242
	 * Handle rendering a field option form element
243
	 *
244
	 * @param  string     $name    Input `name` attribute
245
	 * @param  array      $option  Associative array of options. See the $defaults variable for available keys.
246
	 * @param  mixed      $curr_value Current value of option
247
	 * @return string     HTML output of option
248
	 */
249
	public static function render_field_option( $name = '', $option, $curr_value = NULL ) {
250
251
		$output = '';
252
253
		/**
254
		 * @deprecated setting index 'default' was replaced by 'value'
255
		 * @see GravityView_FieldType::get_field_defaults
256
		 */
257
		if( !empty( $option['default'] ) && empty( $option['value'] ) ) {
258
			$option['value'] = $option['default'];
259
			_deprecated_function( 'GravityView_FieldType::get_field_defaults', '1.1.7', '[value] instead of [default] when defining the setting '. $name .' details' );
260
		}
261
262
		// prepare to render option field type
263
		if( isset( $option['type'] ) ) {
264
265
			$type_class = self::load_type_class( $option );
266
267
			if( class_exists( $type_class ) ) {
268
269
				/** @var GravityView_FieldType $render_type */
270
				$render_type = new $type_class( $name, $option, $curr_value );
271
272
				ob_start();
273
274
				$render_type->render_option();
275
276
				$output = ob_get_clean();
277
278
				/**
279
				 * @filter `gravityview/option/output/{option_type}` Modify the output for a GravityView setting.\n
280
				 * `$option_type` is the type of setting (`radio`, `text`, etc.)
281
				 * @param[in,out] string $output field class name
282
				 * @param[in] array $option  option field data
283
				 */
284
				$output = apply_filters( "gravityview/option/output/{$option['type']}" , $output, $option );
285
			}
286
287
		} // isset option[type]
288
289
		return $output;
290
	}
291
292
293
294
295
296
297
	/**
298
	 * Output a table row for view settings
299
	 * @param  string $key              The key of the input
300
	 * @param  array  $current_settings Associative array of current settings to use as input values, if set. If not set, the defaults are used.
301
	 * @param  string $override_input   [description]
302
	 * @param  string $name             [description]
303
	 * @param  string $id               [description]
304
	 * @return void                   [description]
305
	 */
306
	public static function render_setting_row( $key = '', $current_settings = array(), $override_input = null, $name = 'template_settings[%s]', $id = 'gravityview_se_%s' ) {
307
308
		$settings = \GV\View_Settings::with_defaults( true );
309
310
		// If the key doesn't exist, there's something wrong.
311
		if ( ! $setting = $settings->get( $key ) ) {
312
			return;
313
		}
314
315
		/**
316
		 * @deprecated setting index 'name' was replaced by 'label'
317
		 * @see GravityView_FieldType::get_field_defaults
318
		 */
319
		if( isset( $setting['name'] ) && empty( $setting['label'] ) ) {
320
			$setting['label'] = $setting['name'];
321
			_deprecated_function( 'GravityView_FieldType::get_field_defaults', '1.1.7', '[label] instead of [name] when defining the setting '. $key .' details' );
322
		}
323
324
		$name = esc_attr( sprintf( $name, $key ) );
325
		$setting['id'] = esc_attr( sprintf( $id, $key ) );
326
		$setting['tooltip'] = 'gv_' . $key;
327
328
		// Use default if current setting isn't set.
329
		$curr_value = isset( $current_settings[ $key ] ) ? $current_settings[ $key ] : $setting['value'];
330
331
		// default setting type = text
332
		$setting['type'] = empty( $setting['type'] ) ? 'text' : $setting['type'];
333
334
		// merge tags
335
		if( !isset( $setting['merge_tags'] ) ) {
336
			if( $setting['type'] === 'text' ) {
337
				$setting['merge_tags'] = true;
338
			} else {
339
				$setting['merge_tags'] = false;
340
			}
341
		}
342
343
		$output = '';
344
345
		// render the setting
346
		$type_class = self::load_type_class( $setting );
347
		if( class_exists( $type_class ) ) {
348
			/** @var GravityView_FieldType $render_type */
349
			$render_type = new $type_class( $name, $setting, $curr_value );
350
			ob_start();
351
			$render_type->render_setting( $override_input );
352
			$output = ob_get_clean();
353
		}
354
355
		// Check if setting is specific for a template
356
		if( !empty( $setting['show_in_template'] ) ) {
357
			if( !is_array( $setting['show_in_template'] ) ) {
358
				$setting['show_in_template'] = array( $setting['show_in_template'] );
359
			}
360
			$show_if = ' data-show-if="'. implode( ' ', $setting['show_in_template'] ).'"';
361
		} else {
362
			$show_if = '';
363
		}
364
365
		if( ! empty( $setting['requires'] ) ) {
366
			$show_if .= sprintf( ' data-requires="%s"', $setting['requires'] );
367
		}
368
369
		if( ! empty( $setting['requires_not'] ) ) {
370
			$show_if .= sprintf( ' data-requires-not="%s"', $setting['requires_not'] );
371
		}
372
373
		// output
374
		echo '<tr valign="top" '. $show_if .'>' . $output . '</tr>';
375
376
	}
377
378
379
	/**
380
	 * Given a field type calculates the php class. If not found try to load it.
381
	 * @param  array $field
382
	 * @return string type class name
383
	 */
384
	public static function load_type_class( $field = NULL ) {
385
386
		if( empty( $field['type'] ) ) {
387
			return NULL;
388
		}
389
390
		/**
391
		 * @filter `gravityview/setting/class/{field_type}`
392
		 * @param string $class_suffix  field class suffix; `GravityView_FieldType_{$class_suffix}`
393
		 * @param array $field   field data
394
		 */
395
		$type_class = apply_filters( "gravityview/setting/class/{$field['type']}", 'GravityView_FieldType_' . $field['type'], $field );
396
397
		if( !class_exists( $type_class ) ) {
398
399
			/**
400
			 * @filter `gravityview/setting/class_file/{field_type}`
401
			 * @param string  $field_type_include_path field class file path
402
			 * @param array $field  field data
403
			 */
404
			$class_file = apply_filters( "gravityview/setting/class_file/{$field['type']}", GRAVITYVIEW_DIR . "includes/admin/field-types/type_{$field['type']}.php", $field );
405
406
			if( $class_file ) {
407
				if( file_exists( $class_file ) ) {
408
					require_once( $class_file );
409
				}
410
			}
411
412
		}
413
414
		return $type_class;
415
	}
416
417
418
419
420
421
	/**
422
	 * @deprecated 1.2
423
	 * Render the HTML for a checkbox input to be used on the field & widgets options
424
	 * @param  string $name , name attribute
425
	 * @param  string $current current value
426
	 * @return string         html tags
427
	 */
428
	public static function render_checkbox_option( $name = '', $id = '', $current = '' ) {
429
430
		_deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_checkbox::render_input' );
431
432
		$output  = '<input name="'. esc_attr( $name ) .'" type="hidden" value="0">';
433
		$output .= '<input name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" type="checkbox" value="1" '. checked( $current, '1', false ) .' >';
434
435
		return $output;
436
	}
437
438
439
	/**
440
	 * @deprecated 1.2
441
	 * Render the HTML for an input text to be used on the field & widgets options
442
	 * @param  string $name    Unique name of the field. Exampe: `fields[directory_list-title][5374ff6ab128b][custom_label]`
443
	 * @param  string $current [current value]
444
	 * @param string $add_merge_tags Add merge tags to the input?
445
	 * @param array $args Field settings, including `class` key for CSS class
446
	 * @return string         [html tags]
447
	 */
448
	public static function render_text_option( $name = '', $id = '', $current = '', $add_merge_tags = NULL, $args = array() ) {
449
450
		_deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_text::render_input' );
451
452
		// Show the merge tags if the field is a list view
453
		$is_list = ( preg_match( '/_list-/ism', $name ));
454
455
		// Or is a single entry view
456
		$is_single = ( preg_match( '/single_/ism', $name ));
457
		$show = ( $is_single || $is_list );
458
459
		$class = '';
460
		// and $add_merge_tags is not false
461
		if( $show && $add_merge_tags !== false || $add_merge_tags === 'force' ) {
462
			$class = 'merge-tag-support mt-position-right mt-hide_all_fields ';
463
		}
464
465
		$class .= !empty( $args['class'] ) ? $args['class'] : 'widefat';
466
		$type = !empty( $args['type'] ) ? $args['type'] : 'text';
467
468
		return '<input name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" type="'.esc_attr($type).'" value="'. esc_attr( $current ) .'" class="'.esc_attr( $class ).'">';
469
	}
470
471
	/**
472
	 * @deprecated 1.2
473
	 * Render the HTML for an textarea input to be used on the field & widgets options
474
	 * @param  string $name    Unique name of the field. Exampe: `fields[directory_list-title][5374ff6ab128b][custom_label]`
475
	 * @param  string $current [current value]
476
	 * @param string|boolean $add_merge_tags Add merge tags to the input?
477
	 * @param array $args Field settings, including `class` key for CSS class
478
	 * @return string         [html tags]
479
	 */
480
	public static function render_textarea_option( $name = '', $id = '', $current = '', $add_merge_tags = NULL, $args = array() ) {
481
482
		_deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_textarea::render_input' );
483
484
		// Show the merge tags if the field is a list view
485
		$is_list = ( preg_match( '/_list-/ism', $name ));
486
487
		// Or is a single entry view
488
		$is_single = ( preg_match( '/single_/ism', $name ));
489
		$show = ( $is_single || $is_list );
490
491
		$class = '';
492
		// and $add_merge_tags is not false
493
		if( $show && $add_merge_tags !== false || $add_merge_tags === 'force' ) {
494
			$class = 'merge-tag-support mt-position-right mt-hide_all_fields ';
495
		}
496
497
		$class .= !empty( $args['class'] ) ? 'widefat '.$args['class'] : 'widefat';
498
499
		return '<textarea name="'. esc_attr( $name ) .'" id="'. esc_attr( $id ) .'" class="'.esc_attr( $class ).'">'. esc_textarea( $current ) .'</textarea>';
500
	}
501
502
	/**
503
	 *
504
	 * Render the HTML for a select box to be used on the field & widgets options
505
	 * @deprecated 1.2
506
	 * @param  string $name    [name attribute]
507
	 * @param  array $choices [select options]
508
	 * @param  string $current [current value]
509
	 * @return string          [html tags]
510
	 */
511
	public static function render_select_option( $name = '', $id = '', $choices, $current = '' ) {
512
513
		_deprecated_function( __METHOD__, '1.2', 'GravityView_FieldType_select::render_input' );
514
515
		$output = '<select name="'. $name .'" id="'. $id .'">';
516
		foreach( $choices as $value => $label ) {
517
			$output .= '<option value="'. esc_attr( $value ) .'" '. selected( $value, $current, false ) .'>'. esc_html( $label ) .'</option>';
518
		}
519
		$output .= '</select>';
520
521
		return $output;
522
	}
523
524
525
}
526