Completed
Push — master ( 6337a4...1e5684 )
by Zack
11s
created

includes/fields/class-gravityview-field-custom.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
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';
15
16
	var $contexts = array( 'single', 'multiple', 'edit' );
17
18
	/**
19
	 * @var bool
20
	 * @since 1.15.3
21
	 */
22
	var $is_sortable = false;
23
24
	/**
25
	 * @var bool
26
	 * @since 1.15.3
27
	 */
28
	var $is_searchable = false;
29
30
	var $group = 'gravityview';
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 ) {
42
43
		unset ( $field_options['search_filter'], $field_options['show_as_link'] );
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
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
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