| 1 |
<?php |
| 2 |
namespace Templately\Modules\FullSiteImport\Parsers; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
|
| 6 |
if ( ! defined( 'ABSPATH' ) ) { |
| 7 |
exit; // Exit if accessed directly. |
| 8 |
} |
| 9 |
|
| 10 |
/** |
| 11 |
* WordPress extended RSS file parser implementations |
| 12 |
* Originally made by WordPress part of WordPress/Importer. |
| 13 |
* https://plugins.trac.wordpress.org/browser/wordpress-importer/trunk/parsers/class-wxr-parser-regex.php |
| 14 |
* |
| 15 |
* What was done (by Elementor): |
| 16 |
* Reformat of the code. |
| 17 |
* Changed text domain. |
| 18 |
* Changed methods visibility. |
| 19 |
*/ |
| 20 |
|
| 21 |
/** |
| 22 |
* WXR Parser that uses regular expressions. Fallback for installs without an XML parser. |
| 23 |
*/ |
| 24 |
class WXR_Parser_Regex { |
| 25 |
/** |
| 26 |
* @var bool |
| 27 |
*/ |
| 28 |
private $has_gzip; |
| 29 |
|
| 30 |
private $authors = []; |
| 31 |
private $posts = []; |
| 32 |
private $categories = []; |
| 33 |
private $tags = []; |
| 34 |
private $terms = []; |
| 35 |
private $base_url = ''; |
| 36 |
private $base_blog_url = ''; |
| 37 |
|
| 38 |
/** |
| 39 |
* @param string $file |
| 40 |
* |
| 41 |
* @return array|WP_Error |
| 42 |
*/ |
| 43 |
public function parse( $file ) { |
| 44 |
$wxr_version = ''; |
| 45 |
$in_multiline = false; |
| 46 |
|
| 47 |
$multiline_content = ''; |
| 48 |
|
| 49 |
$multiline_tags = [ |
| 50 |
'item' => [ |
| 51 |
'posts', |
| 52 |
function ( $post ) { |
| 53 |
return $this->process_post( $post ); |
| 54 |
}, |
| 55 |
], |
| 56 |
'wp:category' => [ |
| 57 |
'categories', |
| 58 |
function ( $category ) { |
| 59 |
return $this->process_category( $category ); |
| 60 |
}, |
| 61 |
], |
| 62 |
'wp:tag' => [ |
| 63 |
'tags', |
| 64 |
function ( $tag ) { |
| 65 |
return $this->process_tag( $tag ); |
| 66 |
}, |
| 67 |
], |
| 68 |
'wp:term' => [ |
| 69 |
'terms', |
| 70 |
function ( $term ) { |
| 71 |
return $this->process_term( $term ); |
| 72 |
}, |
| 73 |
], |
| 74 |
]; |
| 75 |
|
| 76 |
$fp = $this->fopen( $file, 'r' ); |
| 77 |
if ( $fp ) { |
| 78 |
while ( ! $this->feof( $fp ) ) { |
| 79 |
$importline = rtrim( $this->fgets( $fp ) ); |
| 80 |
|
| 81 |
if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) ) { |
| 82 |
$wxr_version = $version[1]; |
| 83 |
} |
| 84 |
|
| 85 |
if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) { |
| 86 |
preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url ); |
| 87 |
$this->base_url = $url[1]; |
| 88 |
continue; |
| 89 |
} |
| 90 |
|
| 91 |
if ( false !== strpos( $importline, '<wp:base_blog_url>' ) ) { |
| 92 |
preg_match( '|<wp:base_blog_url>(.*?)</wp:base_blog_url>|is', $importline, $blog_url ); |
| 93 |
$this->base_blog_url = $blog_url[1]; |
| 94 |
continue; |
| 95 |
} else { |
| 96 |
$this->base_blog_url = $this->base_url; |
| 97 |
} |
| 98 |
|
| 99 |
if ( false !== strpos( $importline, '<wp:author>' ) ) { |
| 100 |
preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author ); |
| 101 |
$a = $this->process_author( $author[1] ); |
| 102 |
$this->authors[ $a['author_login'] ] = $a; |
| 103 |
continue; |
| 104 |
} |
| 105 |
|
| 106 |
foreach ( $multiline_tags as $tag => $handler ) { |
| 107 |
// Handle multi-line tags on a singular line. |
| 108 |
if ( preg_match( '|<' . $tag . '>(.*?)</' . $tag . '>|is', $importline, $matches ) ) { |
| 109 |
$this->{$handler[0]}[] = call_user_func( $handler[1], $matches[1] ); |
| 110 |
|
| 111 |
continue; |
| 112 |
} |
| 113 |
|
| 114 |
$pos = strpos( $importline, "<$tag>" ); |
| 115 |
|
| 116 |
if ( false !== $pos ) { |
| 117 |
// Take note of any content after the opening tag. |
| 118 |
$multiline_content = trim( substr( $importline, $pos + strlen( $tag ) + 2 ) ); |
| 119 |
|
| 120 |
// We don't want to have this line added to `$is_multiline` below. |
| 121 |
$importline = ''; |
| 122 |
$in_multiline = $tag; |
| 123 |
|
| 124 |
continue; |
| 125 |
} |
| 126 |
|
| 127 |
$pos = strpos( $importline, "</$tag>" ); |
| 128 |
|
| 129 |
if ( false !== $pos ) { |
| 130 |
$in_multiline = false; |
| 131 |
$multiline_content .= trim( substr( $importline, 0, $pos ) ); |
| 132 |
|
| 133 |
$this->{$handler[0]}[] = call_user_func( $handler[1], $multiline_content ); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
if ( $in_multiline && $importline ) { |
| 138 |
$multiline_content .= $importline . "\n"; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
$this->fclose( $fp ); |
| 143 |
} |
| 144 |
|
| 145 |
if ( ! $wxr_version ) { |
| 146 |
return new WP_Error( 'WXR_parse_error', esc_html__( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'elementor' ) ); |
| 147 |
} |
| 148 |
|
| 149 |
return [ |
| 150 |
'authors' => $this->authors, |
| 151 |
'posts' => $this->posts, |
| 152 |
'categories' => $this->categories, |
| 153 |
'tags' => $this->tags, |
| 154 |
'terms' => $this->terms, |
| 155 |
'base_url' => $this->base_url, |
| 156 |
'base_blog_url' => $this->base_blog_url, |
| 157 |
'version' => $wxr_version, |
| 158 |
]; |
| 159 |
} |
| 160 |
|
| 161 |
private function process_category( $category ) { |
| 162 |
$term = [ |
| 163 |
'term_id' => $this->get_tag( $category, 'wp:term_id' ), |
| 164 |
'cat_name' => $this->get_tag( $category, 'wp:cat_name' ), |
| 165 |
'category_nicename' => $this->get_tag( $category, 'wp:category_nicename' ), |
| 166 |
'category_parent' => $this->get_tag( $category, 'wp:category_parent' ), |
| 167 |
'category_description' => $this->get_tag( $category, 'wp:category_description' ), |
| 168 |
]; |
| 169 |
|
| 170 |
$term_meta = $this->process_meta( $category, 'wp:termmeta' ); |
| 171 |
if ( ! empty( $term_meta ) ) { |
| 172 |
$term['termmeta'] = $term_meta; |
| 173 |
} |
| 174 |
|
| 175 |
return $term; |
| 176 |
} |
| 177 |
|
| 178 |
private function process_tag( $tag ) { |
| 179 |
$term = [ |
| 180 |
'term_id' => $this->get_tag( $tag, 'wp:term_id' ), |
| 181 |
'tag_name' => $this->get_tag( $tag, 'wp:tag_name' ), |
| 182 |
'tag_slug' => $this->get_tag( $tag, 'wp:tag_slug' ), |
| 183 |
'tag_description' => $this->get_tag( $tag, 'wp:tag_description' ), |
| 184 |
]; |
| 185 |
|
| 186 |
$term_meta = $this->process_meta( $tag, 'wp:termmeta' ); |
| 187 |
if ( ! empty( $term_meta ) ) { |
| 188 |
$term['termmeta'] = $term_meta; |
| 189 |
} |
| 190 |
|
| 191 |
return $term; |
| 192 |
} |
| 193 |
|
| 194 |
private function process_term( $term ) { |
| 195 |
$term_data = [ |
| 196 |
'term_id' => $this->get_tag( $term, 'wp:term_id' ), |
| 197 |
'term_taxonomy' => $this->get_tag( $term, 'wp:term_taxonomy' ), |
| 198 |
'slug' => $this->get_tag( $term, 'wp:term_slug' ), |
| 199 |
'term_parent' => $this->get_tag( $term, 'wp:term_parent' ), |
| 200 |
'term_name' => $this->get_tag( $term, 'wp:term_name' ), |
| 201 |
'term_description' => $this->get_tag( $term, 'wp:term_description' ), |
| 202 |
]; |
| 203 |
|
| 204 |
$term_meta = $this->process_meta( $term, 'wp:termmeta' ); |
| 205 |
if ( ! empty( $term_meta ) ) { |
| 206 |
$term_data['termmeta'] = $term_meta; |
| 207 |
} |
| 208 |
|
| 209 |
return $term_data; |
| 210 |
} |
| 211 |
|
| 212 |
private function process_meta( $string, $tag ) { |
| 213 |
$parsed_meta = []; |
| 214 |
|
| 215 |
preg_match_all( "|<$tag>(.+?)</$tag>|is", $string, $meta ); |
| 216 |
|
| 217 |
if ( ! isset( $meta[1] ) ) { |
| 218 |
return $parsed_meta; |
| 219 |
} |
| 220 |
|
| 221 |
foreach ( $meta[1] as $m ) { |
| 222 |
$parsed_meta[] = [ |
| 223 |
'key' => $this->get_tag( $m, 'wp:meta_key' ), |
| 224 |
'value' => $this->get_tag( $m, 'wp:meta_value' ), |
| 225 |
]; |
| 226 |
} |
| 227 |
|
| 228 |
return $parsed_meta; |
| 229 |
} |
| 230 |
|
| 231 |
private function process_author( $a ) { |
| 232 |
return [ |
| 233 |
'author_id' => $this->get_tag( $a, 'wp:author_id' ), |
| 234 |
'author_login' => $this->get_tag( $a, 'wp:author_login' ), |
| 235 |
'author_email' => $this->get_tag( $a, 'wp:author_email' ), |
| 236 |
'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ), |
| 237 |
'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ), |
| 238 |
'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ), |
| 239 |
]; |
| 240 |
} |
| 241 |
|
| 242 |
private function process_post( $post ) { |
| 243 |
$normalize_tag_callback = function ( $matches ) { |
| 244 |
return $this->normalize_tag( $matches ); |
| 245 |
}; |
| 246 |
|
| 247 |
$post_id = $this->get_tag( $post, 'wp:post_id' ); |
| 248 |
$post_title = $this->get_tag( $post, 'title' ); |
| 249 |
$post_date = $this->get_tag( $post, 'wp:post_date' ); |
| 250 |
$post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); |
| 251 |
$comment_status = $this->get_tag( $post, 'wp:comment_status' ); |
| 252 |
$ping_status = $this->get_tag( $post, 'wp:ping_status' ); |
| 253 |
$status = $this->get_tag( $post, 'wp:status' ); |
| 254 |
$post_name = $this->get_tag( $post, 'wp:post_name' ); |
| 255 |
$post_parent = $this->get_tag( $post, 'wp:post_parent' ); |
| 256 |
$menu_order = $this->get_tag( $post, 'wp:menu_order' ); |
| 257 |
$post_type = $this->get_tag( $post, 'wp:post_type' ); |
| 258 |
$post_password = $this->get_tag( $post, 'wp:post_password' ); |
| 259 |
$is_sticky = $this->get_tag( $post, 'wp:is_sticky' ); |
| 260 |
$guid = $this->get_tag( $post, 'guid' ); |
| 261 |
$post_author = $this->get_tag( $post, 'dc:creator' ); |
| 262 |
|
| 263 |
$post_excerpt = $this->get_tag( $post, 'excerpt:encoded' ); |
| 264 |
$post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', $normalize_tag_callback, $post_excerpt ); |
| 265 |
$post_excerpt = str_replace( '<br>', '<br />', $post_excerpt ); |
| 266 |
$post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt ); |
| 267 |
|
| 268 |
$post_content = $this->get_tag( $post, 'content:encoded' ); |
| 269 |
$post_content = preg_replace_callback( '|<(/?[A-Z]+)|', $normalize_tag_callback, $post_content ); |
| 270 |
$post_content = str_replace( '<br>', '<br />', $post_content ); |
| 271 |
$post_content = str_replace( '<hr>', '<hr />', $post_content ); |
| 272 |
|
| 273 |
$postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', 'menu_order', 'post_type', 'post_password', 'is_sticky' ); |
| 274 |
|
| 275 |
$attachment_url = $this->get_tag( $post, 'wp:attachment_url' ); |
| 276 |
if ( $attachment_url ) { |
| 277 |
$postdata['attachment_url'] = $attachment_url; |
| 278 |
} |
| 279 |
|
| 280 |
// Extract attachment_type field |
| 281 |
$attachment_type = $this->get_tag( $post, 'wp:attachment_type' ); |
| 282 |
$postdata['attachment_type'] = $attachment_type ? $attachment_type : null; |
| 283 |
|
| 284 |
preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER ); |
| 285 |
foreach ( $terms as $t ) { |
| 286 |
$post_terms[] = [ |
| 287 |
'slug' => $t[2], |
| 288 |
'domain' => $t[1], |
| 289 |
'name' => str_replace( [ '<![CDATA[', ']]>' ], '', $t[3] ), |
| 290 |
]; |
| 291 |
} |
| 292 |
if ( ! empty( $post_terms ) ) { |
| 293 |
$postdata['terms'] = $post_terms; |
| 294 |
} |
| 295 |
|
| 296 |
preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments ); |
| 297 |
$comments = $comments[1]; |
| 298 |
if ( $comments ) { |
| 299 |
foreach ( $comments as $comment ) { |
| 300 |
$post_comments[] = [ |
| 301 |
'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ), |
| 302 |
'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ), |
| 303 |
'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ), |
| 304 |
'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ), |
| 305 |
'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ), |
| 306 |
'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ), |
| 307 |
'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ), |
| 308 |
'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ), |
| 309 |
'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ), |
| 310 |
'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ), |
| 311 |
'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ), |
| 312 |
'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ), |
| 313 |
'commentmeta' => $this->process_meta( $comment, 'wp:commentmeta' ), |
| 314 |
]; |
| 315 |
} |
| 316 |
} |
| 317 |
if ( ! empty( $post_comments ) ) { |
| 318 |
$postdata['comments'] = $post_comments; |
| 319 |
} |
| 320 |
|
| 321 |
$post_meta = $this->process_meta( $post, 'wp:postmeta' ); |
| 322 |
if ( ! empty( $post_meta ) ) { |
| 323 |
$postdata['postmeta'] = $post_meta; |
| 324 |
} |
| 325 |
|
| 326 |
return $postdata; |
| 327 |
} |
| 328 |
|
| 329 |
private function get_tag( $string, $tag ) { |
| 330 |
preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return ); |
| 331 |
if ( isset( $return[1] ) ) { |
| 332 |
if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) { |
| 333 |
if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) { |
| 334 |
preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches ); |
| 335 |
$return = ''; |
| 336 |
foreach ( $matches[1] as $match ) { |
| 337 |
$return .= $match; |
| 338 |
} |
| 339 |
} else { |
| 340 |
$return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] ); |
| 341 |
} |
| 342 |
} else { |
| 343 |
$return = $return[1]; |
| 344 |
} |
| 345 |
} else { |
| 346 |
$return = ''; |
| 347 |
} |
| 348 |
|
| 349 |
return $return; |
| 350 |
} |
| 351 |
|
| 352 |
private function normalize_tag( $matches ) { |
| 353 |
return '<' . strtolower( $matches[1] ); |
| 354 |
} |
| 355 |
|
| 356 |
private function fopen( $filename, $mode = 'r' ) { |
| 357 |
if ( $this->has_gzip ) { |
| 358 |
return gzopen( $filename, $mode ); |
| 359 |
} |
| 360 |
|
| 361 |
return fopen( $filename, $mode ); |
| 362 |
} |
| 363 |
|
| 364 |
private function feof( $fp ) { |
| 365 |
if ( $this->has_gzip ) { |
| 366 |
return gzeof( $fp ); |
| 367 |
} |
| 368 |
|
| 369 |
return feof( $fp ); |
| 370 |
} |
| 371 |
|
| 372 |
private function fgets( $fp, $len = 8192 ) { |
| 373 |
if ( $this->has_gzip ) { |
| 374 |
return gzgets( $fp, $len ); |
| 375 |
} |
| 376 |
|
| 377 |
return fgets( $fp, $len ); |
| 378 |
} |
| 379 |
|
| 380 |
private function fclose( $fp ) { |
| 381 |
if ( $this->has_gzip ) { |
| 382 |
return gzclose( $fp ); |
| 383 |
} |
| 384 |
|
| 385 |
return fclose( $fp ); |
| 386 |
} |
| 387 |
|
| 388 |
public function __construct() { |
| 389 |
$this->has_gzip = is_callable( 'gzopen' ); |
| 390 |
} |
| 391 |
} |
| 392 |
|