angelovcom.net

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

link-parse-opml.php (2654B)


      1 <?php
      2 /**
      3  * Parse OPML XML files and store in globals.
      4  *
      5  * @package WordPress
      6  * @subpackage Administration
      7  */
      8 
      9 if ( ! defined( 'ABSPATH' ) ) {
     10 	die();
     11 }
     12 
     13 /**
     14  * @global string $opml
     15  */
     16 global $opml;
     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 resource $parser   XML Parser resource.
     31  * @param string   $tag_name XML element name.
     32  * @param array    $attrs    XML element attributes.
     33  */
     34 function startElement( $parser, $tag_name, $attrs ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
     35 	global $names, $urls, $targets, $descriptions, $feeds;
     36 
     37 	if ( 'OUTLINE' === $tag_name ) {
     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'];
     51 		}
     52 
     53 		// Save the data away.
     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'] : '';
     59 	} // End if outline.
     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 resource $parser   XML Parser resource.
     69  * @param string   $tag_name XML tag name.
     70  */
     71 function endElement( $parser, $tag_name ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionNameInvalid
     72 	// Nothing to do.
     73 }
     74 
     75 // Create an XML parser.
     76 if ( ! function_exists( 'xml_parser_create' ) ) {
     77 	trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
     78 	wp_die( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
     79 }
     80 
     81 $xml_parser = xml_parser_create();
     82 
     83 // Set the functions to handle opening and closing tags.
     84 xml_set_element_handler( $xml_parser, 'startElement', 'endElement' );
     85 
     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 	);
     93 }
     94 
     95 // Free up memory used by the XML parser.
     96 xml_parser_free( $xml_parser );
     97 unset( $xml_parser );