1<?php
2/**
3 * Parse OPML XML files and store in globals.
4 *
5 * @package WordPress
6 * @subpackage Administration
7 */
8
9if ( ! defined( 'ABSPATH' ) ) {
10 die();
11}
12
13/**
14 * @global string $opml
15 */
16global $opml;
17
18/**
19 * Starts a new XML tag.
20 *
21 * Callback function for xml_set_element_handler().
22 *
23 * @since 0.71
24 * @access private
25 *
26 * @global array $names
27 * @global array $urls
28 * @global array $targets
29 * @global array $descriptions
30 * @global array $feeds
31 *
32 * @param resource $parser XML Parser resource.
33 * @param string $tag_name XML element name.
34 * @param array $attrs XML element attributes.
35 */
36function startElement( $parser, $tag_name, $attrs ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
37 global $names, $urls, $targets, $descriptions, $feeds;
38
39 if ( 'OUTLINE' === $tag_name ) {
40 $name = '';
41 if ( isset( $attrs['TEXT'] ) ) {
42 $name = $attrs['TEXT'];
43 }
44 if ( isset( $attrs['TITLE'] ) ) {
45 $name = $attrs['TITLE'];
46 }
47 $url = '';
48 if ( isset( $attrs['URL'] ) ) {
49 $url = $attrs['URL'];
50 }
51 if ( isset( $attrs['HTMLURL'] ) ) {
52 $url = $attrs['HTMLURL'];
53 }
54
55 // Save the data away.
56 $names[] = $name;
57 $urls[] = $url;
58 $targets[] = isset( $attrs['TARGET'] ) ? $attrs['TARGET'] : '';
59 $feeds[] = isset( $attrs['XMLURL'] ) ? $attrs['XMLURL'] : '';
60 $descriptions[] = isset( $attrs['DESCRIPTION'] ) ? $attrs['DESCRIPTION'] : '';
61 } // End if outline.
62}
63
64/**
65 * Ends a new XML tag.
66 *
67 * Callback function for xml_set_element_handler().
68 *
69 * @since 0.71
70 * @access private
71 *
72 * @param resource $parser XML Parser resource.
73 * @param string $tag_name XML tag name.
74 */
75function endElement( $parser, $tag_name ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
76 // Nothing to do.
77}
78
79// Create an XML parser.
80if ( ! function_exists( 'xml_parser_create' ) ) {
81 wp_trigger_error( '', __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
82 wp_die( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
83}
84
85$xml_parser = xml_parser_create();
86
87// Set the functions to handle opening and closing tags.
88xml_set_element_handler( $xml_parser, 'startElement', 'endElement' );
89
90if ( ! xml_parse( $xml_parser, $opml, true ) ) {
91 printf(
92 /* translators: 1: Error message, 2: Line number. */
93 __( 'XML Error: %1$s at line %2$s' ),
94 xml_error_string( xml_get_error_code( $xml_parser ) ),
95 xml_get_current_line_number( $xml_parser )
96 );
97}
98
99if ( PHP_VERSION_ID < 80000 ) { // xml_parser_free() has no effect as of PHP 8.0.
100 // Free up memory used by the XML parser.
101 xml_parser_free( $xml_parser );
102}
103
104unset( $xml_parser );
105