author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 21 | 48c4eec2b7e6 |
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 |
* |
|
16 | 30 |
* @param resource $parser XML Parser resource. |
31 |
* @param string $tag_name XML element name. |
|
32 |
* @param array $attrs XML element attributes. |
|
0 | 33 |
*/ |
16 | 34 |
function startElement( $parser, $tag_name, $attrs ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
0 | 35 |
global $names, $urls, $targets, $descriptions, $feeds; |
36 |
||
16 | 37 |
if ( 'OUTLINE' === $tag_name ) { |
5 | 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 |
* |
|
16 | 68 |
* @param resource $parser XML Parser resource. |
69 |
* @param string $tag_name XML tag name. |
|
0 | 70 |
*/ |
16 | 71 |
function endElement( $parser, $tag_name ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid |
5 | 72 |
// Nothing to do. |
0 | 73 |
} |
74 |
||
16 | 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 |
||
16 | 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( |
|
16 | 88 |
/* translators: 1: Error message, 2: Line number. */ |
5 | 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 |
||
16 | 95 |
// Free up memory used by the XML parser. |
9 | 96 |
xml_parser_free( $xml_parser ); |
16 | 97 |
unset( $xml_parser ); |