@@ -10,6 +10,10 @@ discard block |
||
| 10 | 10 | * WordPress Importer class for managing parsing of WXR files. |
| 11 | 11 | */ |
| 12 | 12 | class WXR_Parser { |
| 13 | + |
|
| 14 | + /** |
|
| 15 | + * @param string $file |
|
| 16 | + */ |
|
| 13 | 17 | function parse( $file ) { |
| 14 | 18 | // Attempt to use proper XML parsers first |
| 15 | 19 | if ( extension_loaded( 'simplexml' ) ) { |
@@ -525,6 +529,9 @@ discard block |
||
| 525 | 529 | ); |
| 526 | 530 | } |
| 527 | 531 | |
| 532 | + /** |
|
| 533 | + * @param string $tag |
|
| 534 | + */ |
|
| 528 | 535 | function get_tag( $string, $tag ) { |
| 529 | 536 | preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return ); |
| 530 | 537 | if ( isset( $return[1] ) ) { |
@@ -547,6 +554,9 @@ discard block |
||
| 547 | 554 | return $return; |
| 548 | 555 | } |
| 549 | 556 | |
| 557 | + /** |
|
| 558 | + * @param string $c |
|
| 559 | + */ |
|
| 550 | 560 | function process_category( $c ) { |
| 551 | 561 | return array( |
| 552 | 562 | 'term_id' => $this->get_tag( $c, 'wp:term_id' ), |
@@ -557,6 +567,9 @@ discard block |
||
| 557 | 567 | ); |
| 558 | 568 | } |
| 559 | 569 | |
| 570 | + /** |
|
| 571 | + * @param string $t |
|
| 572 | + */ |
|
| 560 | 573 | function process_tag( $t ) { |
| 561 | 574 | return array( |
| 562 | 575 | 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
@@ -566,6 +579,9 @@ discard block |
||
| 566 | 579 | ); |
| 567 | 580 | } |
| 568 | 581 | |
| 582 | + /** |
|
| 583 | + * @param string $t |
|
| 584 | + */ |
|
| 569 | 585 | function process_term( $t ) { |
| 570 | 586 | return array( |
| 571 | 587 | 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
@@ -577,6 +593,9 @@ discard block |
||
| 577 | 593 | ); |
| 578 | 594 | } |
| 579 | 595 | |
| 596 | + /** |
|
| 597 | + * @param string $a |
|
| 598 | + */ |
|
| 580 | 599 | function process_author( $a ) { |
| 581 | 600 | return array( |
| 582 | 601 | 'author_id' => $this->get_tag( $a, 'wp:author_id' ), |
@@ -588,6 +607,9 @@ discard block |
||
| 588 | 607 | ); |
| 589 | 608 | } |
| 590 | 609 | |
| 610 | + /** |
|
| 611 | + * @param string $post |
|
| 612 | + */ |
|
| 591 | 613 | function process_post( $post ) { |
| 592 | 614 | $post_id = $this->get_tag( $post, 'wp:post_id' ); |
| 593 | 615 | $post_title = $this->get_tag( $post, 'title' ); |
@@ -10,714 +10,714 @@ |
||
| 10 | 10 | * WordPress Importer class for managing parsing of WXR files. |
| 11 | 11 | */ |
| 12 | 12 | class WXR_Parser { |
| 13 | - function parse( $file ) { |
|
| 14 | - // Attempt to use proper XML parsers first |
|
| 15 | - if ( extension_loaded( 'simplexml' ) ) { |
|
| 16 | - $parser = new WXR_Parser_SimpleXML; |
|
| 17 | - $result = $parser->parse( $file ); |
|
| 18 | - |
|
| 19 | - // If SimpleXML succeeds or this is an invalid WXR file then return the results |
|
| 20 | - if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() ) { |
|
| 21 | - return $result; |
|
| 22 | - } |
|
| 23 | - } else if ( extension_loaded( 'xml' ) ) { |
|
| 24 | - $parser = new WXR_Parser_XML; |
|
| 25 | - $result = $parser->parse( $file ); |
|
| 26 | - |
|
| 27 | - // If XMLParser succeeds or this is an invalid WXR file then return the results |
|
| 28 | - if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() ) { |
|
| 29 | - return $result; |
|
| 30 | - } |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - // We have a malformed XML file, so display the error and fallthrough to regex |
|
| 34 | - if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 35 | - echo '<pre>'; |
|
| 36 | - if ( 'SimpleXML_parse_error' == $result->get_error_code() ) { |
|
| 37 | - foreach ( $result->get_error_data() as $error ) { |
|
| 38 | - echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n"; |
|
| 39 | - } |
|
| 40 | - } else if ( 'XML_parse_error' == $result->get_error_code() ) { |
|
| 41 | - $error = $result->get_error_data(); |
|
| 42 | - echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] ); |
|
| 43 | - } |
|
| 44 | - echo '</pre>'; |
|
| 45 | - echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />'; |
|
| 46 | - echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>'; |
|
| 47 | - } |
|
| 48 | - |
|
| 49 | - // use regular expressions if nothing else available or this is bad XML |
|
| 50 | - $parser = new WXR_Parser_Regex; |
|
| 51 | - return $parser->parse( $file ); |
|
| 52 | - } |
|
| 13 | + function parse( $file ) { |
|
| 14 | + // Attempt to use proper XML parsers first |
|
| 15 | + if ( extension_loaded( 'simplexml' ) ) { |
|
| 16 | + $parser = new WXR_Parser_SimpleXML; |
|
| 17 | + $result = $parser->parse( $file ); |
|
| 18 | + |
|
| 19 | + // If SimpleXML succeeds or this is an invalid WXR file then return the results |
|
| 20 | + if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() ) { |
|
| 21 | + return $result; |
|
| 22 | + } |
|
| 23 | + } else if ( extension_loaded( 'xml' ) ) { |
|
| 24 | + $parser = new WXR_Parser_XML; |
|
| 25 | + $result = $parser->parse( $file ); |
|
| 26 | + |
|
| 27 | + // If XMLParser succeeds or this is an invalid WXR file then return the results |
|
| 28 | + if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() ) { |
|
| 29 | + return $result; |
|
| 30 | + } |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + // We have a malformed XML file, so display the error and fallthrough to regex |
|
| 34 | + if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 35 | + echo '<pre>'; |
|
| 36 | + if ( 'SimpleXML_parse_error' == $result->get_error_code() ) { |
|
| 37 | + foreach ( $result->get_error_data() as $error ) { |
|
| 38 | + echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n"; |
|
| 39 | + } |
|
| 40 | + } else if ( 'XML_parse_error' == $result->get_error_code() ) { |
|
| 41 | + $error = $result->get_error_data(); |
|
| 42 | + echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] ); |
|
| 43 | + } |
|
| 44 | + echo '</pre>'; |
|
| 45 | + echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />'; |
|
| 46 | + echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>'; |
|
| 47 | + } |
|
| 48 | + |
|
| 49 | + // use regular expressions if nothing else available or this is bad XML |
|
| 50 | + $parser = new WXR_Parser_Regex; |
|
| 51 | + return $parser->parse( $file ); |
|
| 52 | + } |
|
| 53 | 53 | } |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | 56 | * WXR Parser that makes use of the SimpleXML PHP extension. |
| 57 | 57 | */ |
| 58 | 58 | class WXR_Parser_SimpleXML { |
| 59 | - function parse( $file ) { |
|
| 60 | - $authors = $posts = $categories = $tags = $terms = array(); |
|
| 61 | - |
|
| 62 | - $internal_errors = libxml_use_internal_errors(true); |
|
| 63 | - |
|
| 64 | - $dom = new DOMDocument; |
|
| 65 | - $old_value = null; |
|
| 66 | - if ( function_exists( 'libxml_disable_entity_loader' ) ) { |
|
| 67 | - $old_value = libxml_disable_entity_loader( true ); |
|
| 68 | - } |
|
| 69 | - $success = $dom->loadXML( file_get_contents( $file ) ); |
|
| 70 | - if ( ! is_null( $old_value ) ) { |
|
| 71 | - libxml_disable_entity_loader( $old_value ); |
|
| 72 | - } |
|
| 73 | - |
|
| 74 | - if ( ! $success || isset( $dom->doctype ) ) { |
|
| 75 | - return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() ); |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - $xml = simplexml_import_dom( $dom ); |
|
| 79 | - unset( $dom ); |
|
| 80 | - |
|
| 81 | - // halt if loading produces an error |
|
| 82 | - if ( ! $xml ) { |
|
| 83 | - return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() ); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version'); |
|
| 87 | - if ( ! $wxr_version ) { |
|
| 88 | - return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - $wxr_version = (string) trim( $wxr_version[0] ); |
|
| 92 | - // confirm that we are dealing with the correct file format |
|
| 93 | - if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) ) { |
|
| 94 | - return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $base_url = $xml->xpath('/rss/channel/wp:base_site_url'); |
|
| 98 | - $base_url = (string) trim( $base_url[0] ); |
|
| 99 | - |
|
| 100 | - $namespaces = $xml->getDocNamespaces(); |
|
| 101 | - if ( ! isset( $namespaces['wp'] ) ) { |
|
| 102 | - $namespaces['wp'] = 'http://wordpress.org/export/1.1/'; |
|
| 103 | - } |
|
| 104 | - if ( ! isset( $namespaces['excerpt'] ) ) { |
|
| 105 | - $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/'; |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // grab authors |
|
| 109 | - foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) { |
|
| 110 | - $a = $author_arr->children( $namespaces['wp'] ); |
|
| 111 | - $login = (string) $a->author_login; |
|
| 112 | - $authors[$login] = array( |
|
| 113 | - 'author_id' => (int) $a->author_id, |
|
| 114 | - 'author_login' => $login, |
|
| 115 | - 'author_email' => (string) $a->author_email, |
|
| 116 | - 'author_display_name' => (string) $a->author_display_name, |
|
| 117 | - 'author_first_name' => (string) $a->author_first_name, |
|
| 118 | - 'author_last_name' => (string) $a->author_last_name |
|
| 119 | - ); |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - // grab cats, tags and terms |
|
| 123 | - foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) { |
|
| 124 | - $t = $term_arr->children( $namespaces['wp'] ); |
|
| 125 | - $category = array( |
|
| 126 | - 'term_id' => (int) $t->term_id, |
|
| 127 | - 'category_nicename' => (string) $t->category_nicename, |
|
| 128 | - 'category_parent' => (string) $t->category_parent, |
|
| 129 | - 'cat_name' => (string) $t->cat_name, |
|
| 130 | - 'category_description' => (string) $t->category_description |
|
| 131 | - ); |
|
| 132 | - |
|
| 133 | - foreach ( $t->termmeta as $meta ) { |
|
| 134 | - $category['termmeta'][] = array( |
|
| 135 | - 'key' => (string) $meta->meta_key, |
|
| 136 | - 'value' => (string) $meta->meta_value |
|
| 137 | - ); |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - $categories[] = $category; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) { |
|
| 144 | - $t = $term_arr->children( $namespaces['wp'] ); |
|
| 145 | - $tag = array( |
|
| 146 | - 'term_id' => (int) $t->term_id, |
|
| 147 | - 'tag_slug' => (string) $t->tag_slug, |
|
| 148 | - 'tag_name' => (string) $t->tag_name, |
|
| 149 | - 'tag_description' => (string) $t->tag_description |
|
| 150 | - ); |
|
| 151 | - |
|
| 152 | - foreach ( $t->termmeta as $meta ) { |
|
| 153 | - $tag['termmeta'][] = array( |
|
| 154 | - 'key' => (string) $meta->meta_key, |
|
| 155 | - 'value' => (string) $meta->meta_value |
|
| 156 | - ); |
|
| 157 | - } |
|
| 158 | - |
|
| 159 | - $tags[] = $tag; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) { |
|
| 163 | - $t = $term_arr->children( $namespaces['wp'] ); |
|
| 164 | - $term = array( |
|
| 165 | - 'term_id' => (int) $t->term_id, |
|
| 166 | - 'term_taxonomy' => (string) $t->term_taxonomy, |
|
| 167 | - 'slug' => (string) $t->term_slug, |
|
| 168 | - 'term_parent' => (string) $t->term_parent, |
|
| 169 | - 'term_name' => (string) $t->term_name, |
|
| 170 | - 'term_description' => (string) $t->term_description |
|
| 171 | - ); |
|
| 172 | - |
|
| 173 | - foreach ( $t->termmeta as $meta ) { |
|
| 174 | - $term['termmeta'][] = array( |
|
| 175 | - 'key' => (string) $meta->meta_key, |
|
| 176 | - 'value' => (string) $meta->meta_value |
|
| 177 | - ); |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - $terms[] = $term; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - // grab posts |
|
| 184 | - foreach ( $xml->channel->item as $item ) { |
|
| 185 | - $post = array( |
|
| 186 | - 'post_title' => (string) $item->title, |
|
| 187 | - 'guid' => (string) $item->guid, |
|
| 188 | - ); |
|
| 189 | - |
|
| 190 | - $dc = $item->children( 'http://purl.org/dc/elements/1.1/' ); |
|
| 191 | - $post['post_author'] = (string) $dc->creator; |
|
| 192 | - |
|
| 193 | - $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' ); |
|
| 194 | - $excerpt = $item->children( $namespaces['excerpt'] ); |
|
| 195 | - $post['post_content'] = (string) $content->encoded; |
|
| 196 | - $post['post_excerpt'] = (string) $excerpt->encoded; |
|
| 197 | - |
|
| 198 | - $wp = $item->children( $namespaces['wp'] ); |
|
| 199 | - $post['post_id'] = (int) $wp->post_id; |
|
| 200 | - $post['post_date'] = (string) $wp->post_date; |
|
| 201 | - $post['post_date_gmt'] = (string) $wp->post_date_gmt; |
|
| 202 | - $post['comment_status'] = (string) $wp->comment_status; |
|
| 203 | - $post['ping_status'] = (string) $wp->ping_status; |
|
| 204 | - $post['post_name'] = (string) $wp->post_name; |
|
| 205 | - $post['status'] = (string) $wp->status; |
|
| 206 | - $post['post_parent'] = (int) $wp->post_parent; |
|
| 207 | - $post['menu_order'] = (int) $wp->menu_order; |
|
| 208 | - $post['post_type'] = (string) $wp->post_type; |
|
| 209 | - $post['post_password'] = (string) $wp->post_password; |
|
| 210 | - $post['is_sticky'] = (int) $wp->is_sticky; |
|
| 211 | - |
|
| 212 | - if ( isset($wp->attachment_url) ) { |
|
| 213 | - $post['attachment_url'] = (string) $wp->attachment_url; |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - foreach ( $item->category as $c ) { |
|
| 217 | - $att = $c->attributes(); |
|
| 218 | - if ( isset( $att['nicename'] ) ) { |
|
| 219 | - $post['terms'][] = array( |
|
| 220 | - 'name' => (string) $c, |
|
| 221 | - 'slug' => (string) $att['nicename'], |
|
| 222 | - 'domain' => (string) $att['domain'] |
|
| 223 | - ); |
|
| 224 | - } |
|
| 225 | - } |
|
| 226 | - |
|
| 227 | - foreach ( $wp->postmeta as $meta ) { |
|
| 228 | - $post['postmeta'][] = array( |
|
| 229 | - 'key' => (string) $meta->meta_key, |
|
| 230 | - 'value' => (string) $meta->meta_value |
|
| 231 | - ); |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - foreach ( $wp->comment as $comment ) { |
|
| 235 | - $meta = array(); |
|
| 236 | - if ( isset( $comment->commentmeta ) ) { |
|
| 237 | - foreach ( $comment->commentmeta as $m ) { |
|
| 238 | - $meta[] = array( |
|
| 239 | - 'key' => (string) $m->meta_key, |
|
| 240 | - 'value' => (string) $m->meta_value |
|
| 241 | - ); |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - $post['comments'][] = array( |
|
| 246 | - 'comment_id' => (int) $comment->comment_id, |
|
| 247 | - 'comment_author' => (string) $comment->comment_author, |
|
| 248 | - 'comment_author_email' => (string) $comment->comment_author_email, |
|
| 249 | - 'comment_author_IP' => (string) $comment->comment_author_IP, |
|
| 250 | - 'comment_author_url' => (string) $comment->comment_author_url, |
|
| 251 | - 'comment_date' => (string) $comment->comment_date, |
|
| 252 | - 'comment_date_gmt' => (string) $comment->comment_date_gmt, |
|
| 253 | - 'comment_content' => (string) $comment->comment_content, |
|
| 254 | - 'comment_approved' => (string) $comment->comment_approved, |
|
| 255 | - 'comment_type' => (string) $comment->comment_type, |
|
| 256 | - 'comment_parent' => (string) $comment->comment_parent, |
|
| 257 | - 'comment_user_id' => (int) $comment->comment_user_id, |
|
| 258 | - 'commentmeta' => $meta, |
|
| 259 | - ); |
|
| 260 | - } |
|
| 261 | - |
|
| 262 | - $posts[] = $post; |
|
| 263 | - } |
|
| 264 | - |
|
| 265 | - return array( |
|
| 266 | - 'authors' => $authors, |
|
| 267 | - 'posts' => $posts, |
|
| 268 | - 'categories' => $categories, |
|
| 269 | - 'tags' => $tags, |
|
| 270 | - 'terms' => $terms, |
|
| 271 | - 'base_url' => $base_url, |
|
| 272 | - 'version' => $wxr_version |
|
| 273 | - ); |
|
| 274 | - } |
|
| 59 | + function parse( $file ) { |
|
| 60 | + $authors = $posts = $categories = $tags = $terms = array(); |
|
| 61 | + |
|
| 62 | + $internal_errors = libxml_use_internal_errors(true); |
|
| 63 | + |
|
| 64 | + $dom = new DOMDocument; |
|
| 65 | + $old_value = null; |
|
| 66 | + if ( function_exists( 'libxml_disable_entity_loader' ) ) { |
|
| 67 | + $old_value = libxml_disable_entity_loader( true ); |
|
| 68 | + } |
|
| 69 | + $success = $dom->loadXML( file_get_contents( $file ) ); |
|
| 70 | + if ( ! is_null( $old_value ) ) { |
|
| 71 | + libxml_disable_entity_loader( $old_value ); |
|
| 72 | + } |
|
| 73 | + |
|
| 74 | + if ( ! $success || isset( $dom->doctype ) ) { |
|
| 75 | + return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() ); |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + $xml = simplexml_import_dom( $dom ); |
|
| 79 | + unset( $dom ); |
|
| 80 | + |
|
| 81 | + // halt if loading produces an error |
|
| 82 | + if ( ! $xml ) { |
|
| 83 | + return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() ); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version'); |
|
| 87 | + if ( ! $wxr_version ) { |
|
| 88 | + return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + $wxr_version = (string) trim( $wxr_version[0] ); |
|
| 92 | + // confirm that we are dealing with the correct file format |
|
| 93 | + if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) ) { |
|
| 94 | + return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $base_url = $xml->xpath('/rss/channel/wp:base_site_url'); |
|
| 98 | + $base_url = (string) trim( $base_url[0] ); |
|
| 99 | + |
|
| 100 | + $namespaces = $xml->getDocNamespaces(); |
|
| 101 | + if ( ! isset( $namespaces['wp'] ) ) { |
|
| 102 | + $namespaces['wp'] = 'http://wordpress.org/export/1.1/'; |
|
| 103 | + } |
|
| 104 | + if ( ! isset( $namespaces['excerpt'] ) ) { |
|
| 105 | + $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/'; |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // grab authors |
|
| 109 | + foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) { |
|
| 110 | + $a = $author_arr->children( $namespaces['wp'] ); |
|
| 111 | + $login = (string) $a->author_login; |
|
| 112 | + $authors[$login] = array( |
|
| 113 | + 'author_id' => (int) $a->author_id, |
|
| 114 | + 'author_login' => $login, |
|
| 115 | + 'author_email' => (string) $a->author_email, |
|
| 116 | + 'author_display_name' => (string) $a->author_display_name, |
|
| 117 | + 'author_first_name' => (string) $a->author_first_name, |
|
| 118 | + 'author_last_name' => (string) $a->author_last_name |
|
| 119 | + ); |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + // grab cats, tags and terms |
|
| 123 | + foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) { |
|
| 124 | + $t = $term_arr->children( $namespaces['wp'] ); |
|
| 125 | + $category = array( |
|
| 126 | + 'term_id' => (int) $t->term_id, |
|
| 127 | + 'category_nicename' => (string) $t->category_nicename, |
|
| 128 | + 'category_parent' => (string) $t->category_parent, |
|
| 129 | + 'cat_name' => (string) $t->cat_name, |
|
| 130 | + 'category_description' => (string) $t->category_description |
|
| 131 | + ); |
|
| 132 | + |
|
| 133 | + foreach ( $t->termmeta as $meta ) { |
|
| 134 | + $category['termmeta'][] = array( |
|
| 135 | + 'key' => (string) $meta->meta_key, |
|
| 136 | + 'value' => (string) $meta->meta_value |
|
| 137 | + ); |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + $categories[] = $category; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) { |
|
| 144 | + $t = $term_arr->children( $namespaces['wp'] ); |
|
| 145 | + $tag = array( |
|
| 146 | + 'term_id' => (int) $t->term_id, |
|
| 147 | + 'tag_slug' => (string) $t->tag_slug, |
|
| 148 | + 'tag_name' => (string) $t->tag_name, |
|
| 149 | + 'tag_description' => (string) $t->tag_description |
|
| 150 | + ); |
|
| 151 | + |
|
| 152 | + foreach ( $t->termmeta as $meta ) { |
|
| 153 | + $tag['termmeta'][] = array( |
|
| 154 | + 'key' => (string) $meta->meta_key, |
|
| 155 | + 'value' => (string) $meta->meta_value |
|
| 156 | + ); |
|
| 157 | + } |
|
| 158 | + |
|
| 159 | + $tags[] = $tag; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) { |
|
| 163 | + $t = $term_arr->children( $namespaces['wp'] ); |
|
| 164 | + $term = array( |
|
| 165 | + 'term_id' => (int) $t->term_id, |
|
| 166 | + 'term_taxonomy' => (string) $t->term_taxonomy, |
|
| 167 | + 'slug' => (string) $t->term_slug, |
|
| 168 | + 'term_parent' => (string) $t->term_parent, |
|
| 169 | + 'term_name' => (string) $t->term_name, |
|
| 170 | + 'term_description' => (string) $t->term_description |
|
| 171 | + ); |
|
| 172 | + |
|
| 173 | + foreach ( $t->termmeta as $meta ) { |
|
| 174 | + $term['termmeta'][] = array( |
|
| 175 | + 'key' => (string) $meta->meta_key, |
|
| 176 | + 'value' => (string) $meta->meta_value |
|
| 177 | + ); |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + $terms[] = $term; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + // grab posts |
|
| 184 | + foreach ( $xml->channel->item as $item ) { |
|
| 185 | + $post = array( |
|
| 186 | + 'post_title' => (string) $item->title, |
|
| 187 | + 'guid' => (string) $item->guid, |
|
| 188 | + ); |
|
| 189 | + |
|
| 190 | + $dc = $item->children( 'http://purl.org/dc/elements/1.1/' ); |
|
| 191 | + $post['post_author'] = (string) $dc->creator; |
|
| 192 | + |
|
| 193 | + $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' ); |
|
| 194 | + $excerpt = $item->children( $namespaces['excerpt'] ); |
|
| 195 | + $post['post_content'] = (string) $content->encoded; |
|
| 196 | + $post['post_excerpt'] = (string) $excerpt->encoded; |
|
| 197 | + |
|
| 198 | + $wp = $item->children( $namespaces['wp'] ); |
|
| 199 | + $post['post_id'] = (int) $wp->post_id; |
|
| 200 | + $post['post_date'] = (string) $wp->post_date; |
|
| 201 | + $post['post_date_gmt'] = (string) $wp->post_date_gmt; |
|
| 202 | + $post['comment_status'] = (string) $wp->comment_status; |
|
| 203 | + $post['ping_status'] = (string) $wp->ping_status; |
|
| 204 | + $post['post_name'] = (string) $wp->post_name; |
|
| 205 | + $post['status'] = (string) $wp->status; |
|
| 206 | + $post['post_parent'] = (int) $wp->post_parent; |
|
| 207 | + $post['menu_order'] = (int) $wp->menu_order; |
|
| 208 | + $post['post_type'] = (string) $wp->post_type; |
|
| 209 | + $post['post_password'] = (string) $wp->post_password; |
|
| 210 | + $post['is_sticky'] = (int) $wp->is_sticky; |
|
| 211 | + |
|
| 212 | + if ( isset($wp->attachment_url) ) { |
|
| 213 | + $post['attachment_url'] = (string) $wp->attachment_url; |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + foreach ( $item->category as $c ) { |
|
| 217 | + $att = $c->attributes(); |
|
| 218 | + if ( isset( $att['nicename'] ) ) { |
|
| 219 | + $post['terms'][] = array( |
|
| 220 | + 'name' => (string) $c, |
|
| 221 | + 'slug' => (string) $att['nicename'], |
|
| 222 | + 'domain' => (string) $att['domain'] |
|
| 223 | + ); |
|
| 224 | + } |
|
| 225 | + } |
|
| 226 | + |
|
| 227 | + foreach ( $wp->postmeta as $meta ) { |
|
| 228 | + $post['postmeta'][] = array( |
|
| 229 | + 'key' => (string) $meta->meta_key, |
|
| 230 | + 'value' => (string) $meta->meta_value |
|
| 231 | + ); |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + foreach ( $wp->comment as $comment ) { |
|
| 235 | + $meta = array(); |
|
| 236 | + if ( isset( $comment->commentmeta ) ) { |
|
| 237 | + foreach ( $comment->commentmeta as $m ) { |
|
| 238 | + $meta[] = array( |
|
| 239 | + 'key' => (string) $m->meta_key, |
|
| 240 | + 'value' => (string) $m->meta_value |
|
| 241 | + ); |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + $post['comments'][] = array( |
|
| 246 | + 'comment_id' => (int) $comment->comment_id, |
|
| 247 | + 'comment_author' => (string) $comment->comment_author, |
|
| 248 | + 'comment_author_email' => (string) $comment->comment_author_email, |
|
| 249 | + 'comment_author_IP' => (string) $comment->comment_author_IP, |
|
| 250 | + 'comment_author_url' => (string) $comment->comment_author_url, |
|
| 251 | + 'comment_date' => (string) $comment->comment_date, |
|
| 252 | + 'comment_date_gmt' => (string) $comment->comment_date_gmt, |
|
| 253 | + 'comment_content' => (string) $comment->comment_content, |
|
| 254 | + 'comment_approved' => (string) $comment->comment_approved, |
|
| 255 | + 'comment_type' => (string) $comment->comment_type, |
|
| 256 | + 'comment_parent' => (string) $comment->comment_parent, |
|
| 257 | + 'comment_user_id' => (int) $comment->comment_user_id, |
|
| 258 | + 'commentmeta' => $meta, |
|
| 259 | + ); |
|
| 260 | + } |
|
| 261 | + |
|
| 262 | + $posts[] = $post; |
|
| 263 | + } |
|
| 264 | + |
|
| 265 | + return array( |
|
| 266 | + 'authors' => $authors, |
|
| 267 | + 'posts' => $posts, |
|
| 268 | + 'categories' => $categories, |
|
| 269 | + 'tags' => $tags, |
|
| 270 | + 'terms' => $terms, |
|
| 271 | + 'base_url' => $base_url, |
|
| 272 | + 'version' => $wxr_version |
|
| 273 | + ); |
|
| 274 | + } |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | /** |
| 278 | 278 | * WXR Parser that makes use of the XML Parser PHP extension. |
| 279 | 279 | */ |
| 280 | 280 | class WXR_Parser_XML { |
| 281 | - var $wp_tags = array( |
|
| 282 | - 'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url', |
|
| 283 | - 'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password', |
|
| 284 | - 'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description', |
|
| 285 | - 'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent', |
|
| 286 | - 'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name', |
|
| 287 | - 'wp:author_first_name', 'wp:author_last_name', |
|
| 288 | - ); |
|
| 289 | - var $wp_sub_tags = array( |
|
| 290 | - 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url', |
|
| 291 | - 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content', |
|
| 292 | - 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id', |
|
| 293 | - ); |
|
| 294 | - |
|
| 295 | - function parse( $file ) { |
|
| 296 | - $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false; |
|
| 297 | - $this->authors = $this->posts = $this->term = $this->category = $this->tag = array(); |
|
| 298 | - |
|
| 299 | - $xml = xml_parser_create( 'UTF-8' ); |
|
| 300 | - xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 ); |
|
| 301 | - xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 ); |
|
| 302 | - xml_set_object( $xml, $this ); |
|
| 303 | - xml_set_character_data_handler( $xml, 'cdata' ); |
|
| 304 | - xml_set_element_handler( $xml, 'tag_open', 'tag_close' ); |
|
| 305 | - |
|
| 306 | - if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) { |
|
| 307 | - $current_line = xml_get_current_line_number( $xml ); |
|
| 308 | - $current_column = xml_get_current_column_number( $xml ); |
|
| 309 | - $error_code = xml_get_error_code( $xml ); |
|
| 310 | - $error_string = xml_error_string( $error_code ); |
|
| 311 | - return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) ); |
|
| 312 | - } |
|
| 313 | - xml_parser_free( $xml ); |
|
| 314 | - |
|
| 315 | - if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) ) { |
|
| 316 | - return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 317 | - } |
|
| 318 | - |
|
| 319 | - return array( |
|
| 320 | - 'authors' => $this->authors, |
|
| 321 | - 'posts' => $this->posts, |
|
| 322 | - 'categories' => $this->category, |
|
| 323 | - 'tags' => $this->tag, |
|
| 324 | - 'terms' => $this->term, |
|
| 325 | - 'base_url' => $this->base_url, |
|
| 326 | - 'version' => $this->wxr_version |
|
| 327 | - ); |
|
| 328 | - } |
|
| 329 | - |
|
| 330 | - function tag_open( $parse, $tag, $attr ) { |
|
| 331 | - if ( in_array( $tag, $this->wp_tags ) ) { |
|
| 332 | - $this->in_tag = substr( $tag, 3 ); |
|
| 333 | - return; |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - if ( in_array( $tag, $this->wp_sub_tags ) ) { |
|
| 337 | - $this->in_sub_tag = substr( $tag, 3 ); |
|
| 338 | - return; |
|
| 339 | - } |
|
| 340 | - |
|
| 341 | - switch ( $tag ) { |
|
| 342 | - case 'category': |
|
| 343 | - if ( isset($attr['domain'], $attr['nicename']) ) { |
|
| 344 | - $this->sub_data['domain'] = $attr['domain']; |
|
| 345 | - $this->sub_data['slug'] = $attr['nicename']; |
|
| 346 | - } |
|
| 347 | - break; |
|
| 348 | - case 'item': $this->in_post = true; |
|
| 349 | - case 'title': if ( $this->in_post ) { |
|
| 350 | - $this->in_tag = 'post_title'; |
|
| 351 | - } |
|
| 352 | - break; |
|
| 353 | - case 'guid': $this->in_tag = 'guid'; break; |
|
| 354 | - case 'dc:creator': $this->in_tag = 'post_author'; break; |
|
| 355 | - case 'content:encoded': $this->in_tag = 'post_content'; break; |
|
| 356 | - case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break; |
|
| 357 | - |
|
| 358 | - case 'wp:term_slug': $this->in_tag = 'slug'; break; |
|
| 359 | - case 'wp:meta_key': $this->in_sub_tag = 'key'; break; |
|
| 360 | - case 'wp:meta_value': $this->in_sub_tag = 'value'; break; |
|
| 361 | - } |
|
| 362 | - } |
|
| 363 | - |
|
| 364 | - function cdata( $parser, $cdata ) { |
|
| 365 | - if ( ! trim( $cdata ) ) { |
|
| 366 | - return; |
|
| 367 | - } |
|
| 368 | - |
|
| 369 | - if ( false !== $this->in_tag || false !== $this->in_sub_tag ) { |
|
| 370 | - $this->cdata .= $cdata; |
|
| 371 | - } else { |
|
| 372 | - $this->cdata .= trim( $cdata ); |
|
| 373 | - } |
|
| 374 | - } |
|
| 375 | - |
|
| 376 | - function tag_close( $parser, $tag ) { |
|
| 377 | - switch ( $tag ) { |
|
| 378 | - case 'wp:comment': |
|
| 379 | - unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data |
|
| 380 | - if ( ! empty( $this->sub_data ) ) { |
|
| 381 | - $this->data['comments'][] = $this->sub_data; |
|
| 382 | - } |
|
| 383 | - $this->sub_data = false; |
|
| 384 | - break; |
|
| 385 | - case 'wp:commentmeta': |
|
| 386 | - $this->sub_data['commentmeta'][] = array( |
|
| 387 | - 'key' => $this->sub_data['key'], |
|
| 388 | - 'value' => $this->sub_data['value'] |
|
| 389 | - ); |
|
| 390 | - break; |
|
| 391 | - case 'category': |
|
| 392 | - if ( ! empty( $this->sub_data ) ) { |
|
| 393 | - $this->sub_data['name'] = $this->cdata; |
|
| 394 | - $this->data['terms'][] = $this->sub_data; |
|
| 395 | - } |
|
| 396 | - $this->sub_data = false; |
|
| 397 | - break; |
|
| 398 | - case 'wp:postmeta': |
|
| 399 | - if ( ! empty( $this->sub_data ) ) { |
|
| 400 | - $this->data['postmeta'][] = $this->sub_data; |
|
| 401 | - } |
|
| 402 | - $this->sub_data = false; |
|
| 403 | - break; |
|
| 404 | - case 'item': |
|
| 405 | - $this->posts[] = $this->data; |
|
| 406 | - $this->data = false; |
|
| 407 | - break; |
|
| 408 | - case 'wp:category': |
|
| 409 | - case 'wp:tag': |
|
| 410 | - case 'wp:term': |
|
| 411 | - $n = substr( $tag, 3 ); |
|
| 412 | - array_push( $this->$n, $this->data ); |
|
| 413 | - $this->data = false; |
|
| 414 | - break; |
|
| 415 | - case 'wp:author': |
|
| 416 | - if ( ! empty($this->data['author_login']) ) { |
|
| 417 | - $this->authors[$this->data['author_login']] = $this->data; |
|
| 418 | - } |
|
| 419 | - $this->data = false; |
|
| 420 | - break; |
|
| 421 | - case 'wp:base_site_url': |
|
| 422 | - $this->base_url = $this->cdata; |
|
| 423 | - break; |
|
| 424 | - case 'wp:wxr_version': |
|
| 425 | - $this->wxr_version = $this->cdata; |
|
| 426 | - break; |
|
| 427 | - |
|
| 428 | - default: |
|
| 429 | - if ( $this->in_sub_tag ) { |
|
| 430 | - $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
| 431 | - $this->in_sub_tag = false; |
|
| 432 | - } else if ( $this->in_tag ) { |
|
| 433 | - $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
| 434 | - $this->in_tag = false; |
|
| 435 | - } |
|
| 436 | - } |
|
| 437 | - |
|
| 438 | - $this->cdata = false; |
|
| 439 | - } |
|
| 281 | + var $wp_tags = array( |
|
| 282 | + 'wp:post_id', 'wp:post_date', 'wp:post_date_gmt', 'wp:comment_status', 'wp:ping_status', 'wp:attachment_url', |
|
| 283 | + 'wp:status', 'wp:post_name', 'wp:post_parent', 'wp:menu_order', 'wp:post_type', 'wp:post_password', |
|
| 284 | + 'wp:is_sticky', 'wp:term_id', 'wp:category_nicename', 'wp:category_parent', 'wp:cat_name', 'wp:category_description', |
|
| 285 | + 'wp:tag_slug', 'wp:tag_name', 'wp:tag_description', 'wp:term_taxonomy', 'wp:term_parent', |
|
| 286 | + 'wp:term_name', 'wp:term_description', 'wp:author_id', 'wp:author_login', 'wp:author_email', 'wp:author_display_name', |
|
| 287 | + 'wp:author_first_name', 'wp:author_last_name', |
|
| 288 | + ); |
|
| 289 | + var $wp_sub_tags = array( |
|
| 290 | + 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url', |
|
| 291 | + 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content', |
|
| 292 | + 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id', |
|
| 293 | + ); |
|
| 294 | + |
|
| 295 | + function parse( $file ) { |
|
| 296 | + $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false; |
|
| 297 | + $this->authors = $this->posts = $this->term = $this->category = $this->tag = array(); |
|
| 298 | + |
|
| 299 | + $xml = xml_parser_create( 'UTF-8' ); |
|
| 300 | + xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 ); |
|
| 301 | + xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 ); |
|
| 302 | + xml_set_object( $xml, $this ); |
|
| 303 | + xml_set_character_data_handler( $xml, 'cdata' ); |
|
| 304 | + xml_set_element_handler( $xml, 'tag_open', 'tag_close' ); |
|
| 305 | + |
|
| 306 | + if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) { |
|
| 307 | + $current_line = xml_get_current_line_number( $xml ); |
|
| 308 | + $current_column = xml_get_current_column_number( $xml ); |
|
| 309 | + $error_code = xml_get_error_code( $xml ); |
|
| 310 | + $error_string = xml_error_string( $error_code ); |
|
| 311 | + return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) ); |
|
| 312 | + } |
|
| 313 | + xml_parser_free( $xml ); |
|
| 314 | + |
|
| 315 | + if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) ) { |
|
| 316 | + return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 317 | + } |
|
| 318 | + |
|
| 319 | + return array( |
|
| 320 | + 'authors' => $this->authors, |
|
| 321 | + 'posts' => $this->posts, |
|
| 322 | + 'categories' => $this->category, |
|
| 323 | + 'tags' => $this->tag, |
|
| 324 | + 'terms' => $this->term, |
|
| 325 | + 'base_url' => $this->base_url, |
|
| 326 | + 'version' => $this->wxr_version |
|
| 327 | + ); |
|
| 328 | + } |
|
| 329 | + |
|
| 330 | + function tag_open( $parse, $tag, $attr ) { |
|
| 331 | + if ( in_array( $tag, $this->wp_tags ) ) { |
|
| 332 | + $this->in_tag = substr( $tag, 3 ); |
|
| 333 | + return; |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + if ( in_array( $tag, $this->wp_sub_tags ) ) { |
|
| 337 | + $this->in_sub_tag = substr( $tag, 3 ); |
|
| 338 | + return; |
|
| 339 | + } |
|
| 340 | + |
|
| 341 | + switch ( $tag ) { |
|
| 342 | + case 'category': |
|
| 343 | + if ( isset($attr['domain'], $attr['nicename']) ) { |
|
| 344 | + $this->sub_data['domain'] = $attr['domain']; |
|
| 345 | + $this->sub_data['slug'] = $attr['nicename']; |
|
| 346 | + } |
|
| 347 | + break; |
|
| 348 | + case 'item': $this->in_post = true; |
|
| 349 | + case 'title': if ( $this->in_post ) { |
|
| 350 | + $this->in_tag = 'post_title'; |
|
| 351 | + } |
|
| 352 | + break; |
|
| 353 | + case 'guid': $this->in_tag = 'guid'; break; |
|
| 354 | + case 'dc:creator': $this->in_tag = 'post_author'; break; |
|
| 355 | + case 'content:encoded': $this->in_tag = 'post_content'; break; |
|
| 356 | + case 'excerpt:encoded': $this->in_tag = 'post_excerpt'; break; |
|
| 357 | + |
|
| 358 | + case 'wp:term_slug': $this->in_tag = 'slug'; break; |
|
| 359 | + case 'wp:meta_key': $this->in_sub_tag = 'key'; break; |
|
| 360 | + case 'wp:meta_value': $this->in_sub_tag = 'value'; break; |
|
| 361 | + } |
|
| 362 | + } |
|
| 363 | + |
|
| 364 | + function cdata( $parser, $cdata ) { |
|
| 365 | + if ( ! trim( $cdata ) ) { |
|
| 366 | + return; |
|
| 367 | + } |
|
| 368 | + |
|
| 369 | + if ( false !== $this->in_tag || false !== $this->in_sub_tag ) { |
|
| 370 | + $this->cdata .= $cdata; |
|
| 371 | + } else { |
|
| 372 | + $this->cdata .= trim( $cdata ); |
|
| 373 | + } |
|
| 374 | + } |
|
| 375 | + |
|
| 376 | + function tag_close( $parser, $tag ) { |
|
| 377 | + switch ( $tag ) { |
|
| 378 | + case 'wp:comment': |
|
| 379 | + unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data |
|
| 380 | + if ( ! empty( $this->sub_data ) ) { |
|
| 381 | + $this->data['comments'][] = $this->sub_data; |
|
| 382 | + } |
|
| 383 | + $this->sub_data = false; |
|
| 384 | + break; |
|
| 385 | + case 'wp:commentmeta': |
|
| 386 | + $this->sub_data['commentmeta'][] = array( |
|
| 387 | + 'key' => $this->sub_data['key'], |
|
| 388 | + 'value' => $this->sub_data['value'] |
|
| 389 | + ); |
|
| 390 | + break; |
|
| 391 | + case 'category': |
|
| 392 | + if ( ! empty( $this->sub_data ) ) { |
|
| 393 | + $this->sub_data['name'] = $this->cdata; |
|
| 394 | + $this->data['terms'][] = $this->sub_data; |
|
| 395 | + } |
|
| 396 | + $this->sub_data = false; |
|
| 397 | + break; |
|
| 398 | + case 'wp:postmeta': |
|
| 399 | + if ( ! empty( $this->sub_data ) ) { |
|
| 400 | + $this->data['postmeta'][] = $this->sub_data; |
|
| 401 | + } |
|
| 402 | + $this->sub_data = false; |
|
| 403 | + break; |
|
| 404 | + case 'item': |
|
| 405 | + $this->posts[] = $this->data; |
|
| 406 | + $this->data = false; |
|
| 407 | + break; |
|
| 408 | + case 'wp:category': |
|
| 409 | + case 'wp:tag': |
|
| 410 | + case 'wp:term': |
|
| 411 | + $n = substr( $tag, 3 ); |
|
| 412 | + array_push( $this->$n, $this->data ); |
|
| 413 | + $this->data = false; |
|
| 414 | + break; |
|
| 415 | + case 'wp:author': |
|
| 416 | + if ( ! empty($this->data['author_login']) ) { |
|
| 417 | + $this->authors[$this->data['author_login']] = $this->data; |
|
| 418 | + } |
|
| 419 | + $this->data = false; |
|
| 420 | + break; |
|
| 421 | + case 'wp:base_site_url': |
|
| 422 | + $this->base_url = $this->cdata; |
|
| 423 | + break; |
|
| 424 | + case 'wp:wxr_version': |
|
| 425 | + $this->wxr_version = $this->cdata; |
|
| 426 | + break; |
|
| 427 | + |
|
| 428 | + default: |
|
| 429 | + if ( $this->in_sub_tag ) { |
|
| 430 | + $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
| 431 | + $this->in_sub_tag = false; |
|
| 432 | + } else if ( $this->in_tag ) { |
|
| 433 | + $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
| 434 | + $this->in_tag = false; |
|
| 435 | + } |
|
| 436 | + } |
|
| 437 | + |
|
| 438 | + $this->cdata = false; |
|
| 439 | + } |
|
| 440 | 440 | } |
| 441 | 441 | |
| 442 | 442 | /** |
| 443 | 443 | * WXR Parser that uses regular expressions. Fallback for installs without an XML parser. |
| 444 | 444 | */ |
| 445 | 445 | class WXR_Parser_Regex { |
| 446 | - var $authors = array(); |
|
| 447 | - var $posts = array(); |
|
| 448 | - var $categories = array(); |
|
| 449 | - var $tags = array(); |
|
| 450 | - var $terms = array(); |
|
| 451 | - var $base_url = ''; |
|
| 452 | - |
|
| 453 | - function __construct() { |
|
| 454 | - $this->has_gzip = is_callable( 'gzopen' ); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - function parse( $file ) { |
|
| 458 | - $wxr_version = $in_post = false; |
|
| 459 | - |
|
| 460 | - $fp = $this->fopen( $file, 'r' ); |
|
| 461 | - if ( $fp ) { |
|
| 462 | - while ( ! $this->feof( $fp ) ) { |
|
| 463 | - $importline = rtrim( $this->fgets( $fp ) ); |
|
| 464 | - |
|
| 465 | - if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) ) { |
|
| 466 | - $wxr_version = $version[1]; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) { |
|
| 470 | - preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url ); |
|
| 471 | - $this->base_url = $url[1]; |
|
| 472 | - continue; |
|
| 473 | - } |
|
| 474 | - if ( false !== strpos( $importline, '<wp:category>' ) ) { |
|
| 475 | - preg_match( '|<wp:category>(.*?)</wp:category>|is', $importline, $category ); |
|
| 476 | - $this->categories[] = $this->process_category( $category[1] ); |
|
| 477 | - continue; |
|
| 478 | - } |
|
| 479 | - if ( false !== strpos( $importline, '<wp:tag>' ) ) { |
|
| 480 | - preg_match( '|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag ); |
|
| 481 | - $this->tags[] = $this->process_tag( $tag[1] ); |
|
| 482 | - continue; |
|
| 483 | - } |
|
| 484 | - if ( false !== strpos( $importline, '<wp:term>' ) ) { |
|
| 485 | - preg_match( '|<wp:term>(.*?)</wp:term>|is', $importline, $term ); |
|
| 486 | - $this->terms[] = $this->process_term( $term[1] ); |
|
| 487 | - continue; |
|
| 488 | - } |
|
| 489 | - if ( false !== strpos( $importline, '<wp:author>' ) ) { |
|
| 490 | - preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author ); |
|
| 491 | - $a = $this->process_author( $author[1] ); |
|
| 492 | - $this->authors[$a['author_login']] = $a; |
|
| 493 | - continue; |
|
| 494 | - } |
|
| 495 | - if ( false !== strpos( $importline, '<item>' ) ) { |
|
| 496 | - $post = ''; |
|
| 497 | - $in_post = true; |
|
| 498 | - continue; |
|
| 499 | - } |
|
| 500 | - if ( false !== strpos( $importline, '</item>' ) ) { |
|
| 501 | - $in_post = false; |
|
| 502 | - $this->posts[] = $this->process_post( $post ); |
|
| 503 | - continue; |
|
| 504 | - } |
|
| 505 | - if ( $in_post ) { |
|
| 506 | - $post .= $importline . "\n"; |
|
| 507 | - } |
|
| 508 | - } |
|
| 509 | - |
|
| 510 | - $this->fclose($fp); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - if ( ! $wxr_version ) { |
|
| 514 | - return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - return array( |
|
| 518 | - 'authors' => $this->authors, |
|
| 519 | - 'posts' => $this->posts, |
|
| 520 | - 'categories' => $this->categories, |
|
| 521 | - 'tags' => $this->tags, |
|
| 522 | - 'terms' => $this->terms, |
|
| 523 | - 'base_url' => $this->base_url, |
|
| 524 | - 'version' => $wxr_version |
|
| 525 | - ); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - function get_tag( $string, $tag ) { |
|
| 529 | - preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return ); |
|
| 530 | - if ( isset( $return[1] ) ) { |
|
| 531 | - if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) { |
|
| 532 | - if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) { |
|
| 533 | - preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches ); |
|
| 534 | - $return = ''; |
|
| 535 | - foreach( $matches[1] as $match ) { |
|
| 536 | - $return .= $match; |
|
| 537 | - } |
|
| 538 | - } else { |
|
| 539 | - $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] ); |
|
| 540 | - } |
|
| 541 | - } else { |
|
| 542 | - $return = $return[1]; |
|
| 543 | - } |
|
| 544 | - } else { |
|
| 545 | - $return = ''; |
|
| 546 | - } |
|
| 547 | - return $return; |
|
| 548 | - } |
|
| 549 | - |
|
| 550 | - function process_category( $c ) { |
|
| 551 | - return array( |
|
| 552 | - 'term_id' => $this->get_tag( $c, 'wp:term_id' ), |
|
| 553 | - 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ), |
|
| 554 | - 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ), |
|
| 555 | - 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ), |
|
| 556 | - 'category_description' => $this->get_tag( $c, 'wp:category_description' ), |
|
| 557 | - ); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - function process_tag( $t ) { |
|
| 561 | - return array( |
|
| 562 | - 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
| 563 | - 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ), |
|
| 564 | - 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ), |
|
| 565 | - 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ), |
|
| 566 | - ); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - function process_term( $t ) { |
|
| 570 | - return array( |
|
| 571 | - 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
| 572 | - 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ), |
|
| 573 | - 'slug' => $this->get_tag( $t, 'wp:term_slug' ), |
|
| 574 | - 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ), |
|
| 575 | - 'term_name' => $this->get_tag( $t, 'wp:term_name' ), |
|
| 576 | - 'term_description' => $this->get_tag( $t, 'wp:term_description' ), |
|
| 577 | - ); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - function process_author( $a ) { |
|
| 581 | - return array( |
|
| 582 | - 'author_id' => $this->get_tag( $a, 'wp:author_id' ), |
|
| 583 | - 'author_login' => $this->get_tag( $a, 'wp:author_login' ), |
|
| 584 | - 'author_email' => $this->get_tag( $a, 'wp:author_email' ), |
|
| 585 | - 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ), |
|
| 586 | - 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ), |
|
| 587 | - 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ), |
|
| 588 | - ); |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - function process_post( $post ) { |
|
| 592 | - $post_id = $this->get_tag( $post, 'wp:post_id' ); |
|
| 593 | - $post_title = $this->get_tag( $post, 'title' ); |
|
| 594 | - $post_date = $this->get_tag( $post, 'wp:post_date' ); |
|
| 595 | - $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); |
|
| 596 | - $comment_status = $this->get_tag( $post, 'wp:comment_status' ); |
|
| 597 | - $ping_status = $this->get_tag( $post, 'wp:ping_status' ); |
|
| 598 | - $status = $this->get_tag( $post, 'wp:status' ); |
|
| 599 | - $post_name = $this->get_tag( $post, 'wp:post_name' ); |
|
| 600 | - $post_parent = $this->get_tag( $post, 'wp:post_parent' ); |
|
| 601 | - $menu_order = $this->get_tag( $post, 'wp:menu_order' ); |
|
| 602 | - $post_type = $this->get_tag( $post, 'wp:post_type' ); |
|
| 603 | - $post_password = $this->get_tag( $post, 'wp:post_password' ); |
|
| 604 | - $is_sticky = $this->get_tag( $post, 'wp:is_sticky' ); |
|
| 605 | - $guid = $this->get_tag( $post, 'guid' ); |
|
| 606 | - $post_author = $this->get_tag( $post, 'dc:creator' ); |
|
| 607 | - |
|
| 608 | - $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' ); |
|
| 609 | - $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt ); |
|
| 610 | - $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt ); |
|
| 611 | - $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt ); |
|
| 612 | - |
|
| 613 | - $post_content = $this->get_tag( $post, 'content:encoded' ); |
|
| 614 | - $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content ); |
|
| 615 | - $post_content = str_replace( '<br>', '<br />', $post_content ); |
|
| 616 | - $post_content = str_replace( '<hr>', '<hr />', $post_content ); |
|
| 617 | - |
|
| 618 | - $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', |
|
| 619 | - 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', |
|
| 620 | - 'menu_order', 'post_type', 'post_password', 'is_sticky' |
|
| 621 | - ); |
|
| 622 | - |
|
| 623 | - $attachment_url = $this->get_tag( $post, 'wp:attachment_url' ); |
|
| 624 | - if ( $attachment_url ) { |
|
| 625 | - $postdata['attachment_url'] = $attachment_url; |
|
| 626 | - } |
|
| 627 | - |
|
| 628 | - preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER ); |
|
| 629 | - foreach ( $terms as $t ) { |
|
| 630 | - $post_terms[] = array( |
|
| 631 | - 'slug' => $t[2], |
|
| 632 | - 'domain' => $t[1], |
|
| 633 | - 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ), |
|
| 634 | - ); |
|
| 635 | - } |
|
| 636 | - if ( ! empty( $post_terms ) ) { |
|
| 637 | - $postdata['terms'] = $post_terms; |
|
| 638 | - } |
|
| 639 | - |
|
| 640 | - preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments ); |
|
| 641 | - $comments = $comments[1]; |
|
| 642 | - if ( $comments ) { |
|
| 643 | - foreach ( $comments as $comment ) { |
|
| 644 | - preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta ); |
|
| 645 | - $commentmeta = $commentmeta[1]; |
|
| 646 | - $c_meta = array(); |
|
| 647 | - foreach ( $commentmeta as $m ) { |
|
| 648 | - $c_meta[] = array( |
|
| 649 | - 'key' => $this->get_tag( $m, 'wp:meta_key' ), |
|
| 650 | - 'value' => $this->get_tag( $m, 'wp:meta_value' ), |
|
| 651 | - ); |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $post_comments[] = array( |
|
| 655 | - 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ), |
|
| 656 | - 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ), |
|
| 657 | - 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ), |
|
| 658 | - 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ), |
|
| 659 | - 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ), |
|
| 660 | - 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ), |
|
| 661 | - 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ), |
|
| 662 | - 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ), |
|
| 663 | - 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ), |
|
| 664 | - 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ), |
|
| 665 | - 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ), |
|
| 666 | - 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ), |
|
| 667 | - 'commentmeta' => $c_meta, |
|
| 668 | - ); |
|
| 669 | - } |
|
| 670 | - } |
|
| 671 | - if ( ! empty( $post_comments ) ) { |
|
| 672 | - $postdata['comments'] = $post_comments; |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta ); |
|
| 676 | - $postmeta = $postmeta[1]; |
|
| 677 | - if ( $postmeta ) { |
|
| 678 | - foreach ( $postmeta as $p ) { |
|
| 679 | - $post_postmeta[] = array( |
|
| 680 | - 'key' => $this->get_tag( $p, 'wp:meta_key' ), |
|
| 681 | - 'value' => $this->get_tag( $p, 'wp:meta_value' ), |
|
| 682 | - ); |
|
| 683 | - } |
|
| 684 | - } |
|
| 685 | - if ( ! empty( $post_postmeta ) ) { |
|
| 686 | - $postdata['postmeta'] = $post_postmeta; |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - return $postdata; |
|
| 690 | - } |
|
| 691 | - |
|
| 692 | - function _normalize_tag( $matches ) { |
|
| 693 | - return '<' . strtolower( $matches[1] ); |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - function fopen( $filename, $mode = 'r' ) { |
|
| 697 | - if ( $this->has_gzip ) { |
|
| 698 | - return gzopen( $filename, $mode ); |
|
| 699 | - } |
|
| 700 | - return fopen( $filename, $mode ); |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - function feof( $fp ) { |
|
| 704 | - if ( $this->has_gzip ) { |
|
| 705 | - return gzeof( $fp ); |
|
| 706 | - } |
|
| 707 | - return feof( $fp ); |
|
| 708 | - } |
|
| 709 | - |
|
| 710 | - function fgets( $fp, $len = 8192 ) { |
|
| 711 | - if ( $this->has_gzip ) { |
|
| 712 | - return gzgets( $fp, $len ); |
|
| 713 | - } |
|
| 714 | - return fgets( $fp, $len ); |
|
| 715 | - } |
|
| 716 | - |
|
| 717 | - function fclose( $fp ) { |
|
| 718 | - if ( $this->has_gzip ) { |
|
| 719 | - return gzclose( $fp ); |
|
| 720 | - } |
|
| 721 | - return fclose( $fp ); |
|
| 722 | - } |
|
| 446 | + var $authors = array(); |
|
| 447 | + var $posts = array(); |
|
| 448 | + var $categories = array(); |
|
| 449 | + var $tags = array(); |
|
| 450 | + var $terms = array(); |
|
| 451 | + var $base_url = ''; |
|
| 452 | + |
|
| 453 | + function __construct() { |
|
| 454 | + $this->has_gzip = is_callable( 'gzopen' ); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + function parse( $file ) { |
|
| 458 | + $wxr_version = $in_post = false; |
|
| 459 | + |
|
| 460 | + $fp = $this->fopen( $file, 'r' ); |
|
| 461 | + if ( $fp ) { |
|
| 462 | + while ( ! $this->feof( $fp ) ) { |
|
| 463 | + $importline = rtrim( $this->fgets( $fp ) ); |
|
| 464 | + |
|
| 465 | + if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) ) { |
|
| 466 | + $wxr_version = $version[1]; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) { |
|
| 470 | + preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url ); |
|
| 471 | + $this->base_url = $url[1]; |
|
| 472 | + continue; |
|
| 473 | + } |
|
| 474 | + if ( false !== strpos( $importline, '<wp:category>' ) ) { |
|
| 475 | + preg_match( '|<wp:category>(.*?)</wp:category>|is', $importline, $category ); |
|
| 476 | + $this->categories[] = $this->process_category( $category[1] ); |
|
| 477 | + continue; |
|
| 478 | + } |
|
| 479 | + if ( false !== strpos( $importline, '<wp:tag>' ) ) { |
|
| 480 | + preg_match( '|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag ); |
|
| 481 | + $this->tags[] = $this->process_tag( $tag[1] ); |
|
| 482 | + continue; |
|
| 483 | + } |
|
| 484 | + if ( false !== strpos( $importline, '<wp:term>' ) ) { |
|
| 485 | + preg_match( '|<wp:term>(.*?)</wp:term>|is', $importline, $term ); |
|
| 486 | + $this->terms[] = $this->process_term( $term[1] ); |
|
| 487 | + continue; |
|
| 488 | + } |
|
| 489 | + if ( false !== strpos( $importline, '<wp:author>' ) ) { |
|
| 490 | + preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author ); |
|
| 491 | + $a = $this->process_author( $author[1] ); |
|
| 492 | + $this->authors[$a['author_login']] = $a; |
|
| 493 | + continue; |
|
| 494 | + } |
|
| 495 | + if ( false !== strpos( $importline, '<item>' ) ) { |
|
| 496 | + $post = ''; |
|
| 497 | + $in_post = true; |
|
| 498 | + continue; |
|
| 499 | + } |
|
| 500 | + if ( false !== strpos( $importline, '</item>' ) ) { |
|
| 501 | + $in_post = false; |
|
| 502 | + $this->posts[] = $this->process_post( $post ); |
|
| 503 | + continue; |
|
| 504 | + } |
|
| 505 | + if ( $in_post ) { |
|
| 506 | + $post .= $importline . "\n"; |
|
| 507 | + } |
|
| 508 | + } |
|
| 509 | + |
|
| 510 | + $this->fclose($fp); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + if ( ! $wxr_version ) { |
|
| 514 | + return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + return array( |
|
| 518 | + 'authors' => $this->authors, |
|
| 519 | + 'posts' => $this->posts, |
|
| 520 | + 'categories' => $this->categories, |
|
| 521 | + 'tags' => $this->tags, |
|
| 522 | + 'terms' => $this->terms, |
|
| 523 | + 'base_url' => $this->base_url, |
|
| 524 | + 'version' => $wxr_version |
|
| 525 | + ); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + function get_tag( $string, $tag ) { |
|
| 529 | + preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return ); |
|
| 530 | + if ( isset( $return[1] ) ) { |
|
| 531 | + if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) { |
|
| 532 | + if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) { |
|
| 533 | + preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches ); |
|
| 534 | + $return = ''; |
|
| 535 | + foreach( $matches[1] as $match ) { |
|
| 536 | + $return .= $match; |
|
| 537 | + } |
|
| 538 | + } else { |
|
| 539 | + $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] ); |
|
| 540 | + } |
|
| 541 | + } else { |
|
| 542 | + $return = $return[1]; |
|
| 543 | + } |
|
| 544 | + } else { |
|
| 545 | + $return = ''; |
|
| 546 | + } |
|
| 547 | + return $return; |
|
| 548 | + } |
|
| 549 | + |
|
| 550 | + function process_category( $c ) { |
|
| 551 | + return array( |
|
| 552 | + 'term_id' => $this->get_tag( $c, 'wp:term_id' ), |
|
| 553 | + 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ), |
|
| 554 | + 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ), |
|
| 555 | + 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ), |
|
| 556 | + 'category_description' => $this->get_tag( $c, 'wp:category_description' ), |
|
| 557 | + ); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + function process_tag( $t ) { |
|
| 561 | + return array( |
|
| 562 | + 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
| 563 | + 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ), |
|
| 564 | + 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ), |
|
| 565 | + 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ), |
|
| 566 | + ); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + function process_term( $t ) { |
|
| 570 | + return array( |
|
| 571 | + 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
| 572 | + 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ), |
|
| 573 | + 'slug' => $this->get_tag( $t, 'wp:term_slug' ), |
|
| 574 | + 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ), |
|
| 575 | + 'term_name' => $this->get_tag( $t, 'wp:term_name' ), |
|
| 576 | + 'term_description' => $this->get_tag( $t, 'wp:term_description' ), |
|
| 577 | + ); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + function process_author( $a ) { |
|
| 581 | + return array( |
|
| 582 | + 'author_id' => $this->get_tag( $a, 'wp:author_id' ), |
|
| 583 | + 'author_login' => $this->get_tag( $a, 'wp:author_login' ), |
|
| 584 | + 'author_email' => $this->get_tag( $a, 'wp:author_email' ), |
|
| 585 | + 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ), |
|
| 586 | + 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ), |
|
| 587 | + 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ), |
|
| 588 | + ); |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + function process_post( $post ) { |
|
| 592 | + $post_id = $this->get_tag( $post, 'wp:post_id' ); |
|
| 593 | + $post_title = $this->get_tag( $post, 'title' ); |
|
| 594 | + $post_date = $this->get_tag( $post, 'wp:post_date' ); |
|
| 595 | + $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); |
|
| 596 | + $comment_status = $this->get_tag( $post, 'wp:comment_status' ); |
|
| 597 | + $ping_status = $this->get_tag( $post, 'wp:ping_status' ); |
|
| 598 | + $status = $this->get_tag( $post, 'wp:status' ); |
|
| 599 | + $post_name = $this->get_tag( $post, 'wp:post_name' ); |
|
| 600 | + $post_parent = $this->get_tag( $post, 'wp:post_parent' ); |
|
| 601 | + $menu_order = $this->get_tag( $post, 'wp:menu_order' ); |
|
| 602 | + $post_type = $this->get_tag( $post, 'wp:post_type' ); |
|
| 603 | + $post_password = $this->get_tag( $post, 'wp:post_password' ); |
|
| 604 | + $is_sticky = $this->get_tag( $post, 'wp:is_sticky' ); |
|
| 605 | + $guid = $this->get_tag( $post, 'guid' ); |
|
| 606 | + $post_author = $this->get_tag( $post, 'dc:creator' ); |
|
| 607 | + |
|
| 608 | + $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' ); |
|
| 609 | + $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt ); |
|
| 610 | + $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt ); |
|
| 611 | + $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt ); |
|
| 612 | + |
|
| 613 | + $post_content = $this->get_tag( $post, 'content:encoded' ); |
|
| 614 | + $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content ); |
|
| 615 | + $post_content = str_replace( '<br>', '<br />', $post_content ); |
|
| 616 | + $post_content = str_replace( '<hr>', '<hr />', $post_content ); |
|
| 617 | + |
|
| 618 | + $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', |
|
| 619 | + 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', |
|
| 620 | + 'menu_order', 'post_type', 'post_password', 'is_sticky' |
|
| 621 | + ); |
|
| 622 | + |
|
| 623 | + $attachment_url = $this->get_tag( $post, 'wp:attachment_url' ); |
|
| 624 | + if ( $attachment_url ) { |
|
| 625 | + $postdata['attachment_url'] = $attachment_url; |
|
| 626 | + } |
|
| 627 | + |
|
| 628 | + preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER ); |
|
| 629 | + foreach ( $terms as $t ) { |
|
| 630 | + $post_terms[] = array( |
|
| 631 | + 'slug' => $t[2], |
|
| 632 | + 'domain' => $t[1], |
|
| 633 | + 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ), |
|
| 634 | + ); |
|
| 635 | + } |
|
| 636 | + if ( ! empty( $post_terms ) ) { |
|
| 637 | + $postdata['terms'] = $post_terms; |
|
| 638 | + } |
|
| 639 | + |
|
| 640 | + preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments ); |
|
| 641 | + $comments = $comments[1]; |
|
| 642 | + if ( $comments ) { |
|
| 643 | + foreach ( $comments as $comment ) { |
|
| 644 | + preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta ); |
|
| 645 | + $commentmeta = $commentmeta[1]; |
|
| 646 | + $c_meta = array(); |
|
| 647 | + foreach ( $commentmeta as $m ) { |
|
| 648 | + $c_meta[] = array( |
|
| 649 | + 'key' => $this->get_tag( $m, 'wp:meta_key' ), |
|
| 650 | + 'value' => $this->get_tag( $m, 'wp:meta_value' ), |
|
| 651 | + ); |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $post_comments[] = array( |
|
| 655 | + 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ), |
|
| 656 | + 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ), |
|
| 657 | + 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ), |
|
| 658 | + 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ), |
|
| 659 | + 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ), |
|
| 660 | + 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ), |
|
| 661 | + 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ), |
|
| 662 | + 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ), |
|
| 663 | + 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ), |
|
| 664 | + 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ), |
|
| 665 | + 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ), |
|
| 666 | + 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ), |
|
| 667 | + 'commentmeta' => $c_meta, |
|
| 668 | + ); |
|
| 669 | + } |
|
| 670 | + } |
|
| 671 | + if ( ! empty( $post_comments ) ) { |
|
| 672 | + $postdata['comments'] = $post_comments; |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta ); |
|
| 676 | + $postmeta = $postmeta[1]; |
|
| 677 | + if ( $postmeta ) { |
|
| 678 | + foreach ( $postmeta as $p ) { |
|
| 679 | + $post_postmeta[] = array( |
|
| 680 | + 'key' => $this->get_tag( $p, 'wp:meta_key' ), |
|
| 681 | + 'value' => $this->get_tag( $p, 'wp:meta_value' ), |
|
| 682 | + ); |
|
| 683 | + } |
|
| 684 | + } |
|
| 685 | + if ( ! empty( $post_postmeta ) ) { |
|
| 686 | + $postdata['postmeta'] = $post_postmeta; |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + return $postdata; |
|
| 690 | + } |
|
| 691 | + |
|
| 692 | + function _normalize_tag( $matches ) { |
|
| 693 | + return '<' . strtolower( $matches[1] ); |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + function fopen( $filename, $mode = 'r' ) { |
|
| 697 | + if ( $this->has_gzip ) { |
|
| 698 | + return gzopen( $filename, $mode ); |
|
| 699 | + } |
|
| 700 | + return fopen( $filename, $mode ); |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + function feof( $fp ) { |
|
| 704 | + if ( $this->has_gzip ) { |
|
| 705 | + return gzeof( $fp ); |
|
| 706 | + } |
|
| 707 | + return feof( $fp ); |
|
| 708 | + } |
|
| 709 | + |
|
| 710 | + function fgets( $fp, $len = 8192 ) { |
|
| 711 | + if ( $this->has_gzip ) { |
|
| 712 | + return gzgets( $fp, $len ); |
|
| 713 | + } |
|
| 714 | + return fgets( $fp, $len ); |
|
| 715 | + } |
|
| 716 | + |
|
| 717 | + function fclose( $fp ) { |
|
| 718 | + if ( $this->has_gzip ) { |
|
| 719 | + return gzclose( $fp ); |
|
| 720 | + } |
|
| 721 | + return fclose( $fp ); |
|
| 722 | + } |
|
| 723 | 723 | } |
@@ -10,45 +10,45 @@ discard block |
||
| 10 | 10 | * WordPress Importer class for managing parsing of WXR files. |
| 11 | 11 | */ |
| 12 | 12 | class WXR_Parser { |
| 13 | - function parse( $file ) { |
|
| 13 | + function parse($file) { |
|
| 14 | 14 | // Attempt to use proper XML parsers first |
| 15 | - if ( extension_loaded( 'simplexml' ) ) { |
|
| 15 | + if (extension_loaded('simplexml')) { |
|
| 16 | 16 | $parser = new WXR_Parser_SimpleXML; |
| 17 | - $result = $parser->parse( $file ); |
|
| 17 | + $result = $parser->parse($file); |
|
| 18 | 18 | |
| 19 | 19 | // If SimpleXML succeeds or this is an invalid WXR file then return the results |
| 20 | - if ( ! is_wp_error( $result ) || 'SimpleXML_parse_error' != $result->get_error_code() ) { |
|
| 20 | + if ( ! is_wp_error($result) || 'SimpleXML_parse_error' != $result->get_error_code()) { |
|
| 21 | 21 | return $result; |
| 22 | 22 | } |
| 23 | - } else if ( extension_loaded( 'xml' ) ) { |
|
| 23 | + } else if (extension_loaded('xml')) { |
|
| 24 | 24 | $parser = new WXR_Parser_XML; |
| 25 | - $result = $parser->parse( $file ); |
|
| 25 | + $result = $parser->parse($file); |
|
| 26 | 26 | |
| 27 | 27 | // If XMLParser succeeds or this is an invalid WXR file then return the results |
| 28 | - if ( ! is_wp_error( $result ) || 'XML_parse_error' != $result->get_error_code() ) { |
|
| 28 | + if ( ! is_wp_error($result) || 'XML_parse_error' != $result->get_error_code()) { |
|
| 29 | 29 | return $result; |
| 30 | 30 | } |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | // We have a malformed XML file, so display the error and fallthrough to regex |
| 34 | - if ( isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 34 | + if (isset($result) && defined('IMPORT_DEBUG') && IMPORT_DEBUG) { |
|
| 35 | 35 | echo '<pre>'; |
| 36 | - if ( 'SimpleXML_parse_error' == $result->get_error_code() ) { |
|
| 37 | - foreach ( $result->get_error_data() as $error ) { |
|
| 38 | - echo $error->line . ':' . $error->column . ' ' . esc_html( $error->message ) . "\n"; |
|
| 36 | + if ('SimpleXML_parse_error' == $result->get_error_code()) { |
|
| 37 | + foreach ($result->get_error_data() as $error) { |
|
| 38 | + echo $error->line . ':' . $error->column . ' ' . esc_html($error->message) . "\n"; |
|
| 39 | 39 | } |
| 40 | - } else if ( 'XML_parse_error' == $result->get_error_code() ) { |
|
| 40 | + } else if ('XML_parse_error' == $result->get_error_code()) { |
|
| 41 | 41 | $error = $result->get_error_data(); |
| 42 | - echo $error[0] . ':' . $error[1] . ' ' . esc_html( $error[2] ); |
|
| 42 | + echo $error[0] . ':' . $error[1] . ' ' . esc_html($error[2]); |
|
| 43 | 43 | } |
| 44 | 44 | echo '</pre>'; |
| 45 | - echo '<p><strong>' . __( 'There was an error when reading this WXR file', 'wordpress-importer' ) . '</strong><br />'; |
|
| 46 | - echo __( 'Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer' ) . '</p>'; |
|
| 45 | + echo '<p><strong>' . __('There was an error when reading this WXR file', 'wordpress-importer') . '</strong><br />'; |
|
| 46 | + echo __('Details are shown above. The importer will now try again with a different parser...', 'wordpress-importer') . '</p>'; |
|
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | // use regular expressions if nothing else available or this is bad XML |
| 50 | 50 | $parser = new WXR_Parser_Regex; |
| 51 | - return $parser->parse( $file ); |
|
| 51 | + return $parser->parse($file); |
|
| 52 | 52 | } |
| 53 | 53 | } |
| 54 | 54 | |
@@ -56,58 +56,58 @@ discard block |
||
| 56 | 56 | * WXR Parser that makes use of the SimpleXML PHP extension. |
| 57 | 57 | */ |
| 58 | 58 | class WXR_Parser_SimpleXML { |
| 59 | - function parse( $file ) { |
|
| 59 | + function parse($file) { |
|
| 60 | 60 | $authors = $posts = $categories = $tags = $terms = array(); |
| 61 | 61 | |
| 62 | 62 | $internal_errors = libxml_use_internal_errors(true); |
| 63 | 63 | |
| 64 | 64 | $dom = new DOMDocument; |
| 65 | 65 | $old_value = null; |
| 66 | - if ( function_exists( 'libxml_disable_entity_loader' ) ) { |
|
| 67 | - $old_value = libxml_disable_entity_loader( true ); |
|
| 66 | + if (function_exists('libxml_disable_entity_loader')) { |
|
| 67 | + $old_value = libxml_disable_entity_loader(true); |
|
| 68 | 68 | } |
| 69 | - $success = $dom->loadXML( file_get_contents( $file ) ); |
|
| 70 | - if ( ! is_null( $old_value ) ) { |
|
| 71 | - libxml_disable_entity_loader( $old_value ); |
|
| 69 | + $success = $dom->loadXML(file_get_contents($file)); |
|
| 70 | + if ( ! is_null($old_value)) { |
|
| 71 | + libxml_disable_entity_loader($old_value); |
|
| 72 | 72 | } |
| 73 | 73 | |
| 74 | - if ( ! $success || isset( $dom->doctype ) ) { |
|
| 75 | - return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() ); |
|
| 74 | + if ( ! $success || isset($dom->doctype)) { |
|
| 75 | + return new WP_Error('SimpleXML_parse_error', __('There was an error when reading this WXR file', 'wordpress-importer'), libxml_get_errors()); |
|
| 76 | 76 | } |
| 77 | 77 | |
| 78 | - $xml = simplexml_import_dom( $dom ); |
|
| 79 | - unset( $dom ); |
|
| 78 | + $xml = simplexml_import_dom($dom); |
|
| 79 | + unset($dom); |
|
| 80 | 80 | |
| 81 | 81 | // halt if loading produces an error |
| 82 | - if ( ! $xml ) { |
|
| 83 | - return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this WXR file', 'wordpress-importer' ), libxml_get_errors() ); |
|
| 82 | + if ( ! $xml) { |
|
| 83 | + return new WP_Error('SimpleXML_parse_error', __('There was an error when reading this WXR file', 'wordpress-importer'), libxml_get_errors()); |
|
| 84 | 84 | } |
| 85 | 85 | |
| 86 | 86 | $wxr_version = $xml->xpath('/rss/channel/wp:wxr_version'); |
| 87 | - if ( ! $wxr_version ) { |
|
| 88 | - return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 87 | + if ( ! $wxr_version) { |
|
| 88 | + return new WP_Error('WXR_parse_error', __('This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer')); |
|
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - $wxr_version = (string) trim( $wxr_version[0] ); |
|
| 91 | + $wxr_version = (string) trim($wxr_version[0]); |
|
| 92 | 92 | // confirm that we are dealing with the correct file format |
| 93 | - if ( ! preg_match( '/^\d+\.\d+$/', $wxr_version ) ) { |
|
| 94 | - return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 93 | + if ( ! preg_match('/^\d+\.\d+$/', $wxr_version)) { |
|
| 94 | + return new WP_Error('WXR_parse_error', __('This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer')); |
|
| 95 | 95 | } |
| 96 | 96 | |
| 97 | 97 | $base_url = $xml->xpath('/rss/channel/wp:base_site_url'); |
| 98 | - $base_url = (string) trim( $base_url[0] ); |
|
| 98 | + $base_url = (string) trim($base_url[0]); |
|
| 99 | 99 | |
| 100 | 100 | $namespaces = $xml->getDocNamespaces(); |
| 101 | - if ( ! isset( $namespaces['wp'] ) ) { |
|
| 101 | + if ( ! isset($namespaces['wp'])) { |
|
| 102 | 102 | $namespaces['wp'] = 'http://wordpress.org/export/1.1/'; |
| 103 | 103 | } |
| 104 | - if ( ! isset( $namespaces['excerpt'] ) ) { |
|
| 104 | + if ( ! isset($namespaces['excerpt'])) { |
|
| 105 | 105 | $namespaces['excerpt'] = 'http://wordpress.org/export/1.1/excerpt/'; |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | // grab authors |
| 109 | - foreach ( $xml->xpath('/rss/channel/wp:author') as $author_arr ) { |
|
| 110 | - $a = $author_arr->children( $namespaces['wp'] ); |
|
| 109 | + foreach ($xml->xpath('/rss/channel/wp:author') as $author_arr) { |
|
| 110 | + $a = $author_arr->children($namespaces['wp']); |
|
| 111 | 111 | $login = (string) $a->author_login; |
| 112 | 112 | $authors[$login] = array( |
| 113 | 113 | 'author_id' => (int) $a->author_id, |
@@ -120,8 +120,8 @@ discard block |
||
| 120 | 120 | } |
| 121 | 121 | |
| 122 | 122 | // grab cats, tags and terms |
| 123 | - foreach ( $xml->xpath('/rss/channel/wp:category') as $term_arr ) { |
|
| 124 | - $t = $term_arr->children( $namespaces['wp'] ); |
|
| 123 | + foreach ($xml->xpath('/rss/channel/wp:category') as $term_arr) { |
|
| 124 | + $t = $term_arr->children($namespaces['wp']); |
|
| 125 | 125 | $category = array( |
| 126 | 126 | 'term_id' => (int) $t->term_id, |
| 127 | 127 | 'category_nicename' => (string) $t->category_nicename, |
@@ -130,7 +130,7 @@ discard block |
||
| 130 | 130 | 'category_description' => (string) $t->category_description |
| 131 | 131 | ); |
| 132 | 132 | |
| 133 | - foreach ( $t->termmeta as $meta ) { |
|
| 133 | + foreach ($t->termmeta as $meta) { |
|
| 134 | 134 | $category['termmeta'][] = array( |
| 135 | 135 | 'key' => (string) $meta->meta_key, |
| 136 | 136 | 'value' => (string) $meta->meta_value |
@@ -140,8 +140,8 @@ discard block |
||
| 140 | 140 | $categories[] = $category; |
| 141 | 141 | } |
| 142 | 142 | |
| 143 | - foreach ( $xml->xpath('/rss/channel/wp:tag') as $term_arr ) { |
|
| 144 | - $t = $term_arr->children( $namespaces['wp'] ); |
|
| 143 | + foreach ($xml->xpath('/rss/channel/wp:tag') as $term_arr) { |
|
| 144 | + $t = $term_arr->children($namespaces['wp']); |
|
| 145 | 145 | $tag = array( |
| 146 | 146 | 'term_id' => (int) $t->term_id, |
| 147 | 147 | 'tag_slug' => (string) $t->tag_slug, |
@@ -149,7 +149,7 @@ discard block |
||
| 149 | 149 | 'tag_description' => (string) $t->tag_description |
| 150 | 150 | ); |
| 151 | 151 | |
| 152 | - foreach ( $t->termmeta as $meta ) { |
|
| 152 | + foreach ($t->termmeta as $meta) { |
|
| 153 | 153 | $tag['termmeta'][] = array( |
| 154 | 154 | 'key' => (string) $meta->meta_key, |
| 155 | 155 | 'value' => (string) $meta->meta_value |
@@ -159,8 +159,8 @@ discard block |
||
| 159 | 159 | $tags[] = $tag; |
| 160 | 160 | } |
| 161 | 161 | |
| 162 | - foreach ( $xml->xpath('/rss/channel/wp:term') as $term_arr ) { |
|
| 163 | - $t = $term_arr->children( $namespaces['wp'] ); |
|
| 162 | + foreach ($xml->xpath('/rss/channel/wp:term') as $term_arr) { |
|
| 163 | + $t = $term_arr->children($namespaces['wp']); |
|
| 164 | 164 | $term = array( |
| 165 | 165 | 'term_id' => (int) $t->term_id, |
| 166 | 166 | 'term_taxonomy' => (string) $t->term_taxonomy, |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | 'term_description' => (string) $t->term_description |
| 171 | 171 | ); |
| 172 | 172 | |
| 173 | - foreach ( $t->termmeta as $meta ) { |
|
| 173 | + foreach ($t->termmeta as $meta) { |
|
| 174 | 174 | $term['termmeta'][] = array( |
| 175 | 175 | 'key' => (string) $meta->meta_key, |
| 176 | 176 | 'value' => (string) $meta->meta_value |
@@ -181,21 +181,21 @@ discard block |
||
| 181 | 181 | } |
| 182 | 182 | |
| 183 | 183 | // grab posts |
| 184 | - foreach ( $xml->channel->item as $item ) { |
|
| 184 | + foreach ($xml->channel->item as $item) { |
|
| 185 | 185 | $post = array( |
| 186 | 186 | 'post_title' => (string) $item->title, |
| 187 | 187 | 'guid' => (string) $item->guid, |
| 188 | 188 | ); |
| 189 | 189 | |
| 190 | - $dc = $item->children( 'http://purl.org/dc/elements/1.1/' ); |
|
| 190 | + $dc = $item->children('http://purl.org/dc/elements/1.1/'); |
|
| 191 | 191 | $post['post_author'] = (string) $dc->creator; |
| 192 | 192 | |
| 193 | - $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' ); |
|
| 194 | - $excerpt = $item->children( $namespaces['excerpt'] ); |
|
| 193 | + $content = $item->children('http://purl.org/rss/1.0/modules/content/'); |
|
| 194 | + $excerpt = $item->children($namespaces['excerpt']); |
|
| 195 | 195 | $post['post_content'] = (string) $content->encoded; |
| 196 | 196 | $post['post_excerpt'] = (string) $excerpt->encoded; |
| 197 | 197 | |
| 198 | - $wp = $item->children( $namespaces['wp'] ); |
|
| 198 | + $wp = $item->children($namespaces['wp']); |
|
| 199 | 199 | $post['post_id'] = (int) $wp->post_id; |
| 200 | 200 | $post['post_date'] = (string) $wp->post_date; |
| 201 | 201 | $post['post_date_gmt'] = (string) $wp->post_date_gmt; |
@@ -209,13 +209,13 @@ discard block |
||
| 209 | 209 | $post['post_password'] = (string) $wp->post_password; |
| 210 | 210 | $post['is_sticky'] = (int) $wp->is_sticky; |
| 211 | 211 | |
| 212 | - if ( isset($wp->attachment_url) ) { |
|
| 212 | + if (isset($wp->attachment_url)) { |
|
| 213 | 213 | $post['attachment_url'] = (string) $wp->attachment_url; |
| 214 | 214 | } |
| 215 | 215 | |
| 216 | - foreach ( $item->category as $c ) { |
|
| 216 | + foreach ($item->category as $c) { |
|
| 217 | 217 | $att = $c->attributes(); |
| 218 | - if ( isset( $att['nicename'] ) ) { |
|
| 218 | + if (isset($att['nicename'])) { |
|
| 219 | 219 | $post['terms'][] = array( |
| 220 | 220 | 'name' => (string) $c, |
| 221 | 221 | 'slug' => (string) $att['nicename'], |
@@ -224,17 +224,17 @@ discard block |
||
| 224 | 224 | } |
| 225 | 225 | } |
| 226 | 226 | |
| 227 | - foreach ( $wp->postmeta as $meta ) { |
|
| 227 | + foreach ($wp->postmeta as $meta) { |
|
| 228 | 228 | $post['postmeta'][] = array( |
| 229 | 229 | 'key' => (string) $meta->meta_key, |
| 230 | 230 | 'value' => (string) $meta->meta_value |
| 231 | 231 | ); |
| 232 | 232 | } |
| 233 | 233 | |
| 234 | - foreach ( $wp->comment as $comment ) { |
|
| 234 | + foreach ($wp->comment as $comment) { |
|
| 235 | 235 | $meta = array(); |
| 236 | - if ( isset( $comment->commentmeta ) ) { |
|
| 237 | - foreach ( $comment->commentmeta as $m ) { |
|
| 236 | + if (isset($comment->commentmeta)) { |
|
| 237 | + foreach ($comment->commentmeta as $m) { |
|
| 238 | 238 | $meta[] = array( |
| 239 | 239 | 'key' => (string) $m->meta_key, |
| 240 | 240 | 'value' => (string) $m->meta_value |
@@ -288,32 +288,32 @@ discard block |
||
| 288 | 288 | ); |
| 289 | 289 | var $wp_sub_tags = array( |
| 290 | 290 | 'wp:comment_id', 'wp:comment_author', 'wp:comment_author_email', 'wp:comment_author_url', |
| 291 | - 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content', |
|
| 291 | + 'wp:comment_author_IP', 'wp:comment_date', 'wp:comment_date_gmt', 'wp:comment_content', |
|
| 292 | 292 | 'wp:comment_approved', 'wp:comment_type', 'wp:comment_parent', 'wp:comment_user_id', |
| 293 | 293 | ); |
| 294 | 294 | |
| 295 | - function parse( $file ) { |
|
| 295 | + function parse($file) { |
|
| 296 | 296 | $this->wxr_version = $this->in_post = $this->cdata = $this->data = $this->sub_data = $this->in_tag = $this->in_sub_tag = false; |
| 297 | 297 | $this->authors = $this->posts = $this->term = $this->category = $this->tag = array(); |
| 298 | 298 | |
| 299 | - $xml = xml_parser_create( 'UTF-8' ); |
|
| 300 | - xml_parser_set_option( $xml, XML_OPTION_SKIP_WHITE, 1 ); |
|
| 301 | - xml_parser_set_option( $xml, XML_OPTION_CASE_FOLDING, 0 ); |
|
| 302 | - xml_set_object( $xml, $this ); |
|
| 303 | - xml_set_character_data_handler( $xml, 'cdata' ); |
|
| 304 | - xml_set_element_handler( $xml, 'tag_open', 'tag_close' ); |
|
| 299 | + $xml = xml_parser_create('UTF-8'); |
|
| 300 | + xml_parser_set_option($xml, XML_OPTION_SKIP_WHITE, 1); |
|
| 301 | + xml_parser_set_option($xml, XML_OPTION_CASE_FOLDING, 0); |
|
| 302 | + xml_set_object($xml, $this); |
|
| 303 | + xml_set_character_data_handler($xml, 'cdata'); |
|
| 304 | + xml_set_element_handler($xml, 'tag_open', 'tag_close'); |
|
| 305 | 305 | |
| 306 | - if ( ! xml_parse( $xml, file_get_contents( $file ), true ) ) { |
|
| 307 | - $current_line = xml_get_current_line_number( $xml ); |
|
| 308 | - $current_column = xml_get_current_column_number( $xml ); |
|
| 309 | - $error_code = xml_get_error_code( $xml ); |
|
| 310 | - $error_string = xml_error_string( $error_code ); |
|
| 311 | - return new WP_Error( 'XML_parse_error', 'There was an error when reading this WXR file', array( $current_line, $current_column, $error_string ) ); |
|
| 306 | + if ( ! xml_parse($xml, file_get_contents($file), true)) { |
|
| 307 | + $current_line = xml_get_current_line_number($xml); |
|
| 308 | + $current_column = xml_get_current_column_number($xml); |
|
| 309 | + $error_code = xml_get_error_code($xml); |
|
| 310 | + $error_string = xml_error_string($error_code); |
|
| 311 | + return new WP_Error('XML_parse_error', 'There was an error when reading this WXR file', array($current_line, $current_column, $error_string)); |
|
| 312 | 312 | } |
| 313 | - xml_parser_free( $xml ); |
|
| 313 | + xml_parser_free($xml); |
|
| 314 | 314 | |
| 315 | - if ( ! preg_match( '/^\d+\.\d+$/', $this->wxr_version ) ) { |
|
| 316 | - return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 315 | + if ( ! preg_match('/^\d+\.\d+$/', $this->wxr_version)) { |
|
| 316 | + return new WP_Error('WXR_parse_error', __('This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer')); |
|
| 317 | 317 | } |
| 318 | 318 | |
| 319 | 319 | return array( |
@@ -327,26 +327,26 @@ discard block |
||
| 327 | 327 | ); |
| 328 | 328 | } |
| 329 | 329 | |
| 330 | - function tag_open( $parse, $tag, $attr ) { |
|
| 331 | - if ( in_array( $tag, $this->wp_tags ) ) { |
|
| 332 | - $this->in_tag = substr( $tag, 3 ); |
|
| 330 | + function tag_open($parse, $tag, $attr) { |
|
| 331 | + if (in_array($tag, $this->wp_tags)) { |
|
| 332 | + $this->in_tag = substr($tag, 3); |
|
| 333 | 333 | return; |
| 334 | 334 | } |
| 335 | 335 | |
| 336 | - if ( in_array( $tag, $this->wp_sub_tags ) ) { |
|
| 337 | - $this->in_sub_tag = substr( $tag, 3 ); |
|
| 336 | + if (in_array($tag, $this->wp_sub_tags)) { |
|
| 337 | + $this->in_sub_tag = substr($tag, 3); |
|
| 338 | 338 | return; |
| 339 | 339 | } |
| 340 | 340 | |
| 341 | - switch ( $tag ) { |
|
| 341 | + switch ($tag) { |
|
| 342 | 342 | case 'category': |
| 343 | - if ( isset($attr['domain'], $attr['nicename']) ) { |
|
| 343 | + if (isset($attr['domain'], $attr['nicename'])) { |
|
| 344 | 344 | $this->sub_data['domain'] = $attr['domain']; |
| 345 | 345 | $this->sub_data['slug'] = $attr['nicename']; |
| 346 | 346 | } |
| 347 | 347 | break; |
| 348 | 348 | case 'item': $this->in_post = true; |
| 349 | - case 'title': if ( $this->in_post ) { |
|
| 349 | + case 'title': if ($this->in_post) { |
|
| 350 | 350 | $this->in_tag = 'post_title'; |
| 351 | 351 | } |
| 352 | 352 | break; |
@@ -361,23 +361,23 @@ discard block |
||
| 361 | 361 | } |
| 362 | 362 | } |
| 363 | 363 | |
| 364 | - function cdata( $parser, $cdata ) { |
|
| 365 | - if ( ! trim( $cdata ) ) { |
|
| 364 | + function cdata($parser, $cdata) { |
|
| 365 | + if ( ! trim($cdata)) { |
|
| 366 | 366 | return; |
| 367 | 367 | } |
| 368 | 368 | |
| 369 | - if ( false !== $this->in_tag || false !== $this->in_sub_tag ) { |
|
| 369 | + if (false !== $this->in_tag || false !== $this->in_sub_tag) { |
|
| 370 | 370 | $this->cdata .= $cdata; |
| 371 | 371 | } else { |
| 372 | - $this->cdata .= trim( $cdata ); |
|
| 372 | + $this->cdata .= trim($cdata); |
|
| 373 | 373 | } |
| 374 | 374 | } |
| 375 | 375 | |
| 376 | - function tag_close( $parser, $tag ) { |
|
| 377 | - switch ( $tag ) { |
|
| 376 | + function tag_close($parser, $tag) { |
|
| 377 | + switch ($tag) { |
|
| 378 | 378 | case 'wp:comment': |
| 379 | - unset( $this->sub_data['key'], $this->sub_data['value'] ); // remove meta sub_data |
|
| 380 | - if ( ! empty( $this->sub_data ) ) { |
|
| 379 | + unset($this->sub_data['key'], $this->sub_data['value']); // remove meta sub_data |
|
| 380 | + if ( ! empty($this->sub_data)) { |
|
| 381 | 381 | $this->data['comments'][] = $this->sub_data; |
| 382 | 382 | } |
| 383 | 383 | $this->sub_data = false; |
@@ -389,14 +389,14 @@ discard block |
||
| 389 | 389 | ); |
| 390 | 390 | break; |
| 391 | 391 | case 'category': |
| 392 | - if ( ! empty( $this->sub_data ) ) { |
|
| 392 | + if ( ! empty($this->sub_data)) { |
|
| 393 | 393 | $this->sub_data['name'] = $this->cdata; |
| 394 | 394 | $this->data['terms'][] = $this->sub_data; |
| 395 | 395 | } |
| 396 | 396 | $this->sub_data = false; |
| 397 | 397 | break; |
| 398 | 398 | case 'wp:postmeta': |
| 399 | - if ( ! empty( $this->sub_data ) ) { |
|
| 399 | + if ( ! empty($this->sub_data)) { |
|
| 400 | 400 | $this->data['postmeta'][] = $this->sub_data; |
| 401 | 401 | } |
| 402 | 402 | $this->sub_data = false; |
@@ -408,12 +408,12 @@ discard block |
||
| 408 | 408 | case 'wp:category': |
| 409 | 409 | case 'wp:tag': |
| 410 | 410 | case 'wp:term': |
| 411 | - $n = substr( $tag, 3 ); |
|
| 412 | - array_push( $this->$n, $this->data ); |
|
| 411 | + $n = substr($tag, 3); |
|
| 412 | + array_push($this->$n, $this->data); |
|
| 413 | 413 | $this->data = false; |
| 414 | 414 | break; |
| 415 | 415 | case 'wp:author': |
| 416 | - if ( ! empty($this->data['author_login']) ) { |
|
| 416 | + if ( ! empty($this->data['author_login'])) { |
|
| 417 | 417 | $this->authors[$this->data['author_login']] = $this->data; |
| 418 | 418 | } |
| 419 | 419 | $this->data = false; |
@@ -426,11 +426,11 @@ discard block |
||
| 426 | 426 | break; |
| 427 | 427 | |
| 428 | 428 | default: |
| 429 | - if ( $this->in_sub_tag ) { |
|
| 430 | - $this->sub_data[$this->in_sub_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
| 429 | + if ($this->in_sub_tag) { |
|
| 430 | + $this->sub_data[$this->in_sub_tag] = ! empty($this->cdata) ? $this->cdata : ''; |
|
| 431 | 431 | $this->in_sub_tag = false; |
| 432 | - } else if ( $this->in_tag ) { |
|
| 433 | - $this->data[$this->in_tag] = ! empty( $this->cdata ) ? $this->cdata : ''; |
|
| 432 | + } else if ($this->in_tag) { |
|
| 433 | + $this->data[$this->in_tag] = ! empty($this->cdata) ? $this->cdata : ''; |
|
| 434 | 434 | $this->in_tag = false; |
| 435 | 435 | } |
| 436 | 436 | } |
@@ -451,58 +451,58 @@ discard block |
||
| 451 | 451 | var $base_url = ''; |
| 452 | 452 | |
| 453 | 453 | function __construct() { |
| 454 | - $this->has_gzip = is_callable( 'gzopen' ); |
|
| 454 | + $this->has_gzip = is_callable('gzopen'); |
|
| 455 | 455 | } |
| 456 | 456 | |
| 457 | - function parse( $file ) { |
|
| 457 | + function parse($file) { |
|
| 458 | 458 | $wxr_version = $in_post = false; |
| 459 | 459 | |
| 460 | - $fp = $this->fopen( $file, 'r' ); |
|
| 461 | - if ( $fp ) { |
|
| 462 | - while ( ! $this->feof( $fp ) ) { |
|
| 463 | - $importline = rtrim( $this->fgets( $fp ) ); |
|
| 460 | + $fp = $this->fopen($file, 'r'); |
|
| 461 | + if ($fp) { |
|
| 462 | + while ( ! $this->feof($fp)) { |
|
| 463 | + $importline = rtrim($this->fgets($fp)); |
|
| 464 | 464 | |
| 465 | - if ( ! $wxr_version && preg_match( '|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version ) ) { |
|
| 465 | + if ( ! $wxr_version && preg_match('|<wp:wxr_version>(\d+\.\d+)</wp:wxr_version>|', $importline, $version)) { |
|
| 466 | 466 | $wxr_version = $version[1]; |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | - if ( false !== strpos( $importline, '<wp:base_site_url>' ) ) { |
|
| 470 | - preg_match( '|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url ); |
|
| 469 | + if (false !== strpos($importline, '<wp:base_site_url>')) { |
|
| 470 | + preg_match('|<wp:base_site_url>(.*?)</wp:base_site_url>|is', $importline, $url); |
|
| 471 | 471 | $this->base_url = $url[1]; |
| 472 | 472 | continue; |
| 473 | 473 | } |
| 474 | - if ( false !== strpos( $importline, '<wp:category>' ) ) { |
|
| 475 | - preg_match( '|<wp:category>(.*?)</wp:category>|is', $importline, $category ); |
|
| 476 | - $this->categories[] = $this->process_category( $category[1] ); |
|
| 474 | + if (false !== strpos($importline, '<wp:category>')) { |
|
| 475 | + preg_match('|<wp:category>(.*?)</wp:category>|is', $importline, $category); |
|
| 476 | + $this->categories[] = $this->process_category($category[1]); |
|
| 477 | 477 | continue; |
| 478 | 478 | } |
| 479 | - if ( false !== strpos( $importline, '<wp:tag>' ) ) { |
|
| 480 | - preg_match( '|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag ); |
|
| 481 | - $this->tags[] = $this->process_tag( $tag[1] ); |
|
| 479 | + if (false !== strpos($importline, '<wp:tag>')) { |
|
| 480 | + preg_match('|<wp:tag>(.*?)</wp:tag>|is', $importline, $tag); |
|
| 481 | + $this->tags[] = $this->process_tag($tag[1]); |
|
| 482 | 482 | continue; |
| 483 | 483 | } |
| 484 | - if ( false !== strpos( $importline, '<wp:term>' ) ) { |
|
| 485 | - preg_match( '|<wp:term>(.*?)</wp:term>|is', $importline, $term ); |
|
| 486 | - $this->terms[] = $this->process_term( $term[1] ); |
|
| 484 | + if (false !== strpos($importline, '<wp:term>')) { |
|
| 485 | + preg_match('|<wp:term>(.*?)</wp:term>|is', $importline, $term); |
|
| 486 | + $this->terms[] = $this->process_term($term[1]); |
|
| 487 | 487 | continue; |
| 488 | 488 | } |
| 489 | - if ( false !== strpos( $importline, '<wp:author>' ) ) { |
|
| 490 | - preg_match( '|<wp:author>(.*?)</wp:author>|is', $importline, $author ); |
|
| 491 | - $a = $this->process_author( $author[1] ); |
|
| 489 | + if (false !== strpos($importline, '<wp:author>')) { |
|
| 490 | + preg_match('|<wp:author>(.*?)</wp:author>|is', $importline, $author); |
|
| 491 | + $a = $this->process_author($author[1]); |
|
| 492 | 492 | $this->authors[$a['author_login']] = $a; |
| 493 | 493 | continue; |
| 494 | 494 | } |
| 495 | - if ( false !== strpos( $importline, '<item>' ) ) { |
|
| 495 | + if (false !== strpos($importline, '<item>')) { |
|
| 496 | 496 | $post = ''; |
| 497 | 497 | $in_post = true; |
| 498 | 498 | continue; |
| 499 | 499 | } |
| 500 | - if ( false !== strpos( $importline, '</item>' ) ) { |
|
| 500 | + if (false !== strpos($importline, '</item>')) { |
|
| 501 | 501 | $in_post = false; |
| 502 | - $this->posts[] = $this->process_post( $post ); |
|
| 502 | + $this->posts[] = $this->process_post($post); |
|
| 503 | 503 | continue; |
| 504 | 504 | } |
| 505 | - if ( $in_post ) { |
|
| 505 | + if ($in_post) { |
|
| 506 | 506 | $post .= $importline . "\n"; |
| 507 | 507 | } |
| 508 | 508 | } |
@@ -510,8 +510,8 @@ discard block |
||
| 510 | 510 | $this->fclose($fp); |
| 511 | 511 | } |
| 512 | 512 | |
| 513 | - if ( ! $wxr_version ) { |
|
| 514 | - return new WP_Error( 'WXR_parse_error', __( 'This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer' ) ); |
|
| 513 | + if ( ! $wxr_version) { |
|
| 514 | + return new WP_Error('WXR_parse_error', __('This does not appear to be a WXR file, missing/invalid WXR version number', 'wordpress-importer')); |
|
| 515 | 515 | } |
| 516 | 516 | |
| 517 | 517 | return array( |
@@ -525,18 +525,18 @@ discard block |
||
| 525 | 525 | ); |
| 526 | 526 | } |
| 527 | 527 | |
| 528 | - function get_tag( $string, $tag ) { |
|
| 529 | - preg_match( "|<$tag.*?>(.*?)</$tag>|is", $string, $return ); |
|
| 530 | - if ( isset( $return[1] ) ) { |
|
| 531 | - if ( substr( $return[1], 0, 9 ) == '<![CDATA[' ) { |
|
| 532 | - if ( strpos( $return[1], ']]]]><![CDATA[>' ) !== false ) { |
|
| 533 | - preg_match_all( '|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches ); |
|
| 528 | + function get_tag($string, $tag) { |
|
| 529 | + preg_match("|<$tag.*?>(.*?)</$tag>|is", $string, $return); |
|
| 530 | + if (isset($return[1])) { |
|
| 531 | + if (substr($return[1], 0, 9) == '<![CDATA[') { |
|
| 532 | + if (strpos($return[1], ']]]]><![CDATA[>') !== false) { |
|
| 533 | + preg_match_all('|<!\[CDATA\[(.*?)\]\]>|s', $return[1], $matches); |
|
| 534 | 534 | $return = ''; |
| 535 | - foreach( $matches[1] as $match ) { |
|
| 535 | + foreach ($matches[1] as $match) { |
|
| 536 | 536 | $return .= $match; |
| 537 | 537 | } |
| 538 | 538 | } else { |
| 539 | - $return = preg_replace( '|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1] ); |
|
| 539 | + $return = preg_replace('|^<!\[CDATA\[(.*)\]\]>$|s', '$1', $return[1]); |
|
| 540 | 540 | } |
| 541 | 541 | } else { |
| 542 | 542 | $return = $return[1]; |
@@ -547,177 +547,177 @@ discard block |
||
| 547 | 547 | return $return; |
| 548 | 548 | } |
| 549 | 549 | |
| 550 | - function process_category( $c ) { |
|
| 550 | + function process_category($c) { |
|
| 551 | 551 | return array( |
| 552 | - 'term_id' => $this->get_tag( $c, 'wp:term_id' ), |
|
| 553 | - 'cat_name' => $this->get_tag( $c, 'wp:cat_name' ), |
|
| 554 | - 'category_nicename' => $this->get_tag( $c, 'wp:category_nicename' ), |
|
| 555 | - 'category_parent' => $this->get_tag( $c, 'wp:category_parent' ), |
|
| 556 | - 'category_description' => $this->get_tag( $c, 'wp:category_description' ), |
|
| 552 | + 'term_id' => $this->get_tag($c, 'wp:term_id'), |
|
| 553 | + 'cat_name' => $this->get_tag($c, 'wp:cat_name'), |
|
| 554 | + 'category_nicename' => $this->get_tag($c, 'wp:category_nicename'), |
|
| 555 | + 'category_parent' => $this->get_tag($c, 'wp:category_parent'), |
|
| 556 | + 'category_description' => $this->get_tag($c, 'wp:category_description'), |
|
| 557 | 557 | ); |
| 558 | 558 | } |
| 559 | 559 | |
| 560 | - function process_tag( $t ) { |
|
| 560 | + function process_tag($t) { |
|
| 561 | 561 | return array( |
| 562 | - 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
| 563 | - 'tag_name' => $this->get_tag( $t, 'wp:tag_name' ), |
|
| 564 | - 'tag_slug' => $this->get_tag( $t, 'wp:tag_slug' ), |
|
| 565 | - 'tag_description' => $this->get_tag( $t, 'wp:tag_description' ), |
|
| 562 | + 'term_id' => $this->get_tag($t, 'wp:term_id'), |
|
| 563 | + 'tag_name' => $this->get_tag($t, 'wp:tag_name'), |
|
| 564 | + 'tag_slug' => $this->get_tag($t, 'wp:tag_slug'), |
|
| 565 | + 'tag_description' => $this->get_tag($t, 'wp:tag_description'), |
|
| 566 | 566 | ); |
| 567 | 567 | } |
| 568 | 568 | |
| 569 | - function process_term( $t ) { |
|
| 569 | + function process_term($t) { |
|
| 570 | 570 | return array( |
| 571 | - 'term_id' => $this->get_tag( $t, 'wp:term_id' ), |
|
| 572 | - 'term_taxonomy' => $this->get_tag( $t, 'wp:term_taxonomy' ), |
|
| 573 | - 'slug' => $this->get_tag( $t, 'wp:term_slug' ), |
|
| 574 | - 'term_parent' => $this->get_tag( $t, 'wp:term_parent' ), |
|
| 575 | - 'term_name' => $this->get_tag( $t, 'wp:term_name' ), |
|
| 576 | - 'term_description' => $this->get_tag( $t, 'wp:term_description' ), |
|
| 571 | + 'term_id' => $this->get_tag($t, 'wp:term_id'), |
|
| 572 | + 'term_taxonomy' => $this->get_tag($t, 'wp:term_taxonomy'), |
|
| 573 | + 'slug' => $this->get_tag($t, 'wp:term_slug'), |
|
| 574 | + 'term_parent' => $this->get_tag($t, 'wp:term_parent'), |
|
| 575 | + 'term_name' => $this->get_tag($t, 'wp:term_name'), |
|
| 576 | + 'term_description' => $this->get_tag($t, 'wp:term_description'), |
|
| 577 | 577 | ); |
| 578 | 578 | } |
| 579 | 579 | |
| 580 | - function process_author( $a ) { |
|
| 580 | + function process_author($a) { |
|
| 581 | 581 | return array( |
| 582 | - 'author_id' => $this->get_tag( $a, 'wp:author_id' ), |
|
| 583 | - 'author_login' => $this->get_tag( $a, 'wp:author_login' ), |
|
| 584 | - 'author_email' => $this->get_tag( $a, 'wp:author_email' ), |
|
| 585 | - 'author_display_name' => $this->get_tag( $a, 'wp:author_display_name' ), |
|
| 586 | - 'author_first_name' => $this->get_tag( $a, 'wp:author_first_name' ), |
|
| 587 | - 'author_last_name' => $this->get_tag( $a, 'wp:author_last_name' ), |
|
| 582 | + 'author_id' => $this->get_tag($a, 'wp:author_id'), |
|
| 583 | + 'author_login' => $this->get_tag($a, 'wp:author_login'), |
|
| 584 | + 'author_email' => $this->get_tag($a, 'wp:author_email'), |
|
| 585 | + 'author_display_name' => $this->get_tag($a, 'wp:author_display_name'), |
|
| 586 | + 'author_first_name' => $this->get_tag($a, 'wp:author_first_name'), |
|
| 587 | + 'author_last_name' => $this->get_tag($a, 'wp:author_last_name'), |
|
| 588 | 588 | ); |
| 589 | 589 | } |
| 590 | 590 | |
| 591 | - function process_post( $post ) { |
|
| 592 | - $post_id = $this->get_tag( $post, 'wp:post_id' ); |
|
| 593 | - $post_title = $this->get_tag( $post, 'title' ); |
|
| 594 | - $post_date = $this->get_tag( $post, 'wp:post_date' ); |
|
| 595 | - $post_date_gmt = $this->get_tag( $post, 'wp:post_date_gmt' ); |
|
| 596 | - $comment_status = $this->get_tag( $post, 'wp:comment_status' ); |
|
| 597 | - $ping_status = $this->get_tag( $post, 'wp:ping_status' ); |
|
| 598 | - $status = $this->get_tag( $post, 'wp:status' ); |
|
| 599 | - $post_name = $this->get_tag( $post, 'wp:post_name' ); |
|
| 600 | - $post_parent = $this->get_tag( $post, 'wp:post_parent' ); |
|
| 601 | - $menu_order = $this->get_tag( $post, 'wp:menu_order' ); |
|
| 602 | - $post_type = $this->get_tag( $post, 'wp:post_type' ); |
|
| 603 | - $post_password = $this->get_tag( $post, 'wp:post_password' ); |
|
| 604 | - $is_sticky = $this->get_tag( $post, 'wp:is_sticky' ); |
|
| 605 | - $guid = $this->get_tag( $post, 'guid' ); |
|
| 606 | - $post_author = $this->get_tag( $post, 'dc:creator' ); |
|
| 607 | - |
|
| 608 | - $post_excerpt = $this->get_tag( $post, 'excerpt:encoded' ); |
|
| 609 | - $post_excerpt = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_excerpt ); |
|
| 610 | - $post_excerpt = str_replace( '<br>', '<br />', $post_excerpt ); |
|
| 611 | - $post_excerpt = str_replace( '<hr>', '<hr />', $post_excerpt ); |
|
| 612 | - |
|
| 613 | - $post_content = $this->get_tag( $post, 'content:encoded' ); |
|
| 614 | - $post_content = preg_replace_callback( '|<(/?[A-Z]+)|', array( &$this, '_normalize_tag' ), $post_content ); |
|
| 615 | - $post_content = str_replace( '<br>', '<br />', $post_content ); |
|
| 616 | - $post_content = str_replace( '<hr>', '<hr />', $post_content ); |
|
| 617 | - |
|
| 618 | - $postdata = compact( 'post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', |
|
| 591 | + function process_post($post) { |
|
| 592 | + $post_id = $this->get_tag($post, 'wp:post_id'); |
|
| 593 | + $post_title = $this->get_tag($post, 'title'); |
|
| 594 | + $post_date = $this->get_tag($post, 'wp:post_date'); |
|
| 595 | + $post_date_gmt = $this->get_tag($post, 'wp:post_date_gmt'); |
|
| 596 | + $comment_status = $this->get_tag($post, 'wp:comment_status'); |
|
| 597 | + $ping_status = $this->get_tag($post, 'wp:ping_status'); |
|
| 598 | + $status = $this->get_tag($post, 'wp:status'); |
|
| 599 | + $post_name = $this->get_tag($post, 'wp:post_name'); |
|
| 600 | + $post_parent = $this->get_tag($post, 'wp:post_parent'); |
|
| 601 | + $menu_order = $this->get_tag($post, 'wp:menu_order'); |
|
| 602 | + $post_type = $this->get_tag($post, 'wp:post_type'); |
|
| 603 | + $post_password = $this->get_tag($post, 'wp:post_password'); |
|
| 604 | + $is_sticky = $this->get_tag($post, 'wp:is_sticky'); |
|
| 605 | + $guid = $this->get_tag($post, 'guid'); |
|
| 606 | + $post_author = $this->get_tag($post, 'dc:creator'); |
|
| 607 | + |
|
| 608 | + $post_excerpt = $this->get_tag($post, 'excerpt:encoded'); |
|
| 609 | + $post_excerpt = preg_replace_callback('|<(/?[A-Z]+)|', array(&$this, '_normalize_tag'), $post_excerpt); |
|
| 610 | + $post_excerpt = str_replace('<br>', '<br />', $post_excerpt); |
|
| 611 | + $post_excerpt = str_replace('<hr>', '<hr />', $post_excerpt); |
|
| 612 | + |
|
| 613 | + $post_content = $this->get_tag($post, 'content:encoded'); |
|
| 614 | + $post_content = preg_replace_callback('|<(/?[A-Z]+)|', array(&$this, '_normalize_tag'), $post_content); |
|
| 615 | + $post_content = str_replace('<br>', '<br />', $post_content); |
|
| 616 | + $post_content = str_replace('<hr>', '<hr />', $post_content); |
|
| 617 | + |
|
| 618 | + $postdata = compact('post_id', 'post_author', 'post_date', 'post_date_gmt', 'post_content', 'post_excerpt', |
|
| 619 | 619 | 'post_title', 'status', 'post_name', 'comment_status', 'ping_status', 'guid', 'post_parent', |
| 620 | 620 | 'menu_order', 'post_type', 'post_password', 'is_sticky' |
| 621 | 621 | ); |
| 622 | 622 | |
| 623 | - $attachment_url = $this->get_tag( $post, 'wp:attachment_url' ); |
|
| 624 | - if ( $attachment_url ) { |
|
| 623 | + $attachment_url = $this->get_tag($post, 'wp:attachment_url'); |
|
| 624 | + if ($attachment_url) { |
|
| 625 | 625 | $postdata['attachment_url'] = $attachment_url; |
| 626 | 626 | } |
| 627 | 627 | |
| 628 | - preg_match_all( '|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER ); |
|
| 629 | - foreach ( $terms as $t ) { |
|
| 628 | + preg_match_all('|<category domain="([^"]+?)" nicename="([^"]+?)">(.+?)</category>|is', $post, $terms, PREG_SET_ORDER); |
|
| 629 | + foreach ($terms as $t) { |
|
| 630 | 630 | $post_terms[] = array( |
| 631 | 631 | 'slug' => $t[2], |
| 632 | 632 | 'domain' => $t[1], |
| 633 | - 'name' => str_replace( array( '<![CDATA[', ']]>' ), '', $t[3] ), |
|
| 633 | + 'name' => str_replace(array('<![CDATA[', ']]>'), '', $t[3]), |
|
| 634 | 634 | ); |
| 635 | 635 | } |
| 636 | - if ( ! empty( $post_terms ) ) { |
|
| 636 | + if ( ! empty($post_terms)) { |
|
| 637 | 637 | $postdata['terms'] = $post_terms; |
| 638 | 638 | } |
| 639 | 639 | |
| 640 | - preg_match_all( '|<wp:comment>(.+?)</wp:comment>|is', $post, $comments ); |
|
| 640 | + preg_match_all('|<wp:comment>(.+?)</wp:comment>|is', $post, $comments); |
|
| 641 | 641 | $comments = $comments[1]; |
| 642 | - if ( $comments ) { |
|
| 643 | - foreach ( $comments as $comment ) { |
|
| 644 | - preg_match_all( '|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta ); |
|
| 642 | + if ($comments) { |
|
| 643 | + foreach ($comments as $comment) { |
|
| 644 | + preg_match_all('|<wp:commentmeta>(.+?)</wp:commentmeta>|is', $comment, $commentmeta); |
|
| 645 | 645 | $commentmeta = $commentmeta[1]; |
| 646 | 646 | $c_meta = array(); |
| 647 | - foreach ( $commentmeta as $m ) { |
|
| 647 | + foreach ($commentmeta as $m) { |
|
| 648 | 648 | $c_meta[] = array( |
| 649 | - 'key' => $this->get_tag( $m, 'wp:meta_key' ), |
|
| 650 | - 'value' => $this->get_tag( $m, 'wp:meta_value' ), |
|
| 649 | + 'key' => $this->get_tag($m, 'wp:meta_key'), |
|
| 650 | + 'value' => $this->get_tag($m, 'wp:meta_value'), |
|
| 651 | 651 | ); |
| 652 | 652 | } |
| 653 | 653 | |
| 654 | 654 | $post_comments[] = array( |
| 655 | - 'comment_id' => $this->get_tag( $comment, 'wp:comment_id' ), |
|
| 656 | - 'comment_author' => $this->get_tag( $comment, 'wp:comment_author' ), |
|
| 657 | - 'comment_author_email' => $this->get_tag( $comment, 'wp:comment_author_email' ), |
|
| 658 | - 'comment_author_IP' => $this->get_tag( $comment, 'wp:comment_author_IP' ), |
|
| 659 | - 'comment_author_url' => $this->get_tag( $comment, 'wp:comment_author_url' ), |
|
| 660 | - 'comment_date' => $this->get_tag( $comment, 'wp:comment_date' ), |
|
| 661 | - 'comment_date_gmt' => $this->get_tag( $comment, 'wp:comment_date_gmt' ), |
|
| 662 | - 'comment_content' => $this->get_tag( $comment, 'wp:comment_content' ), |
|
| 663 | - 'comment_approved' => $this->get_tag( $comment, 'wp:comment_approved' ), |
|
| 664 | - 'comment_type' => $this->get_tag( $comment, 'wp:comment_type' ), |
|
| 665 | - 'comment_parent' => $this->get_tag( $comment, 'wp:comment_parent' ), |
|
| 666 | - 'comment_user_id' => $this->get_tag( $comment, 'wp:comment_user_id' ), |
|
| 655 | + 'comment_id' => $this->get_tag($comment, 'wp:comment_id'), |
|
| 656 | + 'comment_author' => $this->get_tag($comment, 'wp:comment_author'), |
|
| 657 | + 'comment_author_email' => $this->get_tag($comment, 'wp:comment_author_email'), |
|
| 658 | + 'comment_author_IP' => $this->get_tag($comment, 'wp:comment_author_IP'), |
|
| 659 | + 'comment_author_url' => $this->get_tag($comment, 'wp:comment_author_url'), |
|
| 660 | + 'comment_date' => $this->get_tag($comment, 'wp:comment_date'), |
|
| 661 | + 'comment_date_gmt' => $this->get_tag($comment, 'wp:comment_date_gmt'), |
|
| 662 | + 'comment_content' => $this->get_tag($comment, 'wp:comment_content'), |
|
| 663 | + 'comment_approved' => $this->get_tag($comment, 'wp:comment_approved'), |
|
| 664 | + 'comment_type' => $this->get_tag($comment, 'wp:comment_type'), |
|
| 665 | + 'comment_parent' => $this->get_tag($comment, 'wp:comment_parent'), |
|
| 666 | + 'comment_user_id' => $this->get_tag($comment, 'wp:comment_user_id'), |
|
| 667 | 667 | 'commentmeta' => $c_meta, |
| 668 | 668 | ); |
| 669 | 669 | } |
| 670 | 670 | } |
| 671 | - if ( ! empty( $post_comments ) ) { |
|
| 671 | + if ( ! empty($post_comments)) { |
|
| 672 | 672 | $postdata['comments'] = $post_comments; |
| 673 | 673 | } |
| 674 | 674 | |
| 675 | - preg_match_all( '|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta ); |
|
| 675 | + preg_match_all('|<wp:postmeta>(.+?)</wp:postmeta>|is', $post, $postmeta); |
|
| 676 | 676 | $postmeta = $postmeta[1]; |
| 677 | - if ( $postmeta ) { |
|
| 678 | - foreach ( $postmeta as $p ) { |
|
| 677 | + if ($postmeta) { |
|
| 678 | + foreach ($postmeta as $p) { |
|
| 679 | 679 | $post_postmeta[] = array( |
| 680 | - 'key' => $this->get_tag( $p, 'wp:meta_key' ), |
|
| 681 | - 'value' => $this->get_tag( $p, 'wp:meta_value' ), |
|
| 680 | + 'key' => $this->get_tag($p, 'wp:meta_key'), |
|
| 681 | + 'value' => $this->get_tag($p, 'wp:meta_value'), |
|
| 682 | 682 | ); |
| 683 | 683 | } |
| 684 | 684 | } |
| 685 | - if ( ! empty( $post_postmeta ) ) { |
|
| 685 | + if ( ! empty($post_postmeta)) { |
|
| 686 | 686 | $postdata['postmeta'] = $post_postmeta; |
| 687 | 687 | } |
| 688 | 688 | |
| 689 | 689 | return $postdata; |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | - function _normalize_tag( $matches ) { |
|
| 693 | - return '<' . strtolower( $matches[1] ); |
|
| 692 | + function _normalize_tag($matches) { |
|
| 693 | + return '<' . strtolower($matches[1]); |
|
| 694 | 694 | } |
| 695 | 695 | |
| 696 | - function fopen( $filename, $mode = 'r' ) { |
|
| 697 | - if ( $this->has_gzip ) { |
|
| 698 | - return gzopen( $filename, $mode ); |
|
| 696 | + function fopen($filename, $mode = 'r') { |
|
| 697 | + if ($this->has_gzip) { |
|
| 698 | + return gzopen($filename, $mode); |
|
| 699 | 699 | } |
| 700 | - return fopen( $filename, $mode ); |
|
| 700 | + return fopen($filename, $mode); |
|
| 701 | 701 | } |
| 702 | 702 | |
| 703 | - function feof( $fp ) { |
|
| 704 | - if ( $this->has_gzip ) { |
|
| 705 | - return gzeof( $fp ); |
|
| 703 | + function feof($fp) { |
|
| 704 | + if ($this->has_gzip) { |
|
| 705 | + return gzeof($fp); |
|
| 706 | 706 | } |
| 707 | - return feof( $fp ); |
|
| 707 | + return feof($fp); |
|
| 708 | 708 | } |
| 709 | 709 | |
| 710 | - function fgets( $fp, $len = 8192 ) { |
|
| 711 | - if ( $this->has_gzip ) { |
|
| 712 | - return gzgets( $fp, $len ); |
|
| 710 | + function fgets($fp, $len = 8192) { |
|
| 711 | + if ($this->has_gzip) { |
|
| 712 | + return gzgets($fp, $len); |
|
| 713 | 713 | } |
| 714 | - return fgets( $fp, $len ); |
|
| 714 | + return fgets($fp, $len); |
|
| 715 | 715 | } |
| 716 | 716 | |
| 717 | - function fclose( $fp ) { |
|
| 718 | - if ( $this->has_gzip ) { |
|
| 719 | - return gzclose( $fp ); |
|
| 717 | + function fclose($fp) { |
|
| 718 | + if ($this->has_gzip) { |
|
| 719 | + return gzclose($fp); |
|
| 720 | 720 | } |
| 721 | - return fclose( $fp ); |
|
| 721 | + return fclose($fp); |
|
| 722 | 722 | } |
| 723 | 723 | } |
@@ -1215,7 +1215,7 @@ |
||
| 1215 | 1215 | * Decide if the given meta key maps to information we will want to import |
| 1216 | 1216 | * |
| 1217 | 1217 | * @param string $key The meta key to check |
| 1218 | - * @return string|bool The key if we do want to import, false if not |
|
| 1218 | + * @return false|string The key if we do want to import, false if not |
|
| 1219 | 1219 | */ |
| 1220 | 1220 | function is_valid_meta_key( $key ) { |
| 1221 | 1221 | // skip attachment metadata since we'll regenerate it from scratch |
@@ -11,7 +11,7 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | 13 | if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) { |
| 14 | - return; |
|
| 14 | + return; |
|
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** Display verbose errors */ |
@@ -21,11 +21,11 @@ discard block |
||
| 21 | 21 | require_once ABSPATH . 'wp-admin/includes/import.php'; |
| 22 | 22 | |
| 23 | 23 | if ( ! class_exists( 'WP_Importer' ) ) { |
| 24 | - $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; |
|
| 25 | - if ( file_exists( $class_wp_importer ) ) { |
|
| 26 | - require $class_wp_importer; |
|
| 27 | - } |
|
| 28 | - } |
|
| 24 | + $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; |
|
| 25 | + if ( file_exists( $class_wp_importer ) ) { |
|
| 26 | + require $class_wp_importer; |
|
| 27 | + } |
|
| 28 | + } |
|
| 29 | 29 | |
| 30 | 30 | // include WXR file parsers |
| 31 | 31 | require dirname( __FILE__ ) . '/parsers.php'; |
@@ -38,227 +38,227 @@ discard block |
||
| 38 | 38 | */ |
| 39 | 39 | if ( class_exists( 'WP_Importer' ) ) { |
| 40 | 40 | class WP_Import extends WP_Importer { |
| 41 | - var $max_wxr_version = 1.2; // max. supported WXR version |
|
| 42 | - |
|
| 43 | - var $id; // WXR attachment ID |
|
| 44 | - |
|
| 45 | - // information to import from WXR file |
|
| 46 | - var $version; |
|
| 47 | - var $authors = array(); |
|
| 48 | - var $posts = array(); |
|
| 49 | - var $terms = array(); |
|
| 50 | - var $categories = array(); |
|
| 51 | - var $tags = array(); |
|
| 52 | - var $base_url = ''; |
|
| 53 | - |
|
| 54 | - // mappings from old information to new |
|
| 55 | - var $processed_authors = array(); |
|
| 56 | - var $author_mapping = array(); |
|
| 57 | - var $processed_terms = array(); |
|
| 58 | - var $processed_posts = array(); |
|
| 59 | - var $post_orphans = array(); |
|
| 60 | - var $processed_menu_items = array(); |
|
| 61 | - var $menu_item_orphans = array(); |
|
| 62 | - var $missing_menu_items = array(); |
|
| 63 | - |
|
| 64 | - var $fetch_attachments = false; |
|
| 65 | - var $url_remap = array(); |
|
| 66 | - var $featured_images = array(); |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * Registered callback function for the WordPress Importer |
|
| 70 | - * |
|
| 71 | - * Manages the three separate stages of the WXR import process |
|
| 72 | - */ |
|
| 73 | - function dispatch() { |
|
| 74 | - $this->header(); |
|
| 75 | - |
|
| 76 | - $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step']; |
|
| 77 | - switch ( $step ) { |
|
| 78 | - case 0: |
|
| 79 | - $this->greet(); |
|
| 80 | - break; |
|
| 81 | - case 1: |
|
| 82 | - check_admin_referer( 'import-upload' ); |
|
| 83 | - if ( $this->handle_upload() ) { |
|
| 84 | - $this->import_options(); |
|
| 85 | - } |
|
| 86 | - break; |
|
| 87 | - case 2: |
|
| 88 | - check_admin_referer( 'import-wordpress' ); |
|
| 89 | - $this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() ); |
|
| 90 | - $this->id = (int) $_POST['import_id']; |
|
| 91 | - $file = get_attached_file( $this->id ); |
|
| 92 | - set_time_limit(0); |
|
| 93 | - $this->import( $file ); |
|
| 94 | - break; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - $this->footer(); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * The main controller for the actual import stage. |
|
| 102 | - * |
|
| 103 | - * @param string $file Path to the WXR file for importing |
|
| 104 | - */ |
|
| 105 | - function import( $file ) { |
|
| 106 | - add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) ); |
|
| 107 | - add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) ); |
|
| 108 | - |
|
| 109 | - $this->import_start( $file ); |
|
| 110 | - |
|
| 111 | - $this->get_author_mapping(); |
|
| 112 | - |
|
| 113 | - wp_suspend_cache_invalidation( true ); |
|
| 114 | - $this->process_categories(); |
|
| 115 | - $this->process_tags(); |
|
| 116 | - $this->process_terms(); |
|
| 117 | - $this->process_posts(); |
|
| 118 | - wp_suspend_cache_invalidation( false ); |
|
| 119 | - |
|
| 120 | - // update incorrect/missing information in the DB |
|
| 121 | - $this->backfill_parents(); |
|
| 122 | - $this->backfill_attachment_urls(); |
|
| 123 | - $this->remap_featured_images(); |
|
| 124 | - |
|
| 125 | - $this->import_end(); |
|
| 126 | - } |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * Parses the WXR file and prepares us for the task of processing parsed data |
|
| 130 | - * |
|
| 131 | - * @param string $file Path to the WXR file for importing |
|
| 132 | - */ |
|
| 133 | - function import_start( $file ) { |
|
| 134 | - if ( ! is_file($file) ) { |
|
| 135 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 136 | - echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>'; |
|
| 137 | - $this->footer(); |
|
| 138 | - die(); |
|
| 139 | - } |
|
| 140 | - |
|
| 141 | - $import_data = $this->parse( $file ); |
|
| 142 | - |
|
| 143 | - if ( is_wp_error( $import_data ) ) { |
|
| 144 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 145 | - echo esc_html( $import_data->get_error_message() ) . '</p>'; |
|
| 146 | - $this->footer(); |
|
| 147 | - die(); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - $this->version = $import_data['version']; |
|
| 151 | - $this->get_authors_from_import( $import_data ); |
|
| 152 | - $this->posts = $import_data['posts']; |
|
| 153 | - $this->terms = $import_data['terms']; |
|
| 154 | - $this->categories = $import_data['categories']; |
|
| 155 | - $this->tags = $import_data['tags']; |
|
| 156 | - $this->base_url = esc_url( $import_data['base_url'] ); |
|
| 157 | - |
|
| 158 | - wp_defer_term_counting( true ); |
|
| 159 | - wp_defer_comment_counting( true ); |
|
| 160 | - |
|
| 161 | - do_action( 'import_start' ); |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Performs post-import cleanup of files and the cache |
|
| 166 | - */ |
|
| 167 | - function import_end() { |
|
| 168 | - wp_import_cleanup( $this->id ); |
|
| 169 | - |
|
| 170 | - wp_cache_flush(); |
|
| 171 | - foreach ( get_taxonomies() as $tax ) { |
|
| 172 | - delete_option( "{$tax}_children" ); |
|
| 173 | - _get_term_hierarchy( $tax ); |
|
| 174 | - } |
|
| 175 | - |
|
| 176 | - wp_defer_term_counting( false ); |
|
| 177 | - wp_defer_comment_counting( false ); |
|
| 178 | - |
|
| 179 | - echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>'; |
|
| 180 | - echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>'; |
|
| 181 | - |
|
| 182 | - do_action( 'import_end' ); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Handles the WXR upload and initial parsing of the file to prepare for |
|
| 187 | - * displaying author import options |
|
| 188 | - * |
|
| 189 | - * @return bool False if error uploading or invalid file, true otherwise |
|
| 190 | - */ |
|
| 191 | - function handle_upload() { |
|
| 192 | - $file = wp_import_handle_upload(); |
|
| 193 | - |
|
| 194 | - if ( isset( $file['error'] ) ) { |
|
| 195 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 196 | - echo esc_html( $file['error'] ) . '</p>'; |
|
| 197 | - return false; |
|
| 198 | - } else if ( ! file_exists( $file['file'] ) ) { |
|
| 199 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 200 | - printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'wordpress-importer' ), esc_html( $file['file'] ) ); |
|
| 201 | - echo '</p>'; |
|
| 202 | - return false; |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - $this->id = (int) $file['id']; |
|
| 206 | - $import_data = $this->parse( $file['file'] ); |
|
| 207 | - if ( is_wp_error( $import_data ) ) { |
|
| 208 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 209 | - echo esc_html( $import_data->get_error_message() ) . '</p>'; |
|
| 210 | - return false; |
|
| 211 | - } |
|
| 212 | - |
|
| 213 | - $this->version = $import_data['version']; |
|
| 214 | - if ( $this->version > $this->max_wxr_version ) { |
|
| 215 | - echo '<div class="error"><p><strong>'; |
|
| 216 | - printf( __( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer' ), esc_html($import_data['version']) ); |
|
| 217 | - echo '</strong></p></div>'; |
|
| 218 | - } |
|
| 219 | - |
|
| 220 | - $this->get_authors_from_import( $import_data ); |
|
| 221 | - |
|
| 222 | - return true; |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - /** |
|
| 226 | - * Retrieve authors from parsed WXR data |
|
| 227 | - * |
|
| 228 | - * Uses the provided author information from WXR 1.1 files |
|
| 229 | - * or extracts info from each post for WXR 1.0 files |
|
| 230 | - * |
|
| 231 | - * @param array $import_data Data returned by a WXR parser |
|
| 232 | - */ |
|
| 233 | - function get_authors_from_import( $import_data ) { |
|
| 234 | - if ( ! empty( $import_data['authors'] ) ) { |
|
| 235 | - $this->authors = $import_data['authors']; |
|
| 236 | - // no author information, grab it from the posts |
|
| 237 | - } else { |
|
| 238 | - foreach ( $import_data['posts'] as $post ) { |
|
| 239 | - $login = sanitize_user( $post['post_author'], true ); |
|
| 240 | - if ( empty( $login ) ) { |
|
| 241 | - printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) ); |
|
| 242 | - echo '<br />'; |
|
| 243 | - continue; |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - if ( ! isset($this->authors[$login]) ) { |
|
| 247 | - $this->authors[$login] = array( |
|
| 248 | - 'author_login' => $login, |
|
| 249 | - 'author_display_name' => $post['post_author'] |
|
| 250 | - ); |
|
| 251 | - } |
|
| 252 | - } |
|
| 253 | - } |
|
| 254 | - } |
|
| 255 | - |
|
| 256 | - /** |
|
| 257 | - * Display pre-import options, author importing/mapping and option to |
|
| 258 | - * fetch attachments |
|
| 259 | - */ |
|
| 260 | - function import_options() { |
|
| 261 | - $j = 0; |
|
| 41 | + var $max_wxr_version = 1.2; // max. supported WXR version |
|
| 42 | + |
|
| 43 | + var $id; // WXR attachment ID |
|
| 44 | + |
|
| 45 | + // information to import from WXR file |
|
| 46 | + var $version; |
|
| 47 | + var $authors = array(); |
|
| 48 | + var $posts = array(); |
|
| 49 | + var $terms = array(); |
|
| 50 | + var $categories = array(); |
|
| 51 | + var $tags = array(); |
|
| 52 | + var $base_url = ''; |
|
| 53 | + |
|
| 54 | + // mappings from old information to new |
|
| 55 | + var $processed_authors = array(); |
|
| 56 | + var $author_mapping = array(); |
|
| 57 | + var $processed_terms = array(); |
|
| 58 | + var $processed_posts = array(); |
|
| 59 | + var $post_orphans = array(); |
|
| 60 | + var $processed_menu_items = array(); |
|
| 61 | + var $menu_item_orphans = array(); |
|
| 62 | + var $missing_menu_items = array(); |
|
| 63 | + |
|
| 64 | + var $fetch_attachments = false; |
|
| 65 | + var $url_remap = array(); |
|
| 66 | + var $featured_images = array(); |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * Registered callback function for the WordPress Importer |
|
| 70 | + * |
|
| 71 | + * Manages the three separate stages of the WXR import process |
|
| 72 | + */ |
|
| 73 | + function dispatch() { |
|
| 74 | + $this->header(); |
|
| 75 | + |
|
| 76 | + $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step']; |
|
| 77 | + switch ( $step ) { |
|
| 78 | + case 0: |
|
| 79 | + $this->greet(); |
|
| 80 | + break; |
|
| 81 | + case 1: |
|
| 82 | + check_admin_referer( 'import-upload' ); |
|
| 83 | + if ( $this->handle_upload() ) { |
|
| 84 | + $this->import_options(); |
|
| 85 | + } |
|
| 86 | + break; |
|
| 87 | + case 2: |
|
| 88 | + check_admin_referer( 'import-wordpress' ); |
|
| 89 | + $this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() ); |
|
| 90 | + $this->id = (int) $_POST['import_id']; |
|
| 91 | + $file = get_attached_file( $this->id ); |
|
| 92 | + set_time_limit(0); |
|
| 93 | + $this->import( $file ); |
|
| 94 | + break; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + $this->footer(); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * The main controller for the actual import stage. |
|
| 102 | + * |
|
| 103 | + * @param string $file Path to the WXR file for importing |
|
| 104 | + */ |
|
| 105 | + function import( $file ) { |
|
| 106 | + add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) ); |
|
| 107 | + add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) ); |
|
| 108 | + |
|
| 109 | + $this->import_start( $file ); |
|
| 110 | + |
|
| 111 | + $this->get_author_mapping(); |
|
| 112 | + |
|
| 113 | + wp_suspend_cache_invalidation( true ); |
|
| 114 | + $this->process_categories(); |
|
| 115 | + $this->process_tags(); |
|
| 116 | + $this->process_terms(); |
|
| 117 | + $this->process_posts(); |
|
| 118 | + wp_suspend_cache_invalidation( false ); |
|
| 119 | + |
|
| 120 | + // update incorrect/missing information in the DB |
|
| 121 | + $this->backfill_parents(); |
|
| 122 | + $this->backfill_attachment_urls(); |
|
| 123 | + $this->remap_featured_images(); |
|
| 124 | + |
|
| 125 | + $this->import_end(); |
|
| 126 | + } |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * Parses the WXR file and prepares us for the task of processing parsed data |
|
| 130 | + * |
|
| 131 | + * @param string $file Path to the WXR file for importing |
|
| 132 | + */ |
|
| 133 | + function import_start( $file ) { |
|
| 134 | + if ( ! is_file($file) ) { |
|
| 135 | + echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 136 | + echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>'; |
|
| 137 | + $this->footer(); |
|
| 138 | + die(); |
|
| 139 | + } |
|
| 140 | + |
|
| 141 | + $import_data = $this->parse( $file ); |
|
| 142 | + |
|
| 143 | + if ( is_wp_error( $import_data ) ) { |
|
| 144 | + echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 145 | + echo esc_html( $import_data->get_error_message() ) . '</p>'; |
|
| 146 | + $this->footer(); |
|
| 147 | + die(); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + $this->version = $import_data['version']; |
|
| 151 | + $this->get_authors_from_import( $import_data ); |
|
| 152 | + $this->posts = $import_data['posts']; |
|
| 153 | + $this->terms = $import_data['terms']; |
|
| 154 | + $this->categories = $import_data['categories']; |
|
| 155 | + $this->tags = $import_data['tags']; |
|
| 156 | + $this->base_url = esc_url( $import_data['base_url'] ); |
|
| 157 | + |
|
| 158 | + wp_defer_term_counting( true ); |
|
| 159 | + wp_defer_comment_counting( true ); |
|
| 160 | + |
|
| 161 | + do_action( 'import_start' ); |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Performs post-import cleanup of files and the cache |
|
| 166 | + */ |
|
| 167 | + function import_end() { |
|
| 168 | + wp_import_cleanup( $this->id ); |
|
| 169 | + |
|
| 170 | + wp_cache_flush(); |
|
| 171 | + foreach ( get_taxonomies() as $tax ) { |
|
| 172 | + delete_option( "{$tax}_children" ); |
|
| 173 | + _get_term_hierarchy( $tax ); |
|
| 174 | + } |
|
| 175 | + |
|
| 176 | + wp_defer_term_counting( false ); |
|
| 177 | + wp_defer_comment_counting( false ); |
|
| 178 | + |
|
| 179 | + echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>'; |
|
| 180 | + echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>'; |
|
| 181 | + |
|
| 182 | + do_action( 'import_end' ); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Handles the WXR upload and initial parsing of the file to prepare for |
|
| 187 | + * displaying author import options |
|
| 188 | + * |
|
| 189 | + * @return bool False if error uploading or invalid file, true otherwise |
|
| 190 | + */ |
|
| 191 | + function handle_upload() { |
|
| 192 | + $file = wp_import_handle_upload(); |
|
| 193 | + |
|
| 194 | + if ( isset( $file['error'] ) ) { |
|
| 195 | + echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 196 | + echo esc_html( $file['error'] ) . '</p>'; |
|
| 197 | + return false; |
|
| 198 | + } else if ( ! file_exists( $file['file'] ) ) { |
|
| 199 | + echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 200 | + printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'wordpress-importer' ), esc_html( $file['file'] ) ); |
|
| 201 | + echo '</p>'; |
|
| 202 | + return false; |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + $this->id = (int) $file['id']; |
|
| 206 | + $import_data = $this->parse( $file['file'] ); |
|
| 207 | + if ( is_wp_error( $import_data ) ) { |
|
| 208 | + echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 209 | + echo esc_html( $import_data->get_error_message() ) . '</p>'; |
|
| 210 | + return false; |
|
| 211 | + } |
|
| 212 | + |
|
| 213 | + $this->version = $import_data['version']; |
|
| 214 | + if ( $this->version > $this->max_wxr_version ) { |
|
| 215 | + echo '<div class="error"><p><strong>'; |
|
| 216 | + printf( __( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer' ), esc_html($import_data['version']) ); |
|
| 217 | + echo '</strong></p></div>'; |
|
| 218 | + } |
|
| 219 | + |
|
| 220 | + $this->get_authors_from_import( $import_data ); |
|
| 221 | + |
|
| 222 | + return true; |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + /** |
|
| 226 | + * Retrieve authors from parsed WXR data |
|
| 227 | + * |
|
| 228 | + * Uses the provided author information from WXR 1.1 files |
|
| 229 | + * or extracts info from each post for WXR 1.0 files |
|
| 230 | + * |
|
| 231 | + * @param array $import_data Data returned by a WXR parser |
|
| 232 | + */ |
|
| 233 | + function get_authors_from_import( $import_data ) { |
|
| 234 | + if ( ! empty( $import_data['authors'] ) ) { |
|
| 235 | + $this->authors = $import_data['authors']; |
|
| 236 | + // no author information, grab it from the posts |
|
| 237 | + } else { |
|
| 238 | + foreach ( $import_data['posts'] as $post ) { |
|
| 239 | + $login = sanitize_user( $post['post_author'], true ); |
|
| 240 | + if ( empty( $login ) ) { |
|
| 241 | + printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) ); |
|
| 242 | + echo '<br />'; |
|
| 243 | + continue; |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + if ( ! isset($this->authors[$login]) ) { |
|
| 247 | + $this->authors[$login] = array( |
|
| 248 | + 'author_login' => $login, |
|
| 249 | + 'author_display_name' => $post['post_author'] |
|
| 250 | + ); |
|
| 251 | + } |
|
| 252 | + } |
|
| 253 | + } |
|
| 254 | + } |
|
| 255 | + |
|
| 256 | + /** |
|
| 257 | + * Display pre-import options, author importing/mapping and option to |
|
| 258 | + * fetch attachments |
|
| 259 | + */ |
|
| 260 | + function import_options() { |
|
| 261 | + $j = 0; |
|
| 262 | 262 | ?> |
| 263 | 263 | <form action="<?php echo admin_url( 'admin.php?import=wordpress&step=2' ); ?>" method="post"> |
| 264 | 264 | <?php wp_nonce_field( 'import-wordpress' ); ?> |
@@ -288,999 +288,999 @@ discard block |
||
| 288 | 288 | <p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', 'wordpress-importer' ); ?>" /></p> |
| 289 | 289 | </form> |
| 290 | 290 | <?php |
| 291 | - } |
|
| 292 | - |
|
| 293 | - /** |
|
| 294 | - * Display import options for an individual author. That is, either create |
|
| 295 | - * a new user based on import info or map to an existing user |
|
| 296 | - * |
|
| 297 | - * @param int $n Index for each author in the form |
|
| 298 | - * @param array $author Author information, e.g. login, display name, email |
|
| 299 | - */ |
|
| 300 | - function author_select( $n, $author ) { |
|
| 301 | - _e( 'Import author:', 'wordpress-importer' ); |
|
| 302 | - echo ' <strong>' . esc_html( $author['author_display_name'] ); |
|
| 303 | - if ( $this->version != '1.0' ) { |
|
| 304 | - echo ' (' . esc_html( $author['author_login'] ) . ')'; |
|
| 305 | - } |
|
| 306 | - echo '</strong><br />'; |
|
| 307 | - |
|
| 308 | - if ( $this->version != '1.0' ) { |
|
| 309 | - echo '<div style="margin-left:18px">'; |
|
| 310 | - } |
|
| 311 | - |
|
| 312 | - $create_users = $this->allow_create_users(); |
|
| 313 | - if ( $create_users ) { |
|
| 314 | - if ( $this->version != '1.0' ) { |
|
| 315 | - _e( 'or create new user with login name:', 'wordpress-importer' ); |
|
| 316 | - $value = ''; |
|
| 317 | - } else { |
|
| 318 | - _e( 'as a new user:', 'wordpress-importer' ); |
|
| 319 | - $value = esc_attr( sanitize_user( $author['author_login'], true ) ); |
|
| 320 | - } |
|
| 321 | - |
|
| 322 | - echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />'; |
|
| 323 | - } |
|
| 324 | - |
|
| 325 | - if ( ! $create_users && $this->version == '1.0' ) { |
|
| 326 | - _e( 'assign posts to an existing user:', 'wordpress-importer' ); |
|
| 327 | - } else { |
|
| 328 | - _e( 'or assign posts to an existing user:', 'wordpress-importer' ); |
|
| 329 | - } |
|
| 330 | - wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) ); |
|
| 331 | - echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />'; |
|
| 332 | - |
|
| 333 | - if ( $this->version != '1.0' ) { |
|
| 334 | - echo '</div>'; |
|
| 335 | - } |
|
| 336 | - } |
|
| 337 | - |
|
| 338 | - /** |
|
| 339 | - * Map old author logins to local user IDs based on decisions made |
|
| 340 | - * in import options form. Can map to an existing user, create a new user |
|
| 341 | - * or falls back to the current user in case of error with either of the previous |
|
| 342 | - */ |
|
| 343 | - function get_author_mapping() { |
|
| 344 | - if ( ! isset( $_POST['imported_authors'] ) ) { |
|
| 345 | - return; |
|
| 346 | - } |
|
| 347 | - |
|
| 348 | - $create_users = $this->allow_create_users(); |
|
| 349 | - |
|
| 350 | - foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) { |
|
| 351 | - // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts. |
|
| 352 | - $santized_old_login = sanitize_user( $old_login, true ); |
|
| 353 | - $old_id = isset( $this->authors[$old_login]['author_id'] ) ? intval($this->authors[$old_login]['author_id']) : false; |
|
| 354 | - |
|
| 355 | - if ( ! empty( $_POST['user_map'][$i] ) ) { |
|
| 356 | - $user = get_userdata( intval($_POST['user_map'][$i]) ); |
|
| 357 | - if ( isset( $user->ID ) ) { |
|
| 358 | - if ( $old_id ) { |
|
| 359 | - $this->processed_authors[$old_id] = $user->ID; |
|
| 360 | - } |
|
| 361 | - $this->author_mapping[$santized_old_login] = $user->ID; |
|
| 362 | - } |
|
| 363 | - } else if ( $create_users ) { |
|
| 364 | - if ( ! empty($_POST['user_new'][$i]) ) { |
|
| 365 | - $user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() ); |
|
| 366 | - } else if ( $this->version != '1.0' ) { |
|
| 367 | - $user_data = array( |
|
| 368 | - 'user_login' => $old_login, |
|
| 369 | - 'user_pass' => wp_generate_password(), |
|
| 370 | - 'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '', |
|
| 371 | - 'display_name' => $this->authors[$old_login]['author_display_name'], |
|
| 372 | - 'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '', |
|
| 373 | - 'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '', |
|
| 374 | - ); |
|
| 375 | - $user_id = wp_insert_user( $user_data ); |
|
| 376 | - } |
|
| 377 | - |
|
| 378 | - if ( ! is_wp_error( $user_id ) ) { |
|
| 379 | - if ( $old_id ) { |
|
| 380 | - $this->processed_authors[$old_id] = $user_id; |
|
| 381 | - } |
|
| 382 | - $this->author_mapping[$santized_old_login] = $user_id; |
|
| 383 | - } else { |
|
| 384 | - printf( __( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html($this->authors[$old_login]['author_display_name']) ); |
|
| 385 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 386 | - echo ' ' . $user_id->get_error_message(); |
|
| 387 | - } |
|
| 388 | - echo '<br />'; |
|
| 389 | - } |
|
| 390 | - } |
|
| 391 | - |
|
| 392 | - // failsafe: if the user_id was invalid, default to the current user |
|
| 393 | - if ( ! isset( $this->author_mapping[$santized_old_login] ) ) { |
|
| 394 | - if ( $old_id ) { |
|
| 395 | - $this->processed_authors[$old_id] = (int) get_current_user_id(); |
|
| 396 | - } |
|
| 397 | - $this->author_mapping[$santized_old_login] = (int) get_current_user_id(); |
|
| 398 | - } |
|
| 399 | - } |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - /** |
|
| 403 | - * Create new categories based on import information |
|
| 404 | - * |
|
| 405 | - * Doesn't create a new category if its slug already exists |
|
| 406 | - */ |
|
| 407 | - function process_categories() { |
|
| 408 | - $this->categories = apply_filters( 'wp_import_categories', $this->categories ); |
|
| 409 | - |
|
| 410 | - if ( empty( $this->categories ) ) { |
|
| 411 | - return; |
|
| 412 | - } |
|
| 413 | - |
|
| 414 | - foreach ( $this->categories as $cat ) { |
|
| 415 | - // if the category already exists leave it alone |
|
| 416 | - $term_id = term_exists( $cat['category_nicename'], 'category' ); |
|
| 417 | - if ( $term_id ) { |
|
| 418 | - if ( is_array($term_id) ) { |
|
| 419 | - $term_id = $term_id['term_id']; |
|
| 420 | - } |
|
| 421 | - if ( isset($cat['term_id']) ) { |
|
| 422 | - $this->processed_terms[intval($cat['term_id'])] = (int) $term_id; |
|
| 423 | - } |
|
| 424 | - continue; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - $category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] ); |
|
| 428 | - $category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : ''; |
|
| 429 | - $catarr = array( |
|
| 430 | - 'category_nicename' => $cat['category_nicename'], |
|
| 431 | - 'category_parent' => $category_parent, |
|
| 432 | - 'cat_name' => $cat['cat_name'], |
|
| 433 | - 'category_description' => $category_description |
|
| 434 | - ); |
|
| 435 | - $catarr = wp_slash( $catarr ); |
|
| 436 | - |
|
| 437 | - $id = wp_insert_category( $catarr ); |
|
| 438 | - if ( ! is_wp_error( $id ) ) { |
|
| 439 | - if ( isset($cat['term_id']) ) { |
|
| 440 | - $this->processed_terms[intval($cat['term_id'])] = $id; |
|
| 441 | - } |
|
| 442 | - } else { |
|
| 443 | - printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) ); |
|
| 444 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 445 | - echo ': ' . $id->get_error_message(); |
|
| 446 | - } |
|
| 447 | - echo '<br />'; |
|
| 448 | - continue; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - $this->process_termmeta( $cat, $id['term_id'] ); |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - unset( $this->categories ); |
|
| 455 | - } |
|
| 456 | - |
|
| 457 | - /** |
|
| 458 | - * Create new post tags based on import information |
|
| 459 | - * |
|
| 460 | - * Doesn't create a tag if its slug already exists |
|
| 461 | - */ |
|
| 462 | - function process_tags() { |
|
| 463 | - $this->tags = apply_filters( 'wp_import_tags', $this->tags ); |
|
| 464 | - |
|
| 465 | - if ( empty( $this->tags ) ) { |
|
| 466 | - return; |
|
| 467 | - } |
|
| 468 | - |
|
| 469 | - foreach ( $this->tags as $tag ) { |
|
| 470 | - // if the tag already exists leave it alone |
|
| 471 | - $term_id = term_exists( $tag['tag_slug'], 'post_tag' ); |
|
| 472 | - if ( $term_id ) { |
|
| 473 | - if ( is_array($term_id) ) { |
|
| 474 | - $term_id = $term_id['term_id']; |
|
| 475 | - } |
|
| 476 | - if ( isset($tag['term_id']) ) { |
|
| 477 | - $this->processed_terms[intval($tag['term_id'])] = (int) $term_id; |
|
| 478 | - } |
|
| 479 | - continue; |
|
| 480 | - } |
|
| 481 | - |
|
| 482 | - $tag = wp_slash( $tag ); |
|
| 483 | - $tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : ''; |
|
| 484 | - $tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc ); |
|
| 485 | - |
|
| 486 | - $id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr ); |
|
| 487 | - if ( ! is_wp_error( $id ) ) { |
|
| 488 | - if ( isset($tag['term_id']) ) { |
|
| 489 | - $this->processed_terms[intval($tag['term_id'])] = $id['term_id']; |
|
| 490 | - } |
|
| 491 | - } else { |
|
| 492 | - printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) ); |
|
| 493 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 494 | - echo ': ' . $id->get_error_message(); |
|
| 495 | - } |
|
| 496 | - echo '<br />'; |
|
| 497 | - continue; |
|
| 498 | - } |
|
| 499 | - |
|
| 500 | - $this->process_termmeta( $tag, $id['term_id'] ); |
|
| 501 | - } |
|
| 502 | - |
|
| 503 | - unset( $this->tags ); |
|
| 504 | - } |
|
| 505 | - |
|
| 506 | - /** |
|
| 507 | - * Create new terms based on import information |
|
| 508 | - * |
|
| 509 | - * Doesn't create a term its slug already exists |
|
| 510 | - */ |
|
| 511 | - function process_terms() { |
|
| 512 | - $this->terms = apply_filters( 'wp_import_terms', $this->terms ); |
|
| 513 | - |
|
| 514 | - if ( empty( $this->terms ) ) { |
|
| 515 | - return; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - foreach ( $this->terms as $term ) { |
|
| 519 | - // if the term already exists in the correct taxonomy leave it alone |
|
| 520 | - $term_id = term_exists( $term['slug'], $term['term_taxonomy'] ); |
|
| 521 | - if ( $term_id ) { |
|
| 522 | - if ( is_array($term_id) ) { |
|
| 523 | - $term_id = $term_id['term_id']; |
|
| 524 | - } |
|
| 525 | - if ( isset($term['term_id']) ) { |
|
| 526 | - $this->processed_terms[intval($term['term_id'])] = (int) $term_id; |
|
| 527 | - } |
|
| 528 | - continue; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - if ( empty( $term['term_parent'] ) ) { |
|
| 532 | - $parent = 0; |
|
| 533 | - } else { |
|
| 534 | - $parent = term_exists( $term['term_parent'], $term['term_taxonomy'] ); |
|
| 535 | - if ( is_array( $parent ) ) { |
|
| 536 | - $parent = $parent['term_id']; |
|
| 537 | - } |
|
| 538 | - } |
|
| 539 | - $term = wp_slash( $term ); |
|
| 540 | - $description = isset( $term['term_description'] ) ? $term['term_description'] : ''; |
|
| 541 | - $termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) ); |
|
| 542 | - |
|
| 543 | - $id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr ); |
|
| 544 | - if ( ! is_wp_error( $id ) ) { |
|
| 545 | - if ( isset($term['term_id']) ) { |
|
| 546 | - $this->processed_terms[intval($term['term_id'])] = $id['term_id']; |
|
| 547 | - } |
|
| 548 | - } else { |
|
| 549 | - printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) ); |
|
| 550 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 551 | - echo ': ' . $id->get_error_message(); |
|
| 552 | - } |
|
| 553 | - echo '<br />'; |
|
| 554 | - continue; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - $this->process_termmeta( $term, $id['term_id'] ); |
|
| 558 | - } |
|
| 559 | - |
|
| 560 | - unset( $this->terms ); |
|
| 561 | - } |
|
| 562 | - |
|
| 563 | - /** |
|
| 564 | - * Add metadata to imported term. |
|
| 565 | - * |
|
| 566 | - * @since 0.6.2 |
|
| 567 | - * |
|
| 568 | - * @param array $term Term data from WXR import. |
|
| 569 | - * @param int $term_id ID of the newly created term. |
|
| 570 | - */ |
|
| 571 | - protected function process_termmeta( $term, $term_id ) { |
|
| 572 | - if ( ! isset( $term['termmeta'] ) ) { |
|
| 573 | - $term['termmeta'] = array(); |
|
| 574 | - } |
|
| 575 | - |
|
| 576 | - /** |
|
| 577 | - * Filters the metadata attached to an imported term. |
|
| 578 | - * |
|
| 579 | - * @since 0.6.2 |
|
| 580 | - * |
|
| 581 | - * @param array $termmeta Array of term meta. |
|
| 582 | - * @param int $term_id ID of the newly created term. |
|
| 583 | - * @param array $term Term data from the WXR import. |
|
| 584 | - */ |
|
| 585 | - $term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term ); |
|
| 586 | - |
|
| 587 | - if ( empty( $term['termmeta'] ) ) { |
|
| 588 | - return; |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - foreach ( $term['termmeta'] as $meta ) { |
|
| 592 | - /** |
|
| 593 | - * Filters the meta key for an imported piece of term meta. |
|
| 594 | - * |
|
| 595 | - * @since 0.6.2 |
|
| 596 | - * |
|
| 597 | - * @param string $meta_key Meta key. |
|
| 598 | - * @param int $term_id ID of the newly created term. |
|
| 599 | - * @param array $term Term data from the WXR import. |
|
| 600 | - */ |
|
| 601 | - $key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term ); |
|
| 602 | - if ( ! $key ) { |
|
| 603 | - continue; |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - // Export gets meta straight from the DB so could have a serialized string |
|
| 607 | - $value = maybe_unserialize( $meta['value'] ); |
|
| 608 | - |
|
| 609 | - add_term_meta( $term_id, $key, $value ); |
|
| 610 | - |
|
| 611 | - /** |
|
| 612 | - * Fires after term meta is imported. |
|
| 613 | - * |
|
| 614 | - * @since 0.6.2 |
|
| 615 | - * |
|
| 616 | - * @param int $term_id ID of the newly created term. |
|
| 617 | - * @param string $key Meta key. |
|
| 618 | - * @param mixed $value Meta value. |
|
| 619 | - */ |
|
| 620 | - do_action( 'import_term_meta', $term_id, $key, $value ); |
|
| 621 | - } |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - /** |
|
| 625 | - * Create new posts based on import information |
|
| 626 | - * |
|
| 627 | - * Posts marked as having a parent which doesn't exist will become top level items. |
|
| 628 | - * Doesn't create a new post if: the post type doesn't exist, the given post ID |
|
| 629 | - * is already noted as imported or a post with the same title and date already exists. |
|
| 630 | - * Note that new/updated terms, comments and meta are imported for the last of the above. |
|
| 631 | - */ |
|
| 632 | - function process_posts() { |
|
| 633 | - $this->posts = apply_filters( 'wp_import_posts', $this->posts ); |
|
| 634 | - |
|
| 635 | - foreach ( $this->posts as $post ) { |
|
| 636 | - $post = apply_filters( 'wp_import_post_data_raw', $post ); |
|
| 637 | - |
|
| 638 | - if ( ! post_type_exists( $post['post_type'] ) ) { |
|
| 639 | - printf( __( 'Failed to import “%s”: Invalid post type %s', 'wordpress-importer' ), |
|
| 640 | - esc_html($post['post_title']), esc_html($post['post_type']) ); |
|
| 641 | - echo '<br />'; |
|
| 642 | - do_action( 'wp_import_post_exists', $post ); |
|
| 643 | - continue; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) ) { |
|
| 647 | - continue; |
|
| 648 | - } |
|
| 649 | - |
|
| 650 | - if ( $post['status'] == 'auto-draft' ) { |
|
| 651 | - continue; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - if ( 'nav_menu_item' == $post['post_type'] ) { |
|
| 655 | - $this->process_menu_item( $post ); |
|
| 656 | - continue; |
|
| 657 | - } |
|
| 658 | - |
|
| 659 | - $post_type_object = get_post_type_object( $post['post_type'] ); |
|
| 660 | - |
|
| 661 | - $post_exists = post_exists( $post['post_title'], '', $post['post_date'] ); |
|
| 662 | - |
|
| 663 | - /** |
|
| 664 | - * Filter ID of the existing post corresponding to post currently importing. |
|
| 665 | - * |
|
| 666 | - * Return 0 to force the post to be imported. Filter the ID to be something else |
|
| 667 | - * to override which existing post is mapped to the imported post. |
|
| 668 | - * |
|
| 669 | - * @see post_exists() |
|
| 670 | - * @since 0.6.2 |
|
| 671 | - * |
|
| 672 | - * @param int $post_exists Post ID, or 0 if post did not exist. |
|
| 673 | - * @param array $post The post array to be inserted. |
|
| 674 | - */ |
|
| 675 | - $post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post ); |
|
| 676 | - |
|
| 677 | - if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) { |
|
| 678 | - printf( __('%s “%s” already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) ); |
|
| 679 | - echo '<br />'; |
|
| 680 | - $comment_post_ID = $post_id = $post_exists; |
|
| 681 | - $this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists ); |
|
| 682 | - } else { |
|
| 683 | - $post_parent = (int) $post['post_parent']; |
|
| 684 | - if ( $post_parent ) { |
|
| 685 | - // if we already know the parent, map it to the new local ID |
|
| 686 | - if ( isset( $this->processed_posts[$post_parent] ) ) { |
|
| 687 | - $post_parent = $this->processed_posts[$post_parent]; |
|
| 688 | - // otherwise record the parent for later |
|
| 689 | - } else { |
|
| 690 | - $this->post_orphans[intval($post['post_id'])] = $post_parent; |
|
| 691 | - $post_parent = 0; |
|
| 692 | - } |
|
| 693 | - } |
|
| 694 | - |
|
| 695 | - // map the post author |
|
| 696 | - $author = sanitize_user( $post['post_author'], true ); |
|
| 697 | - if ( isset( $this->author_mapping[$author] ) ) { |
|
| 698 | - $author = $this->author_mapping[$author]; |
|
| 699 | - } else { |
|
| 700 | - $author = (int) get_current_user_id(); |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - $postdata = array( |
|
| 704 | - 'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], |
|
| 705 | - 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], |
|
| 706 | - 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], |
|
| 707 | - 'post_status' => $post['status'], 'post_name' => $post['post_name'], |
|
| 708 | - 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], |
|
| 709 | - 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], |
|
| 710 | - 'post_type' => $post['post_type'], 'post_password' => $post['post_password'] |
|
| 711 | - ); |
|
| 712 | - |
|
| 713 | - $original_post_ID = $post['post_id']; |
|
| 714 | - $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post ); |
|
| 715 | - |
|
| 716 | - $postdata = wp_slash( $postdata ); |
|
| 717 | - |
|
| 718 | - if ( 'attachment' == $postdata['post_type'] ) { |
|
| 719 | - $remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; |
|
| 720 | - |
|
| 721 | - // try to use _wp_attached file for upload folder placement to ensure the same location as the export site |
|
| 722 | - // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() |
|
| 723 | - $postdata['upload_date'] = $post['post_date']; |
|
| 724 | - if ( isset( $post['postmeta'] ) ) { |
|
| 725 | - foreach( $post['postmeta'] as $meta ) { |
|
| 726 | - if ( $meta['key'] == '_wp_attached_file' ) { |
|
| 727 | - if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) ) { |
|
| 728 | - $postdata['upload_date'] = $matches[0]; |
|
| 729 | - } |
|
| 730 | - break; |
|
| 731 | - } |
|
| 732 | - } |
|
| 733 | - } |
|
| 734 | - |
|
| 735 | - $comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url ); |
|
| 736 | - } else { |
|
| 737 | - $comment_post_ID = $post_id = wp_insert_post( $postdata, true ); |
|
| 738 | - do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post ); |
|
| 739 | - } |
|
| 740 | - |
|
| 741 | - if ( is_wp_error( $post_id ) ) { |
|
| 742 | - printf( __( 'Failed to import %s “%s”', 'wordpress-importer' ), |
|
| 743 | - $post_type_object->labels->singular_name, esc_html($post['post_title']) ); |
|
| 744 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 745 | - echo ': ' . $post_id->get_error_message(); |
|
| 746 | - } |
|
| 747 | - echo '<br />'; |
|
| 748 | - continue; |
|
| 749 | - } |
|
| 750 | - |
|
| 751 | - if ( $post['is_sticky'] == 1 ) { |
|
| 752 | - stick_post( $post_id ); |
|
| 753 | - } |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - // map pre-import ID to local ID |
|
| 757 | - $this->processed_posts[intval($post['post_id'])] = (int) $post_id; |
|
| 758 | - |
|
| 759 | - if ( ! isset( $post['terms'] ) ) { |
|
| 760 | - $post['terms'] = array(); |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - $post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post ); |
|
| 764 | - |
|
| 765 | - // add categories, tags and other terms |
|
| 766 | - if ( ! empty( $post['terms'] ) ) { |
|
| 767 | - $terms_to_set = array(); |
|
| 768 | - foreach ( $post['terms'] as $term ) { |
|
| 769 | - // back compat with WXR 1.0 map 'tag' to 'post_tag' |
|
| 770 | - $taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain']; |
|
| 771 | - $term_exists = term_exists( $term['slug'], $taxonomy ); |
|
| 772 | - $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists; |
|
| 773 | - if ( ! $term_id ) { |
|
| 774 | - $t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) ); |
|
| 775 | - if ( ! is_wp_error( $t ) ) { |
|
| 776 | - $term_id = $t['term_id']; |
|
| 777 | - do_action( 'wp_import_insert_term', $t, $term, $post_id, $post ); |
|
| 778 | - } else { |
|
| 779 | - printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) ); |
|
| 780 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 781 | - echo ': ' . $t->get_error_message(); |
|
| 782 | - } |
|
| 783 | - echo '<br />'; |
|
| 784 | - do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post ); |
|
| 785 | - continue; |
|
| 786 | - } |
|
| 787 | - } |
|
| 788 | - $terms_to_set[$taxonomy][] = intval( $term_id ); |
|
| 789 | - } |
|
| 790 | - |
|
| 791 | - foreach ( $terms_to_set as $tax => $ids ) { |
|
| 792 | - $tt_ids = wp_set_post_terms( $post_id, $ids, $tax ); |
|
| 793 | - do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post ); |
|
| 794 | - } |
|
| 795 | - unset( $post['terms'], $terms_to_set ); |
|
| 796 | - } |
|
| 797 | - |
|
| 798 | - if ( ! isset( $post['comments'] ) ) { |
|
| 799 | - $post['comments'] = array(); |
|
| 800 | - } |
|
| 801 | - |
|
| 802 | - $post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post ); |
|
| 803 | - |
|
| 804 | - // add/update comments |
|
| 805 | - if ( ! empty( $post['comments'] ) ) { |
|
| 806 | - $num_comments = 0; |
|
| 807 | - $inserted_comments = array(); |
|
| 808 | - foreach ( $post['comments'] as $comment ) { |
|
| 809 | - $comment_id = $comment['comment_id']; |
|
| 810 | - $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; |
|
| 811 | - $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; |
|
| 812 | - $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; |
|
| 813 | - $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP']; |
|
| 814 | - $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url']; |
|
| 815 | - $newcomments[$comment_id]['comment_date'] = $comment['comment_date']; |
|
| 816 | - $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt']; |
|
| 817 | - $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; |
|
| 818 | - $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; |
|
| 819 | - $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; |
|
| 820 | - $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; |
|
| 821 | - $newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array(); |
|
| 822 | - if ( isset( $this->processed_authors[$comment['comment_user_id']] ) ) { |
|
| 823 | - $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']]; |
|
| 824 | - } |
|
| 825 | - } |
|
| 826 | - ksort( $newcomments ); |
|
| 827 | - |
|
| 828 | - foreach ( $newcomments as $key => $comment ) { |
|
| 829 | - // if this is a new post we can skip the comment_exists() check |
|
| 830 | - if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) { |
|
| 831 | - if ( isset( $inserted_comments[$comment['comment_parent']] ) ) { |
|
| 832 | - $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; |
|
| 833 | - } |
|
| 834 | - $comment = wp_filter_comment( $comment ); |
|
| 835 | - $inserted_comments[$key] = wp_insert_comment( $comment ); |
|
| 836 | - do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post ); |
|
| 837 | - |
|
| 838 | - foreach( $comment['commentmeta'] as $meta ) { |
|
| 839 | - $value = maybe_unserialize( $meta['value'] ); |
|
| 840 | - add_comment_meta( $inserted_comments[$key], $meta['key'], $value ); |
|
| 841 | - } |
|
| 842 | - |
|
| 843 | - $num_comments++; |
|
| 844 | - } |
|
| 845 | - } |
|
| 846 | - unset( $newcomments, $inserted_comments, $post['comments'] ); |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - if ( ! isset( $post['postmeta'] ) ) { |
|
| 850 | - $post['postmeta'] = array(); |
|
| 851 | - } |
|
| 852 | - |
|
| 853 | - $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post ); |
|
| 854 | - |
|
| 855 | - // add/update post meta |
|
| 856 | - if ( ! empty( $post['postmeta'] ) ) { |
|
| 857 | - foreach ( $post['postmeta'] as $meta ) { |
|
| 858 | - $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post ); |
|
| 859 | - $value = false; |
|
| 860 | - |
|
| 861 | - if ( '_edit_last' == $key ) { |
|
| 862 | - if ( isset( $this->processed_authors[intval($meta['value'])] ) ) { |
|
| 863 | - $value = $this->processed_authors[intval($meta['value'])]; |
|
| 864 | - } else { |
|
| 865 | - $key = false; |
|
| 866 | - } |
|
| 867 | - } |
|
| 868 | - |
|
| 869 | - if ( $key ) { |
|
| 870 | - // export gets meta straight from the DB so could have a serialized string |
|
| 871 | - if ( ! $value ) { |
|
| 872 | - $value = maybe_unserialize( $meta['value'] ); |
|
| 873 | - } |
|
| 874 | - |
|
| 875 | - add_post_meta( $post_id, $key, $value ); |
|
| 876 | - do_action( 'import_post_meta', $post_id, $key, $value ); |
|
| 877 | - |
|
| 878 | - // if the post has a featured image, take note of this in case of remap |
|
| 879 | - if ( '_thumbnail_id' == $key ) { |
|
| 880 | - $this->featured_images[$post_id] = (int) $value; |
|
| 881 | - } |
|
| 882 | - } |
|
| 883 | - } |
|
| 884 | - } |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - unset( $this->posts ); |
|
| 888 | - } |
|
| 889 | - |
|
| 890 | - /** |
|
| 891 | - * Attempt to create a new menu item from import data |
|
| 892 | - * |
|
| 893 | - * Fails for draft, orphaned menu items and those without an associated nav_menu |
|
| 894 | - * or an invalid nav_menu term. If the post type or term object which the menu item |
|
| 895 | - * represents doesn't exist then the menu item will not be imported (waits until the |
|
| 896 | - * end of the import to retry again before discarding). |
|
| 897 | - * |
|
| 898 | - * @param array $item Menu item details from WXR file |
|
| 899 | - */ |
|
| 900 | - function process_menu_item( $item ) { |
|
| 901 | - // skip draft, orphaned menu items |
|
| 902 | - if ( 'draft' == $item['status'] ) { |
|
| 903 | - return; |
|
| 904 | - } |
|
| 905 | - |
|
| 906 | - $menu_slug = false; |
|
| 907 | - if ( isset($item['terms']) ) { |
|
| 908 | - // loop through terms, assume first nav_menu term is correct menu |
|
| 909 | - foreach ( $item['terms'] as $term ) { |
|
| 910 | - if ( 'nav_menu' == $term['domain'] ) { |
|
| 911 | - $menu_slug = $term['slug']; |
|
| 912 | - break; |
|
| 913 | - } |
|
| 914 | - } |
|
| 915 | - } |
|
| 916 | - |
|
| 917 | - // no nav_menu term associated with this menu item |
|
| 918 | - if ( ! $menu_slug ) { |
|
| 919 | - _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' ); |
|
| 920 | - echo '<br />'; |
|
| 921 | - return; |
|
| 922 | - } |
|
| 923 | - |
|
| 924 | - $menu_id = term_exists( $menu_slug, 'nav_menu' ); |
|
| 925 | - if ( ! $menu_id ) { |
|
| 926 | - printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) ); |
|
| 927 | - echo '<br />'; |
|
| 928 | - return; |
|
| 929 | - } else { |
|
| 930 | - $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id; |
|
| 931 | - } |
|
| 932 | - |
|
| 933 | - foreach ( $item['postmeta'] as $meta ) { |
|
| 934 | - $$meta['key'] = $meta['value']; |
|
| 935 | - } |
|
| 936 | - |
|
| 937 | - if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) { |
|
| 938 | - $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)]; |
|
| 939 | - } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) { |
|
| 940 | - $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)]; |
|
| 941 | - } else if ( 'custom' != $_menu_item_type ) { |
|
| 942 | - // associated object is missing or not imported yet, we'll retry later |
|
| 943 | - $this->missing_menu_items[] = $item; |
|
| 944 | - return; |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) { |
|
| 948 | - $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)]; |
|
| 949 | - } else if ( $_menu_item_menu_item_parent ) { |
|
| 950 | - $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent; |
|
| 951 | - $_menu_item_menu_item_parent = 0; |
|
| 952 | - } |
|
| 953 | - |
|
| 954 | - // wp_update_nav_menu_item expects CSS classes as a space separated string |
|
| 955 | - $_menu_item_classes = maybe_unserialize( $_menu_item_classes ); |
|
| 956 | - if ( is_array( $_menu_item_classes ) ) { |
|
| 957 | - $_menu_item_classes = implode( ' ', $_menu_item_classes ); |
|
| 958 | - } |
|
| 959 | - |
|
| 960 | - $args = array( |
|
| 961 | - 'menu-item-object-id' => $_menu_item_object_id, |
|
| 962 | - 'menu-item-object' => $_menu_item_object, |
|
| 963 | - 'menu-item-parent-id' => $_menu_item_menu_item_parent, |
|
| 964 | - 'menu-item-position' => intval( $item['menu_order'] ), |
|
| 965 | - 'menu-item-type' => $_menu_item_type, |
|
| 966 | - 'menu-item-title' => $item['post_title'], |
|
| 967 | - 'menu-item-url' => $_menu_item_url, |
|
| 968 | - 'menu-item-description' => $item['post_content'], |
|
| 969 | - 'menu-item-attr-title' => $item['post_excerpt'], |
|
| 970 | - 'menu-item-target' => $_menu_item_target, |
|
| 971 | - 'menu-item-classes' => $_menu_item_classes, |
|
| 972 | - 'menu-item-xfn' => $_menu_item_xfn, |
|
| 973 | - 'menu-item-status' => $item['status'] |
|
| 974 | - ); |
|
| 975 | - |
|
| 976 | - $id = wp_update_nav_menu_item( $menu_id, 0, $args ); |
|
| 977 | - if ( $id && ! is_wp_error( $id ) ) { |
|
| 978 | - $this->processed_menu_items[intval($item['post_id'])] = (int) $id; |
|
| 979 | - } |
|
| 980 | - } |
|
| 981 | - |
|
| 982 | - /** |
|
| 983 | - * If fetching attachments is enabled then attempt to create a new attachment |
|
| 984 | - * |
|
| 985 | - * @param array $post Attachment post details from WXR |
|
| 986 | - * @param string $url URL to fetch attachment from |
|
| 987 | - * @return int|WP_Error Post ID on success, WP_Error otherwise |
|
| 988 | - */ |
|
| 989 | - function process_attachment( $post, $url ) { |
|
| 990 | - if ( ! $this->fetch_attachments ) { |
|
| 991 | - return new WP_Error( 'attachment_processing_error', |
|
| 992 | - __( 'Fetching attachments is not enabled', 'wordpress-importer' ) ); |
|
| 993 | - } |
|
| 994 | - |
|
| 995 | - // if the URL is absolute, but does not contain address, then upload it assuming base_site_url |
|
| 996 | - if ( preg_match( '|^/[\w\W]+$|', $url ) ) { |
|
| 997 | - $url = rtrim( $this->base_url, '/' ) . $url; |
|
| 998 | - } |
|
| 999 | - |
|
| 1000 | - $upload = $this->fetch_remote_file( $url, $post ); |
|
| 1001 | - if ( is_wp_error( $upload ) ) { |
|
| 1002 | - return $upload; |
|
| 1003 | - } |
|
| 1004 | - |
|
| 1005 | - if ( $info = wp_check_filetype( $upload['file'] ) ) { |
|
| 1006 | - $post['post_mime_type'] = $info['type']; |
|
| 1007 | - } else { |
|
| 1008 | - return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') ); |
|
| 1009 | - } |
|
| 1010 | - |
|
| 1011 | - $post['guid'] = $upload['url']; |
|
| 1012 | - |
|
| 1013 | - // as per wp-admin/includes/upload.php |
|
| 1014 | - $post_id = wp_insert_attachment( $post, $upload['file'] ); |
|
| 1015 | - wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) ); |
|
| 1016 | - |
|
| 1017 | - // remap resized image URLs, works by stripping the extension and remapping the URL stub. |
|
| 1018 | - if ( preg_match( '!^image/!', $info['type'] ) ) { |
|
| 1019 | - $parts = pathinfo( $url ); |
|
| 1020 | - $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2 |
|
| 1021 | - |
|
| 1022 | - $parts_new = pathinfo( $upload['url'] ); |
|
| 1023 | - $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" ); |
|
| 1024 | - |
|
| 1025 | - $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new; |
|
| 1026 | - } |
|
| 1027 | - |
|
| 1028 | - return $post_id; |
|
| 1029 | - } |
|
| 1030 | - |
|
| 1031 | - /** |
|
| 1032 | - * Attempt to download a remote file attachment |
|
| 1033 | - * |
|
| 1034 | - * @param string $url URL of item to fetch |
|
| 1035 | - * @param array $post Attachment details |
|
| 1036 | - * @return array|WP_Error Local file location details on success, WP_Error otherwise |
|
| 1037 | - */ |
|
| 1038 | - function fetch_remote_file( $url, $post ) { |
|
| 1039 | - // extract the file name and extension from the url |
|
| 1040 | - $file_name = basename( $url ); |
|
| 1041 | - |
|
| 1042 | - // get placeholder file in the upload dir with a unique, sanitized filename |
|
| 1043 | - $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] ); |
|
| 1044 | - if ( $upload['error'] ) { |
|
| 1045 | - return new WP_Error( 'upload_dir_error', $upload['error'] ); |
|
| 1046 | - } |
|
| 1047 | - |
|
| 1048 | - // fetch the remote url and write it to the placeholder file |
|
| 1049 | - $headers = wp_get_http( $url, $upload['file'] ); |
|
| 1050 | - |
|
| 1051 | - // request failed |
|
| 1052 | - if ( ! $headers ) { |
|
| 1053 | - @unlink( $upload['file'] ); |
|
| 1054 | - return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') ); |
|
| 1055 | - } |
|
| 1056 | - |
|
| 1057 | - // make sure the fetch was successful |
|
| 1058 | - if ( $headers['response'] != '200' ) { |
|
| 1059 | - @unlink( $upload['file'] ); |
|
| 1060 | - return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) ); |
|
| 1061 | - } |
|
| 1062 | - |
|
| 1063 | - $filesize = filesize( $upload['file'] ); |
|
| 1064 | - |
|
| 1065 | - if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) { |
|
| 1066 | - @unlink( $upload['file'] ); |
|
| 1067 | - return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') ); |
|
| 1068 | - } |
|
| 1069 | - |
|
| 1070 | - if ( 0 == $filesize ) { |
|
| 1071 | - @unlink( $upload['file'] ); |
|
| 1072 | - return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') ); |
|
| 1073 | - } |
|
| 1074 | - |
|
| 1075 | - $max_size = (int) $this->max_attachment_size(); |
|
| 1076 | - if ( ! empty( $max_size ) && $filesize > $max_size ) { |
|
| 1077 | - @unlink( $upload['file'] ); |
|
| 1078 | - return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) ); |
|
| 1079 | - } |
|
| 1080 | - |
|
| 1081 | - // keep track of the old and new urls so we can substitute them later |
|
| 1082 | - $this->url_remap[$url] = $upload['url']; |
|
| 1083 | - $this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed? |
|
| 1084 | - // keep track of the destination if the remote url is redirected somewhere else |
|
| 1085 | - if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url ) { |
|
| 1086 | - $this->url_remap[$headers['x-final-location']] = $upload['url']; |
|
| 1087 | - } |
|
| 1088 | - |
|
| 1089 | - return $upload; |
|
| 1090 | - } |
|
| 1091 | - |
|
| 1092 | - /** |
|
| 1093 | - * Attempt to associate posts and menu items with previously missing parents |
|
| 1094 | - * |
|
| 1095 | - * An imported post's parent may not have been imported when it was first created |
|
| 1096 | - * so try again. Similarly for child menu items and menu items which were missing |
|
| 1097 | - * the object (e.g. post) they represent in the menu |
|
| 1098 | - */ |
|
| 1099 | - function backfill_parents() { |
|
| 1100 | - global $wpdb; |
|
| 1101 | - |
|
| 1102 | - // find parents for post orphans |
|
| 1103 | - foreach ( $this->post_orphans as $child_id => $parent_id ) { |
|
| 1104 | - $local_child_id = $local_parent_id = false; |
|
| 1105 | - if ( isset( $this->processed_posts[$child_id] ) ) { |
|
| 1106 | - $local_child_id = $this->processed_posts[$child_id]; |
|
| 1107 | - } |
|
| 1108 | - if ( isset( $this->processed_posts[$parent_id] ) ) { |
|
| 1109 | - $local_parent_id = $this->processed_posts[$parent_id]; |
|
| 1110 | - } |
|
| 1111 | - |
|
| 1112 | - if ( $local_child_id && $local_parent_id ) { |
|
| 1113 | - $wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' ); |
|
| 1114 | - } |
|
| 1115 | - } |
|
| 1116 | - |
|
| 1117 | - // all other posts/terms are imported, retry menu items with missing associated object |
|
| 1118 | - $missing_menu_items = $this->missing_menu_items; |
|
| 1119 | - foreach ( $missing_menu_items as $item ) { |
|
| 1120 | - $this->process_menu_item( $item ); |
|
| 1121 | - } |
|
| 1122 | - |
|
| 1123 | - // find parents for menu item orphans |
|
| 1124 | - foreach ( $this->menu_item_orphans as $child_id => $parent_id ) { |
|
| 1125 | - $local_child_id = $local_parent_id = 0; |
|
| 1126 | - if ( isset( $this->processed_menu_items[$child_id] ) ) { |
|
| 1127 | - $local_child_id = $this->processed_menu_items[$child_id]; |
|
| 1128 | - } |
|
| 1129 | - if ( isset( $this->processed_menu_items[$parent_id] ) ) { |
|
| 1130 | - $local_parent_id = $this->processed_menu_items[$parent_id]; |
|
| 1131 | - } |
|
| 1132 | - |
|
| 1133 | - if ( $local_child_id && $local_parent_id ) { |
|
| 1134 | - update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id ); |
|
| 1135 | - } |
|
| 1136 | - } |
|
| 1137 | - } |
|
| 1138 | - |
|
| 1139 | - /** |
|
| 1140 | - * Use stored mapping information to update old attachment URLs |
|
| 1141 | - */ |
|
| 1142 | - function backfill_attachment_urls() { |
|
| 1143 | - global $wpdb; |
|
| 1144 | - // make sure we do the longest urls first, in case one is a substring of another |
|
| 1145 | - uksort( $this->url_remap, array(&$this, 'cmpr_strlen') ); |
|
| 1146 | - |
|
| 1147 | - foreach ( $this->url_remap as $from_url => $to_url ) { |
|
| 1148 | - // remap urls in post_content |
|
| 1149 | - $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) ); |
|
| 1150 | - // remap enclosure urls |
|
| 1151 | - $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) ); |
|
| 1152 | - } |
|
| 1153 | - } |
|
| 1154 | - |
|
| 1155 | - /** |
|
| 1156 | - * Update _thumbnail_id meta to new, imported attachment IDs |
|
| 1157 | - */ |
|
| 1158 | - function remap_featured_images() { |
|
| 1159 | - // cycle through posts that have a featured image |
|
| 1160 | - foreach ( $this->featured_images as $post_id => $value ) { |
|
| 1161 | - if ( isset( $this->processed_posts[$value] ) ) { |
|
| 1162 | - $new_id = $this->processed_posts[$value]; |
|
| 1163 | - // only update if there's a difference |
|
| 1164 | - if ( $new_id != $value ) { |
|
| 1165 | - update_post_meta( $post_id, '_thumbnail_id', $new_id ); |
|
| 1166 | - } |
|
| 1167 | - } |
|
| 1168 | - } |
|
| 1169 | - } |
|
| 1170 | - |
|
| 1171 | - /** |
|
| 1172 | - * Parse a WXR file |
|
| 1173 | - * |
|
| 1174 | - * @param string $file Path to WXR file for parsing |
|
| 1175 | - * @return array Information gathered from the WXR file |
|
| 1176 | - */ |
|
| 1177 | - function parse( $file ) { |
|
| 1178 | - $parser = new WXR_Parser(); |
|
| 1179 | - return $parser->parse( $file ); |
|
| 1180 | - } |
|
| 1181 | - |
|
| 1182 | - // Display import page title |
|
| 1183 | - function header() { |
|
| 1184 | - echo '<div class="wrap">'; |
|
| 1185 | - screen_icon(); |
|
| 1186 | - echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>'; |
|
| 1187 | - |
|
| 1188 | - $updates = get_plugin_updates(); |
|
| 1189 | - $basename = plugin_basename(__FILE__); |
|
| 1190 | - if ( isset( $updates[$basename] ) ) { |
|
| 1191 | - $update = $updates[$basename]; |
|
| 1192 | - echo '<div class="error"><p><strong>'; |
|
| 1193 | - printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wordpress-importer' ), $update->update->new_version ); |
|
| 1194 | - echo '</strong></p></div>'; |
|
| 1195 | - } |
|
| 1196 | - } |
|
| 1197 | - |
|
| 1198 | - // Close div.wrap |
|
| 1199 | - function footer() { |
|
| 1200 | - echo '</div>'; |
|
| 1201 | - } |
|
| 1202 | - |
|
| 1203 | - /** |
|
| 1204 | - * Display introductory text and file upload form |
|
| 1205 | - */ |
|
| 1206 | - function greet() { |
|
| 1207 | - echo '<div class="narrow">'; |
|
| 1208 | - echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>'; |
|
| 1209 | - echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>'; |
|
| 1210 | - wp_import_upload_form( 'admin.php?import=wordpress&step=1' ); |
|
| 1211 | - echo '</div>'; |
|
| 1212 | - } |
|
| 1213 | - |
|
| 1214 | - /** |
|
| 1215 | - * Decide if the given meta key maps to information we will want to import |
|
| 1216 | - * |
|
| 1217 | - * @param string $key The meta key to check |
|
| 1218 | - * @return string|bool The key if we do want to import, false if not |
|
| 1219 | - */ |
|
| 1220 | - function is_valid_meta_key( $key ) { |
|
| 1221 | - // skip attachment metadata since we'll regenerate it from scratch |
|
| 1222 | - // skip _edit_lock as not relevant for import |
|
| 1223 | - if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) ) { |
|
| 1224 | - return false; |
|
| 1225 | - } |
|
| 1226 | - return $key; |
|
| 1227 | - } |
|
| 1228 | - |
|
| 1229 | - /** |
|
| 1230 | - * Decide whether or not the importer is allowed to create users. |
|
| 1231 | - * Default is true, can be filtered via import_allow_create_users |
|
| 1232 | - * |
|
| 1233 | - * @return bool True if creating users is allowed |
|
| 1234 | - */ |
|
| 1235 | - function allow_create_users() { |
|
| 1236 | - return apply_filters( 'import_allow_create_users', true ); |
|
| 1237 | - } |
|
| 1238 | - |
|
| 1239 | - /** |
|
| 1240 | - * Decide whether or not the importer should attempt to download attachment files. |
|
| 1241 | - * Default is true, can be filtered via import_allow_fetch_attachments. The choice |
|
| 1242 | - * made at the import options screen must also be true, false here hides that checkbox. |
|
| 1243 | - * |
|
| 1244 | - * @return bool True if downloading attachments is allowed |
|
| 1245 | - */ |
|
| 1246 | - function allow_fetch_attachments() { |
|
| 1247 | - return apply_filters( 'import_allow_fetch_attachments', true ); |
|
| 1248 | - } |
|
| 1249 | - |
|
| 1250 | - /** |
|
| 1251 | - * Decide what the maximum file size for downloaded attachments is. |
|
| 1252 | - * Default is 0 (unlimited), can be filtered via import_attachment_size_limit |
|
| 1253 | - * |
|
| 1254 | - * @return int Maximum attachment file size to import |
|
| 1255 | - */ |
|
| 1256 | - function max_attachment_size() { |
|
| 1257 | - return apply_filters( 'import_attachment_size_limit', 0 ); |
|
| 1258 | - } |
|
| 1259 | - |
|
| 1260 | - /** |
|
| 1261 | - * Added to http_request_timeout filter to force timeout at 60 seconds during import |
|
| 1262 | - * @return int 60 |
|
| 1263 | - */ |
|
| 1264 | - function bump_request_timeout( $val ) { |
|
| 1265 | - return 60; |
|
| 1266 | - } |
|
| 1267 | - |
|
| 1268 | - // return the difference in length between two strings |
|
| 1269 | - function cmpr_strlen( $a, $b ) { |
|
| 1270 | - return strlen($b) - strlen($a); |
|
| 1271 | - } |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + /** |
|
| 294 | + * Display import options for an individual author. That is, either create |
|
| 295 | + * a new user based on import info or map to an existing user |
|
| 296 | + * |
|
| 297 | + * @param int $n Index for each author in the form |
|
| 298 | + * @param array $author Author information, e.g. login, display name, email |
|
| 299 | + */ |
|
| 300 | + function author_select( $n, $author ) { |
|
| 301 | + _e( 'Import author:', 'wordpress-importer' ); |
|
| 302 | + echo ' <strong>' . esc_html( $author['author_display_name'] ); |
|
| 303 | + if ( $this->version != '1.0' ) { |
|
| 304 | + echo ' (' . esc_html( $author['author_login'] ) . ')'; |
|
| 305 | + } |
|
| 306 | + echo '</strong><br />'; |
|
| 307 | + |
|
| 308 | + if ( $this->version != '1.0' ) { |
|
| 309 | + echo '<div style="margin-left:18px">'; |
|
| 310 | + } |
|
| 311 | + |
|
| 312 | + $create_users = $this->allow_create_users(); |
|
| 313 | + if ( $create_users ) { |
|
| 314 | + if ( $this->version != '1.0' ) { |
|
| 315 | + _e( 'or create new user with login name:', 'wordpress-importer' ); |
|
| 316 | + $value = ''; |
|
| 317 | + } else { |
|
| 318 | + _e( 'as a new user:', 'wordpress-importer' ); |
|
| 319 | + $value = esc_attr( sanitize_user( $author['author_login'], true ) ); |
|
| 320 | + } |
|
| 321 | + |
|
| 322 | + echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />'; |
|
| 323 | + } |
|
| 324 | + |
|
| 325 | + if ( ! $create_users && $this->version == '1.0' ) { |
|
| 326 | + _e( 'assign posts to an existing user:', 'wordpress-importer' ); |
|
| 327 | + } else { |
|
| 328 | + _e( 'or assign posts to an existing user:', 'wordpress-importer' ); |
|
| 329 | + } |
|
| 330 | + wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) ); |
|
| 331 | + echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />'; |
|
| 332 | + |
|
| 333 | + if ( $this->version != '1.0' ) { |
|
| 334 | + echo '</div>'; |
|
| 335 | + } |
|
| 336 | + } |
|
| 337 | + |
|
| 338 | + /** |
|
| 339 | + * Map old author logins to local user IDs based on decisions made |
|
| 340 | + * in import options form. Can map to an existing user, create a new user |
|
| 341 | + * or falls back to the current user in case of error with either of the previous |
|
| 342 | + */ |
|
| 343 | + function get_author_mapping() { |
|
| 344 | + if ( ! isset( $_POST['imported_authors'] ) ) { |
|
| 345 | + return; |
|
| 346 | + } |
|
| 347 | + |
|
| 348 | + $create_users = $this->allow_create_users(); |
|
| 349 | + |
|
| 350 | + foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) { |
|
| 351 | + // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts. |
|
| 352 | + $santized_old_login = sanitize_user( $old_login, true ); |
|
| 353 | + $old_id = isset( $this->authors[$old_login]['author_id'] ) ? intval($this->authors[$old_login]['author_id']) : false; |
|
| 354 | + |
|
| 355 | + if ( ! empty( $_POST['user_map'][$i] ) ) { |
|
| 356 | + $user = get_userdata( intval($_POST['user_map'][$i]) ); |
|
| 357 | + if ( isset( $user->ID ) ) { |
|
| 358 | + if ( $old_id ) { |
|
| 359 | + $this->processed_authors[$old_id] = $user->ID; |
|
| 360 | + } |
|
| 361 | + $this->author_mapping[$santized_old_login] = $user->ID; |
|
| 362 | + } |
|
| 363 | + } else if ( $create_users ) { |
|
| 364 | + if ( ! empty($_POST['user_new'][$i]) ) { |
|
| 365 | + $user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() ); |
|
| 366 | + } else if ( $this->version != '1.0' ) { |
|
| 367 | + $user_data = array( |
|
| 368 | + 'user_login' => $old_login, |
|
| 369 | + 'user_pass' => wp_generate_password(), |
|
| 370 | + 'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '', |
|
| 371 | + 'display_name' => $this->authors[$old_login]['author_display_name'], |
|
| 372 | + 'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '', |
|
| 373 | + 'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '', |
|
| 374 | + ); |
|
| 375 | + $user_id = wp_insert_user( $user_data ); |
|
| 376 | + } |
|
| 377 | + |
|
| 378 | + if ( ! is_wp_error( $user_id ) ) { |
|
| 379 | + if ( $old_id ) { |
|
| 380 | + $this->processed_authors[$old_id] = $user_id; |
|
| 381 | + } |
|
| 382 | + $this->author_mapping[$santized_old_login] = $user_id; |
|
| 383 | + } else { |
|
| 384 | + printf( __( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html($this->authors[$old_login]['author_display_name']) ); |
|
| 385 | + if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 386 | + echo ' ' . $user_id->get_error_message(); |
|
| 387 | + } |
|
| 388 | + echo '<br />'; |
|
| 389 | + } |
|
| 390 | + } |
|
| 391 | + |
|
| 392 | + // failsafe: if the user_id was invalid, default to the current user |
|
| 393 | + if ( ! isset( $this->author_mapping[$santized_old_login] ) ) { |
|
| 394 | + if ( $old_id ) { |
|
| 395 | + $this->processed_authors[$old_id] = (int) get_current_user_id(); |
|
| 396 | + } |
|
| 397 | + $this->author_mapping[$santized_old_login] = (int) get_current_user_id(); |
|
| 398 | + } |
|
| 399 | + } |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + /** |
|
| 403 | + * Create new categories based on import information |
|
| 404 | + * |
|
| 405 | + * Doesn't create a new category if its slug already exists |
|
| 406 | + */ |
|
| 407 | + function process_categories() { |
|
| 408 | + $this->categories = apply_filters( 'wp_import_categories', $this->categories ); |
|
| 409 | + |
|
| 410 | + if ( empty( $this->categories ) ) { |
|
| 411 | + return; |
|
| 412 | + } |
|
| 413 | + |
|
| 414 | + foreach ( $this->categories as $cat ) { |
|
| 415 | + // if the category already exists leave it alone |
|
| 416 | + $term_id = term_exists( $cat['category_nicename'], 'category' ); |
|
| 417 | + if ( $term_id ) { |
|
| 418 | + if ( is_array($term_id) ) { |
|
| 419 | + $term_id = $term_id['term_id']; |
|
| 420 | + } |
|
| 421 | + if ( isset($cat['term_id']) ) { |
|
| 422 | + $this->processed_terms[intval($cat['term_id'])] = (int) $term_id; |
|
| 423 | + } |
|
| 424 | + continue; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + $category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] ); |
|
| 428 | + $category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : ''; |
|
| 429 | + $catarr = array( |
|
| 430 | + 'category_nicename' => $cat['category_nicename'], |
|
| 431 | + 'category_parent' => $category_parent, |
|
| 432 | + 'cat_name' => $cat['cat_name'], |
|
| 433 | + 'category_description' => $category_description |
|
| 434 | + ); |
|
| 435 | + $catarr = wp_slash( $catarr ); |
|
| 436 | + |
|
| 437 | + $id = wp_insert_category( $catarr ); |
|
| 438 | + if ( ! is_wp_error( $id ) ) { |
|
| 439 | + if ( isset($cat['term_id']) ) { |
|
| 440 | + $this->processed_terms[intval($cat['term_id'])] = $id; |
|
| 441 | + } |
|
| 442 | + } else { |
|
| 443 | + printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) ); |
|
| 444 | + if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 445 | + echo ': ' . $id->get_error_message(); |
|
| 446 | + } |
|
| 447 | + echo '<br />'; |
|
| 448 | + continue; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + $this->process_termmeta( $cat, $id['term_id'] ); |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + unset( $this->categories ); |
|
| 455 | + } |
|
| 456 | + |
|
| 457 | + /** |
|
| 458 | + * Create new post tags based on import information |
|
| 459 | + * |
|
| 460 | + * Doesn't create a tag if its slug already exists |
|
| 461 | + */ |
|
| 462 | + function process_tags() { |
|
| 463 | + $this->tags = apply_filters( 'wp_import_tags', $this->tags ); |
|
| 464 | + |
|
| 465 | + if ( empty( $this->tags ) ) { |
|
| 466 | + return; |
|
| 467 | + } |
|
| 468 | + |
|
| 469 | + foreach ( $this->tags as $tag ) { |
|
| 470 | + // if the tag already exists leave it alone |
|
| 471 | + $term_id = term_exists( $tag['tag_slug'], 'post_tag' ); |
|
| 472 | + if ( $term_id ) { |
|
| 473 | + if ( is_array($term_id) ) { |
|
| 474 | + $term_id = $term_id['term_id']; |
|
| 475 | + } |
|
| 476 | + if ( isset($tag['term_id']) ) { |
|
| 477 | + $this->processed_terms[intval($tag['term_id'])] = (int) $term_id; |
|
| 478 | + } |
|
| 479 | + continue; |
|
| 480 | + } |
|
| 481 | + |
|
| 482 | + $tag = wp_slash( $tag ); |
|
| 483 | + $tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : ''; |
|
| 484 | + $tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc ); |
|
| 485 | + |
|
| 486 | + $id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr ); |
|
| 487 | + if ( ! is_wp_error( $id ) ) { |
|
| 488 | + if ( isset($tag['term_id']) ) { |
|
| 489 | + $this->processed_terms[intval($tag['term_id'])] = $id['term_id']; |
|
| 490 | + } |
|
| 491 | + } else { |
|
| 492 | + printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) ); |
|
| 493 | + if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 494 | + echo ': ' . $id->get_error_message(); |
|
| 495 | + } |
|
| 496 | + echo '<br />'; |
|
| 497 | + continue; |
|
| 498 | + } |
|
| 499 | + |
|
| 500 | + $this->process_termmeta( $tag, $id['term_id'] ); |
|
| 501 | + } |
|
| 502 | + |
|
| 503 | + unset( $this->tags ); |
|
| 504 | + } |
|
| 505 | + |
|
| 506 | + /** |
|
| 507 | + * Create new terms based on import information |
|
| 508 | + * |
|
| 509 | + * Doesn't create a term its slug already exists |
|
| 510 | + */ |
|
| 511 | + function process_terms() { |
|
| 512 | + $this->terms = apply_filters( 'wp_import_terms', $this->terms ); |
|
| 513 | + |
|
| 514 | + if ( empty( $this->terms ) ) { |
|
| 515 | + return; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + foreach ( $this->terms as $term ) { |
|
| 519 | + // if the term already exists in the correct taxonomy leave it alone |
|
| 520 | + $term_id = term_exists( $term['slug'], $term['term_taxonomy'] ); |
|
| 521 | + if ( $term_id ) { |
|
| 522 | + if ( is_array($term_id) ) { |
|
| 523 | + $term_id = $term_id['term_id']; |
|
| 524 | + } |
|
| 525 | + if ( isset($term['term_id']) ) { |
|
| 526 | + $this->processed_terms[intval($term['term_id'])] = (int) $term_id; |
|
| 527 | + } |
|
| 528 | + continue; |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + if ( empty( $term['term_parent'] ) ) { |
|
| 532 | + $parent = 0; |
|
| 533 | + } else { |
|
| 534 | + $parent = term_exists( $term['term_parent'], $term['term_taxonomy'] ); |
|
| 535 | + if ( is_array( $parent ) ) { |
|
| 536 | + $parent = $parent['term_id']; |
|
| 537 | + } |
|
| 538 | + } |
|
| 539 | + $term = wp_slash( $term ); |
|
| 540 | + $description = isset( $term['term_description'] ) ? $term['term_description'] : ''; |
|
| 541 | + $termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) ); |
|
| 542 | + |
|
| 543 | + $id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr ); |
|
| 544 | + if ( ! is_wp_error( $id ) ) { |
|
| 545 | + if ( isset($term['term_id']) ) { |
|
| 546 | + $this->processed_terms[intval($term['term_id'])] = $id['term_id']; |
|
| 547 | + } |
|
| 548 | + } else { |
|
| 549 | + printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) ); |
|
| 550 | + if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 551 | + echo ': ' . $id->get_error_message(); |
|
| 552 | + } |
|
| 553 | + echo '<br />'; |
|
| 554 | + continue; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + $this->process_termmeta( $term, $id['term_id'] ); |
|
| 558 | + } |
|
| 559 | + |
|
| 560 | + unset( $this->terms ); |
|
| 561 | + } |
|
| 562 | + |
|
| 563 | + /** |
|
| 564 | + * Add metadata to imported term. |
|
| 565 | + * |
|
| 566 | + * @since 0.6.2 |
|
| 567 | + * |
|
| 568 | + * @param array $term Term data from WXR import. |
|
| 569 | + * @param int $term_id ID of the newly created term. |
|
| 570 | + */ |
|
| 571 | + protected function process_termmeta( $term, $term_id ) { |
|
| 572 | + if ( ! isset( $term['termmeta'] ) ) { |
|
| 573 | + $term['termmeta'] = array(); |
|
| 574 | + } |
|
| 575 | + |
|
| 576 | + /** |
|
| 577 | + * Filters the metadata attached to an imported term. |
|
| 578 | + * |
|
| 579 | + * @since 0.6.2 |
|
| 580 | + * |
|
| 581 | + * @param array $termmeta Array of term meta. |
|
| 582 | + * @param int $term_id ID of the newly created term. |
|
| 583 | + * @param array $term Term data from the WXR import. |
|
| 584 | + */ |
|
| 585 | + $term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term ); |
|
| 586 | + |
|
| 587 | + if ( empty( $term['termmeta'] ) ) { |
|
| 588 | + return; |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + foreach ( $term['termmeta'] as $meta ) { |
|
| 592 | + /** |
|
| 593 | + * Filters the meta key for an imported piece of term meta. |
|
| 594 | + * |
|
| 595 | + * @since 0.6.2 |
|
| 596 | + * |
|
| 597 | + * @param string $meta_key Meta key. |
|
| 598 | + * @param int $term_id ID of the newly created term. |
|
| 599 | + * @param array $term Term data from the WXR import. |
|
| 600 | + */ |
|
| 601 | + $key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term ); |
|
| 602 | + if ( ! $key ) { |
|
| 603 | + continue; |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + // Export gets meta straight from the DB so could have a serialized string |
|
| 607 | + $value = maybe_unserialize( $meta['value'] ); |
|
| 608 | + |
|
| 609 | + add_term_meta( $term_id, $key, $value ); |
|
| 610 | + |
|
| 611 | + /** |
|
| 612 | + * Fires after term meta is imported. |
|
| 613 | + * |
|
| 614 | + * @since 0.6.2 |
|
| 615 | + * |
|
| 616 | + * @param int $term_id ID of the newly created term. |
|
| 617 | + * @param string $key Meta key. |
|
| 618 | + * @param mixed $value Meta value. |
|
| 619 | + */ |
|
| 620 | + do_action( 'import_term_meta', $term_id, $key, $value ); |
|
| 621 | + } |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + /** |
|
| 625 | + * Create new posts based on import information |
|
| 626 | + * |
|
| 627 | + * Posts marked as having a parent which doesn't exist will become top level items. |
|
| 628 | + * Doesn't create a new post if: the post type doesn't exist, the given post ID |
|
| 629 | + * is already noted as imported or a post with the same title and date already exists. |
|
| 630 | + * Note that new/updated terms, comments and meta are imported for the last of the above. |
|
| 631 | + */ |
|
| 632 | + function process_posts() { |
|
| 633 | + $this->posts = apply_filters( 'wp_import_posts', $this->posts ); |
|
| 634 | + |
|
| 635 | + foreach ( $this->posts as $post ) { |
|
| 636 | + $post = apply_filters( 'wp_import_post_data_raw', $post ); |
|
| 637 | + |
|
| 638 | + if ( ! post_type_exists( $post['post_type'] ) ) { |
|
| 639 | + printf( __( 'Failed to import “%s”: Invalid post type %s', 'wordpress-importer' ), |
|
| 640 | + esc_html($post['post_title']), esc_html($post['post_type']) ); |
|
| 641 | + echo '<br />'; |
|
| 642 | + do_action( 'wp_import_post_exists', $post ); |
|
| 643 | + continue; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) ) { |
|
| 647 | + continue; |
|
| 648 | + } |
|
| 649 | + |
|
| 650 | + if ( $post['status'] == 'auto-draft' ) { |
|
| 651 | + continue; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + if ( 'nav_menu_item' == $post['post_type'] ) { |
|
| 655 | + $this->process_menu_item( $post ); |
|
| 656 | + continue; |
|
| 657 | + } |
|
| 658 | + |
|
| 659 | + $post_type_object = get_post_type_object( $post['post_type'] ); |
|
| 660 | + |
|
| 661 | + $post_exists = post_exists( $post['post_title'], '', $post['post_date'] ); |
|
| 662 | + |
|
| 663 | + /** |
|
| 664 | + * Filter ID of the existing post corresponding to post currently importing. |
|
| 665 | + * |
|
| 666 | + * Return 0 to force the post to be imported. Filter the ID to be something else |
|
| 667 | + * to override which existing post is mapped to the imported post. |
|
| 668 | + * |
|
| 669 | + * @see post_exists() |
|
| 670 | + * @since 0.6.2 |
|
| 671 | + * |
|
| 672 | + * @param int $post_exists Post ID, or 0 if post did not exist. |
|
| 673 | + * @param array $post The post array to be inserted. |
|
| 674 | + */ |
|
| 675 | + $post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post ); |
|
| 676 | + |
|
| 677 | + if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) { |
|
| 678 | + printf( __('%s “%s” already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) ); |
|
| 679 | + echo '<br />'; |
|
| 680 | + $comment_post_ID = $post_id = $post_exists; |
|
| 681 | + $this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists ); |
|
| 682 | + } else { |
|
| 683 | + $post_parent = (int) $post['post_parent']; |
|
| 684 | + if ( $post_parent ) { |
|
| 685 | + // if we already know the parent, map it to the new local ID |
|
| 686 | + if ( isset( $this->processed_posts[$post_parent] ) ) { |
|
| 687 | + $post_parent = $this->processed_posts[$post_parent]; |
|
| 688 | + // otherwise record the parent for later |
|
| 689 | + } else { |
|
| 690 | + $this->post_orphans[intval($post['post_id'])] = $post_parent; |
|
| 691 | + $post_parent = 0; |
|
| 692 | + } |
|
| 693 | + } |
|
| 694 | + |
|
| 695 | + // map the post author |
|
| 696 | + $author = sanitize_user( $post['post_author'], true ); |
|
| 697 | + if ( isset( $this->author_mapping[$author] ) ) { |
|
| 698 | + $author = $this->author_mapping[$author]; |
|
| 699 | + } else { |
|
| 700 | + $author = (int) get_current_user_id(); |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + $postdata = array( |
|
| 704 | + 'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'], |
|
| 705 | + 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'], |
|
| 706 | + 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'], |
|
| 707 | + 'post_status' => $post['status'], 'post_name' => $post['post_name'], |
|
| 708 | + 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'], |
|
| 709 | + 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'], |
|
| 710 | + 'post_type' => $post['post_type'], 'post_password' => $post['post_password'] |
|
| 711 | + ); |
|
| 712 | + |
|
| 713 | + $original_post_ID = $post['post_id']; |
|
| 714 | + $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post ); |
|
| 715 | + |
|
| 716 | + $postdata = wp_slash( $postdata ); |
|
| 717 | + |
|
| 718 | + if ( 'attachment' == $postdata['post_type'] ) { |
|
| 719 | + $remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; |
|
| 720 | + |
|
| 721 | + // try to use _wp_attached file for upload folder placement to ensure the same location as the export site |
|
| 722 | + // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() |
|
| 723 | + $postdata['upload_date'] = $post['post_date']; |
|
| 724 | + if ( isset( $post['postmeta'] ) ) { |
|
| 725 | + foreach( $post['postmeta'] as $meta ) { |
|
| 726 | + if ( $meta['key'] == '_wp_attached_file' ) { |
|
| 727 | + if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) ) { |
|
| 728 | + $postdata['upload_date'] = $matches[0]; |
|
| 729 | + } |
|
| 730 | + break; |
|
| 731 | + } |
|
| 732 | + } |
|
| 733 | + } |
|
| 734 | + |
|
| 735 | + $comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url ); |
|
| 736 | + } else { |
|
| 737 | + $comment_post_ID = $post_id = wp_insert_post( $postdata, true ); |
|
| 738 | + do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post ); |
|
| 739 | + } |
|
| 740 | + |
|
| 741 | + if ( is_wp_error( $post_id ) ) { |
|
| 742 | + printf( __( 'Failed to import %s “%s”', 'wordpress-importer' ), |
|
| 743 | + $post_type_object->labels->singular_name, esc_html($post['post_title']) ); |
|
| 744 | + if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 745 | + echo ': ' . $post_id->get_error_message(); |
|
| 746 | + } |
|
| 747 | + echo '<br />'; |
|
| 748 | + continue; |
|
| 749 | + } |
|
| 750 | + |
|
| 751 | + if ( $post['is_sticky'] == 1 ) { |
|
| 752 | + stick_post( $post_id ); |
|
| 753 | + } |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + // map pre-import ID to local ID |
|
| 757 | + $this->processed_posts[intval($post['post_id'])] = (int) $post_id; |
|
| 758 | + |
|
| 759 | + if ( ! isset( $post['terms'] ) ) { |
|
| 760 | + $post['terms'] = array(); |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + $post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post ); |
|
| 764 | + |
|
| 765 | + // add categories, tags and other terms |
|
| 766 | + if ( ! empty( $post['terms'] ) ) { |
|
| 767 | + $terms_to_set = array(); |
|
| 768 | + foreach ( $post['terms'] as $term ) { |
|
| 769 | + // back compat with WXR 1.0 map 'tag' to 'post_tag' |
|
| 770 | + $taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain']; |
|
| 771 | + $term_exists = term_exists( $term['slug'], $taxonomy ); |
|
| 772 | + $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists; |
|
| 773 | + if ( ! $term_id ) { |
|
| 774 | + $t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) ); |
|
| 775 | + if ( ! is_wp_error( $t ) ) { |
|
| 776 | + $term_id = $t['term_id']; |
|
| 777 | + do_action( 'wp_import_insert_term', $t, $term, $post_id, $post ); |
|
| 778 | + } else { |
|
| 779 | + printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) ); |
|
| 780 | + if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 781 | + echo ': ' . $t->get_error_message(); |
|
| 782 | + } |
|
| 783 | + echo '<br />'; |
|
| 784 | + do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post ); |
|
| 785 | + continue; |
|
| 786 | + } |
|
| 787 | + } |
|
| 788 | + $terms_to_set[$taxonomy][] = intval( $term_id ); |
|
| 789 | + } |
|
| 790 | + |
|
| 791 | + foreach ( $terms_to_set as $tax => $ids ) { |
|
| 792 | + $tt_ids = wp_set_post_terms( $post_id, $ids, $tax ); |
|
| 793 | + do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post ); |
|
| 794 | + } |
|
| 795 | + unset( $post['terms'], $terms_to_set ); |
|
| 796 | + } |
|
| 797 | + |
|
| 798 | + if ( ! isset( $post['comments'] ) ) { |
|
| 799 | + $post['comments'] = array(); |
|
| 800 | + } |
|
| 801 | + |
|
| 802 | + $post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post ); |
|
| 803 | + |
|
| 804 | + // add/update comments |
|
| 805 | + if ( ! empty( $post['comments'] ) ) { |
|
| 806 | + $num_comments = 0; |
|
| 807 | + $inserted_comments = array(); |
|
| 808 | + foreach ( $post['comments'] as $comment ) { |
|
| 809 | + $comment_id = $comment['comment_id']; |
|
| 810 | + $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; |
|
| 811 | + $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; |
|
| 812 | + $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; |
|
| 813 | + $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP']; |
|
| 814 | + $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url']; |
|
| 815 | + $newcomments[$comment_id]['comment_date'] = $comment['comment_date']; |
|
| 816 | + $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt']; |
|
| 817 | + $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; |
|
| 818 | + $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; |
|
| 819 | + $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; |
|
| 820 | + $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; |
|
| 821 | + $newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array(); |
|
| 822 | + if ( isset( $this->processed_authors[$comment['comment_user_id']] ) ) { |
|
| 823 | + $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']]; |
|
| 824 | + } |
|
| 825 | + } |
|
| 826 | + ksort( $newcomments ); |
|
| 827 | + |
|
| 828 | + foreach ( $newcomments as $key => $comment ) { |
|
| 829 | + // if this is a new post we can skip the comment_exists() check |
|
| 830 | + if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) { |
|
| 831 | + if ( isset( $inserted_comments[$comment['comment_parent']] ) ) { |
|
| 832 | + $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; |
|
| 833 | + } |
|
| 834 | + $comment = wp_filter_comment( $comment ); |
|
| 835 | + $inserted_comments[$key] = wp_insert_comment( $comment ); |
|
| 836 | + do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post ); |
|
| 837 | + |
|
| 838 | + foreach( $comment['commentmeta'] as $meta ) { |
|
| 839 | + $value = maybe_unserialize( $meta['value'] ); |
|
| 840 | + add_comment_meta( $inserted_comments[$key], $meta['key'], $value ); |
|
| 841 | + } |
|
| 842 | + |
|
| 843 | + $num_comments++; |
|
| 844 | + } |
|
| 845 | + } |
|
| 846 | + unset( $newcomments, $inserted_comments, $post['comments'] ); |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + if ( ! isset( $post['postmeta'] ) ) { |
|
| 850 | + $post['postmeta'] = array(); |
|
| 851 | + } |
|
| 852 | + |
|
| 853 | + $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post ); |
|
| 854 | + |
|
| 855 | + // add/update post meta |
|
| 856 | + if ( ! empty( $post['postmeta'] ) ) { |
|
| 857 | + foreach ( $post['postmeta'] as $meta ) { |
|
| 858 | + $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post ); |
|
| 859 | + $value = false; |
|
| 860 | + |
|
| 861 | + if ( '_edit_last' == $key ) { |
|
| 862 | + if ( isset( $this->processed_authors[intval($meta['value'])] ) ) { |
|
| 863 | + $value = $this->processed_authors[intval($meta['value'])]; |
|
| 864 | + } else { |
|
| 865 | + $key = false; |
|
| 866 | + } |
|
| 867 | + } |
|
| 868 | + |
|
| 869 | + if ( $key ) { |
|
| 870 | + // export gets meta straight from the DB so could have a serialized string |
|
| 871 | + if ( ! $value ) { |
|
| 872 | + $value = maybe_unserialize( $meta['value'] ); |
|
| 873 | + } |
|
| 874 | + |
|
| 875 | + add_post_meta( $post_id, $key, $value ); |
|
| 876 | + do_action( 'import_post_meta', $post_id, $key, $value ); |
|
| 877 | + |
|
| 878 | + // if the post has a featured image, take note of this in case of remap |
|
| 879 | + if ( '_thumbnail_id' == $key ) { |
|
| 880 | + $this->featured_images[$post_id] = (int) $value; |
|
| 881 | + } |
|
| 882 | + } |
|
| 883 | + } |
|
| 884 | + } |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + unset( $this->posts ); |
|
| 888 | + } |
|
| 889 | + |
|
| 890 | + /** |
|
| 891 | + * Attempt to create a new menu item from import data |
|
| 892 | + * |
|
| 893 | + * Fails for draft, orphaned menu items and those without an associated nav_menu |
|
| 894 | + * or an invalid nav_menu term. If the post type or term object which the menu item |
|
| 895 | + * represents doesn't exist then the menu item will not be imported (waits until the |
|
| 896 | + * end of the import to retry again before discarding). |
|
| 897 | + * |
|
| 898 | + * @param array $item Menu item details from WXR file |
|
| 899 | + */ |
|
| 900 | + function process_menu_item( $item ) { |
|
| 901 | + // skip draft, orphaned menu items |
|
| 902 | + if ( 'draft' == $item['status'] ) { |
|
| 903 | + return; |
|
| 904 | + } |
|
| 905 | + |
|
| 906 | + $menu_slug = false; |
|
| 907 | + if ( isset($item['terms']) ) { |
|
| 908 | + // loop through terms, assume first nav_menu term is correct menu |
|
| 909 | + foreach ( $item['terms'] as $term ) { |
|
| 910 | + if ( 'nav_menu' == $term['domain'] ) { |
|
| 911 | + $menu_slug = $term['slug']; |
|
| 912 | + break; |
|
| 913 | + } |
|
| 914 | + } |
|
| 915 | + } |
|
| 916 | + |
|
| 917 | + // no nav_menu term associated with this menu item |
|
| 918 | + if ( ! $menu_slug ) { |
|
| 919 | + _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' ); |
|
| 920 | + echo '<br />'; |
|
| 921 | + return; |
|
| 922 | + } |
|
| 923 | + |
|
| 924 | + $menu_id = term_exists( $menu_slug, 'nav_menu' ); |
|
| 925 | + if ( ! $menu_id ) { |
|
| 926 | + printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) ); |
|
| 927 | + echo '<br />'; |
|
| 928 | + return; |
|
| 929 | + } else { |
|
| 930 | + $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id; |
|
| 931 | + } |
|
| 932 | + |
|
| 933 | + foreach ( $item['postmeta'] as $meta ) { |
|
| 934 | + $$meta['key'] = $meta['value']; |
|
| 935 | + } |
|
| 936 | + |
|
| 937 | + if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) { |
|
| 938 | + $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)]; |
|
| 939 | + } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) { |
|
| 940 | + $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)]; |
|
| 941 | + } else if ( 'custom' != $_menu_item_type ) { |
|
| 942 | + // associated object is missing or not imported yet, we'll retry later |
|
| 943 | + $this->missing_menu_items[] = $item; |
|
| 944 | + return; |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) { |
|
| 948 | + $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)]; |
|
| 949 | + } else if ( $_menu_item_menu_item_parent ) { |
|
| 950 | + $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent; |
|
| 951 | + $_menu_item_menu_item_parent = 0; |
|
| 952 | + } |
|
| 953 | + |
|
| 954 | + // wp_update_nav_menu_item expects CSS classes as a space separated string |
|
| 955 | + $_menu_item_classes = maybe_unserialize( $_menu_item_classes ); |
|
| 956 | + if ( is_array( $_menu_item_classes ) ) { |
|
| 957 | + $_menu_item_classes = implode( ' ', $_menu_item_classes ); |
|
| 958 | + } |
|
| 959 | + |
|
| 960 | + $args = array( |
|
| 961 | + 'menu-item-object-id' => $_menu_item_object_id, |
|
| 962 | + 'menu-item-object' => $_menu_item_object, |
|
| 963 | + 'menu-item-parent-id' => $_menu_item_menu_item_parent, |
|
| 964 | + 'menu-item-position' => intval( $item['menu_order'] ), |
|
| 965 | + 'menu-item-type' => $_menu_item_type, |
|
| 966 | + 'menu-item-title' => $item['post_title'], |
|
| 967 | + 'menu-item-url' => $_menu_item_url, |
|
| 968 | + 'menu-item-description' => $item['post_content'], |
|
| 969 | + 'menu-item-attr-title' => $item['post_excerpt'], |
|
| 970 | + 'menu-item-target' => $_menu_item_target, |
|
| 971 | + 'menu-item-classes' => $_menu_item_classes, |
|
| 972 | + 'menu-item-xfn' => $_menu_item_xfn, |
|
| 973 | + 'menu-item-status' => $item['status'] |
|
| 974 | + ); |
|
| 975 | + |
|
| 976 | + $id = wp_update_nav_menu_item( $menu_id, 0, $args ); |
|
| 977 | + if ( $id && ! is_wp_error( $id ) ) { |
|
| 978 | + $this->processed_menu_items[intval($item['post_id'])] = (int) $id; |
|
| 979 | + } |
|
| 980 | + } |
|
| 981 | + |
|
| 982 | + /** |
|
| 983 | + * If fetching attachments is enabled then attempt to create a new attachment |
|
| 984 | + * |
|
| 985 | + * @param array $post Attachment post details from WXR |
|
| 986 | + * @param string $url URL to fetch attachment from |
|
| 987 | + * @return int|WP_Error Post ID on success, WP_Error otherwise |
|
| 988 | + */ |
|
| 989 | + function process_attachment( $post, $url ) { |
|
| 990 | + if ( ! $this->fetch_attachments ) { |
|
| 991 | + return new WP_Error( 'attachment_processing_error', |
|
| 992 | + __( 'Fetching attachments is not enabled', 'wordpress-importer' ) ); |
|
| 993 | + } |
|
| 994 | + |
|
| 995 | + // if the URL is absolute, but does not contain address, then upload it assuming base_site_url |
|
| 996 | + if ( preg_match( '|^/[\w\W]+$|', $url ) ) { |
|
| 997 | + $url = rtrim( $this->base_url, '/' ) . $url; |
|
| 998 | + } |
|
| 999 | + |
|
| 1000 | + $upload = $this->fetch_remote_file( $url, $post ); |
|
| 1001 | + if ( is_wp_error( $upload ) ) { |
|
| 1002 | + return $upload; |
|
| 1003 | + } |
|
| 1004 | + |
|
| 1005 | + if ( $info = wp_check_filetype( $upload['file'] ) ) { |
|
| 1006 | + $post['post_mime_type'] = $info['type']; |
|
| 1007 | + } else { |
|
| 1008 | + return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') ); |
|
| 1009 | + } |
|
| 1010 | + |
|
| 1011 | + $post['guid'] = $upload['url']; |
|
| 1012 | + |
|
| 1013 | + // as per wp-admin/includes/upload.php |
|
| 1014 | + $post_id = wp_insert_attachment( $post, $upload['file'] ); |
|
| 1015 | + wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) ); |
|
| 1016 | + |
|
| 1017 | + // remap resized image URLs, works by stripping the extension and remapping the URL stub. |
|
| 1018 | + if ( preg_match( '!^image/!', $info['type'] ) ) { |
|
| 1019 | + $parts = pathinfo( $url ); |
|
| 1020 | + $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2 |
|
| 1021 | + |
|
| 1022 | + $parts_new = pathinfo( $upload['url'] ); |
|
| 1023 | + $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" ); |
|
| 1024 | + |
|
| 1025 | + $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new; |
|
| 1026 | + } |
|
| 1027 | + |
|
| 1028 | + return $post_id; |
|
| 1029 | + } |
|
| 1030 | + |
|
| 1031 | + /** |
|
| 1032 | + * Attempt to download a remote file attachment |
|
| 1033 | + * |
|
| 1034 | + * @param string $url URL of item to fetch |
|
| 1035 | + * @param array $post Attachment details |
|
| 1036 | + * @return array|WP_Error Local file location details on success, WP_Error otherwise |
|
| 1037 | + */ |
|
| 1038 | + function fetch_remote_file( $url, $post ) { |
|
| 1039 | + // extract the file name and extension from the url |
|
| 1040 | + $file_name = basename( $url ); |
|
| 1041 | + |
|
| 1042 | + // get placeholder file in the upload dir with a unique, sanitized filename |
|
| 1043 | + $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] ); |
|
| 1044 | + if ( $upload['error'] ) { |
|
| 1045 | + return new WP_Error( 'upload_dir_error', $upload['error'] ); |
|
| 1046 | + } |
|
| 1047 | + |
|
| 1048 | + // fetch the remote url and write it to the placeholder file |
|
| 1049 | + $headers = wp_get_http( $url, $upload['file'] ); |
|
| 1050 | + |
|
| 1051 | + // request failed |
|
| 1052 | + if ( ! $headers ) { |
|
| 1053 | + @unlink( $upload['file'] ); |
|
| 1054 | + return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') ); |
|
| 1055 | + } |
|
| 1056 | + |
|
| 1057 | + // make sure the fetch was successful |
|
| 1058 | + if ( $headers['response'] != '200' ) { |
|
| 1059 | + @unlink( $upload['file'] ); |
|
| 1060 | + return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) ); |
|
| 1061 | + } |
|
| 1062 | + |
|
| 1063 | + $filesize = filesize( $upload['file'] ); |
|
| 1064 | + |
|
| 1065 | + if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) { |
|
| 1066 | + @unlink( $upload['file'] ); |
|
| 1067 | + return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') ); |
|
| 1068 | + } |
|
| 1069 | + |
|
| 1070 | + if ( 0 == $filesize ) { |
|
| 1071 | + @unlink( $upload['file'] ); |
|
| 1072 | + return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') ); |
|
| 1073 | + } |
|
| 1074 | + |
|
| 1075 | + $max_size = (int) $this->max_attachment_size(); |
|
| 1076 | + if ( ! empty( $max_size ) && $filesize > $max_size ) { |
|
| 1077 | + @unlink( $upload['file'] ); |
|
| 1078 | + return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) ); |
|
| 1079 | + } |
|
| 1080 | + |
|
| 1081 | + // keep track of the old and new urls so we can substitute them later |
|
| 1082 | + $this->url_remap[$url] = $upload['url']; |
|
| 1083 | + $this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed? |
|
| 1084 | + // keep track of the destination if the remote url is redirected somewhere else |
|
| 1085 | + if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url ) { |
|
| 1086 | + $this->url_remap[$headers['x-final-location']] = $upload['url']; |
|
| 1087 | + } |
|
| 1088 | + |
|
| 1089 | + return $upload; |
|
| 1090 | + } |
|
| 1091 | + |
|
| 1092 | + /** |
|
| 1093 | + * Attempt to associate posts and menu items with previously missing parents |
|
| 1094 | + * |
|
| 1095 | + * An imported post's parent may not have been imported when it was first created |
|
| 1096 | + * so try again. Similarly for child menu items and menu items which were missing |
|
| 1097 | + * the object (e.g. post) they represent in the menu |
|
| 1098 | + */ |
|
| 1099 | + function backfill_parents() { |
|
| 1100 | + global $wpdb; |
|
| 1101 | + |
|
| 1102 | + // find parents for post orphans |
|
| 1103 | + foreach ( $this->post_orphans as $child_id => $parent_id ) { |
|
| 1104 | + $local_child_id = $local_parent_id = false; |
|
| 1105 | + if ( isset( $this->processed_posts[$child_id] ) ) { |
|
| 1106 | + $local_child_id = $this->processed_posts[$child_id]; |
|
| 1107 | + } |
|
| 1108 | + if ( isset( $this->processed_posts[$parent_id] ) ) { |
|
| 1109 | + $local_parent_id = $this->processed_posts[$parent_id]; |
|
| 1110 | + } |
|
| 1111 | + |
|
| 1112 | + if ( $local_child_id && $local_parent_id ) { |
|
| 1113 | + $wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' ); |
|
| 1114 | + } |
|
| 1115 | + } |
|
| 1116 | + |
|
| 1117 | + // all other posts/terms are imported, retry menu items with missing associated object |
|
| 1118 | + $missing_menu_items = $this->missing_menu_items; |
|
| 1119 | + foreach ( $missing_menu_items as $item ) { |
|
| 1120 | + $this->process_menu_item( $item ); |
|
| 1121 | + } |
|
| 1122 | + |
|
| 1123 | + // find parents for menu item orphans |
|
| 1124 | + foreach ( $this->menu_item_orphans as $child_id => $parent_id ) { |
|
| 1125 | + $local_child_id = $local_parent_id = 0; |
|
| 1126 | + if ( isset( $this->processed_menu_items[$child_id] ) ) { |
|
| 1127 | + $local_child_id = $this->processed_menu_items[$child_id]; |
|
| 1128 | + } |
|
| 1129 | + if ( isset( $this->processed_menu_items[$parent_id] ) ) { |
|
| 1130 | + $local_parent_id = $this->processed_menu_items[$parent_id]; |
|
| 1131 | + } |
|
| 1132 | + |
|
| 1133 | + if ( $local_child_id && $local_parent_id ) { |
|
| 1134 | + update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id ); |
|
| 1135 | + } |
|
| 1136 | + } |
|
| 1137 | + } |
|
| 1138 | + |
|
| 1139 | + /** |
|
| 1140 | + * Use stored mapping information to update old attachment URLs |
|
| 1141 | + */ |
|
| 1142 | + function backfill_attachment_urls() { |
|
| 1143 | + global $wpdb; |
|
| 1144 | + // make sure we do the longest urls first, in case one is a substring of another |
|
| 1145 | + uksort( $this->url_remap, array(&$this, 'cmpr_strlen') ); |
|
| 1146 | + |
|
| 1147 | + foreach ( $this->url_remap as $from_url => $to_url ) { |
|
| 1148 | + // remap urls in post_content |
|
| 1149 | + $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) ); |
|
| 1150 | + // remap enclosure urls |
|
| 1151 | + $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) ); |
|
| 1152 | + } |
|
| 1153 | + } |
|
| 1154 | + |
|
| 1155 | + /** |
|
| 1156 | + * Update _thumbnail_id meta to new, imported attachment IDs |
|
| 1157 | + */ |
|
| 1158 | + function remap_featured_images() { |
|
| 1159 | + // cycle through posts that have a featured image |
|
| 1160 | + foreach ( $this->featured_images as $post_id => $value ) { |
|
| 1161 | + if ( isset( $this->processed_posts[$value] ) ) { |
|
| 1162 | + $new_id = $this->processed_posts[$value]; |
|
| 1163 | + // only update if there's a difference |
|
| 1164 | + if ( $new_id != $value ) { |
|
| 1165 | + update_post_meta( $post_id, '_thumbnail_id', $new_id ); |
|
| 1166 | + } |
|
| 1167 | + } |
|
| 1168 | + } |
|
| 1169 | + } |
|
| 1170 | + |
|
| 1171 | + /** |
|
| 1172 | + * Parse a WXR file |
|
| 1173 | + * |
|
| 1174 | + * @param string $file Path to WXR file for parsing |
|
| 1175 | + * @return array Information gathered from the WXR file |
|
| 1176 | + */ |
|
| 1177 | + function parse( $file ) { |
|
| 1178 | + $parser = new WXR_Parser(); |
|
| 1179 | + return $parser->parse( $file ); |
|
| 1180 | + } |
|
| 1181 | + |
|
| 1182 | + // Display import page title |
|
| 1183 | + function header() { |
|
| 1184 | + echo '<div class="wrap">'; |
|
| 1185 | + screen_icon(); |
|
| 1186 | + echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>'; |
|
| 1187 | + |
|
| 1188 | + $updates = get_plugin_updates(); |
|
| 1189 | + $basename = plugin_basename(__FILE__); |
|
| 1190 | + if ( isset( $updates[$basename] ) ) { |
|
| 1191 | + $update = $updates[$basename]; |
|
| 1192 | + echo '<div class="error"><p><strong>'; |
|
| 1193 | + printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wordpress-importer' ), $update->update->new_version ); |
|
| 1194 | + echo '</strong></p></div>'; |
|
| 1195 | + } |
|
| 1196 | + } |
|
| 1197 | + |
|
| 1198 | + // Close div.wrap |
|
| 1199 | + function footer() { |
|
| 1200 | + echo '</div>'; |
|
| 1201 | + } |
|
| 1202 | + |
|
| 1203 | + /** |
|
| 1204 | + * Display introductory text and file upload form |
|
| 1205 | + */ |
|
| 1206 | + function greet() { |
|
| 1207 | + echo '<div class="narrow">'; |
|
| 1208 | + echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>'; |
|
| 1209 | + echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>'; |
|
| 1210 | + wp_import_upload_form( 'admin.php?import=wordpress&step=1' ); |
|
| 1211 | + echo '</div>'; |
|
| 1212 | + } |
|
| 1213 | + |
|
| 1214 | + /** |
|
| 1215 | + * Decide if the given meta key maps to information we will want to import |
|
| 1216 | + * |
|
| 1217 | + * @param string $key The meta key to check |
|
| 1218 | + * @return string|bool The key if we do want to import, false if not |
|
| 1219 | + */ |
|
| 1220 | + function is_valid_meta_key( $key ) { |
|
| 1221 | + // skip attachment metadata since we'll regenerate it from scratch |
|
| 1222 | + // skip _edit_lock as not relevant for import |
|
| 1223 | + if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) ) { |
|
| 1224 | + return false; |
|
| 1225 | + } |
|
| 1226 | + return $key; |
|
| 1227 | + } |
|
| 1228 | + |
|
| 1229 | + /** |
|
| 1230 | + * Decide whether or not the importer is allowed to create users. |
|
| 1231 | + * Default is true, can be filtered via import_allow_create_users |
|
| 1232 | + * |
|
| 1233 | + * @return bool True if creating users is allowed |
|
| 1234 | + */ |
|
| 1235 | + function allow_create_users() { |
|
| 1236 | + return apply_filters( 'import_allow_create_users', true ); |
|
| 1237 | + } |
|
| 1238 | + |
|
| 1239 | + /** |
|
| 1240 | + * Decide whether or not the importer should attempt to download attachment files. |
|
| 1241 | + * Default is true, can be filtered via import_allow_fetch_attachments. The choice |
|
| 1242 | + * made at the import options screen must also be true, false here hides that checkbox. |
|
| 1243 | + * |
|
| 1244 | + * @return bool True if downloading attachments is allowed |
|
| 1245 | + */ |
|
| 1246 | + function allow_fetch_attachments() { |
|
| 1247 | + return apply_filters( 'import_allow_fetch_attachments', true ); |
|
| 1248 | + } |
|
| 1249 | + |
|
| 1250 | + /** |
|
| 1251 | + * Decide what the maximum file size for downloaded attachments is. |
|
| 1252 | + * Default is 0 (unlimited), can be filtered via import_attachment_size_limit |
|
| 1253 | + * |
|
| 1254 | + * @return int Maximum attachment file size to import |
|
| 1255 | + */ |
|
| 1256 | + function max_attachment_size() { |
|
| 1257 | + return apply_filters( 'import_attachment_size_limit', 0 ); |
|
| 1258 | + } |
|
| 1259 | + |
|
| 1260 | + /** |
|
| 1261 | + * Added to http_request_timeout filter to force timeout at 60 seconds during import |
|
| 1262 | + * @return int 60 |
|
| 1263 | + */ |
|
| 1264 | + function bump_request_timeout( $val ) { |
|
| 1265 | + return 60; |
|
| 1266 | + } |
|
| 1267 | + |
|
| 1268 | + // return the difference in length between two strings |
|
| 1269 | + function cmpr_strlen( $a, $b ) { |
|
| 1270 | + return strlen($b) - strlen($a); |
|
| 1271 | + } |
|
| 1272 | 1272 | } |
| 1273 | 1273 | |
| 1274 | 1274 | } // class_exists( 'WP_Importer' ) |
| 1275 | 1275 | |
| 1276 | 1276 | function wordpress_importer_init() { |
| 1277 | - load_plugin_textdomain( 'wordpress-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
|
| 1278 | - |
|
| 1279 | - /** |
|
| 1280 | - * WordPress Importer object for registering the import callback |
|
| 1281 | - * @global WP_Import $wp_import |
|
| 1282 | - */ |
|
| 1283 | - $GLOBALS['wp_import'] = new WP_Import(); |
|
| 1284 | - register_importer( 'wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array( $GLOBALS['wp_import'], 'dispatch' ) ); |
|
| 1277 | + load_plugin_textdomain( 'wordpress-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
|
| 1278 | + |
|
| 1279 | + /** |
|
| 1280 | + * WordPress Importer object for registering the import callback |
|
| 1281 | + * @global WP_Import $wp_import |
|
| 1282 | + */ |
|
| 1283 | + $GLOBALS['wp_import'] = new WP_Import(); |
|
| 1284 | + register_importer( 'wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array( $GLOBALS['wp_import'], 'dispatch' ) ); |
|
| 1285 | 1285 | } |
| 1286 | 1286 | add_action( 'admin_init', 'wordpress_importer_init' ); |
@@ -10,25 +10,25 @@ discard block |
||
| 10 | 10 | License: GPL version 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html |
| 11 | 11 | */ |
| 12 | 12 | |
| 13 | -if ( ! defined( 'WP_LOAD_IMPORTERS' ) ) { |
|
| 13 | +if ( ! defined('WP_LOAD_IMPORTERS')) { |
|
| 14 | 14 | return; |
| 15 | 15 | } |
| 16 | 16 | |
| 17 | 17 | /** Display verbose errors */ |
| 18 | -define( 'IMPORT_DEBUG', false ); |
|
| 18 | +define('IMPORT_DEBUG', false); |
|
| 19 | 19 | |
| 20 | 20 | // Load Importer API |
| 21 | 21 | require_once ABSPATH . 'wp-admin/includes/import.php'; |
| 22 | 22 | |
| 23 | -if ( ! class_exists( 'WP_Importer' ) ) { |
|
| 23 | +if ( ! class_exists('WP_Importer')) { |
|
| 24 | 24 | $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; |
| 25 | - if ( file_exists( $class_wp_importer ) ) { |
|
| 25 | + if (file_exists($class_wp_importer)) { |
|
| 26 | 26 | require $class_wp_importer; |
| 27 | 27 | } |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | // include WXR file parsers |
| 31 | -require dirname( __FILE__ ) . '/parsers.php'; |
|
| 31 | +require dirname(__FILE__) . '/parsers.php'; |
|
| 32 | 32 | |
| 33 | 33 | /** |
| 34 | 34 | * WordPress Importer class for managing the import process of a WXR file |
@@ -36,7 +36,7 @@ discard block |
||
| 36 | 36 | * @package WordPress |
| 37 | 37 | * @subpackage Importer |
| 38 | 38 | */ |
| 39 | -if ( class_exists( 'WP_Importer' ) ) { |
|
| 39 | +if (class_exists('WP_Importer')) { |
|
| 40 | 40 | class WP_Import extends WP_Importer { |
| 41 | 41 | var $max_wxr_version = 1.2; // max. supported WXR version |
| 42 | 42 | |
@@ -73,24 +73,24 @@ discard block |
||
| 73 | 73 | function dispatch() { |
| 74 | 74 | $this->header(); |
| 75 | 75 | |
| 76 | - $step = empty( $_GET['step'] ) ? 0 : (int) $_GET['step']; |
|
| 77 | - switch ( $step ) { |
|
| 76 | + $step = empty($_GET['step']) ? 0 : (int) $_GET['step']; |
|
| 77 | + switch ($step) { |
|
| 78 | 78 | case 0: |
| 79 | 79 | $this->greet(); |
| 80 | 80 | break; |
| 81 | 81 | case 1: |
| 82 | - check_admin_referer( 'import-upload' ); |
|
| 83 | - if ( $this->handle_upload() ) { |
|
| 82 | + check_admin_referer('import-upload'); |
|
| 83 | + if ($this->handle_upload()) { |
|
| 84 | 84 | $this->import_options(); |
| 85 | 85 | } |
| 86 | 86 | break; |
| 87 | 87 | case 2: |
| 88 | - check_admin_referer( 'import-wordpress' ); |
|
| 89 | - $this->fetch_attachments = ( ! empty( $_POST['fetch_attachments'] ) && $this->allow_fetch_attachments() ); |
|
| 88 | + check_admin_referer('import-wordpress'); |
|
| 89 | + $this->fetch_attachments = ( ! empty($_POST['fetch_attachments']) && $this->allow_fetch_attachments()); |
|
| 90 | 90 | $this->id = (int) $_POST['import_id']; |
| 91 | - $file = get_attached_file( $this->id ); |
|
| 91 | + $file = get_attached_file($this->id); |
|
| 92 | 92 | set_time_limit(0); |
| 93 | - $this->import( $file ); |
|
| 93 | + $this->import($file); |
|
| 94 | 94 | break; |
| 95 | 95 | } |
| 96 | 96 | |
@@ -102,20 +102,20 @@ discard block |
||
| 102 | 102 | * |
| 103 | 103 | * @param string $file Path to the WXR file for importing |
| 104 | 104 | */ |
| 105 | - function import( $file ) { |
|
| 106 | - add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) ); |
|
| 107 | - add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) ); |
|
| 105 | + function import($file) { |
|
| 106 | + add_filter('import_post_meta_key', array($this, 'is_valid_meta_key')); |
|
| 107 | + add_filter('http_request_timeout', array(&$this, 'bump_request_timeout')); |
|
| 108 | 108 | |
| 109 | - $this->import_start( $file ); |
|
| 109 | + $this->import_start($file); |
|
| 110 | 110 | |
| 111 | 111 | $this->get_author_mapping(); |
| 112 | 112 | |
| 113 | - wp_suspend_cache_invalidation( true ); |
|
| 113 | + wp_suspend_cache_invalidation(true); |
|
| 114 | 114 | $this->process_categories(); |
| 115 | 115 | $this->process_tags(); |
| 116 | 116 | $this->process_terms(); |
| 117 | 117 | $this->process_posts(); |
| 118 | - wp_suspend_cache_invalidation( false ); |
|
| 118 | + wp_suspend_cache_invalidation(false); |
|
| 119 | 119 | |
| 120 | 120 | // update incorrect/missing information in the DB |
| 121 | 121 | $this->backfill_parents(); |
@@ -130,56 +130,56 @@ discard block |
||
| 130 | 130 | * |
| 131 | 131 | * @param string $file Path to the WXR file for importing |
| 132 | 132 | */ |
| 133 | - function import_start( $file ) { |
|
| 134 | - if ( ! is_file($file) ) { |
|
| 135 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 136 | - echo __( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>'; |
|
| 133 | + function import_start($file) { |
|
| 134 | + if ( ! is_file($file)) { |
|
| 135 | + echo '<p><strong>' . __('Sorry, there has been an error.', 'wordpress-importer') . '</strong><br />'; |
|
| 136 | + echo __('The file does not exist, please try again.', 'wordpress-importer') . '</p>'; |
|
| 137 | 137 | $this->footer(); |
| 138 | 138 | die(); |
| 139 | 139 | } |
| 140 | 140 | |
| 141 | - $import_data = $this->parse( $file ); |
|
| 141 | + $import_data = $this->parse($file); |
|
| 142 | 142 | |
| 143 | - if ( is_wp_error( $import_data ) ) { |
|
| 144 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 145 | - echo esc_html( $import_data->get_error_message() ) . '</p>'; |
|
| 143 | + if (is_wp_error($import_data)) { |
|
| 144 | + echo '<p><strong>' . __('Sorry, there has been an error.', 'wordpress-importer') . '</strong><br />'; |
|
| 145 | + echo esc_html($import_data->get_error_message()) . '</p>'; |
|
| 146 | 146 | $this->footer(); |
| 147 | 147 | die(); |
| 148 | 148 | } |
| 149 | 149 | |
| 150 | 150 | $this->version = $import_data['version']; |
| 151 | - $this->get_authors_from_import( $import_data ); |
|
| 151 | + $this->get_authors_from_import($import_data); |
|
| 152 | 152 | $this->posts = $import_data['posts']; |
| 153 | 153 | $this->terms = $import_data['terms']; |
| 154 | 154 | $this->categories = $import_data['categories']; |
| 155 | 155 | $this->tags = $import_data['tags']; |
| 156 | - $this->base_url = esc_url( $import_data['base_url'] ); |
|
| 156 | + $this->base_url = esc_url($import_data['base_url']); |
|
| 157 | 157 | |
| 158 | - wp_defer_term_counting( true ); |
|
| 159 | - wp_defer_comment_counting( true ); |
|
| 158 | + wp_defer_term_counting(true); |
|
| 159 | + wp_defer_comment_counting(true); |
|
| 160 | 160 | |
| 161 | - do_action( 'import_start' ); |
|
| 161 | + do_action('import_start'); |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | /** |
| 165 | 165 | * Performs post-import cleanup of files and the cache |
| 166 | 166 | */ |
| 167 | 167 | function import_end() { |
| 168 | - wp_import_cleanup( $this->id ); |
|
| 168 | + wp_import_cleanup($this->id); |
|
| 169 | 169 | |
| 170 | 170 | wp_cache_flush(); |
| 171 | - foreach ( get_taxonomies() as $tax ) { |
|
| 172 | - delete_option( "{$tax}_children" ); |
|
| 173 | - _get_term_hierarchy( $tax ); |
|
| 171 | + foreach (get_taxonomies() as $tax) { |
|
| 172 | + delete_option("{$tax}_children"); |
|
| 173 | + _get_term_hierarchy($tax); |
|
| 174 | 174 | } |
| 175 | 175 | |
| 176 | - wp_defer_term_counting( false ); |
|
| 177 | - wp_defer_comment_counting( false ); |
|
| 176 | + wp_defer_term_counting(false); |
|
| 177 | + wp_defer_comment_counting(false); |
|
| 178 | 178 | |
| 179 | - echo '<p>' . __( 'All done.', 'wordpress-importer' ) . ' <a href="' . admin_url() . '">' . __( 'Have fun!', 'wordpress-importer' ) . '</a>' . '</p>'; |
|
| 180 | - echo '<p>' . __( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>'; |
|
| 179 | + echo '<p>' . __('All done.', 'wordpress-importer') . ' <a href="' . admin_url() . '">' . __('Have fun!', 'wordpress-importer') . '</a>' . '</p>'; |
|
| 180 | + echo '<p>' . __('Remember to update the passwords and roles of imported users.', 'wordpress-importer') . '</p>'; |
|
| 181 | 181 | |
| 182 | - do_action( 'import_end' ); |
|
| 182 | + do_action('import_end'); |
|
| 183 | 183 | } |
| 184 | 184 | |
| 185 | 185 | /** |
@@ -191,33 +191,33 @@ discard block |
||
| 191 | 191 | function handle_upload() { |
| 192 | 192 | $file = wp_import_handle_upload(); |
| 193 | 193 | |
| 194 | - if ( isset( $file['error'] ) ) { |
|
| 195 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 196 | - echo esc_html( $file['error'] ) . '</p>'; |
|
| 194 | + if (isset($file['error'])) { |
|
| 195 | + echo '<p><strong>' . __('Sorry, there has been an error.', 'wordpress-importer') . '</strong><br />'; |
|
| 196 | + echo esc_html($file['error']) . '</p>'; |
|
| 197 | 197 | return false; |
| 198 | - } else if ( ! file_exists( $file['file'] ) ) { |
|
| 199 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 200 | - printf( __( 'The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'wordpress-importer' ), esc_html( $file['file'] ) ); |
|
| 198 | + } else if ( ! file_exists($file['file'])) { |
|
| 199 | + echo '<p><strong>' . __('Sorry, there has been an error.', 'wordpress-importer') . '</strong><br />'; |
|
| 200 | + printf(__('The export file could not be found at <code>%s</code>. It is likely that this was caused by a permissions problem.', 'wordpress-importer'), esc_html($file['file'])); |
|
| 201 | 201 | echo '</p>'; |
| 202 | 202 | return false; |
| 203 | 203 | } |
| 204 | 204 | |
| 205 | 205 | $this->id = (int) $file['id']; |
| 206 | - $import_data = $this->parse( $file['file'] ); |
|
| 207 | - if ( is_wp_error( $import_data ) ) { |
|
| 208 | - echo '<p><strong>' . __( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />'; |
|
| 209 | - echo esc_html( $import_data->get_error_message() ) . '</p>'; |
|
| 206 | + $import_data = $this->parse($file['file']); |
|
| 207 | + if (is_wp_error($import_data)) { |
|
| 208 | + echo '<p><strong>' . __('Sorry, there has been an error.', 'wordpress-importer') . '</strong><br />'; |
|
| 209 | + echo esc_html($import_data->get_error_message()) . '</p>'; |
|
| 210 | 210 | return false; |
| 211 | 211 | } |
| 212 | 212 | |
| 213 | 213 | $this->version = $import_data['version']; |
| 214 | - if ( $this->version > $this->max_wxr_version ) { |
|
| 214 | + if ($this->version > $this->max_wxr_version) { |
|
| 215 | 215 | echo '<div class="error"><p><strong>'; |
| 216 | - printf( __( 'This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer' ), esc_html($import_data['version']) ); |
|
| 216 | + printf(__('This WXR file (version %s) may not be supported by this version of the importer. Please consider updating.', 'wordpress-importer'), esc_html($import_data['version'])); |
|
| 217 | 217 | echo '</strong></p></div>'; |
| 218 | 218 | } |
| 219 | 219 | |
| 220 | - $this->get_authors_from_import( $import_data ); |
|
| 220 | + $this->get_authors_from_import($import_data); |
|
| 221 | 221 | |
| 222 | 222 | return true; |
| 223 | 223 | } |
@@ -230,20 +230,20 @@ discard block |
||
| 230 | 230 | * |
| 231 | 231 | * @param array $import_data Data returned by a WXR parser |
| 232 | 232 | */ |
| 233 | - function get_authors_from_import( $import_data ) { |
|
| 234 | - if ( ! empty( $import_data['authors'] ) ) { |
|
| 233 | + function get_authors_from_import($import_data) { |
|
| 234 | + if ( ! empty($import_data['authors'])) { |
|
| 235 | 235 | $this->authors = $import_data['authors']; |
| 236 | 236 | // no author information, grab it from the posts |
| 237 | 237 | } else { |
| 238 | - foreach ( $import_data['posts'] as $post ) { |
|
| 239 | - $login = sanitize_user( $post['post_author'], true ); |
|
| 240 | - if ( empty( $login ) ) { |
|
| 241 | - printf( __( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) ); |
|
| 238 | + foreach ($import_data['posts'] as $post) { |
|
| 239 | + $login = sanitize_user($post['post_author'], true); |
|
| 240 | + if (empty($login)) { |
|
| 241 | + printf(__('Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer'), esc_html($post['post_author'])); |
|
| 242 | 242 | echo '<br />'; |
| 243 | 243 | continue; |
| 244 | 244 | } |
| 245 | 245 | |
| 246 | - if ( ! isset($this->authors[$login]) ) { |
|
| 246 | + if ( ! isset($this->authors[$login])) { |
|
| 247 | 247 | $this->authors[$login] = array( |
| 248 | 248 | 'author_login' => $login, |
| 249 | 249 | 'author_display_name' => $post['post_author'] |
@@ -260,32 +260,32 @@ discard block |
||
| 260 | 260 | function import_options() { |
| 261 | 261 | $j = 0; |
| 262 | 262 | ?> |
| 263 | -<form action="<?php echo admin_url( 'admin.php?import=wordpress&step=2' ); ?>" method="post"> |
|
| 264 | - <?php wp_nonce_field( 'import-wordpress' ); ?> |
|
| 263 | +<form action="<?php echo admin_url('admin.php?import=wordpress&step=2'); ?>" method="post"> |
|
| 264 | + <?php wp_nonce_field('import-wordpress'); ?> |
|
| 265 | 265 | <input type="hidden" name="import_id" value="<?php echo $this->id; ?>" /> |
| 266 | 266 | |
| 267 | -<?php if ( ! empty( $this->authors ) ) : ?> |
|
| 268 | - <h3><?php _e( 'Assign Authors', 'wordpress-importer' ); ?></h3> |
|
| 269 | - <p><?php _e( 'To make it easier for you to edit and save the imported content, you may want to reassign the author of the imported item to an existing user of this site. For example, you may want to import all the entries as <code>admin</code>s entries.', 'wordpress-importer' ); ?></p> |
|
| 270 | -<?php if ( $this->allow_create_users() ) : ?> |
|
| 271 | - <p><?php printf( __( 'If a new user is created by WordPress, a new password will be randomly generated and the new user’s role will be set as %s. Manually changing the new user’s details will be necessary.', 'wordpress-importer' ), esc_html( get_option('default_role') ) ); ?></p> |
|
| 267 | +<?php if ( ! empty($this->authors)) : ?> |
|
| 268 | + <h3><?php _e('Assign Authors', 'wordpress-importer'); ?></h3> |
|
| 269 | + <p><?php _e('To make it easier for you to edit and save the imported content, you may want to reassign the author of the imported item to an existing user of this site. For example, you may want to import all the entries as <code>admin</code>s entries.', 'wordpress-importer'); ?></p> |
|
| 270 | +<?php if ($this->allow_create_users()) : ?> |
|
| 271 | + <p><?php printf(__('If a new user is created by WordPress, a new password will be randomly generated and the new user’s role will be set as %s. Manually changing the new user’s details will be necessary.', 'wordpress-importer'), esc_html(get_option('default_role'))); ?></p> |
|
| 272 | 272 | <?php endif; ?> |
| 273 | 273 | <ol id="authors"> |
| 274 | -<?php foreach ( $this->authors as $author ) : ?> |
|
| 275 | - <li><?php $this->author_select( $j++, $author ); ?></li> |
|
| 274 | +<?php foreach ($this->authors as $author) : ?> |
|
| 275 | + <li><?php $this->author_select($j++, $author); ?></li> |
|
| 276 | 276 | <?php endforeach; ?> |
| 277 | 277 | </ol> |
| 278 | 278 | <?php endif; ?> |
| 279 | 279 | |
| 280 | -<?php if ( $this->allow_fetch_attachments() ) : ?> |
|
| 281 | - <h3><?php _e( 'Import Attachments', 'wordpress-importer' ); ?></h3> |
|
| 280 | +<?php if ($this->allow_fetch_attachments()) : ?> |
|
| 281 | + <h3><?php _e('Import Attachments', 'wordpress-importer'); ?></h3> |
|
| 282 | 282 | <p> |
| 283 | 283 | <input type="checkbox" value="1" name="fetch_attachments" id="import-attachments" /> |
| 284 | - <label for="import-attachments"><?php _e( 'Download and import file attachments', 'wordpress-importer' ); ?></label> |
|
| 284 | + <label for="import-attachments"><?php _e('Download and import file attachments', 'wordpress-importer'); ?></label> |
|
| 285 | 285 | </p> |
| 286 | 286 | <?php endif; ?> |
| 287 | 287 | |
| 288 | - <p class="submit"><input type="submit" class="button" value="<?php esc_attr_e( 'Submit', 'wordpress-importer' ); ?>" /></p> |
|
| 288 | + <p class="submit"><input type="submit" class="button" value="<?php esc_attr_e('Submit', 'wordpress-importer'); ?>" /></p> |
|
| 289 | 289 | </form> |
| 290 | 290 | <?php |
| 291 | 291 | } |
@@ -297,40 +297,40 @@ discard block |
||
| 297 | 297 | * @param int $n Index for each author in the form |
| 298 | 298 | * @param array $author Author information, e.g. login, display name, email |
| 299 | 299 | */ |
| 300 | - function author_select( $n, $author ) { |
|
| 301 | - _e( 'Import author:', 'wordpress-importer' ); |
|
| 302 | - echo ' <strong>' . esc_html( $author['author_display_name'] ); |
|
| 303 | - if ( $this->version != '1.0' ) { |
|
| 304 | - echo ' (' . esc_html( $author['author_login'] ) . ')'; |
|
| 300 | + function author_select($n, $author) { |
|
| 301 | + _e('Import author:', 'wordpress-importer'); |
|
| 302 | + echo ' <strong>' . esc_html($author['author_display_name']); |
|
| 303 | + if ($this->version != '1.0') { |
|
| 304 | + echo ' (' . esc_html($author['author_login']) . ')'; |
|
| 305 | 305 | } |
| 306 | 306 | echo '</strong><br />'; |
| 307 | 307 | |
| 308 | - if ( $this->version != '1.0' ) { |
|
| 308 | + if ($this->version != '1.0') { |
|
| 309 | 309 | echo '<div style="margin-left:18px">'; |
| 310 | 310 | } |
| 311 | 311 | |
| 312 | 312 | $create_users = $this->allow_create_users(); |
| 313 | - if ( $create_users ) { |
|
| 314 | - if ( $this->version != '1.0' ) { |
|
| 315 | - _e( 'or create new user with login name:', 'wordpress-importer' ); |
|
| 313 | + if ($create_users) { |
|
| 314 | + if ($this->version != '1.0') { |
|
| 315 | + _e('or create new user with login name:', 'wordpress-importer'); |
|
| 316 | 316 | $value = ''; |
| 317 | 317 | } else { |
| 318 | - _e( 'as a new user:', 'wordpress-importer' ); |
|
| 319 | - $value = esc_attr( sanitize_user( $author['author_login'], true ) ); |
|
| 318 | + _e('as a new user:', 'wordpress-importer'); |
|
| 319 | + $value = esc_attr(sanitize_user($author['author_login'], true)); |
|
| 320 | 320 | } |
| 321 | 321 | |
| 322 | - echo ' <input type="text" name="user_new['.$n.']" value="'. $value .'" /><br />'; |
|
| 322 | + echo ' <input type="text" name="user_new[' . $n . ']" value="' . $value . '" /><br />'; |
|
| 323 | 323 | } |
| 324 | 324 | |
| 325 | - if ( ! $create_users && $this->version == '1.0' ) { |
|
| 326 | - _e( 'assign posts to an existing user:', 'wordpress-importer' ); |
|
| 325 | + if ( ! $create_users && $this->version == '1.0') { |
|
| 326 | + _e('assign posts to an existing user:', 'wordpress-importer'); |
|
| 327 | 327 | } else { |
| 328 | - _e( 'or assign posts to an existing user:', 'wordpress-importer' ); |
|
| 328 | + _e('or assign posts to an existing user:', 'wordpress-importer'); |
|
| 329 | 329 | } |
| 330 | - wp_dropdown_users( array( 'name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __( '- Select -', 'wordpress-importer' ) ) ); |
|
| 331 | - echo '<input type="hidden" name="imported_authors['.$n.']" value="' . esc_attr( $author['author_login'] ) . '" />'; |
|
| 330 | + wp_dropdown_users(array('name' => "user_map[$n]", 'multi' => true, 'show_option_all' => __('- Select -', 'wordpress-importer'))); |
|
| 331 | + echo '<input type="hidden" name="imported_authors[' . $n . ']" value="' . esc_attr($author['author_login']) . '" />'; |
|
| 332 | 332 | |
| 333 | - if ( $this->version != '1.0' ) { |
|
| 333 | + if ($this->version != '1.0') { |
|
| 334 | 334 | echo '</div>'; |
| 335 | 335 | } |
| 336 | 336 | } |
@@ -341,48 +341,48 @@ discard block |
||
| 341 | 341 | * or falls back to the current user in case of error with either of the previous |
| 342 | 342 | */ |
| 343 | 343 | function get_author_mapping() { |
| 344 | - if ( ! isset( $_POST['imported_authors'] ) ) { |
|
| 344 | + if ( ! isset($_POST['imported_authors'])) { |
|
| 345 | 345 | return; |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | $create_users = $this->allow_create_users(); |
| 349 | 349 | |
| 350 | - foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) { |
|
| 350 | + foreach ((array) $_POST['imported_authors'] as $i => $old_login) { |
|
| 351 | 351 | // Multisite adds strtolower to sanitize_user. Need to sanitize here to stop breakage in process_posts. |
| 352 | - $santized_old_login = sanitize_user( $old_login, true ); |
|
| 353 | - $old_id = isset( $this->authors[$old_login]['author_id'] ) ? intval($this->authors[$old_login]['author_id']) : false; |
|
| 352 | + $santized_old_login = sanitize_user($old_login, true); |
|
| 353 | + $old_id = isset($this->authors[$old_login]['author_id']) ? intval($this->authors[$old_login]['author_id']) : false; |
|
| 354 | 354 | |
| 355 | - if ( ! empty( $_POST['user_map'][$i] ) ) { |
|
| 356 | - $user = get_userdata( intval($_POST['user_map'][$i]) ); |
|
| 357 | - if ( isset( $user->ID ) ) { |
|
| 358 | - if ( $old_id ) { |
|
| 355 | + if ( ! empty($_POST['user_map'][$i])) { |
|
| 356 | + $user = get_userdata(intval($_POST['user_map'][$i])); |
|
| 357 | + if (isset($user->ID)) { |
|
| 358 | + if ($old_id) { |
|
| 359 | 359 | $this->processed_authors[$old_id] = $user->ID; |
| 360 | 360 | } |
| 361 | 361 | $this->author_mapping[$santized_old_login] = $user->ID; |
| 362 | 362 | } |
| 363 | - } else if ( $create_users ) { |
|
| 364 | - if ( ! empty($_POST['user_new'][$i]) ) { |
|
| 365 | - $user_id = wp_create_user( $_POST['user_new'][$i], wp_generate_password() ); |
|
| 366 | - } else if ( $this->version != '1.0' ) { |
|
| 363 | + } else if ($create_users) { |
|
| 364 | + if ( ! empty($_POST['user_new'][$i])) { |
|
| 365 | + $user_id = wp_create_user($_POST['user_new'][$i], wp_generate_password()); |
|
| 366 | + } else if ($this->version != '1.0') { |
|
| 367 | 367 | $user_data = array( |
| 368 | 368 | 'user_login' => $old_login, |
| 369 | 369 | 'user_pass' => wp_generate_password(), |
| 370 | - 'user_email' => isset( $this->authors[$old_login]['author_email'] ) ? $this->authors[$old_login]['author_email'] : '', |
|
| 370 | + 'user_email' => isset($this->authors[$old_login]['author_email']) ? $this->authors[$old_login]['author_email'] : '', |
|
| 371 | 371 | 'display_name' => $this->authors[$old_login]['author_display_name'], |
| 372 | - 'first_name' => isset( $this->authors[$old_login]['author_first_name'] ) ? $this->authors[$old_login]['author_first_name'] : '', |
|
| 373 | - 'last_name' => isset( $this->authors[$old_login]['author_last_name'] ) ? $this->authors[$old_login]['author_last_name'] : '', |
|
| 372 | + 'first_name' => isset($this->authors[$old_login]['author_first_name']) ? $this->authors[$old_login]['author_first_name'] : '', |
|
| 373 | + 'last_name' => isset($this->authors[$old_login]['author_last_name']) ? $this->authors[$old_login]['author_last_name'] : '', |
|
| 374 | 374 | ); |
| 375 | - $user_id = wp_insert_user( $user_data ); |
|
| 375 | + $user_id = wp_insert_user($user_data); |
|
| 376 | 376 | } |
| 377 | 377 | |
| 378 | - if ( ! is_wp_error( $user_id ) ) { |
|
| 379 | - if ( $old_id ) { |
|
| 378 | + if ( ! is_wp_error($user_id)) { |
|
| 379 | + if ($old_id) { |
|
| 380 | 380 | $this->processed_authors[$old_id] = $user_id; |
| 381 | 381 | } |
| 382 | 382 | $this->author_mapping[$santized_old_login] = $user_id; |
| 383 | 383 | } else { |
| 384 | - printf( __( 'Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html($this->authors[$old_login]['author_display_name']) ); |
|
| 385 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 384 | + printf(__('Failed to create new user for %s. Their posts will be attributed to the current user.', 'wordpress-importer'), esc_html($this->authors[$old_login]['author_display_name'])); |
|
| 385 | + if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { |
|
| 386 | 386 | echo ' ' . $user_id->get_error_message(); |
| 387 | 387 | } |
| 388 | 388 | echo '<br />'; |
@@ -390,8 +390,8 @@ discard block |
||
| 390 | 390 | } |
| 391 | 391 | |
| 392 | 392 | // failsafe: if the user_id was invalid, default to the current user |
| 393 | - if ( ! isset( $this->author_mapping[$santized_old_login] ) ) { |
|
| 394 | - if ( $old_id ) { |
|
| 393 | + if ( ! isset($this->author_mapping[$santized_old_login])) { |
|
| 394 | + if ($old_id) { |
|
| 395 | 395 | $this->processed_authors[$old_id] = (int) get_current_user_id(); |
| 396 | 396 | } |
| 397 | 397 | $this->author_mapping[$santized_old_login] = (int) get_current_user_id(); |
@@ -405,53 +405,53 @@ discard block |
||
| 405 | 405 | * Doesn't create a new category if its slug already exists |
| 406 | 406 | */ |
| 407 | 407 | function process_categories() { |
| 408 | - $this->categories = apply_filters( 'wp_import_categories', $this->categories ); |
|
| 408 | + $this->categories = apply_filters('wp_import_categories', $this->categories); |
|
| 409 | 409 | |
| 410 | - if ( empty( $this->categories ) ) { |
|
| 410 | + if (empty($this->categories)) { |
|
| 411 | 411 | return; |
| 412 | 412 | } |
| 413 | 413 | |
| 414 | - foreach ( $this->categories as $cat ) { |
|
| 414 | + foreach ($this->categories as $cat) { |
|
| 415 | 415 | // if the category already exists leave it alone |
| 416 | - $term_id = term_exists( $cat['category_nicename'], 'category' ); |
|
| 417 | - if ( $term_id ) { |
|
| 418 | - if ( is_array($term_id) ) { |
|
| 416 | + $term_id = term_exists($cat['category_nicename'], 'category'); |
|
| 417 | + if ($term_id) { |
|
| 418 | + if (is_array($term_id)) { |
|
| 419 | 419 | $term_id = $term_id['term_id']; |
| 420 | 420 | } |
| 421 | - if ( isset($cat['term_id']) ) { |
|
| 421 | + if (isset($cat['term_id'])) { |
|
| 422 | 422 | $this->processed_terms[intval($cat['term_id'])] = (int) $term_id; |
| 423 | 423 | } |
| 424 | 424 | continue; |
| 425 | 425 | } |
| 426 | 426 | |
| 427 | - $category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] ); |
|
| 428 | - $category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : ''; |
|
| 427 | + $category_parent = empty($cat['category_parent']) ? 0 : category_exists($cat['category_parent']); |
|
| 428 | + $category_description = isset($cat['category_description']) ? $cat['category_description'] : ''; |
|
| 429 | 429 | $catarr = array( |
| 430 | 430 | 'category_nicename' => $cat['category_nicename'], |
| 431 | 431 | 'category_parent' => $category_parent, |
| 432 | 432 | 'cat_name' => $cat['cat_name'], |
| 433 | 433 | 'category_description' => $category_description |
| 434 | 434 | ); |
| 435 | - $catarr = wp_slash( $catarr ); |
|
| 435 | + $catarr = wp_slash($catarr); |
|
| 436 | 436 | |
| 437 | - $id = wp_insert_category( $catarr ); |
|
| 438 | - if ( ! is_wp_error( $id ) ) { |
|
| 439 | - if ( isset($cat['term_id']) ) { |
|
| 437 | + $id = wp_insert_category($catarr); |
|
| 438 | + if ( ! is_wp_error($id)) { |
|
| 439 | + if (isset($cat['term_id'])) { |
|
| 440 | 440 | $this->processed_terms[intval($cat['term_id'])] = $id; |
| 441 | 441 | } |
| 442 | 442 | } else { |
| 443 | - printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) ); |
|
| 444 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 443 | + printf(__('Failed to import category %s', 'wordpress-importer'), esc_html($cat['category_nicename'])); |
|
| 444 | + if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { |
|
| 445 | 445 | echo ': ' . $id->get_error_message(); |
| 446 | 446 | } |
| 447 | 447 | echo '<br />'; |
| 448 | 448 | continue; |
| 449 | 449 | } |
| 450 | 450 | |
| 451 | - $this->process_termmeta( $cat, $id['term_id'] ); |
|
| 451 | + $this->process_termmeta($cat, $id['term_id']); |
|
| 452 | 452 | } |
| 453 | 453 | |
| 454 | - unset( $this->categories ); |
|
| 454 | + unset($this->categories); |
|
| 455 | 455 | } |
| 456 | 456 | |
| 457 | 457 | /** |
@@ -460,47 +460,47 @@ discard block |
||
| 460 | 460 | * Doesn't create a tag if its slug already exists |
| 461 | 461 | */ |
| 462 | 462 | function process_tags() { |
| 463 | - $this->tags = apply_filters( 'wp_import_tags', $this->tags ); |
|
| 463 | + $this->tags = apply_filters('wp_import_tags', $this->tags); |
|
| 464 | 464 | |
| 465 | - if ( empty( $this->tags ) ) { |
|
| 465 | + if (empty($this->tags)) { |
|
| 466 | 466 | return; |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | - foreach ( $this->tags as $tag ) { |
|
| 469 | + foreach ($this->tags as $tag) { |
|
| 470 | 470 | // if the tag already exists leave it alone |
| 471 | - $term_id = term_exists( $tag['tag_slug'], 'post_tag' ); |
|
| 472 | - if ( $term_id ) { |
|
| 473 | - if ( is_array($term_id) ) { |
|
| 471 | + $term_id = term_exists($tag['tag_slug'], 'post_tag'); |
|
| 472 | + if ($term_id) { |
|
| 473 | + if (is_array($term_id)) { |
|
| 474 | 474 | $term_id = $term_id['term_id']; |
| 475 | 475 | } |
| 476 | - if ( isset($tag['term_id']) ) { |
|
| 476 | + if (isset($tag['term_id'])) { |
|
| 477 | 477 | $this->processed_terms[intval($tag['term_id'])] = (int) $term_id; |
| 478 | 478 | } |
| 479 | 479 | continue; |
| 480 | 480 | } |
| 481 | 481 | |
| 482 | - $tag = wp_slash( $tag ); |
|
| 483 | - $tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : ''; |
|
| 484 | - $tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc ); |
|
| 482 | + $tag = wp_slash($tag); |
|
| 483 | + $tag_desc = isset($tag['tag_description']) ? $tag['tag_description'] : ''; |
|
| 484 | + $tagarr = array('slug' => $tag['tag_slug'], 'description' => $tag_desc); |
|
| 485 | 485 | |
| 486 | - $id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr ); |
|
| 487 | - if ( ! is_wp_error( $id ) ) { |
|
| 488 | - if ( isset($tag['term_id']) ) { |
|
| 486 | + $id = wp_insert_term($tag['tag_name'], 'post_tag', $tagarr); |
|
| 487 | + if ( ! is_wp_error($id)) { |
|
| 488 | + if (isset($tag['term_id'])) { |
|
| 489 | 489 | $this->processed_terms[intval($tag['term_id'])] = $id['term_id']; |
| 490 | 490 | } |
| 491 | 491 | } else { |
| 492 | - printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) ); |
|
| 493 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 492 | + printf(__('Failed to import post tag %s', 'wordpress-importer'), esc_html($tag['tag_name'])); |
|
| 493 | + if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { |
|
| 494 | 494 | echo ': ' . $id->get_error_message(); |
| 495 | 495 | } |
| 496 | 496 | echo '<br />'; |
| 497 | 497 | continue; |
| 498 | 498 | } |
| 499 | 499 | |
| 500 | - $this->process_termmeta( $tag, $id['term_id'] ); |
|
| 500 | + $this->process_termmeta($tag, $id['term_id']); |
|
| 501 | 501 | } |
| 502 | 502 | |
| 503 | - unset( $this->tags ); |
|
| 503 | + unset($this->tags); |
|
| 504 | 504 | } |
| 505 | 505 | |
| 506 | 506 | /** |
@@ -509,55 +509,55 @@ discard block |
||
| 509 | 509 | * Doesn't create a term its slug already exists |
| 510 | 510 | */ |
| 511 | 511 | function process_terms() { |
| 512 | - $this->terms = apply_filters( 'wp_import_terms', $this->terms ); |
|
| 512 | + $this->terms = apply_filters('wp_import_terms', $this->terms); |
|
| 513 | 513 | |
| 514 | - if ( empty( $this->terms ) ) { |
|
| 514 | + if (empty($this->terms)) { |
|
| 515 | 515 | return; |
| 516 | 516 | } |
| 517 | 517 | |
| 518 | - foreach ( $this->terms as $term ) { |
|
| 518 | + foreach ($this->terms as $term) { |
|
| 519 | 519 | // if the term already exists in the correct taxonomy leave it alone |
| 520 | - $term_id = term_exists( $term['slug'], $term['term_taxonomy'] ); |
|
| 521 | - if ( $term_id ) { |
|
| 522 | - if ( is_array($term_id) ) { |
|
| 520 | + $term_id = term_exists($term['slug'], $term['term_taxonomy']); |
|
| 521 | + if ($term_id) { |
|
| 522 | + if (is_array($term_id)) { |
|
| 523 | 523 | $term_id = $term_id['term_id']; |
| 524 | 524 | } |
| 525 | - if ( isset($term['term_id']) ) { |
|
| 525 | + if (isset($term['term_id'])) { |
|
| 526 | 526 | $this->processed_terms[intval($term['term_id'])] = (int) $term_id; |
| 527 | 527 | } |
| 528 | 528 | continue; |
| 529 | 529 | } |
| 530 | 530 | |
| 531 | - if ( empty( $term['term_parent'] ) ) { |
|
| 531 | + if (empty($term['term_parent'])) { |
|
| 532 | 532 | $parent = 0; |
| 533 | 533 | } else { |
| 534 | - $parent = term_exists( $term['term_parent'], $term['term_taxonomy'] ); |
|
| 535 | - if ( is_array( $parent ) ) { |
|
| 534 | + $parent = term_exists($term['term_parent'], $term['term_taxonomy']); |
|
| 535 | + if (is_array($parent)) { |
|
| 536 | 536 | $parent = $parent['term_id']; |
| 537 | 537 | } |
| 538 | 538 | } |
| 539 | - $term = wp_slash( $term ); |
|
| 540 | - $description = isset( $term['term_description'] ) ? $term['term_description'] : ''; |
|
| 541 | - $termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) ); |
|
| 539 | + $term = wp_slash($term); |
|
| 540 | + $description = isset($term['term_description']) ? $term['term_description'] : ''; |
|
| 541 | + $termarr = array('slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent)); |
|
| 542 | 542 | |
| 543 | - $id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr ); |
|
| 544 | - if ( ! is_wp_error( $id ) ) { |
|
| 545 | - if ( isset($term['term_id']) ) { |
|
| 543 | + $id = wp_insert_term($term['term_name'], $term['term_taxonomy'], $termarr); |
|
| 544 | + if ( ! is_wp_error($id)) { |
|
| 545 | + if (isset($term['term_id'])) { |
|
| 546 | 546 | $this->processed_terms[intval($term['term_id'])] = $id['term_id']; |
| 547 | 547 | } |
| 548 | 548 | } else { |
| 549 | - printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) ); |
|
| 550 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 549 | + printf(__('Failed to import %s %s', 'wordpress-importer'), esc_html($term['term_taxonomy']), esc_html($term['term_name'])); |
|
| 550 | + if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { |
|
| 551 | 551 | echo ': ' . $id->get_error_message(); |
| 552 | 552 | } |
| 553 | 553 | echo '<br />'; |
| 554 | 554 | continue; |
| 555 | 555 | } |
| 556 | 556 | |
| 557 | - $this->process_termmeta( $term, $id['term_id'] ); |
|
| 557 | + $this->process_termmeta($term, $id['term_id']); |
|
| 558 | 558 | } |
| 559 | 559 | |
| 560 | - unset( $this->terms ); |
|
| 560 | + unset($this->terms); |
|
| 561 | 561 | } |
| 562 | 562 | |
| 563 | 563 | /** |
@@ -568,8 +568,8 @@ discard block |
||
| 568 | 568 | * @param array $term Term data from WXR import. |
| 569 | 569 | * @param int $term_id ID of the newly created term. |
| 570 | 570 | */ |
| 571 | - protected function process_termmeta( $term, $term_id ) { |
|
| 572 | - if ( ! isset( $term['termmeta'] ) ) { |
|
| 571 | + protected function process_termmeta($term, $term_id) { |
|
| 572 | + if ( ! isset($term['termmeta'])) { |
|
| 573 | 573 | $term['termmeta'] = array(); |
| 574 | 574 | } |
| 575 | 575 | |
@@ -582,13 +582,13 @@ discard block |
||
| 582 | 582 | * @param int $term_id ID of the newly created term. |
| 583 | 583 | * @param array $term Term data from the WXR import. |
| 584 | 584 | */ |
| 585 | - $term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term ); |
|
| 585 | + $term['termmeta'] = apply_filters('wp_import_term_meta', $term['termmeta'], $term_id, $term); |
|
| 586 | 586 | |
| 587 | - if ( empty( $term['termmeta'] ) ) { |
|
| 587 | + if (empty($term['termmeta'])) { |
|
| 588 | 588 | return; |
| 589 | 589 | } |
| 590 | 590 | |
| 591 | - foreach ( $term['termmeta'] as $meta ) { |
|
| 591 | + foreach ($term['termmeta'] as $meta) { |
|
| 592 | 592 | /** |
| 593 | 593 | * Filters the meta key for an imported piece of term meta. |
| 594 | 594 | * |
@@ -598,15 +598,15 @@ discard block |
||
| 598 | 598 | * @param int $term_id ID of the newly created term. |
| 599 | 599 | * @param array $term Term data from the WXR import. |
| 600 | 600 | */ |
| 601 | - $key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term ); |
|
| 602 | - if ( ! $key ) { |
|
| 601 | + $key = apply_filters('import_term_meta_key', $meta['key'], $term_id, $term); |
|
| 602 | + if ( ! $key) { |
|
| 603 | 603 | continue; |
| 604 | 604 | } |
| 605 | 605 | |
| 606 | 606 | // Export gets meta straight from the DB so could have a serialized string |
| 607 | - $value = maybe_unserialize( $meta['value'] ); |
|
| 607 | + $value = maybe_unserialize($meta['value']); |
|
| 608 | 608 | |
| 609 | - add_term_meta( $term_id, $key, $value ); |
|
| 609 | + add_term_meta($term_id, $key, $value); |
|
| 610 | 610 | |
| 611 | 611 | /** |
| 612 | 612 | * Fires after term meta is imported. |
@@ -617,7 +617,7 @@ discard block |
||
| 617 | 617 | * @param string $key Meta key. |
| 618 | 618 | * @param mixed $value Meta value. |
| 619 | 619 | */ |
| 620 | - do_action( 'import_term_meta', $term_id, $key, $value ); |
|
| 620 | + do_action('import_term_meta', $term_id, $key, $value); |
|
| 621 | 621 | } |
| 622 | 622 | } |
| 623 | 623 | |
@@ -630,35 +630,35 @@ discard block |
||
| 630 | 630 | * Note that new/updated terms, comments and meta are imported for the last of the above. |
| 631 | 631 | */ |
| 632 | 632 | function process_posts() { |
| 633 | - $this->posts = apply_filters( 'wp_import_posts', $this->posts ); |
|
| 633 | + $this->posts = apply_filters('wp_import_posts', $this->posts); |
|
| 634 | 634 | |
| 635 | - foreach ( $this->posts as $post ) { |
|
| 636 | - $post = apply_filters( 'wp_import_post_data_raw', $post ); |
|
| 635 | + foreach ($this->posts as $post) { |
|
| 636 | + $post = apply_filters('wp_import_post_data_raw', $post); |
|
| 637 | 637 | |
| 638 | - if ( ! post_type_exists( $post['post_type'] ) ) { |
|
| 639 | - printf( __( 'Failed to import “%s”: Invalid post type %s', 'wordpress-importer' ), |
|
| 640 | - esc_html($post['post_title']), esc_html($post['post_type']) ); |
|
| 638 | + if ( ! post_type_exists($post['post_type'])) { |
|
| 639 | + printf(__('Failed to import “%s”: Invalid post type %s', 'wordpress-importer'), |
|
| 640 | + esc_html($post['post_title']), esc_html($post['post_type'])); |
|
| 641 | 641 | echo '<br />'; |
| 642 | - do_action( 'wp_import_post_exists', $post ); |
|
| 642 | + do_action('wp_import_post_exists', $post); |
|
| 643 | 643 | continue; |
| 644 | 644 | } |
| 645 | 645 | |
| 646 | - if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) ) { |
|
| 646 | + if (isset($this->processed_posts[$post['post_id']]) && ! empty($post['post_id'])) { |
|
| 647 | 647 | continue; |
| 648 | 648 | } |
| 649 | 649 | |
| 650 | - if ( $post['status'] == 'auto-draft' ) { |
|
| 650 | + if ($post['status'] == 'auto-draft') { |
|
| 651 | 651 | continue; |
| 652 | 652 | } |
| 653 | 653 | |
| 654 | - if ( 'nav_menu_item' == $post['post_type'] ) { |
|
| 655 | - $this->process_menu_item( $post ); |
|
| 654 | + if ('nav_menu_item' == $post['post_type']) { |
|
| 655 | + $this->process_menu_item($post); |
|
| 656 | 656 | continue; |
| 657 | 657 | } |
| 658 | 658 | |
| 659 | - $post_type_object = get_post_type_object( $post['post_type'] ); |
|
| 659 | + $post_type_object = get_post_type_object($post['post_type']); |
|
| 660 | 660 | |
| 661 | - $post_exists = post_exists( $post['post_title'], '', $post['post_date'] ); |
|
| 661 | + $post_exists = post_exists($post['post_title'], '', $post['post_date']); |
|
| 662 | 662 | |
| 663 | 663 | /** |
| 664 | 664 | * Filter ID of the existing post corresponding to post currently importing. |
@@ -672,18 +672,18 @@ discard block |
||
| 672 | 672 | * @param int $post_exists Post ID, or 0 if post did not exist. |
| 673 | 673 | * @param array $post The post array to be inserted. |
| 674 | 674 | */ |
| 675 | - $post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post ); |
|
| 675 | + $post_exists = apply_filters('wp_import_existing_post', $post_exists, $post); |
|
| 676 | 676 | |
| 677 | - if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) { |
|
| 678 | - printf( __('%s “%s” already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) ); |
|
| 677 | + if ($post_exists && get_post_type($post_exists) == $post['post_type']) { |
|
| 678 | + printf(__('%s “%s” already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title'])); |
|
| 679 | 679 | echo '<br />'; |
| 680 | 680 | $comment_post_ID = $post_id = $post_exists; |
| 681 | - $this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists ); |
|
| 681 | + $this->processed_posts[intval($post['post_id'])] = intval($post_exists); |
|
| 682 | 682 | } else { |
| 683 | 683 | $post_parent = (int) $post['post_parent']; |
| 684 | - if ( $post_parent ) { |
|
| 684 | + if ($post_parent) { |
|
| 685 | 685 | // if we already know the parent, map it to the new local ID |
| 686 | - if ( isset( $this->processed_posts[$post_parent] ) ) { |
|
| 686 | + if (isset($this->processed_posts[$post_parent])) { |
|
| 687 | 687 | $post_parent = $this->processed_posts[$post_parent]; |
| 688 | 688 | // otherwise record the parent for later |
| 689 | 689 | } else { |
@@ -693,8 +693,8 @@ discard block |
||
| 693 | 693 | } |
| 694 | 694 | |
| 695 | 695 | // map the post author |
| 696 | - $author = sanitize_user( $post['post_author'], true ); |
|
| 697 | - if ( isset( $this->author_mapping[$author] ) ) { |
|
| 696 | + $author = sanitize_user($post['post_author'], true); |
|
| 697 | + if (isset($this->author_mapping[$author])) { |
|
| 698 | 698 | $author = $this->author_mapping[$author]; |
| 699 | 699 | } else { |
| 700 | 700 | $author = (int) get_current_user_id(); |
@@ -711,20 +711,20 @@ discard block |
||
| 711 | 711 | ); |
| 712 | 712 | |
| 713 | 713 | $original_post_ID = $post['post_id']; |
| 714 | - $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post ); |
|
| 714 | + $postdata = apply_filters('wp_import_post_data_processed', $postdata, $post); |
|
| 715 | 715 | |
| 716 | - $postdata = wp_slash( $postdata ); |
|
| 716 | + $postdata = wp_slash($postdata); |
|
| 717 | 717 | |
| 718 | - if ( 'attachment' == $postdata['post_type'] ) { |
|
| 718 | + if ('attachment' == $postdata['post_type']) { |
|
| 719 | 719 | $remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid']; |
| 720 | 720 | |
| 721 | 721 | // try to use _wp_attached file for upload folder placement to ensure the same location as the export site |
| 722 | 722 | // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload() |
| 723 | 723 | $postdata['upload_date'] = $post['post_date']; |
| 724 | - if ( isset( $post['postmeta'] ) ) { |
|
| 725 | - foreach( $post['postmeta'] as $meta ) { |
|
| 726 | - if ( $meta['key'] == '_wp_attached_file' ) { |
|
| 727 | - if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) ) { |
|
| 724 | + if (isset($post['postmeta'])) { |
|
| 725 | + foreach ($post['postmeta'] as $meta) { |
|
| 726 | + if ($meta['key'] == '_wp_attached_file') { |
|
| 727 | + if (preg_match('%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches)) { |
|
| 728 | 728 | $postdata['upload_date'] = $matches[0]; |
| 729 | 729 | } |
| 730 | 730 | break; |
@@ -732,81 +732,81 @@ discard block |
||
| 732 | 732 | } |
| 733 | 733 | } |
| 734 | 734 | |
| 735 | - $comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url ); |
|
| 735 | + $comment_post_ID = $post_id = $this->process_attachment($postdata, $remote_url); |
|
| 736 | 736 | } else { |
| 737 | - $comment_post_ID = $post_id = wp_insert_post( $postdata, true ); |
|
| 738 | - do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post ); |
|
| 737 | + $comment_post_ID = $post_id = wp_insert_post($postdata, true); |
|
| 738 | + do_action('wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post); |
|
| 739 | 739 | } |
| 740 | 740 | |
| 741 | - if ( is_wp_error( $post_id ) ) { |
|
| 742 | - printf( __( 'Failed to import %s “%s”', 'wordpress-importer' ), |
|
| 743 | - $post_type_object->labels->singular_name, esc_html($post['post_title']) ); |
|
| 744 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 741 | + if (is_wp_error($post_id)) { |
|
| 742 | + printf(__('Failed to import %s “%s”', 'wordpress-importer'), |
|
| 743 | + $post_type_object->labels->singular_name, esc_html($post['post_title'])); |
|
| 744 | + if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { |
|
| 745 | 745 | echo ': ' . $post_id->get_error_message(); |
| 746 | 746 | } |
| 747 | 747 | echo '<br />'; |
| 748 | 748 | continue; |
| 749 | 749 | } |
| 750 | 750 | |
| 751 | - if ( $post['is_sticky'] == 1 ) { |
|
| 752 | - stick_post( $post_id ); |
|
| 751 | + if ($post['is_sticky'] == 1) { |
|
| 752 | + stick_post($post_id); |
|
| 753 | 753 | } |
| 754 | 754 | } |
| 755 | 755 | |
| 756 | 756 | // map pre-import ID to local ID |
| 757 | 757 | $this->processed_posts[intval($post['post_id'])] = (int) $post_id; |
| 758 | 758 | |
| 759 | - if ( ! isset( $post['terms'] ) ) { |
|
| 759 | + if ( ! isset($post['terms'])) { |
|
| 760 | 760 | $post['terms'] = array(); |
| 761 | 761 | } |
| 762 | 762 | |
| 763 | - $post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post ); |
|
| 763 | + $post['terms'] = apply_filters('wp_import_post_terms', $post['terms'], $post_id, $post); |
|
| 764 | 764 | |
| 765 | 765 | // add categories, tags and other terms |
| 766 | - if ( ! empty( $post['terms'] ) ) { |
|
| 766 | + if ( ! empty($post['terms'])) { |
|
| 767 | 767 | $terms_to_set = array(); |
| 768 | - foreach ( $post['terms'] as $term ) { |
|
| 768 | + foreach ($post['terms'] as $term) { |
|
| 769 | 769 | // back compat with WXR 1.0 map 'tag' to 'post_tag' |
| 770 | - $taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain']; |
|
| 771 | - $term_exists = term_exists( $term['slug'], $taxonomy ); |
|
| 772 | - $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists; |
|
| 773 | - if ( ! $term_id ) { |
|
| 774 | - $t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) ); |
|
| 775 | - if ( ! is_wp_error( $t ) ) { |
|
| 770 | + $taxonomy = ('tag' == $term['domain']) ? 'post_tag' : $term['domain']; |
|
| 771 | + $term_exists = term_exists($term['slug'], $taxonomy); |
|
| 772 | + $term_id = is_array($term_exists) ? $term_exists['term_id'] : $term_exists; |
|
| 773 | + if ( ! $term_id) { |
|
| 774 | + $t = wp_insert_term($term['name'], $taxonomy, array('slug' => $term['slug'])); |
|
| 775 | + if ( ! is_wp_error($t)) { |
|
| 776 | 776 | $term_id = $t['term_id']; |
| 777 | - do_action( 'wp_import_insert_term', $t, $term, $post_id, $post ); |
|
| 777 | + do_action('wp_import_insert_term', $t, $term, $post_id, $post); |
|
| 778 | 778 | } else { |
| 779 | - printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) ); |
|
| 780 | - if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG ) { |
|
| 779 | + printf(__('Failed to import %s %s', 'wordpress-importer'), esc_html($taxonomy), esc_html($term['name'])); |
|
| 780 | + if (defined('IMPORT_DEBUG') && IMPORT_DEBUG) { |
|
| 781 | 781 | echo ': ' . $t->get_error_message(); |
| 782 | 782 | } |
| 783 | 783 | echo '<br />'; |
| 784 | - do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post ); |
|
| 784 | + do_action('wp_import_insert_term_failed', $t, $term, $post_id, $post); |
|
| 785 | 785 | continue; |
| 786 | 786 | } |
| 787 | 787 | } |
| 788 | - $terms_to_set[$taxonomy][] = intval( $term_id ); |
|
| 788 | + $terms_to_set[$taxonomy][] = intval($term_id); |
|
| 789 | 789 | } |
| 790 | 790 | |
| 791 | - foreach ( $terms_to_set as $tax => $ids ) { |
|
| 792 | - $tt_ids = wp_set_post_terms( $post_id, $ids, $tax ); |
|
| 793 | - do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post ); |
|
| 791 | + foreach ($terms_to_set as $tax => $ids) { |
|
| 792 | + $tt_ids = wp_set_post_terms($post_id, $ids, $tax); |
|
| 793 | + do_action('wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post); |
|
| 794 | 794 | } |
| 795 | - unset( $post['terms'], $terms_to_set ); |
|
| 795 | + unset($post['terms'], $terms_to_set); |
|
| 796 | 796 | } |
| 797 | 797 | |
| 798 | - if ( ! isset( $post['comments'] ) ) { |
|
| 798 | + if ( ! isset($post['comments'])) { |
|
| 799 | 799 | $post['comments'] = array(); |
| 800 | 800 | } |
| 801 | 801 | |
| 802 | - $post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post ); |
|
| 802 | + $post['comments'] = apply_filters('wp_import_post_comments', $post['comments'], $post_id, $post); |
|
| 803 | 803 | |
| 804 | 804 | // add/update comments |
| 805 | - if ( ! empty( $post['comments'] ) ) { |
|
| 805 | + if ( ! empty($post['comments'])) { |
|
| 806 | 806 | $num_comments = 0; |
| 807 | 807 | $inserted_comments = array(); |
| 808 | - foreach ( $post['comments'] as $comment ) { |
|
| 809 | - $comment_id = $comment['comment_id']; |
|
| 808 | + foreach ($post['comments'] as $comment) { |
|
| 809 | + $comment_id = $comment['comment_id']; |
|
| 810 | 810 | $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID; |
| 811 | 811 | $newcomments[$comment_id]['comment_author'] = $comment['comment_author']; |
| 812 | 812 | $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email']; |
@@ -817,66 +817,66 @@ discard block |
||
| 817 | 817 | $newcomments[$comment_id]['comment_content'] = $comment['comment_content']; |
| 818 | 818 | $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved']; |
| 819 | 819 | $newcomments[$comment_id]['comment_type'] = $comment['comment_type']; |
| 820 | - $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; |
|
| 821 | - $newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array(); |
|
| 822 | - if ( isset( $this->processed_authors[$comment['comment_user_id']] ) ) { |
|
| 820 | + $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent']; |
|
| 821 | + $newcomments[$comment_id]['commentmeta'] = isset($comment['commentmeta']) ? $comment['commentmeta'] : array(); |
|
| 822 | + if (isset($this->processed_authors[$comment['comment_user_id']])) { |
|
| 823 | 823 | $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']]; |
| 824 | 824 | } |
| 825 | 825 | } |
| 826 | - ksort( $newcomments ); |
|
| 826 | + ksort($newcomments); |
|
| 827 | 827 | |
| 828 | - foreach ( $newcomments as $key => $comment ) { |
|
| 828 | + foreach ($newcomments as $key => $comment) { |
|
| 829 | 829 | // if this is a new post we can skip the comment_exists() check |
| 830 | - if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) { |
|
| 831 | - if ( isset( $inserted_comments[$comment['comment_parent']] ) ) { |
|
| 830 | + if ( ! $post_exists || ! comment_exists($comment['comment_author'], $comment['comment_date'])) { |
|
| 831 | + if (isset($inserted_comments[$comment['comment_parent']])) { |
|
| 832 | 832 | $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']]; |
| 833 | 833 | } |
| 834 | - $comment = wp_filter_comment( $comment ); |
|
| 835 | - $inserted_comments[$key] = wp_insert_comment( $comment ); |
|
| 836 | - do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post ); |
|
| 834 | + $comment = wp_filter_comment($comment); |
|
| 835 | + $inserted_comments[$key] = wp_insert_comment($comment); |
|
| 836 | + do_action('wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post); |
|
| 837 | 837 | |
| 838 | - foreach( $comment['commentmeta'] as $meta ) { |
|
| 839 | - $value = maybe_unserialize( $meta['value'] ); |
|
| 840 | - add_comment_meta( $inserted_comments[$key], $meta['key'], $value ); |
|
| 838 | + foreach ($comment['commentmeta'] as $meta) { |
|
| 839 | + $value = maybe_unserialize($meta['value']); |
|
| 840 | + add_comment_meta($inserted_comments[$key], $meta['key'], $value); |
|
| 841 | 841 | } |
| 842 | 842 | |
| 843 | 843 | $num_comments++; |
| 844 | 844 | } |
| 845 | 845 | } |
| 846 | - unset( $newcomments, $inserted_comments, $post['comments'] ); |
|
| 846 | + unset($newcomments, $inserted_comments, $post['comments']); |
|
| 847 | 847 | } |
| 848 | 848 | |
| 849 | - if ( ! isset( $post['postmeta'] ) ) { |
|
| 849 | + if ( ! isset($post['postmeta'])) { |
|
| 850 | 850 | $post['postmeta'] = array(); |
| 851 | 851 | } |
| 852 | 852 | |
| 853 | - $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post ); |
|
| 853 | + $post['postmeta'] = apply_filters('wp_import_post_meta', $post['postmeta'], $post_id, $post); |
|
| 854 | 854 | |
| 855 | 855 | // add/update post meta |
| 856 | - if ( ! empty( $post['postmeta'] ) ) { |
|
| 857 | - foreach ( $post['postmeta'] as $meta ) { |
|
| 858 | - $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post ); |
|
| 856 | + if ( ! empty($post['postmeta'])) { |
|
| 857 | + foreach ($post['postmeta'] as $meta) { |
|
| 858 | + $key = apply_filters('import_post_meta_key', $meta['key'], $post_id, $post); |
|
| 859 | 859 | $value = false; |
| 860 | 860 | |
| 861 | - if ( '_edit_last' == $key ) { |
|
| 862 | - if ( isset( $this->processed_authors[intval($meta['value'])] ) ) { |
|
| 861 | + if ('_edit_last' == $key) { |
|
| 862 | + if (isset($this->processed_authors[intval($meta['value'])])) { |
|
| 863 | 863 | $value = $this->processed_authors[intval($meta['value'])]; |
| 864 | 864 | } else { |
| 865 | 865 | $key = false; |
| 866 | 866 | } |
| 867 | 867 | } |
| 868 | 868 | |
| 869 | - if ( $key ) { |
|
| 869 | + if ($key) { |
|
| 870 | 870 | // export gets meta straight from the DB so could have a serialized string |
| 871 | - if ( ! $value ) { |
|
| 872 | - $value = maybe_unserialize( $meta['value'] ); |
|
| 871 | + if ( ! $value) { |
|
| 872 | + $value = maybe_unserialize($meta['value']); |
|
| 873 | 873 | } |
| 874 | 874 | |
| 875 | - add_post_meta( $post_id, $key, $value ); |
|
| 876 | - do_action( 'import_post_meta', $post_id, $key, $value ); |
|
| 875 | + add_post_meta($post_id, $key, $value); |
|
| 876 | + do_action('import_post_meta', $post_id, $key, $value); |
|
| 877 | 877 | |
| 878 | 878 | // if the post has a featured image, take note of this in case of remap |
| 879 | - if ( '_thumbnail_id' == $key ) { |
|
| 879 | + if ('_thumbnail_id' == $key) { |
|
| 880 | 880 | $this->featured_images[$post_id] = (int) $value; |
| 881 | 881 | } |
| 882 | 882 | } |
@@ -884,7 +884,7 @@ discard block |
||
| 884 | 884 | } |
| 885 | 885 | } |
| 886 | 886 | |
| 887 | - unset( $this->posts ); |
|
| 887 | + unset($this->posts); |
|
| 888 | 888 | } |
| 889 | 889 | |
| 890 | 890 | /** |
@@ -897,17 +897,17 @@ discard block |
||
| 897 | 897 | * |
| 898 | 898 | * @param array $item Menu item details from WXR file |
| 899 | 899 | */ |
| 900 | - function process_menu_item( $item ) { |
|
| 900 | + function process_menu_item($item) { |
|
| 901 | 901 | // skip draft, orphaned menu items |
| 902 | - if ( 'draft' == $item['status'] ) { |
|
| 902 | + if ('draft' == $item['status']) { |
|
| 903 | 903 | return; |
| 904 | 904 | } |
| 905 | 905 | |
| 906 | 906 | $menu_slug = false; |
| 907 | - if ( isset($item['terms']) ) { |
|
| 907 | + if (isset($item['terms'])) { |
|
| 908 | 908 | // loop through terms, assume first nav_menu term is correct menu |
| 909 | - foreach ( $item['terms'] as $term ) { |
|
| 910 | - if ( 'nav_menu' == $term['domain'] ) { |
|
| 909 | + foreach ($item['terms'] as $term) { |
|
| 910 | + if ('nav_menu' == $term['domain']) { |
|
| 911 | 911 | $menu_slug = $term['slug']; |
| 912 | 912 | break; |
| 913 | 913 | } |
@@ -915,53 +915,53 @@ discard block |
||
| 915 | 915 | } |
| 916 | 916 | |
| 917 | 917 | // no nav_menu term associated with this menu item |
| 918 | - if ( ! $menu_slug ) { |
|
| 919 | - _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' ); |
|
| 918 | + if ( ! $menu_slug) { |
|
| 919 | + _e('Menu item skipped due to missing menu slug', 'wordpress-importer'); |
|
| 920 | 920 | echo '<br />'; |
| 921 | 921 | return; |
| 922 | 922 | } |
| 923 | 923 | |
| 924 | - $menu_id = term_exists( $menu_slug, 'nav_menu' ); |
|
| 925 | - if ( ! $menu_id ) { |
|
| 926 | - printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) ); |
|
| 924 | + $menu_id = term_exists($menu_slug, 'nav_menu'); |
|
| 925 | + if ( ! $menu_id) { |
|
| 926 | + printf(__('Menu item skipped due to invalid menu slug: %s', 'wordpress-importer'), esc_html($menu_slug)); |
|
| 927 | 927 | echo '<br />'; |
| 928 | 928 | return; |
| 929 | 929 | } else { |
| 930 | - $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id; |
|
| 930 | + $menu_id = is_array($menu_id) ? $menu_id['term_id'] : $menu_id; |
|
| 931 | 931 | } |
| 932 | 932 | |
| 933 | - foreach ( $item['postmeta'] as $meta ) { |
|
| 933 | + foreach ($item['postmeta'] as $meta) { |
|
| 934 | 934 | $$meta['key'] = $meta['value']; |
| 935 | 935 | } |
| 936 | 936 | |
| 937 | - if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) { |
|
| 937 | + if ('taxonomy' == $_menu_item_type && isset($this->processed_terms[intval($_menu_item_object_id)])) { |
|
| 938 | 938 | $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)]; |
| 939 | - } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) { |
|
| 939 | + } else if ('post_type' == $_menu_item_type && isset($this->processed_posts[intval($_menu_item_object_id)])) { |
|
| 940 | 940 | $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)]; |
| 941 | - } else if ( 'custom' != $_menu_item_type ) { |
|
| 941 | + } else if ('custom' != $_menu_item_type) { |
|
| 942 | 942 | // associated object is missing or not imported yet, we'll retry later |
| 943 | 943 | $this->missing_menu_items[] = $item; |
| 944 | 944 | return; |
| 945 | 945 | } |
| 946 | 946 | |
| 947 | - if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) { |
|
| 947 | + if (isset($this->processed_menu_items[intval($_menu_item_menu_item_parent)])) { |
|
| 948 | 948 | $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)]; |
| 949 | - } else if ( $_menu_item_menu_item_parent ) { |
|
| 949 | + } else if ($_menu_item_menu_item_parent) { |
|
| 950 | 950 | $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent; |
| 951 | 951 | $_menu_item_menu_item_parent = 0; |
| 952 | 952 | } |
| 953 | 953 | |
| 954 | 954 | // wp_update_nav_menu_item expects CSS classes as a space separated string |
| 955 | - $_menu_item_classes = maybe_unserialize( $_menu_item_classes ); |
|
| 956 | - if ( is_array( $_menu_item_classes ) ) { |
|
| 957 | - $_menu_item_classes = implode( ' ', $_menu_item_classes ); |
|
| 955 | + $_menu_item_classes = maybe_unserialize($_menu_item_classes); |
|
| 956 | + if (is_array($_menu_item_classes)) { |
|
| 957 | + $_menu_item_classes = implode(' ', $_menu_item_classes); |
|
| 958 | 958 | } |
| 959 | 959 | |
| 960 | 960 | $args = array( |
| 961 | 961 | 'menu-item-object-id' => $_menu_item_object_id, |
| 962 | 962 | 'menu-item-object' => $_menu_item_object, |
| 963 | 963 | 'menu-item-parent-id' => $_menu_item_menu_item_parent, |
| 964 | - 'menu-item-position' => intval( $item['menu_order'] ), |
|
| 964 | + 'menu-item-position' => intval($item['menu_order']), |
|
| 965 | 965 | 'menu-item-type' => $_menu_item_type, |
| 966 | 966 | 'menu-item-title' => $item['post_title'], |
| 967 | 967 | 'menu-item-url' => $_menu_item_url, |
@@ -973,8 +973,8 @@ discard block |
||
| 973 | 973 | 'menu-item-status' => $item['status'] |
| 974 | 974 | ); |
| 975 | 975 | |
| 976 | - $id = wp_update_nav_menu_item( $menu_id, 0, $args ); |
|
| 977 | - if ( $id && ! is_wp_error( $id ) ) { |
|
| 976 | + $id = wp_update_nav_menu_item($menu_id, 0, $args); |
|
| 977 | + if ($id && ! is_wp_error($id)) { |
|
| 978 | 978 | $this->processed_menu_items[intval($item['post_id'])] = (int) $id; |
| 979 | 979 | } |
| 980 | 980 | } |
@@ -986,41 +986,41 @@ discard block |
||
| 986 | 986 | * @param string $url URL to fetch attachment from |
| 987 | 987 | * @return int|WP_Error Post ID on success, WP_Error otherwise |
| 988 | 988 | */ |
| 989 | - function process_attachment( $post, $url ) { |
|
| 990 | - if ( ! $this->fetch_attachments ) { |
|
| 991 | - return new WP_Error( 'attachment_processing_error', |
|
| 992 | - __( 'Fetching attachments is not enabled', 'wordpress-importer' ) ); |
|
| 989 | + function process_attachment($post, $url) { |
|
| 990 | + if ( ! $this->fetch_attachments) { |
|
| 991 | + return new WP_Error('attachment_processing_error', |
|
| 992 | + __('Fetching attachments is not enabled', 'wordpress-importer')); |
|
| 993 | 993 | } |
| 994 | 994 | |
| 995 | 995 | // if the URL is absolute, but does not contain address, then upload it assuming base_site_url |
| 996 | - if ( preg_match( '|^/[\w\W]+$|', $url ) ) { |
|
| 997 | - $url = rtrim( $this->base_url, '/' ) . $url; |
|
| 996 | + if (preg_match('|^/[\w\W]+$|', $url)) { |
|
| 997 | + $url = rtrim($this->base_url, '/') . $url; |
|
| 998 | 998 | } |
| 999 | 999 | |
| 1000 | - $upload = $this->fetch_remote_file( $url, $post ); |
|
| 1001 | - if ( is_wp_error( $upload ) ) { |
|
| 1000 | + $upload = $this->fetch_remote_file($url, $post); |
|
| 1001 | + if (is_wp_error($upload)) { |
|
| 1002 | 1002 | return $upload; |
| 1003 | 1003 | } |
| 1004 | 1004 | |
| 1005 | - if ( $info = wp_check_filetype( $upload['file'] ) ) { |
|
| 1005 | + if ($info = wp_check_filetype($upload['file'])) { |
|
| 1006 | 1006 | $post['post_mime_type'] = $info['type']; |
| 1007 | 1007 | } else { |
| 1008 | - return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') ); |
|
| 1008 | + return new WP_Error('attachment_processing_error', __('Invalid file type', 'wordpress-importer')); |
|
| 1009 | 1009 | } |
| 1010 | 1010 | |
| 1011 | 1011 | $post['guid'] = $upload['url']; |
| 1012 | 1012 | |
| 1013 | 1013 | // as per wp-admin/includes/upload.php |
| 1014 | - $post_id = wp_insert_attachment( $post, $upload['file'] ); |
|
| 1015 | - wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) ); |
|
| 1014 | + $post_id = wp_insert_attachment($post, $upload['file']); |
|
| 1015 | + wp_update_attachment_metadata($post_id, wp_generate_attachment_metadata($post_id, $upload['file'])); |
|
| 1016 | 1016 | |
| 1017 | 1017 | // remap resized image URLs, works by stripping the extension and remapping the URL stub. |
| 1018 | - if ( preg_match( '!^image/!', $info['type'] ) ) { |
|
| 1019 | - $parts = pathinfo( $url ); |
|
| 1020 | - $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2 |
|
| 1018 | + if (preg_match('!^image/!', $info['type'])) { |
|
| 1019 | + $parts = pathinfo($url); |
|
| 1020 | + $name = basename($parts['basename'], ".{$parts['extension']}"); // PATHINFO_FILENAME in PHP 5.2 |
|
| 1021 | 1021 | |
| 1022 | - $parts_new = pathinfo( $upload['url'] ); |
|
| 1023 | - $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" ); |
|
| 1022 | + $parts_new = pathinfo($upload['url']); |
|
| 1023 | + $name_new = basename($parts_new['basename'], ".{$parts_new['extension']}"); |
|
| 1024 | 1024 | |
| 1025 | 1025 | $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new; |
| 1026 | 1026 | } |
@@ -1035,54 +1035,54 @@ discard block |
||
| 1035 | 1035 | * @param array $post Attachment details |
| 1036 | 1036 | * @return array|WP_Error Local file location details on success, WP_Error otherwise |
| 1037 | 1037 | */ |
| 1038 | - function fetch_remote_file( $url, $post ) { |
|
| 1038 | + function fetch_remote_file($url, $post) { |
|
| 1039 | 1039 | // extract the file name and extension from the url |
| 1040 | - $file_name = basename( $url ); |
|
| 1040 | + $file_name = basename($url); |
|
| 1041 | 1041 | |
| 1042 | 1042 | // get placeholder file in the upload dir with a unique, sanitized filename |
| 1043 | - $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] ); |
|
| 1044 | - if ( $upload['error'] ) { |
|
| 1045 | - return new WP_Error( 'upload_dir_error', $upload['error'] ); |
|
| 1043 | + $upload = wp_upload_bits($file_name, 0, '', $post['upload_date']); |
|
| 1044 | + if ($upload['error']) { |
|
| 1045 | + return new WP_Error('upload_dir_error', $upload['error']); |
|
| 1046 | 1046 | } |
| 1047 | 1047 | |
| 1048 | 1048 | // fetch the remote url and write it to the placeholder file |
| 1049 | - $headers = wp_get_http( $url, $upload['file'] ); |
|
| 1049 | + $headers = wp_get_http($url, $upload['file']); |
|
| 1050 | 1050 | |
| 1051 | 1051 | // request failed |
| 1052 | - if ( ! $headers ) { |
|
| 1053 | - @unlink( $upload['file'] ); |
|
| 1054 | - return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') ); |
|
| 1052 | + if ( ! $headers) { |
|
| 1053 | + @unlink($upload['file']); |
|
| 1054 | + return new WP_Error('import_file_error', __('Remote server did not respond', 'wordpress-importer')); |
|
| 1055 | 1055 | } |
| 1056 | 1056 | |
| 1057 | 1057 | // make sure the fetch was successful |
| 1058 | - if ( $headers['response'] != '200' ) { |
|
| 1059 | - @unlink( $upload['file'] ); |
|
| 1060 | - return new WP_Error( 'import_file_error', sprintf( __('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']) ) ); |
|
| 1058 | + if ($headers['response'] != '200') { |
|
| 1059 | + @unlink($upload['file']); |
|
| 1060 | + return new WP_Error('import_file_error', sprintf(__('Remote server returned error response %1$d %2$s', 'wordpress-importer'), esc_html($headers['response']), get_status_header_desc($headers['response']))); |
|
| 1061 | 1061 | } |
| 1062 | 1062 | |
| 1063 | - $filesize = filesize( $upload['file'] ); |
|
| 1063 | + $filesize = filesize($upload['file']); |
|
| 1064 | 1064 | |
| 1065 | - if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) { |
|
| 1066 | - @unlink( $upload['file'] ); |
|
| 1067 | - return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') ); |
|
| 1065 | + if (isset($headers['content-length']) && $filesize != $headers['content-length']) { |
|
| 1066 | + @unlink($upload['file']); |
|
| 1067 | + return new WP_Error('import_file_error', __('Remote file is incorrect size', 'wordpress-importer')); |
|
| 1068 | 1068 | } |
| 1069 | 1069 | |
| 1070 | - if ( 0 == $filesize ) { |
|
| 1071 | - @unlink( $upload['file'] ); |
|
| 1072 | - return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') ); |
|
| 1070 | + if (0 == $filesize) { |
|
| 1071 | + @unlink($upload['file']); |
|
| 1072 | + return new WP_Error('import_file_error', __('Zero size file downloaded', 'wordpress-importer')); |
|
| 1073 | 1073 | } |
| 1074 | 1074 | |
| 1075 | 1075 | $max_size = (int) $this->max_attachment_size(); |
| 1076 | - if ( ! empty( $max_size ) && $filesize > $max_size ) { |
|
| 1077 | - @unlink( $upload['file'] ); |
|
| 1078 | - return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) ); |
|
| 1076 | + if ( ! empty($max_size) && $filesize > $max_size) { |
|
| 1077 | + @unlink($upload['file']); |
|
| 1078 | + return new WP_Error('import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size))); |
|
| 1079 | 1079 | } |
| 1080 | 1080 | |
| 1081 | 1081 | // keep track of the old and new urls so we can substitute them later |
| 1082 | 1082 | $this->url_remap[$url] = $upload['url']; |
| 1083 | 1083 | $this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed? |
| 1084 | 1084 | // keep track of the destination if the remote url is redirected somewhere else |
| 1085 | - if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url ) { |
|
| 1085 | + if (isset($headers['x-final-location']) && $headers['x-final-location'] != $url) { |
|
| 1086 | 1086 | $this->url_remap[$headers['x-final-location']] = $upload['url']; |
| 1087 | 1087 | } |
| 1088 | 1088 | |
@@ -1100,38 +1100,38 @@ discard block |
||
| 1100 | 1100 | global $wpdb; |
| 1101 | 1101 | |
| 1102 | 1102 | // find parents for post orphans |
| 1103 | - foreach ( $this->post_orphans as $child_id => $parent_id ) { |
|
| 1103 | + foreach ($this->post_orphans as $child_id => $parent_id) { |
|
| 1104 | 1104 | $local_child_id = $local_parent_id = false; |
| 1105 | - if ( isset( $this->processed_posts[$child_id] ) ) { |
|
| 1105 | + if (isset($this->processed_posts[$child_id])) { |
|
| 1106 | 1106 | $local_child_id = $this->processed_posts[$child_id]; |
| 1107 | 1107 | } |
| 1108 | - if ( isset( $this->processed_posts[$parent_id] ) ) { |
|
| 1108 | + if (isset($this->processed_posts[$parent_id])) { |
|
| 1109 | 1109 | $local_parent_id = $this->processed_posts[$parent_id]; |
| 1110 | 1110 | } |
| 1111 | 1111 | |
| 1112 | - if ( $local_child_id && $local_parent_id ) { |
|
| 1113 | - $wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' ); |
|
| 1112 | + if ($local_child_id && $local_parent_id) { |
|
| 1113 | + $wpdb->update($wpdb->posts, array('post_parent' => $local_parent_id), array('ID' => $local_child_id), '%d', '%d'); |
|
| 1114 | 1114 | } |
| 1115 | 1115 | } |
| 1116 | 1116 | |
| 1117 | 1117 | // all other posts/terms are imported, retry menu items with missing associated object |
| 1118 | 1118 | $missing_menu_items = $this->missing_menu_items; |
| 1119 | - foreach ( $missing_menu_items as $item ) { |
|
| 1120 | - $this->process_menu_item( $item ); |
|
| 1119 | + foreach ($missing_menu_items as $item) { |
|
| 1120 | + $this->process_menu_item($item); |
|
| 1121 | 1121 | } |
| 1122 | 1122 | |
| 1123 | 1123 | // find parents for menu item orphans |
| 1124 | - foreach ( $this->menu_item_orphans as $child_id => $parent_id ) { |
|
| 1124 | + foreach ($this->menu_item_orphans as $child_id => $parent_id) { |
|
| 1125 | 1125 | $local_child_id = $local_parent_id = 0; |
| 1126 | - if ( isset( $this->processed_menu_items[$child_id] ) ) { |
|
| 1126 | + if (isset($this->processed_menu_items[$child_id])) { |
|
| 1127 | 1127 | $local_child_id = $this->processed_menu_items[$child_id]; |
| 1128 | 1128 | } |
| 1129 | - if ( isset( $this->processed_menu_items[$parent_id] ) ) { |
|
| 1129 | + if (isset($this->processed_menu_items[$parent_id])) { |
|
| 1130 | 1130 | $local_parent_id = $this->processed_menu_items[$parent_id]; |
| 1131 | 1131 | } |
| 1132 | 1132 | |
| 1133 | - if ( $local_child_id && $local_parent_id ) { |
|
| 1134 | - update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id ); |
|
| 1133 | + if ($local_child_id && $local_parent_id) { |
|
| 1134 | + update_post_meta($local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id); |
|
| 1135 | 1135 | } |
| 1136 | 1136 | } |
| 1137 | 1137 | } |
@@ -1142,13 +1142,13 @@ discard block |
||
| 1142 | 1142 | function backfill_attachment_urls() { |
| 1143 | 1143 | global $wpdb; |
| 1144 | 1144 | // make sure we do the longest urls first, in case one is a substring of another |
| 1145 | - uksort( $this->url_remap, array(&$this, 'cmpr_strlen') ); |
|
| 1145 | + uksort($this->url_remap, array(&$this, 'cmpr_strlen')); |
|
| 1146 | 1146 | |
| 1147 | - foreach ( $this->url_remap as $from_url => $to_url ) { |
|
| 1147 | + foreach ($this->url_remap as $from_url => $to_url) { |
|
| 1148 | 1148 | // remap urls in post_content |
| 1149 | - $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) ); |
|
| 1149 | + $wpdb->query($wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url)); |
|
| 1150 | 1150 | // remap enclosure urls |
| 1151 | - $result = $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url) ); |
|
| 1151 | + $result = $wpdb->query($wpdb->prepare("UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s) WHERE meta_key='enclosure'", $from_url, $to_url)); |
|
| 1152 | 1152 | } |
| 1153 | 1153 | } |
| 1154 | 1154 | |
@@ -1157,12 +1157,12 @@ discard block |
||
| 1157 | 1157 | */ |
| 1158 | 1158 | function remap_featured_images() { |
| 1159 | 1159 | // cycle through posts that have a featured image |
| 1160 | - foreach ( $this->featured_images as $post_id => $value ) { |
|
| 1161 | - if ( isset( $this->processed_posts[$value] ) ) { |
|
| 1160 | + foreach ($this->featured_images as $post_id => $value) { |
|
| 1161 | + if (isset($this->processed_posts[$value])) { |
|
| 1162 | 1162 | $new_id = $this->processed_posts[$value]; |
| 1163 | 1163 | // only update if there's a difference |
| 1164 | - if ( $new_id != $value ) { |
|
| 1165 | - update_post_meta( $post_id, '_thumbnail_id', $new_id ); |
|
| 1164 | + if ($new_id != $value) { |
|
| 1165 | + update_post_meta($post_id, '_thumbnail_id', $new_id); |
|
| 1166 | 1166 | } |
| 1167 | 1167 | } |
| 1168 | 1168 | } |
@@ -1174,23 +1174,23 @@ discard block |
||
| 1174 | 1174 | * @param string $file Path to WXR file for parsing |
| 1175 | 1175 | * @return array Information gathered from the WXR file |
| 1176 | 1176 | */ |
| 1177 | - function parse( $file ) { |
|
| 1177 | + function parse($file) { |
|
| 1178 | 1178 | $parser = new WXR_Parser(); |
| 1179 | - return $parser->parse( $file ); |
|
| 1179 | + return $parser->parse($file); |
|
| 1180 | 1180 | } |
| 1181 | 1181 | |
| 1182 | 1182 | // Display import page title |
| 1183 | 1183 | function header() { |
| 1184 | 1184 | echo '<div class="wrap">'; |
| 1185 | 1185 | screen_icon(); |
| 1186 | - echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>'; |
|
| 1186 | + echo '<h2>' . __('Import WordPress', 'wordpress-importer') . '</h2>'; |
|
| 1187 | 1187 | |
| 1188 | 1188 | $updates = get_plugin_updates(); |
| 1189 | 1189 | $basename = plugin_basename(__FILE__); |
| 1190 | - if ( isset( $updates[$basename] ) ) { |
|
| 1190 | + if (isset($updates[$basename])) { |
|
| 1191 | 1191 | $update = $updates[$basename]; |
| 1192 | 1192 | echo '<div class="error"><p><strong>'; |
| 1193 | - printf( __( 'A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wordpress-importer' ), $update->update->new_version ); |
|
| 1193 | + printf(__('A new version of this importer is available. Please update to version %s to ensure compatibility with newer export files.', 'wordpress-importer'), $update->update->new_version); |
|
| 1194 | 1194 | echo '</strong></p></div>'; |
| 1195 | 1195 | } |
| 1196 | 1196 | } |
@@ -1205,9 +1205,9 @@ discard block |
||
| 1205 | 1205 | */ |
| 1206 | 1206 | function greet() { |
| 1207 | 1207 | echo '<div class="narrow">'; |
| 1208 | - echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>'; |
|
| 1209 | - echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>'; |
|
| 1210 | - wp_import_upload_form( 'admin.php?import=wordpress&step=1' ); |
|
| 1208 | + echo '<p>' . __('Howdy! Upload your WordPress eXtended RSS (WXR) file and we’ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer') . '</p>'; |
|
| 1209 | + echo '<p>' . __('Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer') . '</p>'; |
|
| 1210 | + wp_import_upload_form('admin.php?import=wordpress&step=1'); |
|
| 1211 | 1211 | echo '</div>'; |
| 1212 | 1212 | } |
| 1213 | 1213 | |
@@ -1217,10 +1217,10 @@ discard block |
||
| 1217 | 1217 | * @param string $key The meta key to check |
| 1218 | 1218 | * @return string|bool The key if we do want to import, false if not |
| 1219 | 1219 | */ |
| 1220 | - function is_valid_meta_key( $key ) { |
|
| 1220 | + function is_valid_meta_key($key) { |
|
| 1221 | 1221 | // skip attachment metadata since we'll regenerate it from scratch |
| 1222 | 1222 | // skip _edit_lock as not relevant for import |
| 1223 | - if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) ) { |
|
| 1223 | + if (in_array($key, array('_wp_attached_file', '_wp_attachment_metadata', '_edit_lock'))) { |
|
| 1224 | 1224 | return false; |
| 1225 | 1225 | } |
| 1226 | 1226 | return $key; |
@@ -1233,7 +1233,7 @@ discard block |
||
| 1233 | 1233 | * @return bool True if creating users is allowed |
| 1234 | 1234 | */ |
| 1235 | 1235 | function allow_create_users() { |
| 1236 | - return apply_filters( 'import_allow_create_users', true ); |
|
| 1236 | + return apply_filters('import_allow_create_users', true); |
|
| 1237 | 1237 | } |
| 1238 | 1238 | |
| 1239 | 1239 | /** |
@@ -1244,7 +1244,7 @@ discard block |
||
| 1244 | 1244 | * @return bool True if downloading attachments is allowed |
| 1245 | 1245 | */ |
| 1246 | 1246 | function allow_fetch_attachments() { |
| 1247 | - return apply_filters( 'import_allow_fetch_attachments', true ); |
|
| 1247 | + return apply_filters('import_allow_fetch_attachments', true); |
|
| 1248 | 1248 | } |
| 1249 | 1249 | |
| 1250 | 1250 | /** |
@@ -1254,19 +1254,19 @@ discard block |
||
| 1254 | 1254 | * @return int Maximum attachment file size to import |
| 1255 | 1255 | */ |
| 1256 | 1256 | function max_attachment_size() { |
| 1257 | - return apply_filters( 'import_attachment_size_limit', 0 ); |
|
| 1257 | + return apply_filters('import_attachment_size_limit', 0); |
|
| 1258 | 1258 | } |
| 1259 | 1259 | |
| 1260 | 1260 | /** |
| 1261 | 1261 | * Added to http_request_timeout filter to force timeout at 60 seconds during import |
| 1262 | 1262 | * @return int 60 |
| 1263 | 1263 | */ |
| 1264 | - function bump_request_timeout( $val ) { |
|
| 1264 | + function bump_request_timeout($val) { |
|
| 1265 | 1265 | return 60; |
| 1266 | 1266 | } |
| 1267 | 1267 | |
| 1268 | 1268 | // return the difference in length between two strings |
| 1269 | - function cmpr_strlen( $a, $b ) { |
|
| 1269 | + function cmpr_strlen($a, $b) { |
|
| 1270 | 1270 | return strlen($b) - strlen($a); |
| 1271 | 1271 | } |
| 1272 | 1272 | } |
@@ -1274,13 +1274,13 @@ discard block |
||
| 1274 | 1274 | } // class_exists( 'WP_Importer' ) |
| 1275 | 1275 | |
| 1276 | 1276 | function wordpress_importer_init() { |
| 1277 | - load_plugin_textdomain( 'wordpress-importer', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' ); |
|
| 1277 | + load_plugin_textdomain('wordpress-importer', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
|
| 1278 | 1278 | |
| 1279 | 1279 | /** |
| 1280 | 1280 | * WordPress Importer object for registering the import callback |
| 1281 | 1281 | * @global WP_Import $wp_import |
| 1282 | 1282 | */ |
| 1283 | 1283 | $GLOBALS['wp_import'] = new WP_Import(); |
| 1284 | - register_importer( 'wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array( $GLOBALS['wp_import'], 'dispatch' ) ); |
|
| 1284 | + register_importer('wordpress', 'WordPress', __('Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'wordpress-importer'), array($GLOBALS['wp_import'], 'dispatch')); |
|
| 1285 | 1285 | } |
| 1286 | -add_action( 'admin_init', 'wordpress_importer_init' ); |
|
| 1286 | +add_action('admin_init', 'wordpress_importer_init'); |
|
@@ -31,6 +31,9 @@ |
||
| 31 | 31 | return wp_insert_attachment( $r, $r['file'], $r['post_parent'] ); |
| 32 | 32 | } |
| 33 | 33 | |
| 34 | + /** |
|
| 35 | + * @param string $file |
|
| 36 | + */ |
|
| 34 | 37 | function create_upload_object( $file, $parent = 0 ) { |
| 35 | 38 | $contents = file_get_contents($file); |
| 36 | 39 | $upload = wp_upload_bits(basename($file), null, $contents); |
@@ -2,62 +2,62 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Attachment extends WP_UnitTest_Factory_For_Post { |
| 4 | 4 | |
| 5 | - /** |
|
| 6 | - * Create an attachment fixture. |
|
| 7 | - * |
|
| 8 | - * @param array $args { |
|
| 9 | - * Array of arguments. Accepts all arguments that can be passed to |
|
| 10 | - * wp_insert_attachment(), in addition to the following: |
|
| 11 | - * @type int $post_parent ID of the post to which the attachment belongs. |
|
| 12 | - * @type string $file Path of the attached file. |
|
| 13 | - * } |
|
| 14 | - * @param int $legacy_parent Deprecated. |
|
| 15 | - * @param array $legacy_args Deprecated |
|
| 16 | - */ |
|
| 17 | - function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) { |
|
| 18 | - // Backward compatibility for legacy argument format. |
|
| 19 | - if ( is_string( $args ) ) { |
|
| 20 | - $file = $args; |
|
| 21 | - $args = $legacy_args; |
|
| 22 | - $args['post_parent'] = $legacy_parent; |
|
| 23 | - $args['file'] = $file; |
|
| 24 | - } |
|
| 25 | - |
|
| 26 | - $r = array_merge( array( |
|
| 27 | - 'file' => '', |
|
| 28 | - 'post_parent' => 0, |
|
| 29 | - ), $args ); |
|
| 30 | - |
|
| 31 | - return wp_insert_attachment( $r, $r['file'], $r['post_parent'] ); |
|
| 32 | - } |
|
| 33 | - |
|
| 34 | - function create_upload_object( $file, $parent = 0 ) { |
|
| 35 | - $contents = file_get_contents($file); |
|
| 36 | - $upload = wp_upload_bits(basename($file), null, $contents); |
|
| 37 | - |
|
| 38 | - $type = ''; |
|
| 39 | - if ( ! empty($upload['type']) ) { |
|
| 40 | - $type = $upload['type']; |
|
| 41 | - } else { |
|
| 42 | - $mime = wp_check_filetype( $upload['file'] ); |
|
| 43 | - if ($mime) { |
|
| 44 | - $type = $mime['type']; |
|
| 45 | - } |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - $attachment = array( |
|
| 49 | - 'post_title' => basename( $upload['file'] ), |
|
| 50 | - 'post_content' => '', |
|
| 51 | - 'post_type' => 'attachment', |
|
| 52 | - 'post_parent' => $parent, |
|
| 53 | - 'post_mime_type' => $type, |
|
| 54 | - 'guid' => $upload[ 'url' ], |
|
| 55 | - ); |
|
| 56 | - |
|
| 57 | - // Save the data |
|
| 58 | - $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent ); |
|
| 59 | - wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); |
|
| 60 | - |
|
| 61 | - return $id; |
|
| 62 | - } |
|
| 5 | + /** |
|
| 6 | + * Create an attachment fixture. |
|
| 7 | + * |
|
| 8 | + * @param array $args { |
|
| 9 | + * Array of arguments. Accepts all arguments that can be passed to |
|
| 10 | + * wp_insert_attachment(), in addition to the following: |
|
| 11 | + * @type int $post_parent ID of the post to which the attachment belongs. |
|
| 12 | + * @type string $file Path of the attached file. |
|
| 13 | + * } |
|
| 14 | + * @param int $legacy_parent Deprecated. |
|
| 15 | + * @param array $legacy_args Deprecated |
|
| 16 | + */ |
|
| 17 | + function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) { |
|
| 18 | + // Backward compatibility for legacy argument format. |
|
| 19 | + if ( is_string( $args ) ) { |
|
| 20 | + $file = $args; |
|
| 21 | + $args = $legacy_args; |
|
| 22 | + $args['post_parent'] = $legacy_parent; |
|
| 23 | + $args['file'] = $file; |
|
| 24 | + } |
|
| 25 | + |
|
| 26 | + $r = array_merge( array( |
|
| 27 | + 'file' => '', |
|
| 28 | + 'post_parent' => 0, |
|
| 29 | + ), $args ); |
|
| 30 | + |
|
| 31 | + return wp_insert_attachment( $r, $r['file'], $r['post_parent'] ); |
|
| 32 | + } |
|
| 33 | + |
|
| 34 | + function create_upload_object( $file, $parent = 0 ) { |
|
| 35 | + $contents = file_get_contents($file); |
|
| 36 | + $upload = wp_upload_bits(basename($file), null, $contents); |
|
| 37 | + |
|
| 38 | + $type = ''; |
|
| 39 | + if ( ! empty($upload['type']) ) { |
|
| 40 | + $type = $upload['type']; |
|
| 41 | + } else { |
|
| 42 | + $mime = wp_check_filetype( $upload['file'] ); |
|
| 43 | + if ($mime) { |
|
| 44 | + $type = $mime['type']; |
|
| 45 | + } |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + $attachment = array( |
|
| 49 | + 'post_title' => basename( $upload['file'] ), |
|
| 50 | + 'post_content' => '', |
|
| 51 | + 'post_type' => 'attachment', |
|
| 52 | + 'post_parent' => $parent, |
|
| 53 | + 'post_mime_type' => $type, |
|
| 54 | + 'guid' => $upload[ 'url' ], |
|
| 55 | + ); |
|
| 56 | + |
|
| 57 | + // Save the data |
|
| 58 | + $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent ); |
|
| 59 | + wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); |
|
| 60 | + |
|
| 61 | + return $id; |
|
| 62 | + } |
|
| 63 | 63 | } |
@@ -14,49 +14,49 @@ |
||
| 14 | 14 | * @param int $legacy_parent Deprecated. |
| 15 | 15 | * @param array $legacy_args Deprecated |
| 16 | 16 | */ |
| 17 | - function create_object( $args, $legacy_parent = 0, $legacy_args = array() ) { |
|
| 17 | + function create_object($args, $legacy_parent = 0, $legacy_args = array()) { |
|
| 18 | 18 | // Backward compatibility for legacy argument format. |
| 19 | - if ( is_string( $args ) ) { |
|
| 19 | + if (is_string($args)) { |
|
| 20 | 20 | $file = $args; |
| 21 | 21 | $args = $legacy_args; |
| 22 | 22 | $args['post_parent'] = $legacy_parent; |
| 23 | 23 | $args['file'] = $file; |
| 24 | 24 | } |
| 25 | 25 | |
| 26 | - $r = array_merge( array( |
|
| 26 | + $r = array_merge(array( |
|
| 27 | 27 | 'file' => '', |
| 28 | 28 | 'post_parent' => 0, |
| 29 | - ), $args ); |
|
| 29 | + ), $args); |
|
| 30 | 30 | |
| 31 | - return wp_insert_attachment( $r, $r['file'], $r['post_parent'] ); |
|
| 31 | + return wp_insert_attachment($r, $r['file'], $r['post_parent']); |
|
| 32 | 32 | } |
| 33 | 33 | |
| 34 | - function create_upload_object( $file, $parent = 0 ) { |
|
| 34 | + function create_upload_object($file, $parent = 0) { |
|
| 35 | 35 | $contents = file_get_contents($file); |
| 36 | 36 | $upload = wp_upload_bits(basename($file), null, $contents); |
| 37 | 37 | |
| 38 | 38 | $type = ''; |
| 39 | - if ( ! empty($upload['type']) ) { |
|
| 39 | + if ( ! empty($upload['type'])) { |
|
| 40 | 40 | $type = $upload['type']; |
| 41 | 41 | } else { |
| 42 | - $mime = wp_check_filetype( $upload['file'] ); |
|
| 42 | + $mime = wp_check_filetype($upload['file']); |
|
| 43 | 43 | if ($mime) { |
| 44 | 44 | $type = $mime['type']; |
| 45 | 45 | } |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | $attachment = array( |
| 49 | - 'post_title' => basename( $upload['file'] ), |
|
| 49 | + 'post_title' => basename($upload['file']), |
|
| 50 | 50 | 'post_content' => '', |
| 51 | 51 | 'post_type' => 'attachment', |
| 52 | 52 | 'post_parent' => $parent, |
| 53 | 53 | 'post_mime_type' => $type, |
| 54 | - 'guid' => $upload[ 'url' ], |
|
| 54 | + 'guid' => $upload['url'], |
|
| 55 | 55 | ); |
| 56 | 56 | |
| 57 | 57 | // Save the data |
| 58 | - $id = wp_insert_attachment( $attachment, $upload[ 'file' ], $parent ); |
|
| 59 | - wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $upload['file'] ) ); |
|
| 58 | + $id = wp_insert_attachment($attachment, $upload['file'], $parent); |
|
| 59 | + wp_update_attachment_metadata($id, wp_generate_attachment_metadata($id, $upload['file'])); |
|
| 60 | 60 | |
| 61 | 61 | return $id; |
| 62 | 62 | } |
@@ -2,6 +2,9 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | + /** |
|
| 6 | + * @param WP_UnitTest_Factory $factory |
|
| 7 | + */ |
|
| 5 | 8 | function __construct( $factory = null ) { |
| 6 | 9 | global $current_site, $base; |
| 7 | 10 | parent::__construct( $factory ); |
@@ -2,35 +2,35 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - function __construct( $factory = null ) { |
|
| 6 | - global $current_site, $base; |
|
| 7 | - parent::__construct( $factory ); |
|
| 8 | - $this->default_generation_definitions = array( |
|
| 9 | - 'domain' => $current_site->domain, |
|
| 10 | - 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ), |
|
| 11 | - 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ), |
|
| 12 | - 'site_id' => $current_site->id, |
|
| 13 | - ); |
|
| 14 | - } |
|
| 5 | + function __construct( $factory = null ) { |
|
| 6 | + global $current_site, $base; |
|
| 7 | + parent::__construct( $factory ); |
|
| 8 | + $this->default_generation_definitions = array( |
|
| 9 | + 'domain' => $current_site->domain, |
|
| 10 | + 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ), |
|
| 11 | + 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ), |
|
| 12 | + 'site_id' => $current_site->id, |
|
| 13 | + ); |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - function create_object( $args ) { |
|
| 17 | - global $wpdb; |
|
| 18 | - $meta = isset( $args['meta'] ) ? $args['meta'] : array( 'public' => 1 ); |
|
| 19 | - $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id(); |
|
| 20 | - // temp tables will trigger db errors when we attempt to reference them as new temp tables |
|
| 21 | - $suppress = $wpdb->suppress_errors(); |
|
| 22 | - $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] ); |
|
| 23 | - $wpdb->suppress_errors( $suppress ); |
|
| 16 | + function create_object( $args ) { |
|
| 17 | + global $wpdb; |
|
| 18 | + $meta = isset( $args['meta'] ) ? $args['meta'] : array( 'public' => 1 ); |
|
| 19 | + $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id(); |
|
| 20 | + // temp tables will trigger db errors when we attempt to reference them as new temp tables |
|
| 21 | + $suppress = $wpdb->suppress_errors(); |
|
| 22 | + $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] ); |
|
| 23 | + $wpdb->suppress_errors( $suppress ); |
|
| 24 | 24 | |
| 25 | - // Tell WP we're done installing. |
|
| 26 | - wp_installing( false ); |
|
| 25 | + // Tell WP we're done installing. |
|
| 26 | + wp_installing( false ); |
|
| 27 | 27 | |
| 28 | - return $blog; |
|
| 29 | - } |
|
| 28 | + return $blog; |
|
| 29 | + } |
|
| 30 | 30 | |
| 31 | - function update_object( $blog_id, $fields ) {} |
|
| 31 | + function update_object( $blog_id, $fields ) {} |
|
| 32 | 32 | |
| 33 | - function get_object_by_id( $blog_id ) { |
|
| 34 | - return get_site( $blog_id ); |
|
| 35 | - } |
|
| 33 | + function get_object_by_id( $blog_id ) { |
|
| 34 | + return get_site( $blog_id ); |
|
| 35 | + } |
|
| 36 | 36 | } |
@@ -2,35 +2,35 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Blog extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - function __construct( $factory = null ) { |
|
| 5 | + function __construct($factory = null) { |
|
| 6 | 6 | global $current_site, $base; |
| 7 | - parent::__construct( $factory ); |
|
| 7 | + parent::__construct($factory); |
|
| 8 | 8 | $this->default_generation_definitions = array( |
| 9 | 9 | 'domain' => $current_site->domain, |
| 10 | - 'path' => new WP_UnitTest_Generator_Sequence( $base . 'testpath%s' ), |
|
| 11 | - 'title' => new WP_UnitTest_Generator_Sequence( 'Site %s' ), |
|
| 10 | + 'path' => new WP_UnitTest_Generator_Sequence($base . 'testpath%s'), |
|
| 11 | + 'title' => new WP_UnitTest_Generator_Sequence('Site %s'), |
|
| 12 | 12 | 'site_id' => $current_site->id, |
| 13 | 13 | ); |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - function create_object( $args ) { |
|
| 16 | + function create_object($args) { |
|
| 17 | 17 | global $wpdb; |
| 18 | - $meta = isset( $args['meta'] ) ? $args['meta'] : array( 'public' => 1 ); |
|
| 19 | - $user_id = isset( $args['user_id'] ) ? $args['user_id'] : get_current_user_id(); |
|
| 18 | + $meta = isset($args['meta']) ? $args['meta'] : array('public' => 1); |
|
| 19 | + $user_id = isset($args['user_id']) ? $args['user_id'] : get_current_user_id(); |
|
| 20 | 20 | // temp tables will trigger db errors when we attempt to reference them as new temp tables |
| 21 | 21 | $suppress = $wpdb->suppress_errors(); |
| 22 | - $blog = wpmu_create_blog( $args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id'] ); |
|
| 23 | - $wpdb->suppress_errors( $suppress ); |
|
| 22 | + $blog = wpmu_create_blog($args['domain'], $args['path'], $args['title'], $user_id, $meta, $args['site_id']); |
|
| 23 | + $wpdb->suppress_errors($suppress); |
|
| 24 | 24 | |
| 25 | 25 | // Tell WP we're done installing. |
| 26 | - wp_installing( false ); |
|
| 26 | + wp_installing(false); |
|
| 27 | 27 | |
| 28 | 28 | return $blog; |
| 29 | 29 | } |
| 30 | 30 | |
| 31 | - function update_object( $blog_id, $fields ) {} |
|
| 31 | + function update_object($blog_id, $fields) {} |
|
| 32 | 32 | |
| 33 | - function get_object_by_id( $blog_id ) { |
|
| 34 | - return get_site( $blog_id ); |
|
| 33 | + function get_object_by_id($blog_id) { |
|
| 34 | + return get_site($blog_id); |
|
| 35 | 35 | } |
| 36 | 36 | } |
@@ -2,6 +2,9 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | + /** |
|
| 6 | + * @param WP_UnitTest_Factory $factory |
|
| 7 | + */ |
|
| 5 | 8 | function __construct( $factory = null ) { |
| 6 | 9 | parent::__construct( $factory ); |
| 7 | 10 | $this->default_generation_definitions = array( |
@@ -2,31 +2,31 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - function __construct( $factory = null ) { |
|
| 6 | - parent::__construct( $factory ); |
|
| 7 | - $this->default_generation_definitions = array( |
|
| 8 | - 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ), |
|
| 9 | - 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ), |
|
| 10 | - 'comment_approved' => 1, |
|
| 11 | - 'comment_content' => 'This is a comment' |
|
| 12 | - ); |
|
| 13 | - } |
|
| 5 | + function __construct( $factory = null ) { |
|
| 6 | + parent::__construct( $factory ); |
|
| 7 | + $this->default_generation_definitions = array( |
|
| 8 | + 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ), |
|
| 9 | + 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ), |
|
| 10 | + 'comment_approved' => 1, |
|
| 11 | + 'comment_content' => 'This is a comment' |
|
| 12 | + ); |
|
| 13 | + } |
|
| 14 | 14 | |
| 15 | - function create_object( $args ) { |
|
| 16 | - return wp_insert_comment( $this->addslashes_deep( $args ) ); |
|
| 17 | - } |
|
| 15 | + function create_object( $args ) { |
|
| 16 | + return wp_insert_comment( $this->addslashes_deep( $args ) ); |
|
| 17 | + } |
|
| 18 | 18 | |
| 19 | - function update_object( $comment_id, $fields ) { |
|
| 20 | - $fields['comment_ID'] = $comment_id; |
|
| 21 | - return wp_update_comment( $this->addslashes_deep( $fields ) ); |
|
| 22 | - } |
|
| 19 | + function update_object( $comment_id, $fields ) { |
|
| 20 | + $fields['comment_ID'] = $comment_id; |
|
| 21 | + return wp_update_comment( $this->addslashes_deep( $fields ) ); |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) { |
|
| 25 | - $args['comment_post_ID'] = $post_id; |
|
| 26 | - return $this->create_many( $count, $args, $generation_definitions ); |
|
| 27 | - } |
|
| 24 | + function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) { |
|
| 25 | + $args['comment_post_ID'] = $post_id; |
|
| 26 | + return $this->create_many( $count, $args, $generation_definitions ); |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - function get_object_by_id( $comment_id ) { |
|
| 30 | - return get_comment( $comment_id ); |
|
| 31 | - } |
|
| 29 | + function get_object_by_id( $comment_id ) { |
|
| 30 | + return get_comment( $comment_id ); |
|
| 31 | + } |
|
| 32 | 32 | } |
@@ -2,31 +2,31 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Comment extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - function __construct( $factory = null ) { |
|
| 6 | - parent::__construct( $factory ); |
|
| 5 | + function __construct($factory = null) { |
|
| 6 | + parent::__construct($factory); |
|
| 7 | 7 | $this->default_generation_definitions = array( |
| 8 | - 'comment_author' => new WP_UnitTest_Generator_Sequence( 'Commenter %s' ), |
|
| 9 | - 'comment_author_url' => new WP_UnitTest_Generator_Sequence( 'http://example.com/%s/' ), |
|
| 8 | + 'comment_author' => new WP_UnitTest_Generator_Sequence('Commenter %s'), |
|
| 9 | + 'comment_author_url' => new WP_UnitTest_Generator_Sequence('http://example.com/%s/'), |
|
| 10 | 10 | 'comment_approved' => 1, |
| 11 | 11 | 'comment_content' => 'This is a comment' |
| 12 | 12 | ); |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | - function create_object( $args ) { |
|
| 16 | - return wp_insert_comment( $this->addslashes_deep( $args ) ); |
|
| 15 | + function create_object($args) { |
|
| 16 | + return wp_insert_comment($this->addslashes_deep($args)); |
|
| 17 | 17 | } |
| 18 | 18 | |
| 19 | - function update_object( $comment_id, $fields ) { |
|
| 19 | + function update_object($comment_id, $fields) { |
|
| 20 | 20 | $fields['comment_ID'] = $comment_id; |
| 21 | - return wp_update_comment( $this->addslashes_deep( $fields ) ); |
|
| 21 | + return wp_update_comment($this->addslashes_deep($fields)); |
|
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - function create_post_comments( $post_id, $count = 1, $args = array(), $generation_definitions = null ) { |
|
| 24 | + function create_post_comments($post_id, $count = 1, $args = array(), $generation_definitions = null) { |
|
| 25 | 25 | $args['comment_post_ID'] = $post_id; |
| 26 | - return $this->create_many( $count, $args, $generation_definitions ); |
|
| 26 | + return $this->create_many($count, $args, $generation_definitions); |
|
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - function get_object_by_id( $comment_id ) { |
|
| 30 | - return get_comment( $comment_id ); |
|
| 29 | + function get_object_by_id($comment_id) { |
|
| 30 | + return get_comment($comment_id); |
|
| 31 | 31 | } |
| 32 | 32 | } |
@@ -2,6 +2,9 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | + /** |
|
| 6 | + * @param WP_UnitTest_Factory $factory |
|
| 7 | + */ |
|
| 5 | 8 | function __construct( $factory = null ) { |
| 6 | 9 | parent::__construct( $factory ); |
| 7 | 10 | $this->default_generation_definitions = array( |
@@ -2,33 +2,33 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - function __construct( $factory = null ) { |
|
| 6 | - parent::__construct( $factory ); |
|
| 7 | - $this->default_generation_definitions = array( |
|
| 8 | - 'domain' => WP_TESTS_DOMAIN, |
|
| 9 | - 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ), |
|
| 10 | - 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ), |
|
| 11 | - 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ), |
|
| 12 | - 'subdomain_install' => false, |
|
| 13 | - ); |
|
| 14 | - } |
|
| 5 | + function __construct( $factory = null ) { |
|
| 6 | + parent::__construct( $factory ); |
|
| 7 | + $this->default_generation_definitions = array( |
|
| 8 | + 'domain' => WP_TESTS_DOMAIN, |
|
| 9 | + 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ), |
|
| 10 | + 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ), |
|
| 11 | + 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ), |
|
| 12 | + 'subdomain_install' => false, |
|
| 13 | + ); |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - function create_object( $args ) { |
|
| 17 | - require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
| 16 | + function create_object( $args ) { |
|
| 17 | + require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
|
| 18 | 18 | |
| 19 | - if ( ! isset( $args['user'] ) ) { |
|
| 20 | - $email = WP_TESTS_EMAIL; |
|
| 21 | - } else { |
|
| 22 | - $email = get_userdata( $args['user'] )->user_email; |
|
| 23 | - } |
|
| 19 | + if ( ! isset( $args['user'] ) ) { |
|
| 20 | + $email = WP_TESTS_EMAIL; |
|
| 21 | + } else { |
|
| 22 | + $email = get_userdata( $args['user'] )->user_email; |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] ); |
|
| 26 | - return $args['network_id']; |
|
| 27 | - } |
|
| 25 | + populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] ); |
|
| 26 | + return $args['network_id']; |
|
| 27 | + } |
|
| 28 | 28 | |
| 29 | - function update_object( $network_id, $fields ) {} |
|
| 29 | + function update_object( $network_id, $fields ) {} |
|
| 30 | 30 | |
| 31 | - function get_object_by_id( $network_id ) { |
|
| 32 | - return get_network( $network_id ); |
|
| 33 | - } |
|
| 31 | + function get_object_by_id( $network_id ) { |
|
| 32 | + return get_network( $network_id ); |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -2,33 +2,33 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Network extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - function __construct( $factory = null ) { |
|
| 6 | - parent::__construct( $factory ); |
|
| 5 | + function __construct($factory = null) { |
|
| 6 | + parent::__construct($factory); |
|
| 7 | 7 | $this->default_generation_definitions = array( |
| 8 | 8 | 'domain' => WP_TESTS_DOMAIN, |
| 9 | - 'title' => new WP_UnitTest_Generator_Sequence( 'Network %s' ), |
|
| 10 | - 'path' => new WP_UnitTest_Generator_Sequence( '/testpath%s/' ), |
|
| 11 | - 'network_id' => new WP_UnitTest_Generator_Sequence( '%s', 2 ), |
|
| 9 | + 'title' => new WP_UnitTest_Generator_Sequence('Network %s'), |
|
| 10 | + 'path' => new WP_UnitTest_Generator_Sequence('/testpath%s/'), |
|
| 11 | + 'network_id' => new WP_UnitTest_Generator_Sequence('%s', 2), |
|
| 12 | 12 | 'subdomain_install' => false, |
| 13 | 13 | ); |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - function create_object( $args ) { |
|
| 16 | + function create_object($args) { |
|
| 17 | 17 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 18 | 18 | |
| 19 | - if ( ! isset( $args['user'] ) ) { |
|
| 19 | + if ( ! isset($args['user'])) { |
|
| 20 | 20 | $email = WP_TESTS_EMAIL; |
| 21 | 21 | } else { |
| 22 | - $email = get_userdata( $args['user'] )->user_email; |
|
| 22 | + $email = get_userdata($args['user'])->user_email; |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - populate_network( $args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install'] ); |
|
| 25 | + populate_network($args['network_id'], $args['domain'], $email, $args['title'], $args['path'], $args['subdomain_install']); |
|
| 26 | 26 | return $args['network_id']; |
| 27 | 27 | } |
| 28 | 28 | |
| 29 | - function update_object( $network_id, $fields ) {} |
|
| 29 | + function update_object($network_id, $fields) {} |
|
| 30 | 30 | |
| 31 | - function get_object_by_id( $network_id ) { |
|
| 32 | - return get_network( $network_id ); |
|
| 31 | + function get_object_by_id($network_id) { |
|
| 32 | + return get_network($network_id); |
|
| 33 | 33 | } |
| 34 | 34 | } |
@@ -2,6 +2,9 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | + /** |
|
| 6 | + * @param WP_UnitTest_Factory $factory |
|
| 7 | + */ |
|
| 5 | 8 | function __construct( $factory = null ) { |
| 6 | 9 | parent::__construct( $factory ); |
| 7 | 10 | $this->default_generation_definitions = array( |
@@ -2,27 +2,27 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - function __construct( $factory = null ) { |
|
| 6 | - parent::__construct( $factory ); |
|
| 7 | - $this->default_generation_definitions = array( |
|
| 8 | - 'post_status' => 'publish', |
|
| 9 | - 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ), |
|
| 10 | - 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ), |
|
| 11 | - 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ), |
|
| 12 | - 'post_type' => 'post' |
|
| 13 | - ); |
|
| 14 | - } |
|
| 5 | + function __construct( $factory = null ) { |
|
| 6 | + parent::__construct( $factory ); |
|
| 7 | + $this->default_generation_definitions = array( |
|
| 8 | + 'post_status' => 'publish', |
|
| 9 | + 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ), |
|
| 10 | + 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ), |
|
| 11 | + 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ), |
|
| 12 | + 'post_type' => 'post' |
|
| 13 | + ); |
|
| 14 | + } |
|
| 15 | 15 | |
| 16 | - function create_object( $args ) { |
|
| 17 | - return wp_insert_post( $args ); |
|
| 18 | - } |
|
| 16 | + function create_object( $args ) { |
|
| 17 | + return wp_insert_post( $args ); |
|
| 18 | + } |
|
| 19 | 19 | |
| 20 | - function update_object( $post_id, $fields ) { |
|
| 21 | - $fields['ID'] = $post_id; |
|
| 22 | - return wp_update_post( $fields ); |
|
| 23 | - } |
|
| 20 | + function update_object( $post_id, $fields ) { |
|
| 21 | + $fields['ID'] = $post_id; |
|
| 22 | + return wp_update_post( $fields ); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - function get_object_by_id( $post_id ) { |
|
| 26 | - return get_post( $post_id ); |
|
| 27 | - } |
|
| 25 | + function get_object_by_id( $post_id ) { |
|
| 26 | + return get_post( $post_id ); |
|
| 27 | + } |
|
| 28 | 28 | } |
@@ -2,27 +2,27 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Post extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - function __construct( $factory = null ) { |
|
| 6 | - parent::__construct( $factory ); |
|
| 5 | + function __construct($factory = null) { |
|
| 6 | + parent::__construct($factory); |
|
| 7 | 7 | $this->default_generation_definitions = array( |
| 8 | 8 | 'post_status' => 'publish', |
| 9 | - 'post_title' => new WP_UnitTest_Generator_Sequence( 'Post title %s' ), |
|
| 10 | - 'post_content' => new WP_UnitTest_Generator_Sequence( 'Post content %s' ), |
|
| 11 | - 'post_excerpt' => new WP_UnitTest_Generator_Sequence( 'Post excerpt %s' ), |
|
| 9 | + 'post_title' => new WP_UnitTest_Generator_Sequence('Post title %s'), |
|
| 10 | + 'post_content' => new WP_UnitTest_Generator_Sequence('Post content %s'), |
|
| 11 | + 'post_excerpt' => new WP_UnitTest_Generator_Sequence('Post excerpt %s'), |
|
| 12 | 12 | 'post_type' => 'post' |
| 13 | 13 | ); |
| 14 | 14 | } |
| 15 | 15 | |
| 16 | - function create_object( $args ) { |
|
| 17 | - return wp_insert_post( $args ); |
|
| 16 | + function create_object($args) { |
|
| 17 | + return wp_insert_post($args); |
|
| 18 | 18 | } |
| 19 | 19 | |
| 20 | - function update_object( $post_id, $fields ) { |
|
| 20 | + function update_object($post_id, $fields) { |
|
| 21 | 21 | $fields['ID'] = $post_id; |
| 22 | - return wp_update_post( $fields ); |
|
| 22 | + return wp_update_post($fields); |
|
| 23 | 23 | } |
| 24 | 24 | |
| 25 | - function get_object_by_id( $post_id ) { |
|
| 26 | - return get_post( $post_id ); |
|
| 25 | + function get_object_by_id($post_id) { |
|
| 26 | + return get_post($post_id); |
|
| 27 | 27 | } |
| 28 | 28 | } |
@@ -5,6 +5,10 @@ discard block |
||
| 5 | 5 | private $taxonomy; |
| 6 | 6 | const DEFAULT_TAXONOMY = 'post_tag'; |
| 7 | 7 | |
| 8 | + /** |
|
| 9 | + * @param WP_UnitTest_Factory $factory |
|
| 10 | + * @param string $taxonomy |
|
| 11 | + */ |
|
| 8 | 12 | function __construct( $factory = null, $taxonomy = null ) { |
| 9 | 13 | parent::__construct( $factory ); |
| 10 | 14 | $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY; |
@@ -33,6 +37,9 @@ discard block |
||
| 33 | 37 | return $term_id_pair['term_id']; |
| 34 | 38 | } |
| 35 | 39 | |
| 40 | + /** |
|
| 41 | + * @param string $taxonomy |
|
| 42 | + */ |
|
| 36 | 43 | function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) { |
| 37 | 44 | return wp_set_post_terms( $post_id, $terms, $taxonomy, $append ); |
| 38 | 45 | } |
@@ -2,48 +2,48 @@ |
||
| 2 | 2 | |
| 3 | 3 | class WP_UnitTest_Factory_For_Term extends WP_UnitTest_Factory_For_Thing { |
| 4 | 4 | |
| 5 | - private $taxonomy; |
|
| 6 | - const DEFAULT_TAXONOMY = 'post_tag'; |
|
| 7 | - |
|
| 8 | - function __construct( $factory = null, $taxonomy = null ) { |
|
| 9 | - parent::__construct( $factory ); |
|
| 10 | - $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY; |
|
| 11 | - $this->default_generation_definitions = array( |
|
| 12 | - 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ), |
|
| 13 | - 'taxonomy' => $this->taxonomy, |
|
| 14 | - 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ), |
|
| 15 | - ); |
|
| 16 | - } |
|
| 17 | - |
|
| 18 | - function create_object( $args ) { |
|
| 19 | - $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args ); |
|
| 20 | - $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args ); |
|
| 21 | - if ( is_wp_error( $term_id_pair ) ) { |
|
| 22 | - return $term_id_pair; |
|
| 23 | - } |
|
| 24 | - return $term_id_pair['term_id']; |
|
| 25 | - } |
|
| 26 | - |
|
| 27 | - function update_object( $term, $fields ) { |
|
| 28 | - $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields ); |
|
| 29 | - if ( is_object( $term ) ) { |
|
| 30 | - $taxonomy = $term->taxonomy; |
|
| 31 | - } |
|
| 32 | - $term_id_pair = wp_update_term( $term, $taxonomy, $fields ); |
|
| 33 | - return $term_id_pair['term_id']; |
|
| 34 | - } |
|
| 35 | - |
|
| 36 | - function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) { |
|
| 37 | - return wp_set_post_terms( $post_id, $terms, $taxonomy, $append ); |
|
| 38 | - } |
|
| 39 | - |
|
| 40 | - function create_and_get( $args = array(), $generation_definitions = null ) { |
|
| 41 | - $term_id = $this->create( $args, $generation_definitions ); |
|
| 42 | - $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy; |
|
| 43 | - return get_term( $term_id, $taxonomy ); |
|
| 44 | - } |
|
| 45 | - |
|
| 46 | - function get_object_by_id( $term_id ) { |
|
| 47 | - return get_term( $term_id, $this->taxonomy ); |
|
| 48 | - } |
|
| 5 | + private $taxonomy; |
|
| 6 | + const DEFAULT_TAXONOMY = 'post_tag'; |
|
| 7 | + |
|
| 8 | + function __construct( $factory = null, $taxonomy = null ) { |
|
| 9 | + parent::__construct( $factory ); |
|
| 10 | + $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY; |
|
| 11 | + $this->default_generation_definitions = array( |
|
| 12 | + 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ), |
|
| 13 | + 'taxonomy' => $this->taxonomy, |
|
| 14 | + 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ), |
|
| 15 | + ); |
|
| 16 | + } |
|
| 17 | + |
|
| 18 | + function create_object( $args ) { |
|
| 19 | + $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args ); |
|
| 20 | + $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args ); |
|
| 21 | + if ( is_wp_error( $term_id_pair ) ) { |
|
| 22 | + return $term_id_pair; |
|
| 23 | + } |
|
| 24 | + return $term_id_pair['term_id']; |
|
| 25 | + } |
|
| 26 | + |
|
| 27 | + function update_object( $term, $fields ) { |
|
| 28 | + $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields ); |
|
| 29 | + if ( is_object( $term ) ) { |
|
| 30 | + $taxonomy = $term->taxonomy; |
|
| 31 | + } |
|
| 32 | + $term_id_pair = wp_update_term( $term, $taxonomy, $fields ); |
|
| 33 | + return $term_id_pair['term_id']; |
|
| 34 | + } |
|
| 35 | + |
|
| 36 | + function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) { |
|
| 37 | + return wp_set_post_terms( $post_id, $terms, $taxonomy, $append ); |
|
| 38 | + } |
|
| 39 | + |
|
| 40 | + function create_and_get( $args = array(), $generation_definitions = null ) { |
|
| 41 | + $term_id = $this->create( $args, $generation_definitions ); |
|
| 42 | + $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy; |
|
| 43 | + return get_term( $term_id, $taxonomy ); |
|
| 44 | + } |
|
| 45 | + |
|
| 46 | + function get_object_by_id( $term_id ) { |
|
| 47 | + return get_term( $term_id, $this->taxonomy ); |
|
| 48 | + } |
|
| 49 | 49 | } |
@@ -5,45 +5,45 @@ |
||
| 5 | 5 | private $taxonomy; |
| 6 | 6 | const DEFAULT_TAXONOMY = 'post_tag'; |
| 7 | 7 | |
| 8 | - function __construct( $factory = null, $taxonomy = null ) { |
|
| 9 | - parent::__construct( $factory ); |
|
| 8 | + function __construct($factory = null, $taxonomy = null) { |
|
| 9 | + parent::__construct($factory); |
|
| 10 | 10 | $this->taxonomy = $taxonomy ? $taxonomy : self::DEFAULT_TAXONOMY; |
| 11 | 11 | $this->default_generation_definitions = array( |
| 12 | - 'name' => new WP_UnitTest_Generator_Sequence( 'Term %s' ), |
|
| 12 | + 'name' => new WP_UnitTest_Generator_Sequence('Term %s'), |
|
| 13 | 13 | 'taxonomy' => $this->taxonomy, |
| 14 | - 'description' => new WP_UnitTest_Generator_Sequence( 'Term description %s' ), |
|
| 14 | + 'description' => new WP_UnitTest_Generator_Sequence('Term description %s'), |
|
| 15 | 15 | ); |
| 16 | 16 | } |
| 17 | 17 | |
| 18 | - function create_object( $args ) { |
|
| 19 | - $args = array_merge( array( 'taxonomy' => $this->taxonomy ), $args ); |
|
| 20 | - $term_id_pair = wp_insert_term( $args['name'], $args['taxonomy'], $args ); |
|
| 21 | - if ( is_wp_error( $term_id_pair ) ) { |
|
| 18 | + function create_object($args) { |
|
| 19 | + $args = array_merge(array('taxonomy' => $this->taxonomy), $args); |
|
| 20 | + $term_id_pair = wp_insert_term($args['name'], $args['taxonomy'], $args); |
|
| 21 | + if (is_wp_error($term_id_pair)) { |
|
| 22 | 22 | return $term_id_pair; |
| 23 | 23 | } |
| 24 | 24 | return $term_id_pair['term_id']; |
| 25 | 25 | } |
| 26 | 26 | |
| 27 | - function update_object( $term, $fields ) { |
|
| 28 | - $fields = array_merge( array( 'taxonomy' => $this->taxonomy ), $fields ); |
|
| 29 | - if ( is_object( $term ) ) { |
|
| 27 | + function update_object($term, $fields) { |
|
| 28 | + $fields = array_merge(array('taxonomy' => $this->taxonomy), $fields); |
|
| 29 | + if (is_object($term)) { |
|
| 30 | 30 | $taxonomy = $term->taxonomy; |
| 31 | 31 | } |
| 32 | - $term_id_pair = wp_update_term( $term, $taxonomy, $fields ); |
|
| 32 | + $term_id_pair = wp_update_term($term, $taxonomy, $fields); |
|
| 33 | 33 | return $term_id_pair['term_id']; |
| 34 | 34 | } |
| 35 | 35 | |
| 36 | - function add_post_terms( $post_id, $terms, $taxonomy, $append = true ) { |
|
| 37 | - return wp_set_post_terms( $post_id, $terms, $taxonomy, $append ); |
|
| 36 | + function add_post_terms($post_id, $terms, $taxonomy, $append = true) { |
|
| 37 | + return wp_set_post_terms($post_id, $terms, $taxonomy, $append); |
|
| 38 | 38 | } |
| 39 | 39 | |
| 40 | - function create_and_get( $args = array(), $generation_definitions = null ) { |
|
| 41 | - $term_id = $this->create( $args, $generation_definitions ); |
|
| 42 | - $taxonomy = isset( $args['taxonomy'] ) ? $args['taxonomy'] : $this->taxonomy; |
|
| 43 | - return get_term( $term_id, $taxonomy ); |
|
| 40 | + function create_and_get($args = array(), $generation_definitions = null) { |
|
| 41 | + $term_id = $this->create($args, $generation_definitions); |
|
| 42 | + $taxonomy = isset($args['taxonomy']) ? $args['taxonomy'] : $this->taxonomy; |
|
| 43 | + return get_term($term_id, $taxonomy); |
|
| 44 | 44 | } |
| 45 | 45 | |
| 46 | - function get_object_by_id( $term_id ) { |
|
| 47 | - return get_term( $term_id, $this->taxonomy ); |
|
| 46 | + function get_object_by_id($term_id) { |
|
| 47 | + return get_term($term_id, $this->taxonomy); |
|
| 48 | 48 | } |
| 49 | 49 | } |
@@ -52,6 +52,9 @@ |
||
| 52 | 52 | |
| 53 | 53 | abstract function get_object_by_id( $object_id ); |
| 54 | 54 | |
| 55 | + /** |
|
| 56 | + * @param integer $count |
|
| 57 | + */ |
|
| 55 | 58 | function create_many( $count, $args = array(), $generation_definitions = null ) { |
| 56 | 59 | $results = array(); |
| 57 | 60 | for ( $i = 0; $i < $count; $i++ ) { |
@@ -5,114 +5,114 @@ |
||
| 5 | 5 | */ |
| 6 | 6 | abstract class WP_UnitTest_Factory_For_Thing { |
| 7 | 7 | |
| 8 | - var $default_generation_definitions; |
|
| 9 | - var $factory; |
|
| 10 | - |
|
| 11 | - /** |
|
| 12 | - * Creates a new factory, which will create objects of a specific Thing |
|
| 13 | - * |
|
| 14 | - * @param object $factory Global factory that can be used to create other objects on the system |
|
| 15 | - * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values |
|
| 16 | - * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence}, |
|
| 17 | - * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}. |
|
| 18 | - */ |
|
| 19 | - function __construct( $factory, $default_generation_definitions = array() ) { |
|
| 20 | - $this->factory = $factory; |
|
| 21 | - $this->default_generation_definitions = $default_generation_definitions; |
|
| 22 | - } |
|
| 23 | - |
|
| 24 | - abstract function create_object( $args ); |
|
| 25 | - abstract function update_object( $object, $fields ); |
|
| 26 | - |
|
| 27 | - function create( $args = array(), $generation_definitions = null ) { |
|
| 28 | - if ( is_null( $generation_definitions ) ) { |
|
| 29 | - $generation_definitions = $this->default_generation_definitions; |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks ); |
|
| 33 | - $created = $this->create_object( $generated_args ); |
|
| 34 | - if ( !$created || is_wp_error( $created ) ) { |
|
| 35 | - return $created; |
|
| 36 | - } |
|
| 37 | - |
|
| 38 | - if ( $callbacks ) { |
|
| 39 | - $updated_fields = $this->apply_callbacks( $callbacks, $created ); |
|
| 40 | - $save_result = $this->update_object( $created, $updated_fields ); |
|
| 41 | - if ( !$save_result || is_wp_error( $save_result ) ) { |
|
| 42 | - return $save_result; |
|
| 43 | - } |
|
| 44 | - } |
|
| 45 | - return $created; |
|
| 46 | - } |
|
| 47 | - |
|
| 48 | - function create_and_get( $args = array(), $generation_definitions = null ) { |
|
| 49 | - $object_id = $this->create( $args, $generation_definitions ); |
|
| 50 | - return $this->get_object_by_id( $object_id ); |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - abstract function get_object_by_id( $object_id ); |
|
| 54 | - |
|
| 55 | - function create_many( $count, $args = array(), $generation_definitions = null ) { |
|
| 56 | - $results = array(); |
|
| 57 | - for ( $i = 0; $i < $count; $i++ ) { |
|
| 58 | - $results[] = $this->create( $args, $generation_definitions ); |
|
| 59 | - } |
|
| 60 | - return $results; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { |
|
| 64 | - $callbacks = array(); |
|
| 65 | - if ( is_null( $generation_definitions ) ) { |
|
| 66 | - $generation_definitions = $this->default_generation_definitions; |
|
| 67 | - } |
|
| 68 | - |
|
| 69 | - // Use the same incrementor for all fields belonging to this object. |
|
| 70 | - $gen = new WP_UnitTest_Generator_Sequence(); |
|
| 71 | - $incr = $gen->get_incr(); |
|
| 72 | - |
|
| 73 | - foreach( array_keys( $generation_definitions ) as $field_name ) { |
|
| 74 | - if ( !isset( $args[$field_name] ) ) { |
|
| 75 | - $generator = $generation_definitions[$field_name]; |
|
| 76 | - if ( is_scalar( $generator ) ) { |
|
| 77 | - $args[$field_name] = $generator; |
|
| 78 | - } elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) { |
|
| 79 | - $callbacks[$field_name] = $generator; |
|
| 80 | - } elseif ( is_object( $generator ) ) { |
|
| 81 | - $args[ $field_name ] = sprintf( $generator->get_template_string(), $incr ); |
|
| 82 | - } else { |
|
| 83 | - return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' ); |
|
| 84 | - } |
|
| 85 | - } |
|
| 86 | - } |
|
| 87 | - |
|
| 88 | - return $args; |
|
| 89 | - } |
|
| 90 | - |
|
| 91 | - function apply_callbacks( $callbacks, $created ) { |
|
| 92 | - $updated_fields = array(); |
|
| 93 | - foreach( $callbacks as $field_name => $generator ) { |
|
| 94 | - $updated_fields[$field_name] = $generator->call( $created ); |
|
| 95 | - } |
|
| 96 | - return $updated_fields; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - function callback( $function ) { |
|
| 100 | - return new WP_UnitTest_Factory_Callback_After_Create( $function ); |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - function addslashes_deep($value) { |
|
| 104 | - if ( is_array( $value ) ) { |
|
| 105 | - $value = array_map( array( $this, 'addslashes_deep' ), $value ); |
|
| 106 | - } elseif ( is_object( $value ) ) { |
|
| 107 | - $vars = get_object_vars( $value ); |
|
| 108 | - foreach ($vars as $key=>$data) { |
|
| 109 | - $value->{$key} = $this->addslashes_deep( $data ); |
|
| 110 | - } |
|
| 111 | - } elseif ( is_string( $value ) ) { |
|
| 112 | - $value = addslashes( $value ); |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - return $value; |
|
| 116 | - } |
|
| 8 | + var $default_generation_definitions; |
|
| 9 | + var $factory; |
|
| 10 | + |
|
| 11 | + /** |
|
| 12 | + * Creates a new factory, which will create objects of a specific Thing |
|
| 13 | + * |
|
| 14 | + * @param object $factory Global factory that can be used to create other objects on the system |
|
| 15 | + * @param array $default_generation_definitions Defines what default values should the properties of the object have. The default values |
|
| 16 | + * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence}, |
|
| 17 | + * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}. |
|
| 18 | + */ |
|
| 19 | + function __construct( $factory, $default_generation_definitions = array() ) { |
|
| 20 | + $this->factory = $factory; |
|
| 21 | + $this->default_generation_definitions = $default_generation_definitions; |
|
| 22 | + } |
|
| 23 | + |
|
| 24 | + abstract function create_object( $args ); |
|
| 25 | + abstract function update_object( $object, $fields ); |
|
| 26 | + |
|
| 27 | + function create( $args = array(), $generation_definitions = null ) { |
|
| 28 | + if ( is_null( $generation_definitions ) ) { |
|
| 29 | + $generation_definitions = $this->default_generation_definitions; |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks ); |
|
| 33 | + $created = $this->create_object( $generated_args ); |
|
| 34 | + if ( !$created || is_wp_error( $created ) ) { |
|
| 35 | + return $created; |
|
| 36 | + } |
|
| 37 | + |
|
| 38 | + if ( $callbacks ) { |
|
| 39 | + $updated_fields = $this->apply_callbacks( $callbacks, $created ); |
|
| 40 | + $save_result = $this->update_object( $created, $updated_fields ); |
|
| 41 | + if ( !$save_result || is_wp_error( $save_result ) ) { |
|
| 42 | + return $save_result; |
|
| 43 | + } |
|
| 44 | + } |
|
| 45 | + return $created; |
|
| 46 | + } |
|
| 47 | + |
|
| 48 | + function create_and_get( $args = array(), $generation_definitions = null ) { |
|
| 49 | + $object_id = $this->create( $args, $generation_definitions ); |
|
| 50 | + return $this->get_object_by_id( $object_id ); |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + abstract function get_object_by_id( $object_id ); |
|
| 54 | + |
|
| 55 | + function create_many( $count, $args = array(), $generation_definitions = null ) { |
|
| 56 | + $results = array(); |
|
| 57 | + for ( $i = 0; $i < $count; $i++ ) { |
|
| 58 | + $results[] = $this->create( $args, $generation_definitions ); |
|
| 59 | + } |
|
| 60 | + return $results; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { |
|
| 64 | + $callbacks = array(); |
|
| 65 | + if ( is_null( $generation_definitions ) ) { |
|
| 66 | + $generation_definitions = $this->default_generation_definitions; |
|
| 67 | + } |
|
| 68 | + |
|
| 69 | + // Use the same incrementor for all fields belonging to this object. |
|
| 70 | + $gen = new WP_UnitTest_Generator_Sequence(); |
|
| 71 | + $incr = $gen->get_incr(); |
|
| 72 | + |
|
| 73 | + foreach( array_keys( $generation_definitions ) as $field_name ) { |
|
| 74 | + if ( !isset( $args[$field_name] ) ) { |
|
| 75 | + $generator = $generation_definitions[$field_name]; |
|
| 76 | + if ( is_scalar( $generator ) ) { |
|
| 77 | + $args[$field_name] = $generator; |
|
| 78 | + } elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) { |
|
| 79 | + $callbacks[$field_name] = $generator; |
|
| 80 | + } elseif ( is_object( $generator ) ) { |
|
| 81 | + $args[ $field_name ] = sprintf( $generator->get_template_string(), $incr ); |
|
| 82 | + } else { |
|
| 83 | + return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' ); |
|
| 84 | + } |
|
| 85 | + } |
|
| 86 | + } |
|
| 87 | + |
|
| 88 | + return $args; |
|
| 89 | + } |
|
| 90 | + |
|
| 91 | + function apply_callbacks( $callbacks, $created ) { |
|
| 92 | + $updated_fields = array(); |
|
| 93 | + foreach( $callbacks as $field_name => $generator ) { |
|
| 94 | + $updated_fields[$field_name] = $generator->call( $created ); |
|
| 95 | + } |
|
| 96 | + return $updated_fields; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + function callback( $function ) { |
|
| 100 | + return new WP_UnitTest_Factory_Callback_After_Create( $function ); |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + function addslashes_deep($value) { |
|
| 104 | + if ( is_array( $value ) ) { |
|
| 105 | + $value = array_map( array( $this, 'addslashes_deep' ), $value ); |
|
| 106 | + } elseif ( is_object( $value ) ) { |
|
| 107 | + $vars = get_object_vars( $value ); |
|
| 108 | + foreach ($vars as $key=>$data) { |
|
| 109 | + $value->{$key} = $this->addslashes_deep( $data ); |
|
| 110 | + } |
|
| 111 | + } elseif ( is_string( $value ) ) { |
|
| 112 | + $value = addslashes( $value ); |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + return $value; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | 118 | } |
@@ -16,53 +16,53 @@ discard block |
||
| 16 | 16 | * can be generators -- an object with next() method. There are some default generators: {@link WP_UnitTest_Generator_Sequence}, |
| 17 | 17 | * {@link WP_UnitTest_Generator_Locale_Name}, {@link WP_UnitTest_Factory_Callback_After_Create}. |
| 18 | 18 | */ |
| 19 | - function __construct( $factory, $default_generation_definitions = array() ) { |
|
| 19 | + function __construct($factory, $default_generation_definitions = array()) { |
|
| 20 | 20 | $this->factory = $factory; |
| 21 | 21 | $this->default_generation_definitions = $default_generation_definitions; |
| 22 | 22 | } |
| 23 | 23 | |
| 24 | - abstract function create_object( $args ); |
|
| 25 | - abstract function update_object( $object, $fields ); |
|
| 24 | + abstract function create_object($args); |
|
| 25 | + abstract function update_object($object, $fields); |
|
| 26 | 26 | |
| 27 | - function create( $args = array(), $generation_definitions = null ) { |
|
| 28 | - if ( is_null( $generation_definitions ) ) { |
|
| 27 | + function create($args = array(), $generation_definitions = null) { |
|
| 28 | + if (is_null($generation_definitions)) { |
|
| 29 | 29 | $generation_definitions = $this->default_generation_definitions; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - $generated_args = $this->generate_args( $args, $generation_definitions, $callbacks ); |
|
| 33 | - $created = $this->create_object( $generated_args ); |
|
| 34 | - if ( !$created || is_wp_error( $created ) ) { |
|
| 32 | + $generated_args = $this->generate_args($args, $generation_definitions, $callbacks); |
|
| 33 | + $created = $this->create_object($generated_args); |
|
| 34 | + if ( ! $created || is_wp_error($created)) { |
|
| 35 | 35 | return $created; |
| 36 | 36 | } |
| 37 | 37 | |
| 38 | - if ( $callbacks ) { |
|
| 39 | - $updated_fields = $this->apply_callbacks( $callbacks, $created ); |
|
| 40 | - $save_result = $this->update_object( $created, $updated_fields ); |
|
| 41 | - if ( !$save_result || is_wp_error( $save_result ) ) { |
|
| 38 | + if ($callbacks) { |
|
| 39 | + $updated_fields = $this->apply_callbacks($callbacks, $created); |
|
| 40 | + $save_result = $this->update_object($created, $updated_fields); |
|
| 41 | + if ( ! $save_result || is_wp_error($save_result)) { |
|
| 42 | 42 | return $save_result; |
| 43 | 43 | } |
| 44 | 44 | } |
| 45 | 45 | return $created; |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | - function create_and_get( $args = array(), $generation_definitions = null ) { |
|
| 49 | - $object_id = $this->create( $args, $generation_definitions ); |
|
| 50 | - return $this->get_object_by_id( $object_id ); |
|
| 48 | + function create_and_get($args = array(), $generation_definitions = null) { |
|
| 49 | + $object_id = $this->create($args, $generation_definitions); |
|
| 50 | + return $this->get_object_by_id($object_id); |
|
| 51 | 51 | } |
| 52 | 52 | |
| 53 | - abstract function get_object_by_id( $object_id ); |
|
| 53 | + abstract function get_object_by_id($object_id); |
|
| 54 | 54 | |
| 55 | - function create_many( $count, $args = array(), $generation_definitions = null ) { |
|
| 55 | + function create_many($count, $args = array(), $generation_definitions = null) { |
|
| 56 | 56 | $results = array(); |
| 57 | - for ( $i = 0; $i < $count; $i++ ) { |
|
| 58 | - $results[] = $this->create( $args, $generation_definitions ); |
|
| 57 | + for ($i = 0; $i < $count; $i++) { |
|
| 58 | + $results[] = $this->create($args, $generation_definitions); |
|
| 59 | 59 | } |
| 60 | 60 | return $results; |
| 61 | 61 | } |
| 62 | 62 | |
| 63 | - function generate_args( $args = array(), $generation_definitions = null, &$callbacks = null ) { |
|
| 63 | + function generate_args($args = array(), $generation_definitions = null, &$callbacks = null) { |
|
| 64 | 64 | $callbacks = array(); |
| 65 | - if ( is_null( $generation_definitions ) ) { |
|
| 65 | + if (is_null($generation_definitions)) { |
|
| 66 | 66 | $generation_definitions = $this->default_generation_definitions; |
| 67 | 67 | } |
| 68 | 68 | |
@@ -70,17 +70,17 @@ discard block |
||
| 70 | 70 | $gen = new WP_UnitTest_Generator_Sequence(); |
| 71 | 71 | $incr = $gen->get_incr(); |
| 72 | 72 | |
| 73 | - foreach( array_keys( $generation_definitions ) as $field_name ) { |
|
| 74 | - if ( !isset( $args[$field_name] ) ) { |
|
| 73 | + foreach (array_keys($generation_definitions) as $field_name) { |
|
| 74 | + if ( ! isset($args[$field_name])) { |
|
| 75 | 75 | $generator = $generation_definitions[$field_name]; |
| 76 | - if ( is_scalar( $generator ) ) { |
|
| 76 | + if (is_scalar($generator)) { |
|
| 77 | 77 | $args[$field_name] = $generator; |
| 78 | - } elseif ( is_object( $generator ) && method_exists( $generator, 'call' ) ) { |
|
| 78 | + } elseif (is_object($generator) && method_exists($generator, 'call')) { |
|
| 79 | 79 | $callbacks[$field_name] = $generator; |
| 80 | - } elseif ( is_object( $generator ) ) { |
|
| 81 | - $args[ $field_name ] = sprintf( $generator->get_template_string(), $incr ); |
|
| 80 | + } elseif (is_object($generator)) { |
|
| 81 | + $args[$field_name] = sprintf($generator->get_template_string(), $incr); |
|
| 82 | 82 | } else { |
| 83 | - return new WP_Error( 'invalid_argument', 'Factory default value should be either a scalar or an generator object.' ); |
|
| 83 | + return new WP_Error('invalid_argument', 'Factory default value should be either a scalar or an generator object.'); |
|
| 84 | 84 | } |
| 85 | 85 | } |
| 86 | 86 | } |
@@ -88,28 +88,28 @@ discard block |
||
| 88 | 88 | return $args; |
| 89 | 89 | } |
| 90 | 90 | |
| 91 | - function apply_callbacks( $callbacks, $created ) { |
|
| 91 | + function apply_callbacks($callbacks, $created) { |
|
| 92 | 92 | $updated_fields = array(); |
| 93 | - foreach( $callbacks as $field_name => $generator ) { |
|
| 94 | - $updated_fields[$field_name] = $generator->call( $created ); |
|
| 93 | + foreach ($callbacks as $field_name => $generator) { |
|
| 94 | + $updated_fields[$field_name] = $generator->call($created); |
|
| 95 | 95 | } |
| 96 | 96 | return $updated_fields; |
| 97 | 97 | } |
| 98 | 98 | |
| 99 | - function callback( $function ) { |
|
| 100 | - return new WP_UnitTest_Factory_Callback_After_Create( $function ); |
|
| 99 | + function callback($function) { |
|
| 100 | + return new WP_UnitTest_Factory_Callback_After_Create($function); |
|
| 101 | 101 | } |
| 102 | 102 | |
| 103 | 103 | function addslashes_deep($value) { |
| 104 | - if ( is_array( $value ) ) { |
|
| 105 | - $value = array_map( array( $this, 'addslashes_deep' ), $value ); |
|
| 106 | - } elseif ( is_object( $value ) ) { |
|
| 107 | - $vars = get_object_vars( $value ); |
|
| 104 | + if (is_array($value)) { |
|
| 105 | + $value = array_map(array($this, 'addslashes_deep'), $value); |
|
| 106 | + } elseif (is_object($value)) { |
|
| 107 | + $vars = get_object_vars($value); |
|
| 108 | 108 | foreach ($vars as $key=>$data) { |
| 109 | - $value->{$key} = $this->addslashes_deep( $data ); |
|
| 109 | + $value->{$key} = $this->addslashes_deep($data); |
|
| 110 | 110 | } |
| 111 | - } elseif ( is_string( $value ) ) { |
|
| 112 | - $value = addslashes( $value ); |
|
| 111 | + } elseif (is_string($value)) { |
|
| 112 | + $value = addslashes($value); |
|
| 113 | 113 | } |
| 114 | 114 | |
| 115 | 115 | return $value; |