-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-thinkery-xml-parser.php
54 lines (46 loc) · 1.47 KB
/
class-thinkery-xml-parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
/**
* WordPress eXtended RSS file parser implementations
*
* @package Thinkery
* @subpackage Importer
*/
/**
* This parses the Thinkery XML file.
*/
class Thinkery_XML_Parser {
function parse( $file ) {
$things = array();
$internal_errors = libxml_use_internal_errors(true);
$dom = new DOMDocument;
$old_value = null;
if ( function_exists( 'libxml_disable_entity_loader' ) ) {
$old_value = libxml_disable_entity_loader( true );
}
$success = $dom->loadXML( file_get_contents( $file ) );
if ( ! is_null( $old_value ) ) {
libxml_disable_entity_loader( $old_value );
}
if ( ! $success || isset( $dom->doctype ) ) {
return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
}
$xml = simplexml_import_dom( $dom );
unset( $dom );
if ( ! $xml )
return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() );
foreach ( $xml->xpath( '//thinkery/thing' ) as $thing ) {
$post = array(
'post_title' => (string) $thing->title,
'guid' => (string) $thing->url,
'post_type' => Thinkery_Things::CPT,
'post_content' => (string) $thing->html,
'post_date' => date( 'Y-m-d H:i:s', strtotime( $thing->date ) ),
'tags' => strlen( $thing->tags ) ? explode( ' ', $thing->tags ) : array(),
);
$posts[] = $post;
}
return array(
'posts' => $posts,
);
}
}