Completed
Pull Request — develop (#1487)
by Gennady
07:46
created

GravityView_Field_Custom   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 184
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 22.54%

Importance

Changes 0
Metric Value
dl 0
loc 184
ccs 16
cts 71
cp 0.2254
rs 10
c 0
b 0
f 0
wmc 19
lcom 0
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A field_options() 0 42 2
B show_field_in_edit_entry() 0 44 6
B add_filtersorts() 0 41 10
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 = true;
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
		add_filter( 'gravityview/view/add_filtersorts', array( $this, 'add_filtersorts' ), 10, 2 );
39
40
		parent::__construct();
41
	}
42
43
	public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
44
45
		unset ( $field_options['search_filter'], $field_options['show_as_link'] );
46
47
		$new_fields = array(
48
			'content' => array(
49
				'type' => 'textarea',
50
				'label' => __( 'Custom Content', 'gravityview' ),
51
				'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="https://docs.gravityview.co/article/252-gvlogic-shortcode" data-beacon-article-sidebar="552355bfe4b0221aadf2572b">', '</a>' ) . ' ' . sprintf( __( 'Click the arrow icon next to the content area to add %sMerge Tags%s.', 'gravityview' ), '<a href="https://docs.gravityview.co/article/76-merge-tags" data-beacon-article-inline="54c67bbbe4b051242988551d">', '</a>' ),
52
				'value' => '',
53
				'class'	=> 'code',
54
				'merge_tags' => 'force',
55
				'rows' => 15,
56
				'show_all_fields' => true, // Show the `{all_fields}` and `{pricing_fields}` merge tags
57
			),
58
			'wpautop' => array(
59
				'type' => 'checkbox',
60
				'label' => __( 'Automatically add paragraphs to content', 'gravityview' ),
61
				'tooltip' => __( 'Wrap each block of text in an HTML paragraph tag (recommended for text).', 'gravityview' ),
62
				'value' => '',
63
			),
64
			'oembed' => array(
65
				'type' => 'checkbox',
66
				'label' => __( 'Render oEmbeds', 'gravityview' ),
67
				'desc' => sprintf( _x( 'Automatically convert oEmbed URLs into embedded content (%slearn more%s).', 'HTML link pointing to WordPress article on oEmbed', 'gravityview' ), '<a href="https://codex.wordpress.org/Embeds" rel="external noopener noreferrer">', '</a>' ),
68
				'value' => '',
69
			),
70
			'admin_label' => array(
71
				'type' => 'text',
72
				'class' => 'widefat',
73
				'label' => __( 'Admin Label', 'gravityview' ),
74
				'desc' => __( 'A label that is only shown in the GravityView View configuration screen.', 'gravityview' ),
75
				'value' => '',
76
			),
77
		);
78
79
		if ( 'edit' === $context ) {
80
			unset( $field_options['custom_label'], $field_options['show_label'], $field_options['allow_edit_cap'], $new_fields['wpautop'], $new_fields['oembed'] );
81
		}
82
83
		return $new_fields + $field_options;
84
	}
85
86
	/**
87
	 * Adds the GravityView Custom Content field to the Edit Entry form
88
	 *
89
	 * It does this by pretending to be a HTML field so that Gravity Forms displays it
90
	 *
91
	 * @since 1.19.2
92
	 *
93
	 * @param GF_Field[] $fields Gravity Forms form fields
94
	 * @param array|null $edit_fields Fields for the Edit Entry tab configured in the View Configuration
95
	 * @param array $form GF Form array (`fields` key modified to have only fields configured to show in Edit Entry)
96
	 * @param int $view_id View ID
97
	 *
98
	 * @return GF_Field[] If Custom Content field exists, returns fields array with the fields inserted. Otherwise, returns unmodified fields array.
99
	 */
100
	public function show_field_in_edit_entry( $fields, $edit_fields = null, $form = array(), $view_id = 0 ) {
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...
101
102
		// Not configured; show all fields.
103
		if ( is_null( $edit_fields ) ) {
104
			return $fields;
105
		}
106
107
		$new_fields = array();
108
		$i = 0;
109
110
		$entry = gravityview()->request->is_edit_entry();
111
112
		// Loop through the configured Edit Entry fields and add Custom Content fields if there are any
113
		// TODO: Make this available to other custom GV field types
114
		foreach ( (array) $edit_fields as $edit_field ) {
115
116
			if( 'custom' === \GV\Utils::get( $edit_field, 'id') ) {
117
118
				$field_data = array(
119
					'label' => \GV\Utils::get( $edit_field, 'custom_label' ),
120
					'customLabel' => \GV\Utils::get( $edit_field, 'custom_label' ),
121
				    'content' => \GV\Utils::get( $edit_field, 'content' ),
122
				);
123
124
				// Replace merge tags in the content
125
				foreach ( $field_data as $key => $field_datum ) {
126
					$field_data[ $key ] = GravityView_Merge_Tags::replace_variables( $field_datum, $form, $entry->as_entry(), false, false );
127
				}
128
129
				$field_data['cssClass'] = \GV\Utils::get( $edit_field, 'custom_class' );
130
131
				$new_fields[] = new GF_Field_HTML( $field_data );
132
133
			} else {
134
				if( isset( $fields[ $i ] ) ) {
135
					$new_fields[] =  $fields[ $i ];
136
				}
137
				$i++;
138
			}
139
140
		}
141
142
		return $new_fields;
143
	}
144
145
	/**
146
	 * Sort by custom content field if required!
147
	 *
148
	 * @param \GV\View $view The view.
149
	 * @param \GV\Request $request The request.
150
	 *
151
	 * @return void
152
	 */
153 70
	public function add_filtersorts( $view, $request ) {
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
154 70
		if ( $view->fields->by_type( 'custom' )->count() ) {
155
			add_filter( 'gravityview/entries/sort', $callback = function( $compare, $entry1, $entry2, $view, $request ) {
156 2
				if ( ! $sorts = \GV\Utils::_GET( 'sort' ) ) {
157 1
					$sorts = array_combine( $view->settings->get( 'sort_field' ), $view->settings->get( 'sort_direction' ) );
158
				}
159
160
				/**
161
				 * We'll actually need to sort everything on the PHP side now.
162
				 * This is weird...
163
				 */
164 1
				$renderer = new \GV\Field_Renderer();
165
166 1
				foreach ( $sorts as $key => $direction ) {
167 1
					if ( strpos( $key, 'custom_' ) === 0 ) {
168
						$field = $view->fields->get( str_replace( 'custom_', '', $key ) );
169
					} else {
170 1
						$field  = is_numeric( $key ) ? \GV\GF_Field::by_id( $view->form, $key ) : \GV\Internal_Field::by_id( $key );
171
					}
172
173 1
					$source = is_numeric( $key ) ? $view->form : new \GV\Internal_Source();
174
175 1
					$value1 = $renderer->render( $field, $view, $source, $entry1, $request );
176 1
					$value2 = $renderer->render( $field, $view, $source, $entry2, $request );
177
178
					// @todo numerics
179 1
					if ( $value1 === $value2 ) {
180
						continue;
181
					}
182
183 1
					return ( ( $value1 > $value2 ) ? -1 : 1 ) * ( strtolower( $direction ) == 'asc' ) ? 1 : -1;
184
				}
185
186
				return 0; // They're the same.
187 6
			}, 10, 5 );
188
189
			add_action( 'gravityview/view/remove_filtersorts', function() use ( $callback ) {
190
				remove_action( 'gravityview/entries/sort', $callback );
191 6
			} );
192
		}
193 70
	}
194
195
}
196
197
new GravityView_Field_Custom;
198