PluginProbe ʕ •ᴥ•ʔ
Starter Sites & Templates by Neve / 1.4.1
Starter Sites & Templates by Neve v1.4.1
1.4.1 1.4.0 1.3.0 1.2.29 1.2.28 1.2.6 1.2.7 1.2.8 1.2.9 trunk 1.0.10 1.0.11 1.0.7 1.0.8 1.0.9 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.20 1.1.21 1.1.22 1.1.23 1.1.24 1.1.25 1.1.26 1.1.27 1.1.28 1.1.29 1.1.3 1.1.30 1.1.31 1.1.32 1.1.33 1.1.34 1.1.35 1.1.36 1.1.37 1.1.38 1.1.39 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.2.0 1.2.1 1.2.10 1.2.11 1.2.12 1.2.13 1.2.14 1.2.15 1.2.16 1.2.17 1.2.18 1.2.19 1.2.2 1.2.20 1.2.21 1.2.22 1.2.23 1.2.24 1.2.25 1.2.26 1.2.27 1.2.3 1.2.4 1.2.5
templates-patterns-collection / includes / Importers / WP / WXR_Parser_SimpleXML.php
templates-patterns-collection / includes / Importers / WP Last commit date
Atomic_Wind_Meta_Handler.php 3 months ago Beaver_Data_Fix.php 5 years ago Beaver_ParserXML.php 5 years ago Elementor_Meta_Handler.php 3 months ago WP_Import.php 3 months ago WXR_Parser.php 5 years ago WXR_Parser_SimpleXML.php 4 years ago WXR_Parser_XML.php 5 years ago
WXR_Parser_SimpleXML.php
208 lines
1 <?php
2
3 namespace TIOB\Importers\WP;
4 use WP_Error;
5
6 /**
7 * WXR Parser that makes use of the SimpleXML PHP extension.
8 */
9 class WXR_Parser_SimpleXML {
10 /**
11 * @param string $file file path.
12 *
13 * @return array | WP_Error
14 */
15 public function parse( $file ) {
16 global $wp_filesystem;
17 WP_Filesystem();
18 $authors = $posts = $categories = $tags = $terms = array();
19 $internal_errors = libxml_use_internal_errors( true );
20 $dom = new \DOMDocument;
21 $old_value = null;
22 if ( \PHP_VERSION_ID < 80000 && function_exists( 'libxml_disable_entity_loader' ) ) {
23 $old_value = libxml_disable_entity_loader( true );
24 }
25 $success = $dom->loadXML( $wp_filesystem->get_contents( $file ) );
26 if ( ! is_null( $old_value ) ) {
27 libxml_disable_entity_loader( $old_value );
28 }
29 if ( ! $success || isset( $dom->doctype ) ) {
30 return new WP_Error( 'SimpleXML_parse_error', 'There was an error when reading this WXR file', libxml_get_errors() );
31 }
32 $xml = simplexml_import_dom( $dom );
33 unset( $dom );
34 // halt if loading produces an error
35 if ( ! $xml ) {
36 return new WP_Error( 'SimpleXML_parse_error', 'There was an error when reading this WXR file', libxml_get_errors() );
37 }
38 $wxr_version = $xml->xpath( '/rss/channel/wp:wxr_version' );
39 if ( ! $wxr_version ) {
40 return new WP_Error( 'WXR_parse_error', 'This does not appear to be a WXR file, missing/invalid WXR version number' );
41 }
42 $wxr_version = (string) trim( $wxr_version[0] );
43 // confirm that we are dealing with the correct file format
44 if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) ) {
45 return new WP_Error( 'WXR_parse_error', 'This does not appear to be a WXR file, missing/invalid WXR version number' );
46 }
47 $base_url = $xml->xpath( '/rss/channel/wp:base_site_url' );
48 $base_url = (string) trim( $base_url[0] );
49 $base_blog_url = $xml->xpath( '/rss/channel/wp:base_blog_url' );
50 $base_blog_url = (string) trim( $base_blog_url[0] );
51 $namespaces = $xml->getDocNamespaces();
52 if ( ! isset( $namespaces['wp'] ) ) {
53 $namespaces['wp'] = 'http://wordpress.org/export/1.1/';
54 }
55 if ( ! isset( $namespaces['excerpt'] ) ) {
56 $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/';
57 }
58 // grab authors
59 foreach ( $xml->xpath( '/rss/channel/wp:author' ) as $author_arr ) {
60 $a = $author_arr->children( $namespaces['wp'] );
61 $login = (string) $a->author_login;
62 $authors[ $login ] = array(
63 'author_id' => (int) $a->author_id,
64 'author_login' => $login,
65 'author_email' => (string) $a->author_email,
66 'author_display_name' => (string) $a->author_display_name,
67 'author_first_name' => (string) $a->author_first_name,
68 'author_last_name' => (string) $a->author_last_name,
69 );
70 }
71 // grab cats, tags and terms
72 foreach ( $xml->xpath( '/rss/channel/wp:category' ) as $term_arr ) {
73 $t = $term_arr->children( $namespaces['wp'] );
74 $category = array(
75 'term_id' => (int) $t->term_id,
76 'category_nicename' => (string) $t->category_nicename,
77 'category_parent' => (string) $t->category_parent,
78 'cat_name' => (string) $t->cat_name,
79 'category_description' => (string) $t->category_description,
80 );
81 foreach ( $t->termmeta as $meta ) {
82 $category['termmeta'][] = array(
83 'key' => (string) $meta->meta_key,
84 'value' => (string) $meta->meta_value,
85 );
86 }
87 $categories[] = $category;
88 }
89 foreach ( $xml->xpath( '/rss/channel/wp:tag' ) as $term_arr ) {
90 $t = $term_arr->children( $namespaces['wp'] );
91 $tag = array(
92 'term_id' => (int) $t->term_id,
93 'tag_slug' => (string) $t->tag_slug,
94 'tag_name' => (string) $t->tag_name,
95 'tag_description' => (string) $t->tag_description,
96 );
97 foreach ( $t->termmeta as $meta ) {
98 $tag['termmeta'][] = array(
99 'key' => (string) $meta->meta_key,
100 'value' => (string) $meta->meta_value,
101 );
102 }
103 $tags[] = $tag;
104 }
105 foreach ( $xml->xpath( '/rss/channel/wp:term' ) as $term_arr ) {
106 $t = $term_arr->children( $namespaces['wp'] );
107 $term = array(
108 'term_id' => (int) $t->term_id,
109 'term_taxonomy' => (string) $t->term_taxonomy,
110 'slug' => (string) $t->term_slug,
111 'term_parent' => (string) $t->term_parent,
112 'term_name' => (string) $t->term_name,
113 'term_description' => (string) $t->term_description,
114 );
115 foreach ( $t->termmeta as $meta ) {
116 $term['termmeta'][] = array(
117 'key' => (string) $meta->meta_key,
118 'value' => (string) $meta->meta_value,
119 );
120 }
121 $terms[] = $term;
122 }
123 // grab posts
124 foreach ( $xml->channel->item as $item ) {
125 $post = array(
126 'post_title' => (string) $item->title,
127 'guid' => (string) $item->guid,
128 );
129 $dc = $item->children( 'http://purl.org/dc/elements/1.1/' );
130 $post['post_author'] = (string) $dc->creator;
131 $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' );
132 $excerpt = $item->children( $namespaces['excerpt'] );
133 $post['post_content'] = (string) $content->encoded;
134 $post['post_excerpt'] = (string) $excerpt->encoded;
135 $wp = $item->children( $namespaces['wp'] );
136 $post['post_id'] = (int) $wp->post_id;
137 $post['post_date'] = (string) $wp->post_date;
138 $post['post_date_gmt'] = (string) $wp->post_date_gmt;
139 $post['comment_status'] = (string) $wp->comment_status;
140 $post['ping_status'] = (string) $wp->ping_status;
141 $post['post_name'] = (string) $wp->post_name;
142 $post['status'] = (string) $wp->status;
143 $post['post_parent'] = (int) $wp->post_parent;
144 $post['menu_order'] = (int) $wp->menu_order;
145 $post['post_type'] = (string) $wp->post_type;
146 $post['post_password'] = (string) $wp->post_password;
147 $post['is_sticky'] = (int) $wp->is_sticky;
148 if ( isset( $wp->attachment_url ) ) {
149 $post['attachment_url'] = (string) $wp->attachment_url;
150 }
151 foreach ( $item->category as $c ) {
152 $att = $c->attributes();
153 if ( isset( $att['nicename'] ) ) {
154 $post['terms'][] = array(
155 'name' => (string) $c,
156 'slug' => (string) $att['nicename'],
157 'domain' => (string) $att['domain'],
158 );
159 }
160 }
161 foreach ( $wp->postmeta as $meta ) {
162 $post['postmeta'][] = array(
163 'key' => (string) $meta->meta_key,
164 'value' => (string) $meta->meta_value,
165 );
166 }
167 foreach ( $wp->comment as $comment ) {
168 $meta = array();
169 if ( isset( $comment->commentmeta ) ) {
170 foreach ( $comment->commentmeta as $m ) {
171 $meta[] = array(
172 'key' => (string) $m->meta_key,
173 'value' => (string) $m->meta_value,
174 );
175 }
176 }
177 $post['comments'][] = array(
178 'comment_id' => (int) $comment->comment_id,
179 'comment_author' => (string) $comment->comment_author,
180 'comment_author_email' => (string) $comment->comment_author_email,
181 'comment_author_IP' => (string) $comment->comment_author_IP,
182 'comment_author_url' => (string) $comment->comment_author_url,
183 'comment_date' => (string) $comment->comment_date,
184 'comment_date_gmt' => (string) $comment->comment_date_gmt,
185 'comment_content' => (string) $comment->comment_content,
186 'comment_approved' => (string) $comment->comment_approved,
187 'comment_type' => (string) $comment->comment_type,
188 'comment_parent' => (string) $comment->comment_parent,
189 'comment_user_id' => (int) $comment->comment_user_id,
190 'commentmeta' => $meta,
191 );
192 }
193 $posts[] = $post;
194 }
195
196 return array(
197 'authors' => $authors,
198 'posts' => $posts,
199 'categories' => $categories,
200 'tags' => $tags,
201 'terms' => $terms,
202 'base_url' => $base_url,
203 'base_blog_url' => $base_blog_url,
204 'version' => $wxr_version,
205 );
206 }
207 }
208