Completed
Pull Request — development (#753)
by Ihsan
07:15
created

Gravity_Form_Field::to_json()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
ccs 0
cts 3
cp 0
crap 2
1
<?php
2
3
namespace Carbon_Fields\Field;
4
5
/**
6
 * Gravity Form selection field class
7
 */
8
class Gravity_Form_Field extends Select_Field {
9
10
	/**
11
	 * Whether the Gravity Forms plugin is installed and activated.
12
	 *
13
	 * @return bool
14
	 */
15
	public function is_plugin_active() {
16
		if ( class_exists( '\RGFormsModel' ) && method_exists( '\RGFormsModel', 'get_forms' ) ) {
17
			return true;
18
		}
19
20
		return false;
21
	}
22
23
	/**
24
	 * {@inheritDoc}
25
	 */
26
	protected function load_options() {
27
		return $this->get_gravity_form_options();
28
	}
29
30
	/**
31
	 * Set the available forms as field options
32
	 *
33
	 * @return array
34
	 */
35
	protected function get_gravity_form_options() {
36
		if ( ! $this->is_plugin_active() ) {
37
			return array();
38
		}
39
40
		$forms = \RGFormsModel::get_forms( null, 'title' );
41
42
		if ( ! is_array( $forms ) || empty( $forms ) ) {
43
			return array();
44
		}
45
46
		$options = array(
47
			'' => __( 'No form', 'carbon-fields' ),
48
		);
49
50
		foreach ( $forms as $form ) {
51
			$options[ $form->id ] = $form->title;
52
		}
53
54
		return apply_filters( 'carbon_fields_gravity_form_options', $options );
55
	}
56
}
57