Completed
Pull Request — master (#815)
by Zack
10:24 queued 07:03
created

show_field_in_edit_entry()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 42
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 20
nc 5
nop 4
dl 0
loc 42
rs 8.439
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 129.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * @file class-gravityview-field-custom.php
4
 * @package GravityView
5
 * @subpackage includes\fields
6
 */
7
8
/**
9
 * Add custom options for Code field
10
 * @since 1.2
11
 */
12
class GravityView_Field_Custom extends GravityView_Field {
13
14
	var $name = 'custom';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $name.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
15
16
	var $contexts = array( 'single', 'multiple', 'edit' );
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $contexts.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
17
18
	/**
19
	 * @var bool
20
	 * @since 1.15.3
21
	 */
22
	var $is_sortable = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_sortable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
23
24
	/**
25
	 * @var bool
26
	 * @since 1.15.3
27
	 */
28
	var $is_searchable = false;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $is_searchable.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
29
30
	var $group = 'gravityview';
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $group.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
31
32
	public function __construct() {
33
34
		$this->label = esc_html__( 'Custom Content', 'gravityview' );
35
36
		add_filter( 'gravityview/edit_entry/form_fields', array( $this, 'show_field_in_edit_entry' ), 10, 4 );
37
38
		parent::__construct();
39
	}
40
41
	function field_options( $field_options, $template_id, $field_id, $context, $input_type ) {
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...
42
43
		unset ( $field_options['search_filter'], $field_options['show_as_link'] );
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
44
45
		$new_fields = array(
46
			'content' => array(
47
				'type' => 'textarea',
48
				'label' => __( 'Custom Content', 'gravityview' ),
49
				'desc' => sprintf( __( 'Enter text or HTML. Also supports shortcodes. You can show or hide data using the %s shortcode (%slearn more%s).', 'gravityview' ), '<code>[gvlogic]</code>', '<a href="http://docs.gravityview.co/article/252-gvlogic-shortcode">', '</a>' ),
50
				'value' => '',
51
				'class'	=> 'code',
52
				'merge_tags' => 'force',
53
				'show_all_fields' => true, // Show the `{all_fields}` and `{pricing_fields}` merge tags
54
			),
55
			'wpautop' => array(
56
				'type' => 'checkbox',
57
				'label' => __( 'Automatically add paragraphs to content', 'gravityview' ),
58
				'tooltip' => __( 'Wrap each block of text in an HTML paragraph tag (recommended for text).', 'gravityview' ),
59
				'value' => '',
60
			),
61
		);
62
63
		if( 'edit' === $context ) {
64
			unset( $field_options['custom_label'], $field_options['show_label'], $field_options['allow_edit_cap'], $new_fields['wpautop'] );
65
		}
66
67
		return $new_fields + $field_options;
68
	}
69
70
	/**
71
	 * Adds the GravityView Custom Content field to the Edit Entry form
72
	 *
73
	 * It does this by pretending to be a HTML field so that Gravity Forms displays it
74
	 *
75
	 * @since 1.19.2
76
	 *
77
	 * @param GF_Field[] $fields Gravity Forms form fields
78
	 * @param array|null $edit_fields Fields for the Edit Entry tab configured in the View Configuration
79
	 * @param array $form GF Form array (`fields` key modified to have only fields configured to show in Edit Entry)
80
	 * @param int $view_id View ID
81
	 *
82
	 * @return GF_Field[] If Custom Content field exists, returns fields array with the fields inserted. Otherwise, returns unmodified fields array.
83
	 */
84
	public function show_field_in_edit_entry( $fields, $edit_fields = null, $form, $view_id ) {
0 ignored issues
show
Unused Code introduced by
The parameter $view_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
85
86
		// Not configured; show all fields.
87
		if ( is_null( $edit_fields ) ) {
88
			return $fields;
89
		}
90
91
		$new_fields = array();
92
		$i = 0;
93
94
		$entry = GravityView_View::getInstance()->getCurrentEntry();
95
96
		// Loop through the configured Edit Entry fields and add Custom Content fields if there are any
97
		// TODO: Make this available to other custom GV field types
98
		foreach ( (array) $edit_fields as $edit_field ) {
99
100
			if( 'custom' === rgar( $edit_field, 'id') ) {
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces before closing bracket; 0 found
Loading history...
101
102
				$field_data = array(
103
					'label' => rgar( $edit_field, 'custom_label' ),
104
					'customLabel' => rgar( $edit_field, 'custom_label' ),
105
				    'content' => rgar( $edit_field, 'content' ),
106
				);
107
108
				// Replace merge tags in the content
109
				foreach ( $field_data as $key => $field_datum ) {
110
					$field_data[ $key ] = GravityView_Merge_Tags::replace_variables( $field_datum, $form, $entry, false, false );
111
				}
112
113
				$field_data['cssClass'] = rgar( $edit_field, 'custom_class' );
114
115
				$new_fields[] = new GF_Field_HTML( $field_data );
116
117
			} else {
118
				$new_fields[] = $fields[ $i ];
119
				$i++;
120
			}
121
122
		}
123
124
		return $new_fields;
125
	}
126
127
}
128
129
new GravityView_Field_Custom;
130