Completed
Push — master ( ac7d8e...ec16eb )
by Stephanie
05:53 queued 02:59
created

FrmFieldFormHtml::replace_entry_key()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 4
rs 10
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(
56
			'param'   => 'html',
57
			'default' => 'custom_html',
58
		) );
59
	}
60
61
	/**
62
	 * @since 3.0
63
	 *
64
	 * @param array $atts
65
	 */
66
	private function set_field_id( $atts ) {
67
		$this->set_from_field( $atts, array(
68
			'param'   => 'field_id',
69
			'default' => 'id',
70
		) );
71
	}
72
73
	/**
74
	 * @since 3.0
75
	 *
76
	 * @param array $atts
77
	 */
78
	private function set_pass_args( $atts ) {
79
		$this->pass_args = $atts;
80
		$exclude = array( 'field_obj', 'html' );
81
82
		foreach ( $exclude as $ex ) {
83
			if ( isset( $atts[ $ex ] ) ) {
84
				unset( $this->pass_args[ $ex ] );
85
			}
86
		}
87
	}
88
89
	/**
90
	 * @since 3.0
91
	 *
92
	 * @param array $atts
93
	 * @param array $set
94
	 */
95
	private function set_from_field( $atts, $set ) {
96
		if ( isset( $atts[ $set['param'] ] ) ) {
97
			$this->{$set['param']} = $atts[ $set['param'] ];
98
		} else {
99
			$this->{$set['param']} = $this->field_obj->get_field_column( $set['default'] );
100
		}
101
	}
102
103
	public function get_html() {
104
		$this->replace_shortcodes_before_input();
105
		$this->replace_shortcodes_with_atts();
106
		$this->replace_shortcodes_after_input();
107
108
		return $this->html;
109
	}
110
111
	/**
112
	 * @since 3.0
113
	 */
114
	private function replace_shortcodes_before_input() {
115
		$this->html = apply_filters( 'frm_before_replace_shortcodes', $this->html, $this->field_obj->get_field(), $this->pass_args['errors'], $this->form );
116
117
		$this->replace_field_values();
118
119
		$this->replace_required_label_shortcode();
120
		$this->replace_required_class();
121
		$this->maybe_replace_description_shortcode( false );
122
		$this->replace_error_shortcode();
123
		$this->add_class_to_label();
124
		$this->add_field_div_classes();
125
126
		$this->replace_entry_key();
127
		$this->replace_form_shortcodes();
128
		$this->process_wp_shortcodes();
129
		$this->maybe_replace_description_shortcode( true );
130
	}
131
132
	/**
133
	 * @since 3.0
134
	 */
135
	private function replace_field_values() {
136
		//replace [id]
137
		$this->html = str_replace( '[id]', $this->field_id, $this->html );
138
139
		// set the label for
140
		$this->html = str_replace( 'field_[key]', $this->html_id, $this->html );
141
142
		//replace [key]
143
		$this->html = str_replace( '[key]', $this->field_obj->get_field_column('field_key'), $this->html );
144
145
		//replace [field_name]
146
		$this->html = str_replace('[field_name]', $this->field_obj->get_field_column('name'), $this->html );
147
	}
148
149
	/**
150
	 * @since 3.0
151
	 */
152
	private function replace_required_label_shortcode() {
153
		$required = FrmField::is_required( $this->field_obj->get_field() ) ? $this->field_obj->get_field_column('required_indicator') : '';
154
		FrmShortcodeHelper::remove_inline_conditions( ! empty( $required ), 'required_label', $required, $this->html );
155
	}
156
157
	/**
158
	 * If this is an HTML field, the values are included in the description.
159
	 * In this case, we don't want to run the wp shortcodes with the description included.
160
	 *
161
	 * @since 3.0
162
	 */
163
	private function maybe_replace_description_shortcode( $wp_processed = false ) {
164
		$is_html = 'html' === $this->field_obj->get_field_column( 'type' );
165
		$should_replace = ( $is_html && $wp_processed ) || ( ! $is_html && ! $wp_processed );
166
		if ( $should_replace ) {
167
			$this->replace_description_shortcode();
168
		}
169
	}
170
171
	/**
172
	 * @since 3.0
173
	 */
174
	private function replace_description_shortcode() {
175
		$this->maybe_add_description_id();
176
		$description = $this->field_obj->get_field_column('description');
177
		FrmShortcodeHelper::remove_inline_conditions( ( $description && $description != '' ), 'description', $description, $this->html );
178
	}
179
180
	/**
181
	 * Add an ID to the description for aria-describedby.
182
	 * This ID was added to the HTML in v3.0.
183
	 * @since 3.0
184
	 */
185
	private function maybe_add_description_id() {
186
		$description = $this->field_obj->get_field_column('description');
187
		if ( $description != '' ) {
188
189
			$description_html = preg_match_all( '/(\[if\s+description\])(.*?)(\[\/if\s+description\])/mis', $this->html, $inner_html );
0 ignored issues
show
Unused Code introduced by
$description_html is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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