Completed
Pull Request — 2.x (#4373)
by
unknown
04:21
created

functions-view_template.php ➔ frontier_if_block()   D

Complexity

Conditions 15
Paths 108

Size

Total Lines 85
Code Lines 42

Duplication

Lines 18
Ratio 21.18 %

Importance

Changes 0
Metric Value
cc 15
eloc 42
nc 108
nop 2
dl 18
loc 85
rs 4.7773
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Utility function for processesing fromtier based templates
4
 * @package Pods_Frontier_Template_Editor\view_template
5
 */
6
7
// add filters
8
add_filter( "pods_templates_post_template", "frontier_end_template", 25, 4 );
9
add_filter( "pods_templates_do_template", "frontier_do_shortcode", 25, 1 );
10
11
// template shortcode handlers
12
add_shortcode( "pod_sub_template", "frontier_do_subtemplate" );
13
add_shortcode( "pod_once_template", "frontier_template_once_blocks" );
14
add_shortcode( "pod_after_template", "frontier_template_blocks" );
15
add_shortcode( "pod_before_template", "frontier_template_blocks" );
16
add_shortcode( "pod_if_field", "frontier_if_block" );
17
18
/**
19
 * Return array of valid frontier type shortcode tags
20
 *
21
 * @return array
22
 */
23
function frontier_get_shortcodes() {
24
	$shortcodes = array( 'each', 'pod_sub_template', 'once', 'pod_once_template', 'before', 'pod_before_template', 'after', 'pod_after_template', 'if', 'pod_if_field' );
25
26
	return $shortcodes;
27
}
28
29
/**
30
 * @param $content
31
 *
32
 * @return string
33
 * @since 2.4.3
34
 */
35
function frontier_do_shortcode( $content ) {
36
37
	$content = pods_do_shortcode( $content, frontier_get_shortcodes() );
38
39
	return $content;
40
41
}
42
43
/**
44
 * decodes a like nested shortcode based template
45
 *
46
 *
47
 * @param string encoded template to be decoded
48
 * @param array attributed provided from parent
49
 *
50
 * @return string
51
 * @since 2.4
52
 */
53
function frontier_decode_template( $code, $atts ) {
54
55
	$code = base64_decode( $code );
56
	$pod = pods( $atts[ 'pod' ] );
57
58
	if ( isset( $atts[ 'pod' ] ) ) {
59
		$code = str_replace( '@pod', $atts[ 'pod' ], $code );
60
	}
61
	if ( isset( $atts[ 'id' ] ) ) {
62
		$code = str_replace( '{@EntryID}', $atts[ 'id' ], $code );
63
	}
64
	if ( isset( $atts[ 'index' ] ) ) {
65
		$code = str_replace( '{_index}', $atts[ 'index' ], $code );
66
	}
67
68
	return $code;
69
}
70
71
/**
72
 * processes if condition within a template
73
 *
74
 *
75
 * @param array attributes from template
76
 * @param string encoded template to be decoded
77
 *
78
 * @return string
79
 * @since 2.4
80
 */
81
function frontier_if_block( $atts, $code ) {
82
83
	$pod = pods( $atts[ 'pod' ], $atts[ 'id' ] );
84
	$code = explode( '[else]', frontier_decode_template( $code, $atts ) );
85
86
	// sysvals
87
	$system_values = array(
88
		'_index',
89
	);
90
91
	// field data
92
	$field_data = null;
93
94
	if ( in_array( $atts[ 'field' ], $system_values) ) {
95
		switch ( $atts[ 'field' ] ){
96
			case '_index':
97
				$field_data = $atts['index'];
98
				break;
99
		}
100
	}
101
	else{
102
103
		$field_data = $pod->field( $atts[ 'field' ] );
104
105
	}
106
107
	if ( ! empty( $field_data ) || isset( $atts[ 'value' ] ) ) {
108
		// theres a field - let go deeper
109
		if ( isset( $atts[ 'value' ] ) ) {
110
111
			// check if + or - are present
112
			if( substr( $atts[ 'value' ], 0, 1) === '+' ){
113
				// is greater
114
				$atts[ 'value' ] = (float) substr( $atts[ 'value' ], 1) + 1;
115
				if(  (float) $field_data > $atts[ 'value' ] ){
116
					// is greater - set it the same to allow
117
					$atts[ 'value' ] = $field_data;
118
				}
119
120
			}elseif( substr( $atts[ 'value' ], 0, 1) === '-' ){
121
				// is smaller
122
				$atts[ 'value' ] = (float) substr( $atts[ 'value' ], 1) - 1;
123
				if( (float) $field_data <  $atts[ 'value' ] ){
124
					// is greater - set it the same to allow
125
					$atts[ 'value' ] = $field_data;
126
				}
127
128
			}
129
130
			if ( $field_data == $atts[ 'value' ] ) {
131
				// IF statement true, use [IF] content as template
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
132
				$template = $pod->do_magic_tags( $code[ 0 ] );
133 View Code Duplication
			} else {
134
				if ( isset( $code[ 1 ] ) ) {
135
					// Switch [else] portion of shortcode into active content
136
					$template = $pod->do_magic_tags( $code[ 1 ] );
137
				} else {
138
					// Value did not match, nothing should be displayed
139
					$template = '';
140
				}
141
			}
142
		} else {
143
			// Field exists and is not empty, use [if] content
144
			$template = $pod->do_magic_tags( $code[ 0 ] );
145
		}
146 View Code Duplication
	} else {
147
		// No 'field' value, switch to [else] content
0 ignored issues
show
Unused Code Comprehensibility introduced by
36% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
148
		if ( isset( $code[ 1 ] ) ) {
149
			$template = $pod->do_magic_tags( $code[ 1 ] );
150
		} else {
151
			// No 'field' value and no [else] tag
152
			$template = '';
153
		}
154
	}
155
	if ( defined( 'PODS_SHORTCODE_ALLOW_SUB_SHORTCODES' ) && PODS_SHORTCODE_ALLOW_SUB_SHORTCODES ) {
156
		// Allow all nested shortcodes
157
		$template = do_shortcode( $template );
158
	} else {
159
		// Only allow nested frontier shortcodes
160
		$template = pods_do_shortcode( $template, frontier_get_shortcodes() );
161
	}
162
163
	return $template;
164
165
}
166
167
/**
168
 * processes before and after template blocks
169
 *
170
 *
171
 * @param array attributes from template
172
 * @param string encoded template to be decoded
173
 * @param string shortcode slug used to process
174
 *
175
 * @return null
176
 * @since 2.4
177
 */
178
function frontier_template_blocks( $atts, $code, $slug ) {
179
180
	global $template_post_blocks;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
181
	if ( !isset( $template_post_blocks ) ) {
182
		$template_post_blocks = array(
183
			'before' => null,
184
			'after' => null,
185
		);
186
	}
187
	if ( $slug === 'pod_before_template' ) {
188 View Code Duplication
		if ( !isset( $template_post_blocks[ 'before' ][ $atts[ 'pod' ] ] ) ) {
189
			$template_post_blocks[ 'before' ][ $atts[ 'pod' ] ] = pods_do_shortcode( frontier_decode_template( $code, $atts ), array(
190
				'if',
191
				'else'
192
			) );
193
		}
194
195
	}
196 View Code Duplication
	elseif ( $slug === 'pod_after_template' ) {
197
		if ( !isset( $template_post_blocks[ 'after' ][ $atts[ 'pod' ] ] ) ) {
198
			$template_post_blocks[ 'after' ][ $atts[ 'pod' ] ] = pods_do_shortcode( frontier_decode_template( $code, $atts ), array(
199
				'if',
200
				'else'
201
			) );
202
		}
203
	}
204
205
	return null;
206
}
207
208
/**
209
 * processes once blocks
210
 *
211
 *
212
 * @param array attributes from template
213
 * @param string encoded template to be decoded
214
 *
215
 * @return string template code
216
 * @since 2.4
217
 */
218
function frontier_template_once_blocks( $atts, $code ) {
219
220
	global $frontier_once_hashes;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
221
222
	if ( !isset( $frontier_once_hashes ) ) {
223
		$frontier_once_hashes = array();
224
	}
225
226
	$blockhash = md5( $code . $atts[ 'id' ] );
227
	if ( in_array( $blockhash, $frontier_once_hashes ) ) {
228
		return '';
229
	}
230
	$frontier_once_hashes[ ] = $blockhash;
231
232
	return pods_do_shortcode( frontier_decode_template( $code, $atts ), frontier_get_shortcodes() );
233
}
234
235
/**
236
 * processes template code within an each command from the base template
237
 *
238
 *
239
 * @param array attributes from template
240
 * @param string template to be processed
241
 *
242
 * @return null
243
 * @since 2.4
244
 */
245
function frontier_do_subtemplate( $atts, $content ) {
246
247
	$out = null;
248
	$pod = pods( $atts[ 'pod' ], $atts[ 'id' ] );
249
250
	$params = array(
251
		'name' => $atts[ 'field' ],
252
	);
253
254
	$entries = $pod->field( $atts[ 'field' ] );
255
	if ( ! empty( $entries ) ) {
256
257
		/**
258
		 * Note on the change below for issue #3018:
259
		 * ... || 'taxonomy' == $pod->fields[ $atts[ 'field' ] ][ 'type' ]
260
		 *
261
		 * calling field() above for a taxonomy object field will populate
262
		 * $pod->fields[ $field_name ] for the object field's data, in this
263
		 * case a taxonomy object field. Without calling
264
		 * $pod->field( $field_name ), it would not normally be available in
265
		 * the $pod->fields array and is something to not expect to be there in
266
		 * 3.0 as this was unintentional.
267
		 */
268
		if ( ! empty( $pod->fields[ $atts[ 'field' ] ][ 'table_info' ] ) || 'taxonomy' == $pod->fields[ $atts[ 'field' ] ][ 'type' ] ) {
269
			foreach ( $entries as $key => $entry ) {
270
				$subpod = pods( $pod->fields[ $atts[ 'field' ] ][ 'pick_val' ] );
271
272
				$subatts = array(
273
					'id'  => $entry[ $subpod->api->pod_data[ 'field_id' ] ],
274
					'pod' => $pod->fields[ $atts[ 'field' ] ][ 'pick_val' ]
275
				);
276
277
				$template = frontier_decode_template( $content, array_merge( $atts, $subatts ) );
278
				$template = str_replace( '{_index}', $key, $template );
279
				$template = str_replace( '{@' . $atts[ 'field' ] . '.', '{@', $template );
280
281
				// Kludge to work with taxonomies, pending a better solution: see issue #3018
282
				$target_id = null;
283
				if ( isset( $entry[ 'ID' ] ) ) {
284
					$target_id = $entry[ 'ID' ];
285
				} elseif ( isset( $entry[ 'term_id' ] ) ) {
286
					$target_id = $entry[ 'term_id' ];
287
				}
288
289
				$out .= pods_shortcode( array(
290
					'name'  => $pod->fields[ $atts[ 'field' ] ][ 'pick_val' ],
291
					'slug'  => $target_id,
292
					'index' => $key
293
				), $template );
294
295
			}
296
		} elseif ( 'file' == $pod->fields[ $atts[ 'field' ] ][ 'type' ] && 'attachment' == $pod->fields[ $atts[ 'field' ] ][ 'options' ][ 'file_uploader' ] && 'multi' == $pod->fields[ $atts[ 'field' ] ][ 'options' ][ 'file_format_type' ] ) {
297
			$template = frontier_decode_template( $content, $atts );
298
			foreach ( $entries as $key => $entry ) {
299
				$content = str_replace( '{_index}', $key, $template );
300
				$content = str_replace( '{@_img', '{@image_attachment.' . $entry[ 'ID' ], $content );
301
				$content = str_replace( '{@_src', '{@image_attachment_url.' . $entry[ 'ID' ], $content );
302
				$content = str_replace( '{@' . $atts[ 'field' ] . '}', '{@image_attachment.' . $entry[ 'ID' ] . '}', $content );
303
304
				$out .= pods_do_shortcode( $pod->do_magic_tags( $content ), array(
305
					'each',
306
					'pod_sub_template',
307
					'once',
308
					'pod_once_template',
309
					'before',
310
					'pod_before_template',
311
					'after',
312
					'pod_after_template',
313
					'if',
314
					'pod_if_field'
315
				) );
316
			}
317
318
		}
319
	}
320
321
	return pods_do_shortcode( $out, array(
322
		'each',
323
		'pod_sub_template',
324
		'once',
325
		'pod_once_template',
326
		'before',
327
		'pod_before_template',
328
		'after',
329
		'pod_after_template',
330
		'if',
331
		'pod_if_field'
332
	) );
333
}
334
335
/**
336
 * processes template code within an each command from the base template
337
 *
338
 *
339
 * @param array attributes from template
340
 * @param string template to be processed
341
 *
342
 * @return null
343
 * @since 2.4
344
 */
345
function frontier_prefilter_template( $code, $template, $pod ) {
346
347
	global $frontier_once_tags;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
348
349
	$commands = array(
350
		'each' => 'pod_sub_template',
351
		'once' => 'pod_once_template',
352
		'before' => 'pod_before_template',
353
		'after' => 'pod_after_template',
354
		'if' => 'pod_if_field',
355
	);
356
357
	$commands = array_merge( $commands, get_option( 'pods_frontier_extra_commands', array()  ) );
358
359
	/**
360
	 * Add additional control blocks to Pods templates
361
	 *
362
	 * Can also be use to remove each/once/before/after/if functionality from Pods Templates
363
	 *
364
	 * @param array $commands The control blocks in the form of 'tag' => 'shortcode'
365
	 *
366
	 * @return array An array of control blocks, and shortcodes used to power them.
367
	 *
368
	 * @since 1.0.0
369
	 */
370
	$commands = apply_filters( 'pods_frontier_template_commands', $commands );
371
372
	$aliases = array();
373
	foreach ( $commands as $command => $shortcode ) {
374
		preg_match_all( "/(\[" . $command . "(.*?)]|\[\/" . $command . "\])/m", $code, $matches );
375
		if ( !empty( $matches[ 0 ] ) ) {
376
			// holder for found blocks.
377
			$blocks = array();
378
			$indexCount = 0;
379
			foreach ( $matches[ 0 ] as $key => $tag ) {
380
				if ( false === strpos( $tag, '[/' ) ) {
381
					// open tag
382
					$field = null;
383
					$value = null;
384
					$ID = '{@EntryID}';
385
					$atts = ' pod="@pod" id="' . $ID . '"';
386
					if ( !empty( $matches[ 2 ][ $key ] ) ) {
387
						// get atts if any
388
						//$atts = shortcode_parse_atts(str_replace('.', '____', $matches[2][$key]));
0 ignored issues
show
Unused Code Comprehensibility introduced by
71% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
389
						$atts = array();
390
						$pattern = '/(\w.+)\s*=\s*"([^"]*)"(?:\s|$)/';
391
						$text = preg_replace( "/[\x{00a0}\x{200b}]+/u", " ", $matches[ 2 ][ $key ] );
392
						if ( preg_match_all( $pattern, $text, $match, PREG_SET_ORDER ) ) {
393
							$field = $match[ 0 ][ 1 ];
394
							$value = $match[ 0 ][ 2 ];
395
						}
396
						else {
397
							$field = trim( $matches[ 2 ][ $key ] );
398
						}
399
						if ( false !== strpos( $field, '.' ) ) {
400
							$path = explode( '.', $field );
401
							$field = array_pop( $path );
402
							$ID = '{@' . implode( '.', $path ) . '.' . $pod->api->pod_data[ 'field_id' ] . '}';
403
						}
404
						$atts = ' id="' . $ID . '" pod="@pod" field="' . $field . '"';
405
						if ( !empty( $value ) ) {
406
							$atts .= ' value="' . $value . '"';
407
						}
408
					}
409
410
					$newtag = $shortcode . '__' . $key;
411
					$tags[ $indexCount ] = $newtag;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$tags was never initialized. Although not strictly required by PHP, it is generally a good practice to add $tags = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
412
					$aliases[ ] = $newtag;
413
					$code = preg_replace( "/(" . preg_quote( $tag ) . ")/m", "[" . $newtag . $atts . " index=\"{_index}\"]", $code, 1 );
414
					$indexCount++;
415
				}
416
				else {
417
					// close tag
418
					$indexCount--;
419
					$newclose = $tags[ $indexCount ];
0 ignored issues
show
Bug introduced by
The variable $tags does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
420
					$code = preg_replace( "/(" . preg_quote( $tag, '/' ) . ")/m", "[/" . $newclose . "]", $code, 1 );
421
422
				}
423
			}
424
			if ( $command == 'if' ) {
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
425
				//dump($pod);
426
			}
427
		}
428
	}
429
	// get new aliased shotcodes
430
431
	if ( !empty( $aliases ) ) {
432
		$code = frontier_backtrack_template( $code, $aliases );
433
	}
434
	$code = str_replace( '@pod', $pod->pod, $code );
435
	$code = str_replace( '@EntryID', '@' . $pod->api->pod_data[ 'field_id' ], $code );
436
437
	return $code;
438
}
439
440
function frontier_backtrack_template( $code, $aliases ) {
441
442
	$regex = frontier_get_regex( $aliases );
443
	preg_match_all( '/' . $regex . '/s', $code, $used );
444
445
	if ( !empty( $used[ 2 ] ) ) {
446
		foreach ( $used[ 2 ] as $key => $alias ) {
447
			$shortcodes = explode( '__', $alias );
448
			$content = $used[ 5 ][ $key ];
449
			$atts = shortcode_parse_atts( $used[ 3 ][ $key ] );
450
			if ( !empty( $atts ) ) {
451
				if ( !empty( $atts[ 'field' ] ) ) {
452
					$content = str_replace( $atts[ 'field' ] . '.', '', $content );
453
				}
454
				preg_match_all( '/' . $regex . '/s', $content, $subused );
455
				if ( !empty( $subused[ 2 ] ) ) {
456
					$content = frontier_backtrack_template( $content, $aliases );
457
				}
458
				$codecontent = "[" . $shortcodes[ 0 ] . " " . trim( $used[ 3 ][ $key ] ) . " seq=\"" . $shortcodes[ 1 ] . "\"]" . base64_encode( $content ) . "[/" . $shortcodes[ 0 ] . "]";
459
			}
460
			else {
461
				$codecontent = "[" . $shortcodes[ 0 ] . " seq=\"" . $shortcodes[ 1 ] . "\"]" . base64_encode( $content ) . "[/" . $shortcodes[ 0 ] . "]";
462
			}
463
			$code = str_replace( $used[ 0 ][ $key ], $codecontent, $code );
464
		}
465
	}
466
467
	return $code;
468
}
469
470
function frontier_get_regex( $codes ) {
471
472
	// A custom version of the shortcode regex as to only use podsfrontier codes.
473
	// this makes it easier to cycle through and get the used codes for inclusion
474
	$validcodes = join( '|', array_map( 'preg_quote', $codes ) );
475
476
	return '\\[' // Opening bracket
477
	. '(\\[?)' // 1: Optional second opening bracket for escaping shortcodes: [[tag]]
478
	. "($validcodes)" // 2: selected codes only
479
	. '\\b' // Word boundary
480
	. '(' // 3: Unroll the loop: Inside the opening shortcode tag
481
	. '[^\\]\\/]*' // Not a closing bracket or forward slash
482
	. '(?:' . '\\/(?!\\])' // A forward slash not followed by a closing bracket
483
	. '[^\\]\\/]*' // Not a closing bracket or forward slash
484
	. ')*?' . ')' . '(?:' . '(\\/)' // 4: Self closing tag ...
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
485
	. '\\]' // ... and closing bracket
486
	. '|' . '\\]' // Closing bracket
487
	. '(?:' . '(' // 5: Unroll the loop: Optionally, anything between the opening and closing shortcode tags
488
	. '[^\\[]*+' // Not an opening bracket
489
	. '(?:' . '\\[(?!\\/\\2\\])' // An opening bracket not followed by the closing shortcode tag
490
	. '[^\\[]*+' // Not an opening bracket
491
	. ')*+' . ')' . '\\[\\/\\2\\]' // Closing shortcode tag
492
	. ')?' . ')' . '(\\]?)'; // 6: Optional second closing brocket for escaping shortcodes: [[tag]]
493
494
}
495
496
function frontier_end_template( $code, $base, $template, $pod ) {
497
498
	global $template_post_blocks;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
499
500 View Code Duplication
	if ( !empty( $template_post_blocks[ 'before' ][ $pod->pod ] ) ) {
501
		$code = $template_post_blocks[ 'before' ][ $pod->pod ] . $code;
502
503
		unset( $template_post_blocks[ 'before' ][ $pod->pod ] );
504
	}
505
506 View Code Duplication
	if ( !empty( $template_post_blocks[ 'after' ][ $pod->pod ] ) ) {
507
		$code .= $template_post_blocks[ 'after' ][ $pod->pod ];
508
509
		unset( $template_post_blocks[ 'after' ][ $pod->pod ] );
510
	}
511
512
	return pods_do_shortcode( $code, frontier_get_shortcodes() );
513
}
514