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

GravityView_Field_Custom   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Test Coverage

Coverage 33.33%

Importance

Changes 0
Metric Value
dl 0
loc 208
ccs 27
cts 81
cp 0.3333
rs 10
c 0
b 0
f 0
wmc 23
lcom 0
cbo 7

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 1
A field_options() 0 44 2
B show_field_in_edit_entry() 0 44 6
C add_filtersorts() 0 59 14
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
	/**
44
	 * @inheritDoc
45
	 * @since 2.9.1 Added "This content is numeric" setting
46
	 */
47
	public function field_options( $field_options, $template_id, $field_id, $context, $input_type, $form_id ) {
48
49
		unset ( $field_options['search_filter'], $field_options['show_as_link'] );
50
51
		$new_fields = array(
52
			'content' => array(
53
				'type' => 'textarea',
54
				'label' => __( 'Custom Content', 'gravityview' ),
55
				'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>' ),
56
				'value' => '',
57
				'class'	=> 'code',
58
				'merge_tags' => 'force',
59
				'rows' => 15,
60
				'show_all_fields' => true, // Show the `{all_fields}` and `{pricing_fields}` merge tags
61
			),
62
			'wpautop' => array(
63
				'type' => 'checkbox',
64
				'label' => __( 'Automatically add paragraphs to content', 'gravityview' ),
65
				'tooltip' => __( 'Wrap each block of text in an HTML paragraph tag (recommended for text).', 'gravityview' ),
66
				'value' => '',
67
			),
68
			'oembed' => array(
69
				'type' => 'checkbox',
70
				'label' => __( 'Render oEmbeds', 'gravityview' ),
71
				'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>' ),
72
				'value' => '',
73
			),
74
			'admin_label' => array(
75
				'type' => 'text',
76
				'class' => 'widefat',
77
				'label' => __( 'Admin Label', 'gravityview' ),
78
				'desc' => __( 'A label that is only shown in the GravityView View configuration screen.', 'gravityview' ),
79
				'value' => '',
80
			),
81
		);
82
83
		$this->add_field_support( 'is_numeric', $new_fields );
84
85
		if ( 'edit' === $context ) {
86
			unset( $field_options['custom_label'], $field_options['show_label'], $field_options['allow_edit_cap'], $new_fields['wpautop'], $new_fields['oembed'] );
87
		}
88
89
		return $new_fields + $field_options;
90
	}
91
92
	/**
93
	 * Adds the GravityView Custom Content field to the Edit Entry form
94
	 *
95
	 * It does this by pretending to be a HTML field so that Gravity Forms displays it
96
	 *
97
	 * @since 1.19.2
98
	 *
99
	 * @param GF_Field[] $fields Gravity Forms form fields
100
	 * @param array|null $edit_fields Fields for the Edit Entry tab configured in the View Configuration
101
	 * @param array $form GF Form array (`fields` key modified to have only fields configured to show in Edit Entry)
102
	 * @param int $view_id View ID
103
	 *
104
	 * @return GF_Field[] If Custom Content field exists, returns fields array with the fields inserted. Otherwise, returns unmodified fields array.
105
	 */
106
	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...
107
108
		// Not configured; show all fields.
109
		if ( is_null( $edit_fields ) ) {
110
			return $fields;
111
		}
112
113
		$new_fields = array();
114
		$i = 0;
115
116
		$entry = gravityview()->request->is_edit_entry();
117
118
		// Loop through the configured Edit Entry fields and add Custom Content fields if there are any
119
		// TODO: Make this available to other custom GV field types
120
		foreach ( (array) $edit_fields as $edit_field ) {
121
122
			if( 'custom' === \GV\Utils::get( $edit_field, 'id') ) {
123
124
				$field_data = array(
125
					'label' => \GV\Utils::get( $edit_field, 'custom_label' ),
126
					'customLabel' => \GV\Utils::get( $edit_field, 'custom_label' ),
127
				    'content' => \GV\Utils::get( $edit_field, 'content' ),
128
				);
129
130
				// Replace merge tags in the content
131
				foreach ( $field_data as $key => $field_datum ) {
132
					$field_data[ $key ] = GravityView_Merge_Tags::replace_variables( $field_datum, $form, $entry->as_entry(), false, false );
133
				}
134
135
				$field_data['cssClass'] = \GV\Utils::get( $edit_field, 'custom_class' );
136
137
				$new_fields[] = new GF_Field_HTML( $field_data );
138
139
			} else {
140
				if( isset( $fields[ $i ] ) ) {
141
					$new_fields[] =  $fields[ $i ];
142
				}
143
				$i++;
144
			}
145
146
		}
147
148
		return $new_fields;
149
	}
150
151
	/**
152
	 * Sort by custom content field if required!
153
	 *
154
	 * @param \GV\View $view The view.
155
	 * @param \GV\Request $request The request.
156
	 *
157
	 * @return void
158
	 */
159 75
	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...
160 75
		if ( ! $view->fields->by_type( 'custom' )->count() ) {
161 66
			return; // No sorts.
162
		}
163
164 9
		if ( ! $sorts = \GV\Utils::_GET( 'sort' ) ) {
165 9
			$sorts = array_combine( (array)$view->settings->get( 'sort_field' ), (array)$view->settings->get( 'sort_direction' ) );
166
		}
167
168 9
		if ( strpos( implode( ':', array_keys( $sorts ) ), 'custom_' ) === false ) {
169 8
			return; // No custom sorts here.
170
		}
171
172
		add_filter( 'gravityview/entries/sort', $callback = function( $compare, $entry1, $entry2, $view, $request ) use ( $sorts ) {
173
			/**
174
			 * We'll actually need to sort everything on the PHP side now.
175
			 * This is weird...
176
			 */
177 1
			$renderer = new \GV\Field_Renderer();
178
179 1
			foreach ( $sorts as $key => $direction ) {
180 1
				if ( strpos( $key, 'custom_' ) === 0 ) {
181 1
					$field = $view->fields->get( str_replace( 'custom_', '', $key ) );
182
				} else {
183
					$field  = is_numeric( $key ) ? \GV\GF_Field::by_id( $view->form, $key ) : \GV\Internal_Field::by_id( $key );
184
				}
185
186 1
				$source = is_numeric( $key ) ? $view->form : new \GV\Internal_Source();
187
188 1
				$value1 = $renderer->render( $field, $view, $source, $entry1, $request );
189 1
				$value2 = $renderer->render( $field, $view, $source, $entry2, $request );
190
191 1
				if ( $value1 === $value2 ) {
192 1
					continue;
193
				}
194
195 1
				if ( $field->is_numeric ) { // @todo do we not have a filter that controls sorting?
196 1
					if ( is_numeric( $value1 ) ) {
197 1
						$value1 = floatval( $value1 );
198
					}
199
200 1
					if ( is_numeric( $value2 ) ) {
201 1
						$value2 = floatval( $value2 );
202
					}
203
204 1
					return strnatcmp( $value1, $value2 ) * ( ( strtolower( $direction ) == 'desc' ) ? -1 : 1 );
205
				} else {
206 1
					return strcmp( $value1, $value2 ) * ( ( strtolower( $direction ) == 'desc' ) ? -1 : 1 );
207
				}
208
209
			}
210
211 1
			return 0; // They're the same.
212 1
		}, 10, 5 );
213
214
		add_action( 'gravityview/view/remove_filtersorts', function() use ( $callback ) {
215
			remove_action( 'gravityview/entries/sort', $callback );
216 1
		} );
217 1
	}
218
219
}
220
221
new GravityView_Field_Custom;
222