Completed
Push — develop ( 8c385e...3fb87e )
by Zack
08:06
created

GravityView_Widget_Gravity_Forms   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 6.38%

Importance

Changes 0
Metric Value
dl 0
loc 113
ccs 3
cts 47
cp 0.0638
rs 10
c 0
b 0
f 0
wmc 7
lcom 0
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 56 2
A add_to_allowlist() 0 6 1
A render_frontend() 0 27 4
1
<?php
2
3
/**
4
 * Widget to display a Gravity Forms form
5
 */
6
class GravityView_Widget_Gravity_Forms extends \GV\Widget {
7
8
	/**
9
	 * Does this get displayed on a single entry?
10
	 * @var boolean
11
	 */
12
	protected $show_on_single = true;
13
14
	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...
15
16
		$this->widget_description = __('Display a Gravity Forms form.', 'gravityview' );
17
18
		$default_values = array(
19
			'header' => 1,
20
			'footer' => 1,
21
		);
22
23
		// check for available gravity forms
24
		$forms = gravityview_get_forms();
25
26
		$choices = array(
27
			0 => '&mdash; ' . esc_html__( 'list of forms', 'gravityview' ) . '&mdash;',
28
		);
29
30
		foreach ( $forms as $form ) {
31
			$choices[ $form['id'] ] = $form['title'];
32
		}
33
34
		$settings = array(
35
			'form_id' => array(
36
				'type' => 'select',
37
				'label' => __( 'Form to display', 'gravityview' ),
38
				'value' => '',
39
				'options' => $choices,
40
			),
41
			'title' => array(
42
				'type' => 'checkbox',
43
				'label' => __( 'Show form title?', 'gravityview' ),
44
				'value' => 1,
45
			),
46
			'description' => array(
47
				'type' => 'checkbox',
48
				'label' => __( 'Show form description?', 'gravityview' ),
49
				'value' => 1,
50
			),
51
			'ajax' => array(
52
				'type' => 'checkbox',
53
				'label' => __( 'Enable AJAX', 'gravityview' ),
54
				'desc' => '',
55
				'value' => 1,
56
			),
57
			'field_values' => array(
58
				'type' => 'text',
59
				'class' => 'code',
60
				'label' => __( 'Field value parameters', 'gravityview' ),
61
				'desc' => '',
62
				'value' => '',
63
			),
64
		);
65
66
		add_filter( 'gravityview/widget/hide_until_searched/whitelist', array( $this, 'add_to_allowlist' ) );
67
68
		parent::__construct( __( 'Gravity Forms', 'gravityview' ) , 'gravityforms', $default_values, $settings );
69
	}
70
71
	/**
72
	 * Add widget to a list of allowed "Hide Until Searched" items
73
	 *
74
	 * @param array $allowlist Array of widgets to show before a search is performed, if the setting is enabled.
75
	 *
76
	 * @return array
77
	 */
78 4
	function add_to_allowlist( $allowlist ) {
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...
79
80 4
		$allowlist[] = 'gravityforms';
81
82 4
		return $allowlist;
83
	}
84
85
	/**
86
	 * @param array $widget_args
87
	 * @param string $content
88
	 * @param string $context
89
	 */
90
	public function render_frontend( $widget_args, $content = '', $context = '') {
91
92
		if ( ! $this->pre_render_frontend() ) {
93
			return;
94
		}
95
96
		$form_id = \GV\Utils::get( $widget_args, 'form_id' );
97
98
		if ( empty( $form_id ) ) {
99
			return;
100
		}
101
102
		$title       = \GV\Utils::get( $widget_args, 'title' );
103
		$description = \GV\Utils::get( $widget_args, 'description' );
104
		$field_values = \GV\Utils::get( $widget_args, 'field_values' );
105
		$ajax = \GV\Utils::get( $widget_args, 'ajax' );
106
107
		gravity_form( $form_id, ! empty( $title ), ! empty( $description ), false, $field_values, $ajax );
108
109
		// If the form has been submitted, show the confirmation above the form, then show the form again below.
110
		if ( isset( GFFormDisplay::$submission[ $form_id ] ) ) {
111
112
			unset( GFFormDisplay::$submission[ $form_id ] );
113
114
			gravity_form( $form_id, ! empty( $description ), ! empty( $title ) );
115
		}
116
	}
117
118
}
119
120
new GravityView_Widget_Gravity_Forms;
121