Completed
Branch 3.0 (1aee71)
by Stephanie
03:09
created

FrmFieldFormHtml::get_html()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * @since 3.0
4
 */
5
6
class FrmFieldFormHtml {
7
8
	private $html;
9
10
	private $html_id;
11
12
	/**
13
	 *   @var FrmFieldType
14
	 */
15
	private $field_obj;
16
17
	private $field_id;
18
19
	private $form = array();
20
21
	private $pass_args = array();
22
23
	/**
24
	 * @since 3.0
25
	 *
26
	 * @param array $atts
27
	 */
28
	public function __construct( $atts ) {
29
		$this->_set( 'field_obj', $atts );
30
		$this->set_field_id( $atts );
31
		$this->_set( 'form', $atts );
32
		$this->_set( 'html_id', $atts );
33
		$this->set_html( $atts );
34
		$this->set_pass_args( $atts );
35
	}
36
37
	/**
38
	 * @since 3.0
39
	 *
40
	 * @param string $param
41
	 * @param array $atts
42
	 */
43
	private function _set( $param, $atts ) {
44
		if ( isset( $atts[ $param ] ) ) {
45
			$this->{$param} = $atts[ $param ];
46
		}
47
	}
48
49
	/**
50
	 * @since 3.0
51
	 *
52
	 * @param array $atts
53
	 */
54
	private function set_html( $atts ) {
55
		$this->set_from_field( $atts, array( 'param' => 'html', 'default' => 'custom_html' ) );
56
	}
57
58
	/**
59
	 * @since 3.0
60
	 *
61
	 * @param array $atts
62
	 */
63
	private function set_field_id( $atts ) {
64
		$this->set_from_field( $atts, array( 'param' => 'field_id', 'default' => 'id' ) );
65
	}
66
67
	/**
68
	 * @since 3.0
69
	 *
70
	 * @param array $atts
71
	 */
72
	private function set_pass_args( $atts ) {
73
		$this->pass_args = $atts;
74
		$exclude = array( 'field_obj', 'html' );
75
		
76
		foreach ( $exclude as $ex ) {
77
			if ( isset( $atts[ $ex ] ) ) {
78
				unset( $this->pass_args[ $ex ] );
79
			}
80
		}
81
	}
82
83
	/**
84
	 * @since 3.0
85
	 *
86
	 * @param array $atts
87
	 * @param array $set
88
	 */
89
	private function set_from_field( $atts, $set ) {
90
		if ( isset( $atts[ $set['param'] ] ) ) {
91
			$this->{$set['param']} = $atts[ $set['param'] ];
92
		} else {
93
			$this->{$set['param']} = $this->field_obj->get_field_column( $set['default'] );
94
		}
95
	}
96
97
	public function get_html() {
98
		$this->replace_shortcodes_before_input();
99
		$this->replace_shortcodes_with_atts();
100
		$this->replace_shortcodes_after_input();
101
102
		return $this->html;
103
	}
104
105
	/**
106
	 * @since 3.0
107
	 */
108
	private function replace_shortcodes_before_input() {
109
		$this->html = apply_filters( 'frm_before_replace_shortcodes', $this->html, $this->field_obj->get_field(), $this->pass_args['errors'], $this->form );
110
111
		$this->replace_field_values();
112
113
		$this->replace_required_label_shortcode();
114
		$this->replace_required_class();
115
		$this->replace_description_shortcode();
116
		$this->replace_error_shortcode();
117
		$this->add_class_to_label();
118
		$this->add_field_div_classes();
119
120
		$this->replace_entry_key();
121
		$this->replace_form_shortcodes();
122
		$this->process_wp_shortcodes();
123
	}
124
125
	/**
126
	 * @since 3.0
127
	 */
128
	private function replace_field_values() {
129
		//replace [id]
130
		$this->html = str_replace( '[id]', $this->field_id, $this->html );
131
132
		// set the label for
133
		$this->html = str_replace( 'field_[key]', $this->html_id, $this->html );
134
135
		//replace [key]
136
		$this->html = str_replace( '[key]', $this->field_obj->get_field_column('field_key'), $this->html );
137
138
		//replace [field_name]
139
		$this->html = str_replace('[field_name]', $this->field_obj->get_field_column('name'), $this->html );
140
	}
141
142
	/**
143
	 * @since 3.0
144
	 */
145
	private function replace_required_label_shortcode() {
146
		$required = FrmField::is_required( $this->field_obj->get_field() ) ? $this->field_obj->get_field_column('required_indicator') : '';
147
		FrmShortcodeHelper::remove_inline_conditions( ! empty( $required ), 'required_label', $required, $this->html );
148
	}
149
150
	/**
151
	 * @since 3.0
152
	 */
153
	private function replace_description_shortcode() {
154
		$description = $this->field_obj->get_field_column('description');
155
		FrmShortcodeHelper::remove_inline_conditions( ( $description && $description != '' ), 'description', $description, $this->html );
156
	}
157
158
	/**
159
	 * @since 3.0
160
	 */
161
	private function replace_error_shortcode() {
162
		$error = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? $this->pass_args['errors'][ 'field' . $this->field_id ] : false;
163
		FrmShortcodeHelper::remove_inline_conditions( ! empty( $error ), 'error', $error, $this->html );
164
	}
165
166
	/**
167
	 * replace [required_class]
168
	 *
169
	 * @since 3.0
170
	 */
171
	private function replace_required_class() {
172
		$required_class = FrmField::is_required( $this->field_obj->get_field() ) ? ' frm_required_field' : '';
173
		$this->html = str_replace( '[required_class]', $required_class, $this->html );
174
	}
175
176
	/**
177
	 * @since 3.0
178
	 */
179
	private function replace_form_shortcodes() {
180
		if ( $this->form ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->form of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
181
			$form = (array) $this->form;
182
183
			//replace [form_key]
184
			$this->html = str_replace( '[form_key]', $form['form_key'], $this->html );
185
186
			//replace [form_name]
187
			$this->html = str_replace( '[form_name]', $form['name'], $this->html );
188
		}
189
	}
190
191
	/**
192
	 * @since 3.0
193
	 */
194
	public function replace_shortcodes_after_input() {
195
		$this->html .= "\n";
196
197
		//Return html if conf_field to prevent loop
198
		if ( $this->field_obj->get_field_column('conf_field') == 'stop' ) {
199
			return;
200
		}
201
202
		$this->filter_for_more_shortcodes();
203
	}
204
205
	/**
206
	 * @since 3.0
207
	 */
208
	private function filter_for_more_shortcodes() {
209
		$atts = $this->pass_args;
210
211
		//If field is not in repeating section
212
		if ( empty( $atts['section_id'] ) ) {
213
			$atts = array( 'errors' => $this->pass_args['errors'], 'form' => $this->form );
214
		}
215
		$this->html = apply_filters( 'frm_replace_shortcodes', $this->html, $this->field_obj->get_field(), $atts );
216
	}
217
218
	/**
219
	 * Remove [collapse_this] if it's still included after all processing
220
	 * @since 3.0
221
	 *
222
	 * @param string $html
223
	 */
224
	public function remove_collapse_shortcode( &$html ) {
225
		if ( strpos( $html, '[collapse_this]' ) ) {
226
			$html = str_replace( '[collapse_this]', '', $html );
227
		}
228
	}
229
230
	/**
231
	 * @since 3.0
232
	 */
233
	private function replace_shortcodes_with_atts() {
234
		preg_match_all("/\[(input|deletelink)\b(.*?)(?:(\/))?\]/s", $this->html, $shortcodes, PREG_PATTERN_ORDER);
235
236
		foreach ( $shortcodes[0] as $short_key => $tag ) {
237
			$shortcode_atts = FrmShortcodeHelper::get_shortcode_attribute_array( $shortcodes[2][ $short_key ] );
238
			$tag = FrmShortcodeHelper::get_shortcode_tag( $shortcodes, $short_key, array( 'conditional' => false, 'conditional_check' => false ) );
239
240
			$replace_with = '';
241
242
			if ( $tag == 'deletelink' && FrmAppHelper::pro_is_installed() ) {
243
				$replace_with = FrmProEntriesController::entry_delete_link( $shortcode_atts );
244
			} elseif ( $tag == 'input' ) {
245
				$replace_with = $this->replace_input_shortcode( $shortcode_atts );
246
			}
247
248
			$this->html = str_replace( $shortcodes[0][ $short_key ], $replace_with, $this->html );
249
		}
250
	}
251
252
	/**
253
	 * @param array $shortcode_atts
254
	 *
255
	 * @return string
256
	 */
257
	private function replace_input_shortcode( $shortcode_atts ) {
258
		$shortcode_atts = $this->prepare_input_shortcode_atts( $shortcode_atts );
259
		return $this->field_obj->include_front_field_input( $this->pass_args, $shortcode_atts );
260
	}
261
262
	/**
263
	 * @param array $shortcode_atts
264
	 *
265
	 * @return array
266
	 */
267
	private function prepare_input_shortcode_atts( $shortcode_atts ) {
268
		if ( isset( $shortcode_atts['opt'] ) ) {
269
			$shortcode_atts['opt']--;
270
		}
271
272
		$field_class = isset( $shortcode_atts['class'] ) ? $shortcode_atts['class'] : '';
273
		$this->field_obj->set_field_column( 'input_class', $field_class );
274
275
		if ( isset( $shortcode_atts['class'] ) ) {
276
			unset( $shortcode_atts['class'] );
277
		}
278
279
		$this->field_obj->set_field_column( 'shortcodes', $shortcode_atts );
280
281
		return $shortcode_atts;
282
	}
283
284
	/**
285
	 * Add the label position class into the HTML
286
	 * If the label position is inside, add a class to show the label if the field has a value.
287
	 *
288
	 * @since 3.0
289
	 */
290
	private function add_class_to_label() {
291
		$label_class = $this->field_obj->get_label_class();
292
		$this->html = str_replace( '[label_position]', $label_class, $this->html );
293
		if ( $this->field_obj->get_field_column('label') == 'inside' && $this->field_obj->get_field_column('value') != '' ) {
294
			$this->html = str_replace( 'frm_primary_label', 'frm_primary_label frm_visible', $this->html );
295
		}
296
	}
297
298
	/**
299
	 * replace [entry_key]
300
	 *
301
	 * @since 3.0
302
	 */
303
	private function replace_entry_key() {
304
		$entry_key = FrmAppHelper::simple_get( 'entry', 'sanitize_title' );
305
		$this->html = str_replace( '[entry_key]', $entry_key, $this->html );
306
	}
307
308
	/**
309
	 * Add classes to a field div
310
	 *
311
	 * @since 3.0
312
	 */
313
	private function add_field_div_classes() {
314
		$classes = $this->get_field_div_classes();
315
316
		if ( $this->field_obj->get_field_column('type') == 'html' && strpos( $this->html, '[error_class]' ) === false ) {
317
			// there is no error_class shortcode for HTML fields
318
			$this->html = str_replace( 'class="frm_form_field', 'class="frm_form_field ' . $classes, $this->html );
319
		}
320
		$this->html = str_replace( '[error_class]', $classes, $this->html );
321
	}
322
323
324
	/**
325
	 * Get the classes for a field div
326
	 *
327
	 * @since 3.0
328
	 *
329
	 * @return string $classes
330
	 */
331
	private function get_field_div_classes() {
332
		// Add error class
333
		$classes = isset( $this->pass_args['errors'][ 'field' . $this->field_id ] ) ? ' frm_blank_field' : '';
334
335
		// Add label position class
336
		$classes .= ' frm_' . $this->field_obj->get_field_column('label') . '_container';
337
338
		// Add CSS layout classes
339
		$extra_classes = $this->field_obj->get_field_column('classes');
340
		if ( ! empty( $extra_classes ) ) {
341
			if ( ! strpos( $this->html, 'frm_form_field ') ) {
342
				$classes .= ' frm_form_field';
343
			}
344
			$classes .= ' ' . $extra_classes;
345
		}
346
347
		$classes .= $this->field_obj->get_container_class();
348
349
		// Get additional classes
350
		return apply_filters( 'frm_field_div_classes', $classes, $this->field_obj->get_field(), array( 'field_id' => $this->field_id ) );
351
	}
352
353
	/**
354
	 * This filters shortcodes in the field HTML
355
	 *
356
	 * @since 3.0
357
	 */
358
	private function process_wp_shortcodes() {
359
		if ( apply_filters( 'frm_do_html_shortcodes', true ) ) {
360
			$this->html = do_shortcode( $this->html );
361
		}
362
	}
363
}
364