functions.php ➔ printcenter_prettify_xml()   C
last analyzed

Complexity

Conditions 8
Paths 24

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 72

Importance

Changes 0
Metric Value
cc 8
eloc 20
nc 24
nop 2
dl 0
loc 36
ccs 0
cts 27
cp 0
crap 72
rs 5.3846
c 0
b 0
f 0
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 22 and the first side effect is on line 12.

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
 * Helper functions
4
 *
5
 * @package     PrintCenter\Functions
6
 * @since       1.0.0
7
 */
8
9
10
// Exit if accessed directly
11
if( ! defined( 'ABSPATH' ) ) {
12
	exit;
13
}
14
15
16
/**
17
 * Fetch an array of available shirt types
18
 *
19
 * @since       1.0.0
20
 * @return      array $shirts The available shirt types
21
 */
22
function printcenter_get_shirts() {
23
	$shirts = array();
24
25
	$posts = get_posts( array(
26
		'posts_per_page' => 999999,
27
		'post_type'      => 'ssi_product',
28
		'post_status'    => 'publish'
29
	) );
30
31
	if( count( $posts ) > 0 ) {
32
		foreach( $posts as $post ) {
33
			$sku          = get_post_meta( $post->ID, '_ssi_sku', true );
34
			$shirts[$sku] = $post->post_title;
35
		}
36
	}
37
38
	return $shirts;
39
}
40
41
42
/**
43
 * Prettifies an XML string
44
 *
45
 * @since       1.0.0
46
 * @param       string $xml The XML as a string
47
 * @param       boolean $html_output True if the output should be escaped (for use in HTML)
48
 * @return      string The prettified XML
49
 */
50
function printcenter_prettify_xml( $xml, $html_output = false ) {
51
	$xml_obj = new SimpleXMLElement($xml);
52
	$level   = 4;
53
	$indent  = 0; // current indentation level
54
	$pretty  = array();
55
56
	// Get an array containing each XML element
57
	$xml = explode( "\n", preg_replace( '/>\s*</', ">\n<", $xml_obj->asXML() ) );
58
59
	// Shift off opening XML tag if present
60
	if( count( $xml ) && preg_match( '/^<\?\s*xml/', $xml[0] ) ) {
61
		$pretty[] = array_shift($xml);
62
	}
63
64
	foreach( $xml as $el ) {
65
		if( preg_match( '/^<([\w])+[^>\/]*>$/U', $el ) ) {
66
			// opening tag, increase indent
67
			$pretty[] = str_repeat( ' ', $indent ) . $el;
68
			$indent += $level;
69
		} else {
70
			if( preg_match( '/^<\/.+>$/', $el ) ) {
71
				$indent -= $level;  // closing tag, decrease indent
72
			}
73
74
			if( $indent < 0 ) {
75
				$indent += $level;
76
			}
77
78
			$pretty[] = str_repeat( ' ', $indent ) . $el;
79
		}
80
	}
81
82
	$xml = implode( "\n", $pretty );
83
84
	return ( $html_output ) ? htmlentities( $xml ) : $xml;
85
}