link-parse-opml.php 2.6 KB

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