| 1 |
<?php |
| 2 |
namespace Templately\Modules\FullSiteImport\Parsers; |
| 3 |
|
| 4 |
|
| 5 |
use Templately\Modules\FullSiteImport\Utils\Utils; |
| 6 |
use WP_Error; |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; // Exit if accessed directly. |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* WordPress extended RSS file parser implementations, |
| 14 |
* Originally made by WordPress part of WordPress/Importer. |
| 15 |
* https://plugins.trac.wordpress.org/browser/wordpress-importer/trunk/parsers/class-wxr-parser-simplexml.php |
| 16 |
* |
| 17 |
* What was done (by Elementor): |
| 18 |
* Reformat of the code. |
| 19 |
* Removed variable '$internal_errors'. |
| 20 |
* Changed text domain. |
| 21 |
*/ |
| 22 |
|
| 23 |
/** |
| 24 |
* WXR Parser that makes use of the SimpleXML PHP extension. |
| 25 |
*/ |
| 26 |
class WXR_Parser_SimpleXML { |
| 27 |
|
| 28 |
/** |
| 29 |
* @param string $file |
| 30 |
* |
| 31 |
* @return array|WP_Error |
| 32 |
*/ |
| 33 |
public function parse( $file ) { |
| 34 |
$authors = []; |
| 35 |
$posts = []; |
| 36 |
$categories = []; |
| 37 |
$tags = []; |
| 38 |
$terms = []; |
| 39 |
|
| 40 |
libxml_use_internal_errors( true ); |
| 41 |
|
| 42 |
$dom = new \DOMDocument(); |
| 43 |
$old_value = null; |
| 44 |
|
| 45 |
$libxml_disable_entity_loader_exists = function_exists( 'libxml_disable_entity_loader' ); |
| 46 |
|
| 47 |
if ( $libxml_disable_entity_loader_exists ) { |
| 48 |
$old_value = @libxml_disable_entity_loader( true ); // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated |
| 49 |
} |
| 50 |
|
| 51 |
$success = $dom->loadXML( Utils::file_get_contents( $file ) ); |
| 52 |
|
| 53 |
if ( $libxml_disable_entity_loader_exists && ! is_null( $old_value ) ) { |
| 54 |
@libxml_disable_entity_loader( $old_value ); // phpcs:ignore Generic.PHP.DeprecatedFunctions.Deprecated |
| 55 |
} |
| 56 |
|
| 57 |
if ( ! $success || isset( $dom->doctype ) ) { |
| 58 |
return new WP_Error( 'SimpleXML_parse_error', esc_html__( 'There was an error when reading this WXR file', 'elementor' ), libxml_get_errors() ); |
| 59 |
} |
| 60 |
|
| 61 |
$xml = simplexml_import_dom( $dom ); |
| 62 |
unset( $dom ); |
| 63 |
|
| 64 |
// Halt if loading produces an error. |
| 65 |
if ( ! $xml ) { |
| 66 |
return new WP_Error( 'SimpleXML_parse_error', esc_html__( 'There was an error when reading this WXR file', 'elementor' ), libxml_get_errors() ); |
| 67 |
} |
| 68 |
|
| 69 |
$wxr_version = $xml->xpath( '/rss/channel/wp:wxr_version' ); |
| 70 |
if ( ! $wxr_version ) { |
| 71 |
return new WP_Error( 'WXR_parse_error', esc_html__( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'elementor' ) ); |
| 72 |
} |
| 73 |
|
| 74 |
$wxr_version = (string) trim( $wxr_version[0] ); |
| 75 |
// Confirm that we are dealing with the correct file format. |
| 76 |
if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) ) { |
| 77 |
return new WP_Error( 'WXR_parse_error', esc_html__( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'elementor' ) ); |
| 78 |
} |
| 79 |
|
| 80 |
$base_url = $xml->xpath( '/rss/channel/wp:base_site_url' ); |
| 81 |
$base_url = (string) trim( $base_url[0] ?? '' ); |
| 82 |
|
| 83 |
$base_blog_url = $xml->xpath( '/rss/channel/wp:base_blog_url' ); |
| 84 |
if ( $base_blog_url ) { |
| 85 |
$base_blog_url = (string) trim( $base_blog_url[0] ); |
| 86 |
} else { |
| 87 |
$base_blog_url = $base_url; |
| 88 |
} |
| 89 |
|
| 90 |
$page_on_front = $xml->xpath( '/rss/channel/wp:page_on_front' ); |
| 91 |
|
| 92 |
if ( $page_on_front ) { |
| 93 |
$page_on_front = (int) $page_on_front[0]; |
| 94 |
} |
| 95 |
|
| 96 |
$namespaces = $xml->getDocNamespaces(); |
| 97 |
if ( ! isset( $namespaces['wp'] ) ) { |
| 98 |
$namespaces['wp'] = 'http://wordpress.org/export/1.1/'; |
| 99 |
} |
| 100 |
if ( ! isset( $namespaces['excerpt'] ) ) { |
| 101 |
$namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/'; |
| 102 |
} |
| 103 |
|
| 104 |
// Grab authors. |
| 105 |
foreach ( $xml->xpath( '/rss/channel/wp:author' ) as $author_arr ) { |
| 106 |
$a = $author_arr->children( $namespaces['wp'] ); |
| 107 |
$login = (string) $a->author_login; |
| 108 |
$authors[ $login ] = [ |
| 109 |
'author_id' => (int) $a->author_id, |
| 110 |
'author_login' => $login, |
| 111 |
'author_email' => (string) $a->author_email, |
| 112 |
'author_display_name' => (string) $a->author_display_name, |
| 113 |
'author_first_name' => (string) $a->author_first_name, |
| 114 |
'author_last_name' => (string) $a->author_last_name, |
| 115 |
]; |
| 116 |
} |
| 117 |
|
| 118 |
// Grab cats, tags and terms. |
| 119 |
foreach ( $xml->xpath( '/rss/channel/wp:category' ) as $term_arr ) { |
| 120 |
$t = $term_arr->children( $namespaces['wp'] ); |
| 121 |
$category = [ |
| 122 |
'term_id' => (int) $t->term_id, |
| 123 |
'category_nicename' => (string) $t->category_nicename, |
| 124 |
'category_parent' => (string) $t->category_parent, |
| 125 |
'cat_name' => (string) $t->cat_name, |
| 126 |
'category_description' => (string) $t->category_description, |
| 127 |
]; |
| 128 |
|
| 129 |
foreach ( $t->termmeta as $meta ) { |
| 130 |
$category['termmeta'][] = [ |
| 131 |
'key' => (string) $meta->meta_key, |
| 132 |
'value' => (string) $meta->meta_value, |
| 133 |
]; |
| 134 |
} |
| 135 |
|
| 136 |
$categories[] = $category; |
| 137 |
} |
| 138 |
|
| 139 |
foreach ( $xml->xpath( '/rss/channel/wp:tag' ) as $term_arr ) { |
| 140 |
$t = $term_arr->children( $namespaces['wp'] ); |
| 141 |
$tag = [ |
| 142 |
'term_id' => (int) $t->term_id, |
| 143 |
'tag_slug' => (string) $t->tag_slug, |
| 144 |
'tag_name' => (string) $t->tag_name, |
| 145 |
'tag_description' => (string) $t->tag_description, |
| 146 |
]; |
| 147 |
|
| 148 |
foreach ( $t->termmeta as $meta ) { |
| 149 |
$tag['termmeta'][] = [ |
| 150 |
'key' => (string) $meta->meta_key, |
| 151 |
'value' => (string) $meta->meta_value, |
| 152 |
]; |
| 153 |
} |
| 154 |
|
| 155 |
$tags[] = $tag; |
| 156 |
} |
| 157 |
|
| 158 |
foreach ( $xml->xpath( '/rss/channel/wp:term' ) as $term_arr ) { |
| 159 |
$t = $term_arr->children( $namespaces['wp'] ); |
| 160 |
$term = [ |
| 161 |
'term_id' => (int) $t->term_id, |
| 162 |
'term_taxonomy' => (string) $t->term_taxonomy, |
| 163 |
'slug' => (string) $t->term_slug, |
| 164 |
'term_parent' => (string) $t->term_parent, |
| 165 |
'term_name' => (string) $t->term_name, |
| 166 |
'term_description' => (string) $t->term_description, |
| 167 |
]; |
| 168 |
|
| 169 |
foreach ( $t->termmeta as $meta ) { |
| 170 |
$term['termmeta'][] = [ |
| 171 |
'key' => (string) $meta->meta_key, |
| 172 |
'value' => (string) $meta->meta_value, |
| 173 |
]; |
| 174 |
} |
| 175 |
|
| 176 |
$terms[] = $term; |
| 177 |
} |
| 178 |
|
| 179 |
// Grab posts. |
| 180 |
foreach ( $xml->channel->item as $item ) { |
| 181 |
$post = [ |
| 182 |
'post_title' => (string) $item->title, |
| 183 |
'guid' => (string) $item->guid, |
| 184 |
]; |
| 185 |
|
| 186 |
$dc = $item->children( 'http://purl.org/dc/elements/1.1/' ); |
| 187 |
$post['post_author'] = (string) $dc->creator; |
| 188 |
|
| 189 |
$content = $item->children( 'http://purl.org/rss/1.0/modules/content/' ); |
| 190 |
$excerpt = $item->children( $namespaces['excerpt'] ); |
| 191 |
$post['post_content'] = (string) $content->encoded; |
| 192 |
$post['post_excerpt'] = (string) $excerpt->encoded; |
| 193 |
|
| 194 |
$wp = $item->children( $namespaces['wp'] ); |
| 195 |
$post['post_id'] = (int) $wp->post_id; |
| 196 |
$post['post_date'] = (string) $wp->post_date; |
| 197 |
$post['post_date_gmt'] = (string) $wp->post_date_gmt; |
| 198 |
$post['comment_status'] = (string) $wp->comment_status; |
| 199 |
$post['ping_status'] = (string) $wp->ping_status; |
| 200 |
$post['post_name'] = (string) $wp->post_name; |
| 201 |
$post['status'] = (string) $wp->status; |
| 202 |
$post['post_parent'] = (int) $wp->post_parent; |
| 203 |
$post['menu_order'] = (int) $wp->menu_order; |
| 204 |
$post['post_type'] = (string) $wp->post_type; |
| 205 |
$post['post_password'] = (string) $wp->post_password; |
| 206 |
$post['is_sticky'] = (int) $wp->is_sticky; |
| 207 |
|
| 208 |
if ( isset( $wp->attachment_url ) ) { |
| 209 |
$post['attachment_url'] = (string) $wp->attachment_url; |
| 210 |
} |
| 211 |
|
| 212 |
// Extract attachment_type field |
| 213 |
if ( isset( $wp->attachment_type ) ) { |
| 214 |
$post['attachment_type'] = (string) $wp->attachment_type; |
| 215 |
} else { |
| 216 |
$post['attachment_type'] = null; |
| 217 |
} |
| 218 |
|
| 219 |
foreach ( $item->category as $c ) { |
| 220 |
$att = $c->attributes(); |
| 221 |
if ( isset( $att['nicename'] ) ) { |
| 222 |
$post['terms'][] = [ |
| 223 |
'name' => (string) $c, |
| 224 |
'slug' => (string) $att['nicename'], |
| 225 |
'domain' => (string) $att['domain'], |
| 226 |
]; |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
foreach ( $wp->postmeta as $meta ) { |
| 231 |
$post['postmeta'][] = [ |
| 232 |
'key' => (string) $meta->meta_key, |
| 233 |
'value' => (string) $meta->meta_value, |
| 234 |
]; |
| 235 |
} |
| 236 |
|
| 237 |
foreach ( $wp->comment as $comment ) { |
| 238 |
$meta = []; |
| 239 |
if ( isset( $comment->commentmeta ) ) { |
| 240 |
foreach ( $comment->commentmeta as $m ) { |
| 241 |
$meta[] = [ |
| 242 |
'key' => (string) $m->meta_key, |
| 243 |
'value' => (string) $m->meta_value, |
| 244 |
]; |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
$post['comments'][] = [ |
| 249 |
'comment_id' => (int) $comment->comment_id, |
| 250 |
'comment_author' => (string) $comment->comment_author, |
| 251 |
'comment_author_email' => (string) $comment->comment_author_email, |
| 252 |
'comment_author_IP' => (string) $comment->comment_author_IP, |
| 253 |
'comment_author_url' => (string) $comment->comment_author_url, |
| 254 |
'comment_date' => (string) $comment->comment_date, |
| 255 |
'comment_date_gmt' => (string) $comment->comment_date_gmt, |
| 256 |
'comment_content' => (string) $comment->comment_content, |
| 257 |
'comment_approved' => (string) $comment->comment_approved, |
| 258 |
'comment_type' => (string) $comment->comment_type, |
| 259 |
'comment_parent' => (string) $comment->comment_parent, |
| 260 |
'comment_user_id' => (int) $comment->comment_user_id, |
| 261 |
'commentmeta' => $meta, |
| 262 |
]; |
| 263 |
} |
| 264 |
|
| 265 |
$posts[] = $post; |
| 266 |
} |
| 267 |
|
| 268 |
return [ |
| 269 |
'authors' => $authors, |
| 270 |
'posts' => $posts, |
| 271 |
'categories' => $categories, |
| 272 |
'tags' => $tags, |
| 273 |
'terms' => $terms, |
| 274 |
'base_url' => $base_url, |
| 275 |
'base_blog_url' => $base_blog_url, |
| 276 |
'page_on_front' => $page_on_front, |
| 277 |
'version' => $wxr_version, |
| 278 |
]; |
| 279 |
} |
| 280 |
} |
| 281 |
|