← All changes
|
includes/import/xml-parsers/wxr-parser-simple-xml.php
+382
-0
2.7.7
→
2.14.0
View file →
| @@ -1,0 +1,382 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\import\XmlParsers; | |
| 4 | + | |
| 5 | +use SimpleXMLElement; | |
| 6 | +use WP_Error; | |
| 7 | + | |
| 8 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 9 | + exit; | |
| 10 | +} | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * WXR Parser that makes use of the SimpleXML PHP extension. | |
| 14 | + */ | |
| 15 | +class WXR_Parser_SimpleXML { | |
| 16 | + public function parse( $file ) { | |
| 17 | + $authors = array(); | |
| 18 | + $posts = array(); | |
| 19 | + $categories = array(); | |
| 20 | + $tags = array(); | |
| 21 | + $terms = array(); | |
| 22 | + | |
| 23 | + $internal_errors = libxml_use_internal_errors( true ); | |
| 24 | + | |
| 25 | + $dom = new \DOMDocument(); | |
| 26 | + $old_value = null; | |
| 27 | + if ( function_exists( 'libxml_disable_entity_loader' ) && PHP_VERSION_ID < 80000 ) { | |
| 28 | + // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated | |
| 29 | + $old_value = libxml_disable_entity_loader( true ); | |
| 30 | + } | |
| 31 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents | |
| 32 | + $success = $dom->loadXML( file_get_contents( $file ) ); | |
| 33 | + if ( ! is_null( $old_value ) ) { | |
| 34 | + // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated | |
| 35 | + libxml_disable_entity_loader( $old_value ); | |
| 36 | + } | |
| 37 | + | |
| 38 | + if ( ! $success || isset( $dom->doctype ) ) { | |
| 39 | + return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'ablocks' ), libxml_get_errors() ); | |
| 40 | + } | |
| 41 | + | |
| 42 | + $xml = simplexml_import_dom( $dom ); | |
| 43 | + unset( $dom ); | |
| 44 | + | |
| 45 | + // halt if loading produces an error | |
| 46 | + if ( ! $xml ) { | |
| 47 | + return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'ablocks' ), libxml_get_errors() ); | |
| 48 | + } | |
| 49 | + | |
| 50 | + $wxr_version = $xml->xpath( '/rss/channel/wp:wxr_version' ); | |
| 51 | + if ( ! $wxr_version ) { | |
| 52 | + return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'ablocks' ) ); | |
| 53 | + } | |
| 54 | + | |
| 55 | + $wxr_version = (string) trim( $wxr_version[0] ); | |
| 56 | + // confirm that we are dealing with the correct file format | |
| 57 | + if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) ) { | |
| 58 | + return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'ablocks' ) ); | |
| 59 | + } | |
| 60 | + | |
| 61 | + $base_url = $xml->xpath( '/rss/channel/wp:base_site_url' ); | |
| 62 | + $base_url = trim( isset( $base_url[0] ) ? $base_url[0] : '' ); | |
| 63 | + | |
| 64 | + $base_blog_url = $xml->xpath( '/rss/channel/wp:base_blog_url' ); | |
| 65 | + if ( $base_blog_url ) { | |
| 66 | + $base_blog_url = trim( $base_blog_url[0] ); | |
| 67 | + } else { | |
| 68 | + $base_blog_url = $base_url; | |
| 69 | + } | |
| 70 | + | |
| 71 | + $show_on_front = $xml->xpath( '/rss/channel/ablocks_options/show_on_front' ); | |
| 72 | + if ( $show_on_front ) { | |
| 73 | + $show_on_front = trim( $show_on_front[0] ); | |
| 74 | + } | |
| 75 | + | |
| 76 | + $page_on_front = $xml->xpath( '/rss/channel/ablocks_options/page_on_front' ); | |
| 77 | + if ( $page_on_front ) { | |
| 78 | + $page_on_front = (int) $page_on_front[0]; | |
| 79 | + } | |
| 80 | + | |
| 81 | + $page_for_posts = $xml->xpath( '/rss/channel/ablocks_options/page_for_posts' ); | |
| 82 | + if ( $page_for_posts ) { | |
| 83 | + $page_for_posts = (int) $page_for_posts[0]; | |
| 84 | + } | |
| 85 | + | |
| 86 | + $namespaces = $xml->getDocNamespaces(); | |
| 87 | + if ( ! isset( $namespaces['wp'] ) ) { | |
| 88 | + $namespaces['wp'] = 'http://wordpress.org/export/1.1/'; | |
| 89 | + } | |
| 90 | + if ( ! isset( $namespaces['excerpt'] ) ) { | |
| 91 | + $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/'; | |
| 92 | + } | |
| 93 | + | |
| 94 | + // grab authors | |
| 95 | + foreach ( $xml->xpath( '/rss/channel/wp:author' ) as $author_arr ) { | |
| 96 | + $a = $author_arr->children( $namespaces['wp'] ); | |
| 97 | + $login = (string) $a->author_login; | |
| 98 | + $authors[ $login ] = array( | |
| 99 | + 'author_id' => (int) $a->author_id, | |
| 100 | + 'author_login' => $login, | |
| 101 | + 'author_email' => (string) $a->author_email, | |
| 102 | + 'author_display_name' => (string) $a->author_display_name, | |
| 103 | + 'author_first_name' => (string) $a->author_first_name, | |
| 104 | + 'author_last_name' => (string) $a->author_last_name, | |
| 105 | + ); | |
| 106 | + } | |
| 107 | + | |
| 108 | + // grab cats, tags and terms | |
| 109 | + foreach ( $xml->xpath( '/rss/channel/wp:category' ) as $term_arr ) { | |
| 110 | + $t = $term_arr->children( $namespaces['wp'] ); | |
| 111 | + $category = array( | |
| 112 | + 'term_id' => (int) $t->term_id, | |
| 113 | + 'category_nicename' => (string) $t->category_nicename, | |
| 114 | + 'category_parent' => (string) $t->category_parent, | |
| 115 | + 'cat_name' => (string) $t->cat_name, | |
| 116 | + 'category_description' => (string) $t->category_description, | |
| 117 | + ); | |
| 118 | + | |
| 119 | + foreach ( $t->termmeta as $meta ) { | |
| 120 | + $category['termmeta'][] = array( | |
| 121 | + 'key' => (string) $meta->meta_key, | |
| 122 | + 'value' => (string) $meta->meta_value, | |
| 123 | + ); | |
| 124 | + } | |
| 125 | + | |
| 126 | + $categories[] = $category; | |
| 127 | + } | |
| 128 | + | |
| 129 | + foreach ( $xml->xpath( '/rss/channel/wp:tag' ) as $term_arr ) { | |
| 130 | + $t = $term_arr->children( $namespaces['wp'] ); | |
| 131 | + $tag = array( | |
| 132 | + 'term_id' => (int) $t->term_id, | |
| 133 | + 'tag_slug' => (string) $t->tag_slug, | |
| 134 | + 'tag_name' => (string) $t->tag_name, | |
| 135 | + 'tag_description' => (string) $t->tag_description, | |
| 136 | + ); | |
| 137 | + | |
| 138 | + foreach ( $t->termmeta as $meta ) { | |
| 139 | + $tag['termmeta'][] = array( | |
| 140 | + 'key' => (string) $meta->meta_key, | |
| 141 | + 'value' => (string) $meta->meta_value, | |
| 142 | + ); | |
| 143 | + } | |
| 144 | + | |
| 145 | + $tags[] = $tag; | |
| 146 | + } | |
| 147 | + | |
| 148 | + foreach ( $xml->xpath( '/rss/channel/wp:term' ) as $term_arr ) { | |
| 149 | + $t = $term_arr->children( $namespaces['wp'] ); | |
| 150 | + $term = array( | |
| 151 | + 'term_id' => (int) $t->term_id, | |
| 152 | + 'term_taxonomy' => (string) $t->term_taxonomy, | |
| 153 | + 'slug' => (string) $t->term_slug, | |
| 154 | + 'term_parent' => (string) $t->term_parent, | |
| 155 | + 'term_name' => (string) $t->term_name, | |
| 156 | + 'term_description' => (string) $t->term_description, | |
| 157 | + ); | |
| 158 | + | |
| 159 | + foreach ( $t->termmeta as $meta ) { | |
| 160 | + $term['termmeta'][] = array( | |
| 161 | + 'key' => (string) $meta->meta_key, | |
| 162 | + 'value' => (string) $meta->meta_value, | |
| 163 | + ); | |
| 164 | + } | |
| 165 | + | |
| 166 | + $terms[] = $term; | |
| 167 | + } | |
| 168 | + | |
| 169 | + // grab posts | |
| 170 | + foreach ( $xml->channel->item as $item ) { | |
| 171 | + $post = array( | |
| 172 | + 'post_title' => (string) $item->title, | |
| 173 | + 'guid' => (string) $item->guid, | |
| 174 | + ); | |
| 175 | + | |
| 176 | + $dc = $item->children( 'http://purl.org/dc/elements/1.1/' ); | |
| 177 | + $post['post_author'] = (string) $dc->creator; | |
| 178 | + | |
| 179 | + $wp = $item->children( $namespaces['wp'] ); | |
| 180 | + $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' ); | |
| 181 | + $excerpt = $item->children( $namespaces['excerpt'] ); | |
| 182 | + $content = (string) $content->encoded; | |
| 183 | + if ( in_array( (string) $wp->post_type, array( 'wp_template', 'wp_template_part' ), true ) ) { | |
| 184 | + $content = preg_replace( | |
| 185 | + '/\s*,?\s*"theme"\s*:\s*"[^"]+"/', | |
| 186 | + '', | |
| 187 | + $content | |
| 188 | + ); | |
| 189 | + } | |
| 190 | + $post['post_content'] = $content; | |
| 191 | + $post['post_excerpt'] = (string) $excerpt->encoded; | |
| 192 | + | |
| 193 | + $post['post_id'] = (int) $wp->post_id; | |
| 194 | + $post['post_date'] = (string) $wp->post_date; | |
| 195 | + $post['post_date_gmt'] = (string) $wp->post_date_gmt; | |
| 196 | + $post['comment_status'] = (string) $wp->comment_status; | |
| 197 | + $post['ping_status'] = (string) $wp->ping_status; | |
| 198 | + $post['post_name'] = (string) $wp->post_name; | |
| 199 | + $post['status'] = (string) $wp->status; | |
| 200 | + $post['post_parent'] = (int) $wp->post_parent; | |
| 201 | + $post['menu_order'] = (int) $wp->menu_order; | |
| 202 | + $post['post_type'] = (string) $wp->post_type; | |
| 203 | + $post['post_password'] = (string) $wp->post_password; | |
| 204 | + $post['is_sticky'] = (int) $wp->is_sticky; | |
| 205 | + | |
| 206 | + if ( isset( $wp->attachment_url ) ) { | |
| 207 | + $post['attachment_url'] = (string) $wp->attachment_url; | |
| 208 | + } | |
| 209 | + | |
| 210 | + foreach ( $item->category as $c ) { | |
| 211 | + $att = $c->attributes(); | |
| 212 | + if ( isset( $att['nicename'] ) ) { | |
| 213 | + $post['terms'][] = array( | |
| 214 | + 'name' => (string) $c, | |
| 215 | + 'slug' => (string) $att['nicename'], | |
| 216 | + 'domain' => (string) $att['domain'], | |
| 217 | + ); | |
| 218 | + } | |
| 219 | + } | |
| 220 | + | |
| 221 | + foreach ( $wp->postmeta as $meta ) { | |
| 222 | + $post['postmeta'][] = array( | |
| 223 | + 'key' => (string) $meta->meta_key, | |
| 224 | + 'value' => (string) $meta->meta_value, | |
| 225 | + ); | |
| 226 | + } | |
| 227 | + | |
| 228 | + foreach ( $wp->comment as $comment ) { | |
| 229 | + $meta = array(); | |
| 230 | + if ( isset( $comment->commentmeta ) ) { | |
| 231 | + foreach ( $comment->commentmeta as $m ) { | |
| 232 | + $meta[] = array( | |
| 233 | + 'key' => (string) $m->meta_key, | |
| 234 | + 'value' => (string) $m->meta_value, | |
| 235 | + ); | |
| 236 | + } | |
| 237 | + } | |
| 238 | + | |
| 239 | + $post['comments'][] = array( | |
| 240 | + 'comment_id' => (int) $comment->comment_id, | |
| 241 | + 'comment_author' => (string) $comment->comment_author, | |
| 242 | + 'comment_author_email' => (string) $comment->comment_author_email, | |
| 243 | + 'comment_author_IP' => (string) $comment->comment_author_IP, | |
| 244 | + 'comment_author_url' => (string) $comment->comment_author_url, | |
| 245 | + 'comment_date' => (string) $comment->comment_date, | |
| 246 | + 'comment_date_gmt' => (string) $comment->comment_date_gmt, | |
| 247 | + 'comment_content' => (string) $comment->comment_content, | |
| 248 | + 'comment_approved' => (string) $comment->comment_approved, | |
| 249 | + 'comment_type' => (string) $comment->comment_type, | |
| 250 | + 'comment_parent' => (string) $comment->comment_parent, | |
| 251 | + 'comment_user_id' => (int) $comment->comment_user_id, | |
| 252 | + 'commentmeta' => $meta, | |
| 253 | + ); | |
| 254 | + }//end foreach | |
| 255 | + | |
| 256 | + $posts[] = $post; | |
| 257 | + }//end foreach | |
| 258 | + | |
| 259 | + return array( | |
| 260 | + 'authors' => $authors, | |
| 261 | + 'posts' => $posts, | |
| 262 | + 'categories' => $categories, | |
| 263 | + 'tags' => $tags, | |
| 264 | + 'terms' => $terms, | |
| 265 | + 'base_url' => $base_url, | |
| 266 | + 'base_blog_url' => $base_blog_url, | |
| 267 | + 'version' => $wxr_version, | |
| 268 | + 'show_on_front' => $show_on_front, | |
| 269 | + 'page_on_front' => $page_on_front, | |
| 270 | + 'page_for_posts' => $page_for_posts, | |
| 271 | + 'storeengine_pages' => $this->get_storeengine_pages( $xml ), | |
| 272 | + 'academy_pages' => $this->get_academy_pages( $xml ), | |
| 273 | + 'ablocks_pages' => $this->get_ablocks_pages( $xml ), | |
| 274 | + 'ablocks_options' => $this->get_ablocks_options( $xml ), | |
| 275 | + ); | |
| 276 | + } | |
| 277 | + | |
| 278 | + private function get_storeengine_pages( SimpleXMLElement $xml ): array { | |
| 279 | + $storeengine_pages = [ | |
| 280 | + 'shop_page' => 0, | |
| 281 | + 'cart_page' => 0, | |
| 282 | + 'checkout_page' => 0, | |
| 283 | + 'thankyou_page' => 0, | |
| 284 | + 'dashboard_page' => 0, | |
| 285 | + 'membership_pricing_page' => 0, | |
| 286 | + 'affiliate_registration_page' => 0, | |
| 287 | + ]; | |
| 288 | + | |
| 289 | + foreach ( array_keys( $storeengine_pages ) as $page_key ) { | |
| 290 | + $value = $xml->xpath( '/rss/channel/ablocks_options/storeengine_' . $page_key ); | |
| 291 | + if ( $value ) { | |
| 292 | + $storeengine_pages[ $page_key ] = (int) $value[0]; | |
| 293 | + } | |
| 294 | + } | |
| 295 | + | |
| 296 | + return $storeengine_pages; | |
| 297 | + } | |
| 298 | + | |
| 299 | + private function get_academy_pages( SimpleXMLElement $xml ): array { | |
| 300 | + $academy_pages = [ | |
| 301 | + 'frontend_dashboard_page' => 0, | |
| 302 | + 'frontend_student_reg_page' => 0, | |
| 303 | + 'password_reset_page' => 0, | |
| 304 | + 'lessons_page' => 0, | |
| 305 | + 'course_page' => 0, | |
| 306 | + 'frontend_instructor_reg_page' => 0, | |
| 307 | + 'tutor_booking_page' => 0, | |
| 308 | + ]; | |
| 309 | + | |
| 310 | + foreach ( array_keys( $academy_pages ) as $page_key ) { | |
| 311 | + $value = $xml->xpath( '/rss/channel/ablocks_options/academy_' . $page_key ); | |
| 312 | + if ( $value ) { | |
| 313 | + $academy_pages[ $page_key ] = (int) $value[0]; | |
| 314 | + } | |
| 315 | + } | |
| 316 | + | |
| 317 | + return $academy_pages; | |
| 318 | + } | |
| 319 | + | |
| 320 | + private function get_ablocks_pages( SimpleXMLElement $xml ): array { | |
| 321 | + $ablocks_pages = [ | |
| 322 | + 'login_page' => 0, | |
| 323 | + 'registration_page' => 0, | |
| 324 | + 'forget_password_page' => 0, | |
| 325 | + ]; | |
| 326 | + | |
| 327 | + foreach ( array_keys( $ablocks_pages ) as $page_key ) { | |
| 328 | + $value = $xml->xpath( '/rss/channel/ablocks_options/ablocks_' . $page_key ); | |
| 329 | + if ( $value ) { | |
| 330 | + $ablocks_pages[ $page_key ] = (int) $value[0]; | |
| 331 | + } | |
| 332 | + } | |
| 333 | + | |
| 334 | + return $ablocks_pages; | |
| 335 | + } | |
| 336 | + | |
| 337 | + private function get_ablocks_options( SimpleXMLElement $xml ): array { | |
| 338 | + $ablocks_options = [ | |
| 339 | + 'default_container_width' => null, | |
| 340 | + 'container_padding' => null, | |
| 341 | + 'container_element_gap' => null, | |
| 342 | + 'global_color' => 'json', | |
| 343 | + 'global_typography' => 'json', | |
| 344 | + 'global_font_family_fallback' => null, | |
| 345 | + 'global_body_text_color' => null, | |
| 346 | + 'global_body_typography' => 'json', | |
| 347 | + 'global_body_paragraph_space' => 'json', | |
| 348 | + 'global_link_color' => null, | |
| 349 | + 'global_link_hover_color' => null, | |
| 350 | + 'global_link_typography' => 'json', | |
| 351 | + 'global_link_hover_typography' => 'json', | |
| 352 | + 'global_h1_color' => null, | |
| 353 | + 'global_h1_typography' => 'json', | |
| 354 | + 'global_h2_color' => null, | |
| 355 | + 'global_h2_typography' => 'json', | |
| 356 | + 'global_h3_color' => null, | |
| 357 | + 'global_h3_typography' => 'json', | |
| 358 | + 'global_h4_color' => null, | |
| 359 | + 'global_h4_typography' => 'json', | |
| 360 | + 'global_h5_color' => null, | |
| 361 | + 'global_h5_typography' => 'json', | |
| 362 | + 'global_h6_color' => null, | |
| 363 | + 'global_h6_typography' => 'json', | |
| 364 | + 'frontend_dashboard_page' => null, | |
| 365 | + 'frontend_dashboard_sub_pages' => 'json', | |
| 366 | + 'global_classes' => 'json', | |
| 367 | + ]; | |
| 368 | + | |
| 369 | + foreach ( array_keys( $ablocks_options ) as $page_key ) { | |
| 370 | + $value = $xml->xpath( '/rss/channel/ablocks_options/ablocks_' . $page_key ); | |
| 371 | + if ( $value ) { | |
| 372 | + $ablocks_options[ $page_key ] = 'json' === $ablocks_options[ $page_key ] ? json_decode( | |
| 373 | + base64_decode( (string) $value[0] ), true | |
| 374 | + ) : (string) $value[0]; | |
| 375 | + } else { | |
| 376 | + unset( $ablocks_options[ $page_key ] ); | |
| 377 | + } | |
| 378 | + } | |
| 379 | + | |
| 380 | + return $ablocks_options; | |
| 381 | + } | |
| 382 | +} | |