home *** CD-ROM | disk | FTP | other *** search
/ HTML Examples / WP.iso / wordpress / wp-admin / link-parse-opml.php < prev    next >
Encoding:
PHP Script  |  2016-10-23  |  2.3 KB  |  96 lines

  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.  * @global string $opml
  14.  */
  15. global $opml;
  16.  
  17. /**
  18.  * XML callback function for the start of a new XML tag.
  19.  *
  20.  * @since 0.71
  21.  * @access private
  22.  *
  23.  * @global array $names
  24.  * @global array $urls
  25.  * @global array $targets
  26.  * @global array $descriptions
  27.  * @global array $feeds
  28.  *
  29.  * @param mixed $parser XML Parser resource.
  30.  * @param string $tagName XML element name.
  31.  * @param array $attrs XML element attributes.
  32.  */
  33. function startElement($parser, $tagName, $attrs) {
  34.     global $names, $urls, $targets, $descriptions, $feeds;
  35.  
  36.     if ( 'OUTLINE' === $tagName ) {
  37.         $name = '';
  38.         if ( isset( $attrs['TEXT'] ) ) {
  39.             $name = $attrs['TEXT'];
  40.         }
  41.         if ( isset( $attrs['TITLE'] ) ) {
  42.             $name = $attrs['TITLE'];
  43.         }
  44.         $url = '';
  45.         if ( isset( $attrs['URL'] ) ) {
  46.             $url = $attrs['URL'];
  47.         }
  48.         if ( isset( $attrs['HTMLURL'] ) ) {
  49.             $url = $attrs['HTMLURL'];
  50.         }
  51.  
  52.         // Save the data away.
  53.         $names[] = $name;
  54.         $urls[] = $url;
  55.         $targets[] = isset( $attrs['TARGET'] ) ? $attrs['TARGET'] :  '';
  56.         $feeds[] = isset( $attrs['XMLURL'] ) ? $attrs['XMLURL'] :  '';
  57.         $descriptions[] = isset( $attrs['DESCRIPTION'] ) ? $attrs['DESCRIPTION'] :  '';
  58.     } // End if outline.
  59. }
  60.  
  61. /**
  62.  * XML callback function that is called at the end of a XML tag.
  63.  *
  64.  * @since 0.71
  65.  * @access private
  66.  *
  67.  * @param mixed $parser XML Parser resource.
  68.  * @param string $tagName XML tag name.
  69.  */
  70. function endElement($parser, $tagName) {
  71.     // Nothing to do.
  72. }
  73.  
  74. // Create an XML parser
  75. if ( ! function_exists( 'xml_parser_create' ) ) {
  76.     trigger_error( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
  77.     wp_die( __( "PHP's XML extension is not available. Please contact your hosting provider to enable PHP's XML extension." ) );
  78. }
  79.  
  80. $xml_parser = xml_parser_create();
  81.  
  82. // Set the functions to handle opening and closing tags
  83. xml_set_element_handler($xml_parser, "startElement", "endElement");
  84.  
  85. if ( ! xml_parse( $xml_parser, $opml, true ) ) {
  86.     printf(
  87.         /* translators: 1: error message, 2: line number */
  88.         __( 'XML Error: %1$s at line %2$s' ),
  89.         xml_error_string( xml_get_error_code( $xml_parser ) ),
  90.         xml_get_current_line_number( $xml_parser )
  91.     );
  92. }
  93.  
  94. // Free up memory used by the XML parser
  95. xml_parser_free($xml_parser);
  96.