Completed
Push — develop ( e6c68e...c10a82 )
by Zack
07:43
created

GravityView_Widget_Gravity_Forms   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 136
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Test Coverage

Coverage 5.77%

Importance

Changes 0
Metric Value
dl 0
loc 136
ccs 3
cts 52
cp 0.0577
rs 10
c 0
b 0
f 0
wmc 11
lcom 0
cbo 3

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 45 1
A _get_form_choices() 0 26 5
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
		$settings = array(
24
			'form_id' => array(
25
				'type' => 'select',
26
				'label' => __( 'Form to display', 'gravityview' ),
27
				'value' => '',
28
				'options' => $this->_get_form_choices(),
29
			),
30
			'title' => array(
31
				'type' => 'checkbox',
32
				'label' => __( 'Show form title?', 'gravityview' ),
33
				'value' => 1,
34
			),
35
			'description' => array(
36
				'type' => 'checkbox',
37
				'label' => __( 'Show form description?', 'gravityview' ),
38
				'value' => 1,
39
			),
40
			'ajax' => array(
41
				'type' => 'checkbox',
42
				'label' => __( 'Enable AJAX', 'gravityview' ),
43
				'desc' => '',
44
				'value' => 1,
45
			),
46
			'field_values' => array(
47
				'type' => 'text',
48
				'class' => 'code',
49
				'label' => __( 'Field value parameters', 'gravityview' ),
50
				'desc' => '',
51
				'value' => '',
52
			),
53
		);
54
55
		add_filter( 'gravityview/widget/hide_until_searched/whitelist', array( $this, 'add_to_allowlist' ) );
56
57
		parent::__construct( __( 'Gravity Forms', 'gravityview' ) , 'gravityforms', $default_values, $settings );
58
	}
59
60
	/**
61
	 * Returns an array of active forms to show as choices for the widget
62
	 *
63
	 * @since 2.9.0.1
64
	 *
65
	 * @return array Array with key set to Form ID => Form Title, with `0` as default placeholder.
66
	 */
67
	private function _get_form_choices() {
68
69
		$choices = array(
70
			0 => '&mdash; ' . esc_html__( 'list of forms', 'gravityview' ) . '&mdash;',
71
		);
72
73
		if ( ! class_exists( 'GFAPI' ) ) {
74
			return $choices;
75
		}
76
77
		/**
78
		 * gravityview_get_forms() is currently running too early as widgets_init runs before init and
79
		 * when most Gravity Forms plugins register their own fields like GP Terms of Service.
80
		 */
81
		if( \GV\Admin_Request::is_admin() && ! GFForms::is_gravity_page() ) {
82
83
			// check for available gravity forms
84
			$forms = gravityview_get_forms();
85
86
			foreach ( $forms as $form ) {
87
				$choices[ $form['id'] ] = $form['title'];
88
			}
89
		}
90
91
		return $choices;
92
	}
93
94
	/**
95
	 * Add widget to a list of allowed "Hide Until Searched" items
96
	 *
97
	 * @param array $allowlist Array of widgets to show before a search is performed, if the setting is enabled.
98
	 *
99
	 * @return array
100
	 */
101 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...
102
103 4
		$allowlist[] = 'gravityforms';
104
105 4
		return $allowlist;
106
	}
107
108
	/**
109
	 * @param array $widget_args
110
	 * @param string $content
111
	 * @param string $context
112
	 */
113
	public function render_frontend( $widget_args, $content = '', $context = '') {
114
115
		if ( ! $this->pre_render_frontend() ) {
116
			return;
117
		}
118
119
		$form_id = \GV\Utils::get( $widget_args, 'form_id' );
120
121
		if ( empty( $form_id ) ) {
122
			return;
123
		}
124
125
		$title       = \GV\Utils::get( $widget_args, 'title' );
126
		$description = \GV\Utils::get( $widget_args, 'description' );
127
		$field_values = \GV\Utils::get( $widget_args, 'field_values' );
128
		$ajax = \GV\Utils::get( $widget_args, 'ajax' );
129
130
		gravity_form( $form_id, ! empty( $title ), ! empty( $description ), false, $field_values, $ajax );
131
132
		// If the form has been submitted, show the confirmation above the form, then show the form again below.
133
		if ( isset( GFFormDisplay::$submission[ $form_id ] ) ) {
134
135
			unset( GFFormDisplay::$submission[ $form_id ] );
136
137
			gravity_form( $form_id, ! empty( $description ), ! empty( $title ) );
138
		}
139
	}
140
141
}
142
143
new GravityView_Widget_Gravity_Forms;
144