← All changes
|
includes/Admin/Importer/Parsers/WXR_Parser_Regex.php
+69
-66
3.3.4
→
4.9.3
View file →
| @@ -1,5 +1,6 @@ | ||
| 1 | 1 | <?php |
| 2 | +// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- inherited WP Importer hook contract preserved for compat with external listeners. | |
| 2 | 3 | namespace WPDeveloper\BetterDocs\Admin\Importer\Parsers; |
| 3 | 4 | |
| 4 | 5 | use WP_Error; |
| 5 | 6 | |
| @@ -26,14 +27,14 @@ | ||
| 26 | 27 | * @var bool |
| 27 | 28 | */ |
| 28 | 29 | private $has_gzip; |
| 29 | 30 | |
| 30 | - private $authors = []; | |
| 31 | - private $posts = []; | |
| 32 | - private $categories = []; | |
| 33 | - private $tags = []; | |
| 34 | - private $terms = []; | |
| 35 | - private $base_url = ''; | |
| 31 | + private $authors = []; | |
| 32 | + private $posts = []; | |
| 33 | + private $categories = []; | |
| 34 | + private $tags = []; | |
| 35 | + private $terms = []; | |
| 36 | + private $base_url = ''; | |
| 36 | 37 | private $base_blog_url = ''; |
| 37 | 38 | |
| 38 | 39 | /** |
| 39 | 40 | * @param string $file |
| @@ -40,15 +41,15 @@ | ||
| 40 | 41 | * |
| 41 | 42 | * @return array|WP_Error |
| 42 | 43 | */ |
| 43 | 44 | public function parse( $file ) { |
| 44 | - $wxr_version = ''; | |
| 45 | + $wxr_version = ''; | |
| 45 | 46 | $in_multiline = false; |
| 46 | 47 | |
| 47 | 48 | $multiline_content = ''; |
| 48 | 49 | |
| 49 | 50 | $multiline_tags = [ |
| 50 | - 'item' => [ | |
| 51 | + 'item' => [ | |
| 51 | 52 | 'posts', |
| 52 | 53 | function ( $post ) { |
| 53 | 54 | return $this->process_post( $post ); |
| 54 | 55 | }, |
| @@ -58,15 +59,15 @@ | ||
| 58 | 59 | function ( $category ) { |
| 59 | 60 | return $this->process_category( $category ); |
| 60 | 61 | }, |
| 61 | 62 | ], |
| 62 | - 'wp:tag' => [ | |
| 63 | + 'wp:tag' => [ | |
| 63 | 64 | 'tags', |
| 64 | 65 | function ( $tag ) { |
| 65 | 66 | return $this->process_tag( $tag ); |
| 66 | 67 | }, |
| 67 | 68 | ], |
| 68 | - 'wp:term' => [ | |
| 69 | + 'wp:term' => [ | |
| 69 | 70 | 'terms', |
| 70 | 71 | function ( $term ) { |
| 71 | 72 | return $this->process_term( $term ); |
| 72 | 73 | }, |
| @@ -97,9 +98,9 @@ | ||
| 97 | 98 | } |
| 98 | 99 | |
| 99 | 100 | if ( false !== strpos( $importline, '<wp:author>' ) ) { |
| 100 | 101 | preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author ); |
| 101 | - $a = $this->process_author( $author[1] ); | |
| 102 | + $a = $this->process_author( $author[1] ); | |
| 102 | 103 | $this->authors[ $a['author_login'] ] = $a; |
| 103 | 104 | continue; |
| 104 | 105 | } |
| 105 | 106 | |
| @@ -117,9 +118,9 @@ | ||
| 117 | 118 | // Take note of any content after the opening tag. |
| 118 | 119 | $multiline_content = trim( substr( $importline, $pos + strlen( $tag ) + 2 ) ); |
| 119 | 120 | |
| 120 | 121 | // We don't want to have this line added to `$is_multiline` below. |
| 121 | - $importline = ''; | |
| 122 | + $importline = ''; | |
| 122 | 123 | $in_multiline = $tag; |
| 123 | 124 | |
| 124 | 125 | continue; |
| 125 | 126 | } |
| @@ -126,9 +127,9 @@ | ||
| 126 | 127 | |
| 127 | 128 | $pos = strpos( $importline, "</$tag>" ); |
| 128 | 129 | |
| 129 | 130 | if ( false !== $pos ) { |
| 130 | - $in_multiline = false; | |
| 131 | + $in_multiline = false; | |
| 131 | 132 | $multiline_content .= trim( substr( $importline, 0, $pos ) ); |
| 132 | 133 | |
| 133 | 134 | $this->{$handler[0]}[] = call_user_func( $handler[1], $multiline_content ); |
| 134 | 135 | } |
| @@ -146,25 +147,25 @@ | ||
| 146 | 147 | return new WP_Error( 'WXR_parse_error', esc_html__( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'betterdocs' ) ); |
| 147 | 148 | } |
| 148 | 149 | |
| 149 | 150 | 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, | |
| 151 | + 'authors' => $this->authors, | |
| 152 | + 'posts' => $this->posts, | |
| 153 | + 'categories' => $this->categories, | |
| 154 | + 'tags' => $this->tags, | |
| 155 | + 'terms' => $this->terms, | |
| 156 | + 'base_url' => $this->base_url, | |
| 156 | 157 | 'base_blog_url' => $this->base_blog_url, |
| 157 | - 'version' => $wxr_version, | |
| 158 | + 'version' => $wxr_version, | |
| 158 | 159 | ]; |
| 159 | 160 | } |
| 160 | 161 | |
| 161 | 162 | private function process_category( $category ) { |
| 162 | 163 | $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' ), | |
| 164 | + 'term_id' => $this->get_tag( $category, 'wp:term_id' ), | |
| 165 | + 'cat_name' => $this->get_tag( $category, 'wp:cat_name' ), | |
| 166 | + 'category_nicename' => $this->get_tag( $category, 'wp:category_nicename' ), | |
| 167 | + 'category_parent' => $this->get_tag( $category, 'wp:category_parent' ), | |
| 167 | 168 | 'category_description' => $this->get_tag( $category, 'wp:category_description' ), |
| 168 | 169 | ]; |
| 169 | 170 | |
| 170 | 171 | $term_meta = $this->process_meta( $category, 'wp:termmeta' ); |
| @@ -176,11 +177,11 @@ | ||
| 176 | 177 | } |
| 177 | 178 | |
| 178 | 179 | private function process_tag( $tag ) { |
| 179 | 180 | $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' ), | |
| 181 | + 'term_id' => $this->get_tag( $tag, 'wp:term_id' ), | |
| 182 | + 'tag_name' => $this->get_tag( $tag, 'wp:tag_name' ), | |
| 183 | + 'tag_slug' => $this->get_tag( $tag, 'wp:tag_slug' ), | |
| 183 | 184 | 'tag_description' => $this->get_tag( $tag, 'wp:tag_description' ), |
| 184 | 185 | ]; |
| 185 | 186 | |
| 186 | 187 | $term_meta = $this->process_meta( $tag, 'wp:termmeta' ); |
| @@ -192,13 +193,13 @@ | ||
| 192 | 193 | } |
| 193 | 194 | |
| 194 | 195 | private function process_term( $term ) { |
| 195 | 196 | $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' ), | |
| 197 | + 'term_id' => $this->get_tag( $term, 'wp:term_id' ), | |
| 198 | + 'term_taxonomy' => $this->get_tag( $term, 'wp:term_taxonomy' ), | |
| 199 | + 'slug' => $this->get_tag( $term, 'wp:term_slug' ), | |
| 200 | + 'term_parent' => $this->get_tag( $term, 'wp:term_parent' ), | |
| 201 | + 'term_name' => $this->get_tag( $term, 'wp:term_name' ), | |
| 201 | 202 | 'term_description' => $this->get_tag( $term, 'wp:term_description' ), |
| 202 | 203 | ]; |
| 203 | 204 | |
| 204 | 205 | $term_meta = $this->process_meta( $term, 'wp:termmeta' ); |
| @@ -219,9 +220,9 @@ | ||
| 219 | 220 | } |
| 220 | 221 | |
| 221 | 222 | foreach ( $meta[1] as $m ) { |
| 222 | 223 | $parsed_meta[] = [ |
| 223 | - 'key' => $this->get_tag( $m, 'wp:meta_key' ), | |
| 224 | + 'key' => $this->get_tag( $m, 'wp:meta_key' ), | |
| 224 | 225 | 'value' => $this->get_tag( $m, 'wp:meta_value' ), |
| 225 | 226 | ]; |
| 226 | 227 | } |
| 227 | 228 | |
| @@ -229,14 +230,14 @@ | ||
| 229 | 230 | } |
| 230 | 231 | |
| 231 | 232 | private function process_author( $a ) { |
| 232 | 233 | 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' ), | |
| 234 | + 'author_id' => $this->get_tag( $a, 'wp:author_id' ), | |
| 235 | + 'author_login' => $this->get_tag( $a, 'wp:author_login' ), | |
| 236 | + 'author_email' => $this->get_tag( $a, 'wp:author_email' ), | |
| 236 | 237 | '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' ), | |
| 238 | + 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ), | |
| 239 | + 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ), | |
| 239 | 240 | ]; |
| 240 | 241 | } |
| 241 | 242 | |
| 242 | 243 | private function process_post( $post ) { |
| @@ -243,23 +244,23 @@ | ||
| 243 | 244 | $normalize_tag_callback = function ( $matches ) { |
| 244 | 245 | return $this->normalize_tag( $matches ); |
| 245 | 246 | }; |
| 246 | 247 | |
| 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' ); | |
| 248 | + $post_id = $this->get_tag( $post, 'wp:post_id' ); | |
| 249 | + $post_title = $this->get_tag( $post, 'title' ); | |
| 250 | + $post_date = $this->get_tag( $post, 'wp:post_date' ); | |
| 251 | + $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); | |
| 251 | 252 | $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' ); | |
| 253 | + $ping_status = $this->get_tag( $post, 'wp:ping_status' ); | |
| 254 | + $status = $this->get_tag( $post, 'wp:status' ); | |
| 255 | + $post_name = $this->get_tag( $post, 'wp:post_name' ); | |
| 256 | + $post_parent = $this->get_tag( $post, 'wp:post_parent' ); | |
| 257 | + $menu_order = $this->get_tag( $post, 'wp:menu_order' ); | |
| 258 | + $post_type = $this->get_tag( $post, 'wp:post_type' ); | |
| 259 | + $post_password = $this->get_tag( $post, 'wp:post_password' ); | |
| 260 | + $is_sticky = $this->get_tag( $post, 'wp:is_sticky' ); | |
| 261 | + $guid = $this->get_tag( $post, 'guid' ); | |
| 262 | + $post_author = $this->get_tag( $post, 'dc:creator' ); | |
| 262 | 263 | |
| 263 | 264 | $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' ); |
| 264 | 265 | $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', $normalize_tag_callback, $post_excerpt ); |
| 265 | 266 | $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt ); |
| @@ -279,11 +280,11 @@ | ||
| 279 | 280 | |
| 280 | 281 | preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER ); |
| 281 | 282 | foreach ( $terms as $t ) { |
| 282 | 283 | $post_terms[] = [ |
| 283 | - 'slug' => $t[2], | |
| 284 | + 'slug' => $t[2], | |
| 284 | 285 | 'domain' => $t[1], |
| 285 | - 'name' => str_replace( [ '<![CDATA[', ']]>' ], '', $t[3] ), | |
| 286 | + 'name' => str_replace( [ '<![CDATA[', ']]>' ], '', $t[3] ), | |
| 286 | 287 | ]; |
| 287 | 288 | } |
| 288 | 289 | if ( ! empty( $post_terms ) ) { |
| 289 | 290 | $postdata['terms'] = $post_terms; |
| @@ -293,21 +294,21 @@ | ||
| 293 | 294 | $comments = $comments[1]; |
| 294 | 295 | if ( $comments ) { |
| 295 | 296 | foreach ( $comments as $comment ) { |
| 296 | 297 | $post_comments[] = [ |
| 297 | - 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ), | |
| 298 | - 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ), | |
| 298 | + 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ), | |
| 299 | + 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ), | |
| 299 | 300 | 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ), |
| 300 | - 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ), | |
| 301 | - 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ), | |
| 302 | - 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ), | |
| 303 | - 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ), | |
| 304 | - 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ), | |
| 305 | - 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ), | |
| 306 | - 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ), | |
| 307 | - 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ), | |
| 308 | - 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ), | |
| 309 | - 'commentmeta' => $this->process_meta( $comment, 'wp:commentmeta' ), | |
| 301 | + 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ), | |
| 302 | + 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ), | |
| 303 | + 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ), | |
| 304 | + 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ), | |
| 305 | + 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ), | |
| 306 | + 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ), | |
| 307 | + 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ), | |
| 308 | + 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ), | |
| 309 | + 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ), | |
| 310 | + 'commentmeta' => $this->process_meta( $comment, 'wp:commentmeta' ), | |
| 310 | 311 | ]; |
| 311 | 312 | } |
| 312 | 313 | } |
| 313 | 314 | if ( ! empty( $post_comments ) ) { |
| @@ -353,8 +354,9 @@ | ||
| 353 | 354 | if ( $this->has_gzip ) { |
| 354 | 355 | return gzopen( $filename, $mode ); |
| 355 | 356 | } |
| 356 | 357 | |
| 358 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- streaming read; WP_Filesystem cannot replace this. | |
| 357 | 359 | return fopen( $filename, $mode ); |
| 358 | 360 | } |
| 359 | 361 | |
| 360 | 362 | private function feof( $fp ) { |
| @@ -377,8 +379,9 @@ | ||
| 377 | 379 | if ( $this->has_gzip ) { |
| 378 | 380 | return gzclose( $fp ); |
| 379 | 381 | } |
| 380 | 382 | |
| 383 | + // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- streaming close; WP_Filesystem cannot replace this. | |
| 381 | 384 | return fclose( $fp ); |
| 382 | 385 | } |
| 383 | 386 | |
| 384 | 387 | public function __construct() { |