Completed
Pull Request — 2.x (#3397)
by Phil
06:26
created

PodsUIFieldData::emit_script()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 3
nc 1
nop 0
1
<?php
2
3
/**
4
 * @package Pods
5
 */
6
class PodsUIFieldData {
7
8
	private $export_data = array();
9
10
	/**
11
	 * PodsFieldData constructor.
12
	 *
13
	 * @param string $field_type
14
	 * @param array  $data
15
	 */
16
	public function __construct( $field_type, $data ) {
17
18
		$data[ 'field_type' ] = $field_type;
19
		$this->export_data    = $data;
20
	}
21
22
	/**
23
	 * Sends direct output
24
	 */
25
	public function emit_script() { ?>
26
		<script type="application/json" class="data"><?php echo self::json_encode_hex_tag( $this->export_data ); ?></script>
27
	<?php }
28
29
	/**
30
	 * Provides PHP 5.2 support for the JSON_HEX_TAG param with json_encode
31
	 *
32
	 * @param string $data
33
	 *
34
	 * @return string
35
	 */
36
	private static function json_encode_hex_tag( $data ) {
37
38
		$search  = array( '<', '>' );
39
		$replace = array( '\u003C', '\u003E' );
40
41
		if ( defined( 'JSON_HEX_TAG' ) ) {
42
			$string = json_encode( $data, JSON_HEX_TAG );
0 ignored issues
show
Unused Code introduced by
The call to json_encode() has too many arguments starting with JSON_HEX_TAG.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
43
		} else {
44
			$string = json_encode( $data );
45
			$string = str_replace( $search, $replace, $string );
46
		}
47
48
		return $string;
49
	}
50
}
51