author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:28:13 +0200 | |
changeset 9 | 177826044cd9 |
parent 7 | cf61fcea0001 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* Parse OPML XML files and store in globals. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 | 9 |
if ( ! defined( 'ABSPATH' ) ) { |
0 | 10 |
die(); |
9 | 11 |
} |
0 | 12 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
13 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
14 |
* @global string $opml |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
15 |
*/ |
5 | 16 |
global $opml; |
0 | 17 |
|
18 |
/** |
|
19 |
* XML callback function for the start of a new XML tag. |
|
20 |
* |
|
21 |
* @since 0.71 |
|
22 |
* @access private |
|
23 |
* |
|
24 |
* @global array $names |
|
25 |
* @global array $urls |
|
26 |
* @global array $targets |
|
27 |
* @global array $descriptions |
|
28 |
* @global array $feeds |
|
29 |
* |
|
30 |
* @param mixed $parser XML Parser resource. |
|
31 |
* @param string $tagName XML element name. |
|
32 |
* @param array $attrs XML element attributes. |
|
33 |
*/ |
|
9 | 34 |
function startElement( $parser, $tagName, $attrs ) { |
0 | 35 |
global $names, $urls, $targets, $descriptions, $feeds; |
36 |
||
5 | 37 |
if ( 'OUTLINE' === $tagName ) { |
38 |
$name = ''; |
|
39 |
if ( isset( $attrs['TEXT'] ) ) { |
|
40 |
$name = $attrs['TEXT']; |
|
41 |
} |
|
42 |
if ( isset( $attrs['TITLE'] ) ) { |
|
43 |
$name = $attrs['TITLE']; |
|
44 |
} |
|
45 |
$url = ''; |
|
46 |
if ( isset( $attrs['URL'] ) ) { |
|
47 |
$url = $attrs['URL']; |
|
48 |
} |
|
49 |
if ( isset( $attrs['HTMLURL'] ) ) { |
|
50 |
$url = $attrs['HTMLURL']; |
|
0 | 51 |
} |
52 |
||
5 | 53 |
// Save the data away. |
9 | 54 |
$names[] = $name; |
55 |
$urls[] = $url; |
|
56 |
$targets[] = isset( $attrs['TARGET'] ) ? $attrs['TARGET'] : ''; |
|
57 |
$feeds[] = isset( $attrs['XMLURL'] ) ? $attrs['XMLURL'] : ''; |
|
58 |
$descriptions[] = isset( $attrs['DESCRIPTION'] ) ? $attrs['DESCRIPTION'] : ''; |
|
5 | 59 |
} // End if outline. |
0 | 60 |
} |
61 |
||
62 |
/** |
|
63 |
* XML callback function that is called at the end of a XML tag. |
|
64 |
* |
|
65 |
* @since 0.71 |
|
66 |
* @access private |
|
67 |
* |
|
68 |
* @param mixed $parser XML Parser resource. |
|
69 |
* @param string $tagName XML tag name. |
|
70 |
*/ |
|
9 | 71 |
function endElement( $parser, $tagName ) { |
5 | 72 |
// Nothing to do. |
0 | 73 |
} |
74 |
||
75 |
// Create an XML parser |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
if ( ! function_exists( 'xml_parser_create' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
wp_die( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
80 |
|
0 | 81 |
$xml_parser = xml_parser_create(); |
82 |
||
83 |
// Set the functions to handle opening and closing tags |
|
9 | 84 |
xml_set_element_handler( $xml_parser, 'startElement', 'endElement' ); |
0 | 85 |
|
5 | 86 |
if ( ! xml_parse( $xml_parser, $opml, true ) ) { |
87 |
printf( |
|
88 |
/* translators: 1: error message, 2: line number */ |
|
89 |
__( 'XML Error: %1$s at line %2$s' ), |
|
90 |
xml_error_string( xml_get_error_code( $xml_parser ) ), |
|
91 |
xml_get_current_line_number( $xml_parser ) |
|
92 |
); |
|
0 | 93 |
} |
94 |
||
95 |
// Free up memory used by the XML parser |
|
9 | 96 |
xml_parser_free( $xml_parser ); |