importer.php 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223
  1. <?php
  2. /**
  3. * VamTam Content Importer
  4. */
  5. if ( ! defined( 'WP_LOAD_IMPORTERS' ) )
  6. return;
  7. /** Display verbose errors */
  8. if ( ! defined( 'IMPORT_DEBUG' ) ) {
  9. define( 'IMPORT_DEBUG', false );
  10. }
  11. // Load Importer API
  12. require_once ABSPATH . 'wp-admin/includes/import.php';
  13. if ( ! class_exists( 'WP_Importer' ) ) {
  14. $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php';
  15. if ( file_exists( $class_wp_importer ) )
  16. require $class_wp_importer;
  17. }
  18. // include WXR file parsers
  19. require dirname( __FILE__ ) . '/parsers.php';
  20. /**
  21. * WordPress Importer class for managing the import process of a WXR file
  22. *
  23. * @package Importer
  24. */
  25. if ( class_exists( 'WP_Importer' ) ) {
  26. class Vamtam_Import extends WP_Importer {
  27. var $max_wxr_version = 1.1; // max. supported WXR version
  28. var $id; // WXR attachment ID
  29. // information to import from WXR file
  30. var $version;
  31. var $authors = array();
  32. var $posts = array();
  33. var $terms = array();
  34. var $categories = array();
  35. var $tags = array();
  36. var $base_url = '';
  37. var $front_page = 0;
  38. var $woocommerce_pages = 0;
  39. // mappings from old information to new
  40. var $processed_authors = array();
  41. var $author_mapping = array();
  42. var $processed_terms = array();
  43. var $processed_posts = array();
  44. var $processed_attachments = array();
  45. var $post_orphans = array();
  46. var $processed_menu_items = array();
  47. var $menu_item_orphans = array();
  48. var $missing_menu_items = array();
  49. var $fetch_attachments = false;
  50. var $url_remap = array();
  51. var $featured_images = array();
  52. function __construct() {
  53. /* nothing */ }
  54. /**
  55. * Registered callback function for the WordPress Importer
  56. *
  57. * Manages the three separate stages of the WXR import process
  58. */
  59. function dispatch() {
  60. $this->header();
  61. $step = empty( $_GET['step'] ) ? 0 : $_GET['step'];
  62. check_admin_referer( 'vamtam-import' );
  63. $file = VAMTAM_SAMPLES_DIR . 'content.xml';
  64. $this->fetch_attachments = ( $this->allow_fetch_attachments() );
  65. $this->one_click = true;
  66. update_option( 'vamtam-used-one-click-import', true );
  67. set_time_limit( 0 );
  68. $this->import( $file );
  69. $this->footer();
  70. }
  71. /**
  72. * The main controller for the actual import stage.
  73. *
  74. * @param string $file Path to the WXR file for importing
  75. */
  76. function import( $file ) {
  77. global $vamtam_defaults;
  78. add_filter( 'import_post_meta_key', array( $this, 'is_valid_meta_key' ) );
  79. add_filter( 'http_request_timeout', array( &$this, 'bump_request_timeout' ) );
  80. do_action( 'vamtam_before_content_import' );
  81. $start = microtime( true );
  82. $this->import_start( $file );
  83. $this->get_author_mapping();
  84. wp_suspend_cache_invalidation( true );
  85. $this->process_categories();
  86. $this->process_tags();
  87. $this->process_terms();
  88. $this->process_posts();
  89. wp_suspend_cache_invalidation( false );
  90. if ( get_option( 'vamtam_theme', false ) === false && is_array( $vamtam_defaults ) ) {
  91. update_option( 'vamtam_theme', $vamtam_defaults );
  92. }
  93. // update incorrect/missing information in the DB
  94. $this->backfill_parents();
  95. $this->backfill_attachment_urls();
  96. $this->remap_featured_images();
  97. $this->import_end();
  98. $end = microtime( true );
  99. echo 'Parse and Import: ' . ( $end - $start ) . "s\n";
  100. do_action( 'vamtam_after_content_import' );
  101. }
  102. /**
  103. * Parses the WXR file and prepares us for the task of processing parsed data
  104. *
  105. * @param string $file Path to the WXR file for importing
  106. */
  107. function import_start( $file ) {
  108. if ( ! is_file( $file ) ) {
  109. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  110. echo esc_html__( 'The file does not exist, please try again.', 'wordpress-importer' ) . '</p>';
  111. $this->footer();
  112. die();
  113. }
  114. $import_data = $this->parse( $file );
  115. if ( is_wp_error( $import_data ) ) {
  116. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  117. echo esc_html( $import_data->get_error_message() ) . '</p>';
  118. $this->footer();
  119. die();
  120. }
  121. $this->version = $import_data['version'];
  122. $this->get_authors_from_import( $import_data );
  123. $this->posts = $import_data['posts'];
  124. $this->terms = $import_data['terms'];
  125. $this->categories = $import_data['categories'];
  126. $this->tags = $import_data['tags'];
  127. $this->base_url = esc_url( $import_data['base_url'] );
  128. if ( isset( $import_data['frontpage'] ) ) {
  129. $this->front_page = $import_data['frontpage'];
  130. }
  131. if ( isset( $import_data['woocommerce_pages'] ) ) {
  132. $this->woocommerce_pages = $import_data['woocommerce_pages'];
  133. }
  134. wp_defer_term_counting( true );
  135. wp_defer_comment_counting( true );
  136. do_action( 'import_start' );
  137. }
  138. /**
  139. * Performs post-import cleanup of files and the cache
  140. */
  141. function import_end() {
  142. wp_import_cleanup( $this->id );
  143. wp_cache_flush();
  144. foreach ( get_taxonomies() as $tax ) {
  145. delete_option( "{$tax}_children" );
  146. _get_term_hierarchy( $tax );
  147. }
  148. wp_defer_term_counting( false );
  149. wp_defer_comment_counting( false );
  150. echo '<p>' . esc_html__( 'All done.', 'wordpress-importer' ) . ' <a href="' . esc_url( admin_url() ) . '">' . esc_html__( 'Have fun!', 'wordpress-importer' ) . '</a></p>';
  151. echo '<p>' . esc_html__( 'Remember to update the passwords and roles of imported users.', 'wordpress-importer' ) . '</p>';
  152. if ( $this->one_click ) {
  153. if ( $this->front_page ) {
  154. update_option( 'page_on_front', $this->processed_posts[ intval( $this->front_page ) ] );
  155. update_option( 'show_on_front', 'page' );
  156. }
  157. if ( $this->woocommerce_pages ) {
  158. $wc_pages = json_decode( $this->woocommerce_pages );
  159. foreach ( $wc_pages as $option => $pageid ) {
  160. if ( isset( $this->processed_posts[ intval( $pageid ) ] ) ) {
  161. update_option( $option, $this->processed_posts[ intval( $pageid ) ] );
  162. }
  163. }
  164. }
  165. update_option( 'vamtam_last_import_map', array(
  166. 'posts' => $this->processed_posts,
  167. 'terms' => $this->processed_terms,
  168. 'authors' => $this->processed_authors,
  169. 'attachments' => $this->processed_attachments,
  170. ) );
  171. }
  172. do_action( 'import_end' );
  173. }
  174. /**
  175. * Handles the WXR upload and initial parsing of the file to prepare for
  176. * displaying author import options
  177. *
  178. * @return bool False if error uploading or invalid file, true otherwise
  179. */
  180. function handle_upload() {
  181. $file = wp_import_handle_upload();
  182. if ( isset( $file['error'] ) ) {
  183. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  184. echo esc_html( $file['error'] ) . '</p>';
  185. return false;
  186. }
  187. $this->id = (int) $file['id'];
  188. $import_data = $this->parse( $file['file'] );
  189. if ( is_wp_error( $import_data ) ) {
  190. echo '<p><strong>' . esc_html__( 'Sorry, there has been an error.', 'wordpress-importer' ) . '</strong><br />';
  191. echo esc_html( $import_data->get_error_message() ) . '</p>';
  192. return false;
  193. }
  194. $this->version = $import_data['version'];
  195. if ( $this->version > $this->max_wxr_version ) {
  196. echo '<div class="error"><p><strong>';
  197. printf( esc_html__( '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'] ) );
  198. echo '</strong></p></div>';
  199. }
  200. $this->get_authors_from_import( $import_data );
  201. return true;
  202. }
  203. /**
  204. * Retrieve authors from parsed WXR data
  205. *
  206. * Uses the provided author information from WXR 1.1 files
  207. * or extracts info from each post for WXR 1.0 files
  208. *
  209. * @param array $import_data Data returned by a WXR parser
  210. */
  211. function get_authors_from_import( $import_data ) {
  212. if ( ! empty( $import_data['authors'] ) ) {
  213. $this->authors = $import_data['authors'];
  214. // no author information, grab it from the posts
  215. } else {
  216. foreach ( $import_data['posts'] as $post ) {
  217. $login = sanitize_user( $post['post_author'], true );
  218. if ( empty( $login ) ) {
  219. printf( esc_html__( 'Failed to import author %s. Their posts will be attributed to the current user.', 'wordpress-importer' ), esc_html( $post['post_author'] ) ); echo '<br />';
  220. continue;
  221. }
  222. if ( ! isset( $this->authors[ $login ] ) )
  223. $this->authors[ $login ] = array(
  224. 'author_login' => $login,
  225. 'author_display_name' => $post['post_author'],
  226. );
  227. }
  228. }
  229. }
  230. /**
  231. * Map old author logins to local user IDs based on decisions made
  232. * in import options form. Can map to an existing user, create a new user
  233. * or falls back to the current user in case of error with either of the previous
  234. */
  235. function get_author_mapping() {
  236. if ( ! isset( $_POST['imported_authors'] ) )
  237. return;
  238. $create_users = $this->allow_create_users();
  239. foreach ( (array) $_POST['imported_authors'] as $i => $old_login ) {
  240. $old_id = isset( $this->authors[ $old_login ]['author_id'] ) ? intval( $this->authors[ $old_login ]['author_id'] ) : false;
  241. if ( ! empty( $_POST['user_map'][ $i ] ) ) {
  242. $user = get_userdata( intval( $_POST['user_map'][ $i ] ) );
  243. if ( isset( $user->ID ) ) {
  244. if ( $old_id )
  245. $this->processed_authors[ $old_id ] = $user->ID;
  246. $this->author_mapping[ $old_login ] = $user->ID;
  247. }
  248. } else if ( $create_users ) {
  249. if ( ! empty( $_POST['user_new'][ $i ] ) ) {
  250. $user_id = wp_create_user( $_POST['user_new'][ $i ], wp_generate_password() );
  251. } else if ( $this->version != '1.0' ) {
  252. $user_data = array(
  253. 'user_login' => $old_login,
  254. 'user_pass' => wp_generate_password(),
  255. 'user_email' => isset( $this->authors[ $old_login ]['author_email'] ) ? $this->authors[ $old_login ]['author_email'] : '',
  256. 'display_name' => $this->authors[ $old_login ]['author_display_name'],
  257. 'first_name' => isset( $this->authors[ $old_login ]['author_first_name'] ) ? $this->authors[ $old_login ]['author_first_name'] : '',
  258. 'last_name' => isset( $this->authors[ $old_login ]['author_last_name'] ) ? $this->authors[ $old_login ]['author_last_name'] : '',
  259. );
  260. $user_id = wp_insert_user( $user_data );
  261. }
  262. if ( ! is_wp_error( $user_id ) ) {
  263. if ( $old_id )
  264. $this->processed_authors[ $old_id ] = $user_id;
  265. $this->author_mapping[ $old_login ] = $user_id;
  266. } else {
  267. printf( esc_html__( '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'] ) );
  268. if ( defined( 'IMPORT_DEBUG' ) && IMPORT_DEBUG ) {
  269. echo ' ' . $user_id->get_error_message(); // xss ok
  270. }
  271. echo '<br />';
  272. }
  273. }
  274. // failsafe: if the user_id was invalid, default to the current user
  275. if ( ! isset( $this->author_mapping[ $old_login ] ) ) {
  276. if ( $old_id )
  277. $this->processed_authors[ $old_id ] = (int) get_current_user_id();
  278. $this->author_mapping[ $old_login ] = (int) get_current_user_id();
  279. }
  280. }
  281. }
  282. /**
  283. * Create new categories based on import information
  284. *
  285. * Doesn't create a new category if its slug already exists
  286. */
  287. function process_categories() {
  288. $this->categories = apply_filters( 'wp_import_categories', $this->categories );
  289. if ( empty( $this->categories ) )
  290. return;
  291. foreach ( $this->categories as $cat ) {
  292. // if the category already exists leave it alone
  293. $term_id = term_exists( $cat['category_nicename'], 'category' );
  294. if ( $term_id ) {
  295. if ( is_array($term_id) ) $term_id = $term_id['term_id'];
  296. if ( isset($cat['term_id']) )
  297. $this->processed_terms[intval($cat['term_id'])] = (int) $term_id;
  298. continue;
  299. }
  300. $category_parent = empty( $cat['category_parent'] ) ? 0 : category_exists( $cat['category_parent'] );
  301. $category_description = isset( $cat['category_description'] ) ? $cat['category_description'] : '';
  302. $catarr = array(
  303. 'category_nicename' => $cat['category_nicename'],
  304. 'category_parent' => $category_parent,
  305. 'cat_name' => $cat['cat_name'],
  306. 'category_description' => $category_description
  307. );
  308. $catarr = wp_slash( $catarr );
  309. $id = wp_insert_category( $catarr );
  310. if ( ! is_wp_error( $id ) ) {
  311. if ( isset($cat['term_id']) )
  312. $this->processed_terms[intval($cat['term_id'])] = $id;
  313. } else {
  314. printf( __( 'Failed to import category %s', 'wordpress-importer' ), esc_html($cat['category_nicename']) );
  315. if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
  316. echo ': ' . $id->get_error_message();
  317. echo '<br />';
  318. continue;
  319. }
  320. $this->process_termmeta( $cat, $id['term_id'] );
  321. }
  322. unset( $this->categories );
  323. }
  324. /**
  325. * Create new post tags based on import information
  326. *
  327. * Doesn't create a tag if its slug already exists
  328. */
  329. function process_tags() {
  330. $this->tags = apply_filters( 'wp_import_tags', $this->tags );
  331. if ( empty( $this->tags ) )
  332. return;
  333. foreach ( $this->tags as $tag ) {
  334. // if the tag already exists leave it alone
  335. $term_id = term_exists( $tag['tag_slug'], 'post_tag' );
  336. if ( $term_id ) {
  337. if ( is_array($term_id) ) $term_id = $term_id['term_id'];
  338. if ( isset($tag['term_id']) )
  339. $this->processed_terms[intval($tag['term_id'])] = (int) $term_id;
  340. continue;
  341. }
  342. $tag = wp_slash( $tag );
  343. $tag_desc = isset( $tag['tag_description'] ) ? $tag['tag_description'] : '';
  344. $tagarr = array( 'slug' => $tag['tag_slug'], 'description' => $tag_desc );
  345. $id = wp_insert_term( $tag['tag_name'], 'post_tag', $tagarr );
  346. if ( ! is_wp_error( $id ) ) {
  347. if ( isset($tag['term_id']) )
  348. $this->processed_terms[intval($tag['term_id'])] = $id['term_id'];
  349. } else {
  350. printf( __( 'Failed to import post tag %s', 'wordpress-importer' ), esc_html($tag['tag_name']) );
  351. if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
  352. echo ': ' . $id->get_error_message();
  353. echo '<br />';
  354. continue;
  355. }
  356. $this->process_termmeta( $tag, $id['term_id'] );
  357. }
  358. unset( $this->tags );
  359. }
  360. /**
  361. * Create new terms based on import information
  362. *
  363. * Doesn't create a term its slug already exists
  364. */
  365. function process_terms() {
  366. $this->terms = apply_filters( 'wp_import_terms', $this->terms );
  367. if ( empty( $this->terms ) )
  368. return;
  369. foreach ( $this->terms as $term ) {
  370. // if the term already exists in the correct taxonomy leave it alone
  371. $term_id = term_exists( $term['slug'], $term['term_taxonomy'] );
  372. if ( $term_id ) {
  373. if ( is_array($term_id) ) $term_id = $term_id['term_id'];
  374. if ( isset($term['term_id']) )
  375. $this->processed_terms[intval($term['term_id'])] = (int) $term_id;
  376. continue;
  377. }
  378. if ( empty( $term['term_parent'] ) ) {
  379. $parent = 0;
  380. } else {
  381. $parent = term_exists( $term['term_parent'], $term['term_taxonomy'] );
  382. if ( is_array( $parent ) ) $parent = $parent['term_id'];
  383. }
  384. $term = wp_slash( $term );
  385. $description = isset( $term['term_description'] ) ? $term['term_description'] : '';
  386. $termarr = array( 'slug' => $term['slug'], 'description' => $description, 'parent' => intval($parent) );
  387. $id = wp_insert_term( $term['term_name'], $term['term_taxonomy'], $termarr );
  388. if ( ! is_wp_error( $id ) ) {
  389. if ( isset($term['term_id']) )
  390. $this->processed_terms[intval($term['term_id'])] = $id['term_id'];
  391. } else {
  392. printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($term['term_taxonomy']), esc_html($term['term_name']) );
  393. if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
  394. echo ': ' . $id->get_error_message();
  395. echo '<br />';
  396. continue;
  397. }
  398. $this->process_termmeta( $term, $id['term_id'] );
  399. }
  400. unset( $this->terms );
  401. }
  402. /**
  403. * Add metadata to imported term.
  404. *
  405. * @since 0.6.2
  406. *
  407. * @param array $term Term data from WXR import.
  408. * @param int $term_id ID of the newly created term.
  409. */
  410. protected function process_termmeta( $term, $term_id ) {
  411. if ( ! isset( $term['termmeta'] ) ) {
  412. $term['termmeta'] = array();
  413. }
  414. /**
  415. * Filters the metadata attached to an imported term.
  416. *
  417. * @since 0.6.2
  418. *
  419. * @param array $termmeta Array of term meta.
  420. * @param int $term_id ID of the newly created term.
  421. * @param array $term Term data from the WXR import.
  422. */
  423. $term['termmeta'] = apply_filters( 'wp_import_term_meta', $term['termmeta'], $term_id, $term );
  424. if ( empty( $term['termmeta'] ) ) {
  425. return;
  426. }
  427. foreach ( $term['termmeta'] as $meta ) {
  428. /**
  429. * Filters the meta key for an imported piece of term meta.
  430. *
  431. * @since 0.6.2
  432. *
  433. * @param string $meta_key Meta key.
  434. * @param int $term_id ID of the newly created term.
  435. * @param array $term Term data from the WXR import.
  436. */
  437. $key = apply_filters( 'import_term_meta_key', $meta['key'], $term_id, $term );
  438. if ( ! $key ) {
  439. continue;
  440. }
  441. // Export gets meta straight from the DB so could have a serialized string
  442. $value = maybe_unserialize( $meta['value'] );
  443. add_term_meta( $term_id, $key, $value );
  444. /**
  445. * Fires after term meta is imported.
  446. *
  447. * @since 0.6.2
  448. *
  449. * @param int $term_id ID of the newly created term.
  450. * @param string $key Meta key.
  451. * @param mixed $value Meta value.
  452. */
  453. do_action( 'import_term_meta', $term_id, $key, $value );
  454. }
  455. }
  456. /**
  457. * Create new posts based on import information
  458. *
  459. * Posts marked as having a parent which doesn't exist will become top level items.
  460. * Doesn't create a new post if: the post type doesn't exist, the given post ID
  461. * is already noted as imported or a post with the same title and date already exists.
  462. * Note that new/updated terms, comments and meta are imported for the last of the above.
  463. */
  464. function process_posts() {
  465. $this->posts = apply_filters( 'wp_import_posts', $this->posts );
  466. foreach ( $this->posts as $post ) {
  467. $post = apply_filters( 'wp_import_post_data_raw', $post );
  468. if ( ! post_type_exists( $post['post_type'] ) ) {
  469. printf( __( 'Failed to import &#8220;%s&#8221;: Invalid post type %s', 'wordpress-importer' ),
  470. esc_html($post['post_title']), esc_html($post['post_type']) );
  471. echo '<br />';
  472. do_action( 'wp_import_post_exists', $post );
  473. continue;
  474. }
  475. if ( isset( $this->processed_posts[$post['post_id']] ) && ! empty( $post['post_id'] ) )
  476. continue;
  477. if ( $post['status'] == 'auto-draft' )
  478. continue;
  479. if ( 'nav_menu_item' == $post['post_type'] ) {
  480. $this->process_menu_item( $post );
  481. continue;
  482. }
  483. $post_type_object = get_post_type_object( $post['post_type'] );
  484. $post_exists = post_exists( $post['post_title'], '', $post['post_date'] );
  485. /**
  486. * Filter ID of the existing post corresponding to post currently importing.
  487. *
  488. * Return 0 to force the post to be imported. Filter the ID to be something else
  489. * to override which existing post is mapped to the imported post.
  490. *
  491. * @see post_exists()
  492. * @since 0.6.2
  493. *
  494. * @param int $post_exists Post ID, or 0 if post did not exist.
  495. * @param array $post The post array to be inserted.
  496. */
  497. $post_exists = apply_filters( 'wp_import_existing_post', $post_exists, $post );
  498. if ( $post_exists && get_post_type( $post_exists ) == $post['post_type'] ) {
  499. printf( __('%s &#8220;%s&#8221; already exists.', 'wordpress-importer'), $post_type_object->labels->singular_name, esc_html($post['post_title']) );
  500. echo '<br />';
  501. $comment_post_ID = $post_id = $post_exists;
  502. $this->processed_posts[ intval( $post['post_id'] ) ] = intval( $post_exists );
  503. } else {
  504. $post_parent = (int) $post['post_parent'];
  505. if ( $post_parent ) {
  506. // if we already know the parent, map it to the new local ID
  507. if ( isset( $this->processed_posts[$post_parent] ) ) {
  508. $post_parent = $this->processed_posts[$post_parent];
  509. // otherwise record the parent for later
  510. } else {
  511. $this->post_orphans[intval($post['post_id'])] = $post_parent;
  512. $post_parent = 0;
  513. }
  514. }
  515. // map the post author
  516. $author = sanitize_user( $post['post_author'], true );
  517. if ( isset( $this->author_mapping[$author] ) )
  518. $author = $this->author_mapping[$author];
  519. else
  520. $author = (int) get_current_user_id();
  521. $postdata = array(
  522. 'import_id' => $post['post_id'], 'post_author' => $author, 'post_date' => $post['post_date'],
  523. 'post_date_gmt' => $post['post_date_gmt'], 'post_content' => $post['post_content'],
  524. 'post_excerpt' => $post['post_excerpt'], 'post_title' => $post['post_title'],
  525. 'post_status' => $post['status'], 'post_name' => $post['post_name'],
  526. 'comment_status' => $post['comment_status'], 'ping_status' => $post['ping_status'],
  527. 'guid' => $post['guid'], 'post_parent' => $post_parent, 'menu_order' => $post['menu_order'],
  528. 'post_type' => $post['post_type'], 'post_password' => $post['post_password']
  529. );
  530. $original_post_ID = $post['post_id'];
  531. $postdata = apply_filters( 'wp_import_post_data_processed', $postdata, $post );
  532. $postdata = wp_slash( $postdata );
  533. if ( 'attachment' == $postdata['post_type'] ) {
  534. $remote_url = ! empty($post['attachment_url']) ? $post['attachment_url'] : $post['guid'];
  535. // try to use _wp_attached file for upload folder placement to ensure the same location as the export site
  536. // e.g. location is 2003/05/image.jpg but the attachment post_date is 2010/09, see media_handle_upload()
  537. $postdata['upload_date'] = $post['post_date'];
  538. if ( isset( $post['postmeta'] ) ) {
  539. foreach( $post['postmeta'] as $meta ) {
  540. if ( $meta['key'] == '_wp_attached_file' ) {
  541. if ( preg_match( '%^[0-9]{4}/[0-9]{2}%', $meta['value'], $matches ) )
  542. $postdata['upload_date'] = $matches[0];
  543. break;
  544. }
  545. }
  546. }
  547. $comment_post_ID = $post_id = $this->process_attachment( $postdata, $remote_url );
  548. } else {
  549. $comment_post_ID = $post_id = wp_insert_post( $postdata, true );
  550. do_action( 'wp_import_insert_post', $post_id, $original_post_ID, $postdata, $post );
  551. }
  552. if ( is_wp_error( $post_id ) ) {
  553. printf( __( 'Failed to import %s &#8220;%s&#8221;', 'wordpress-importer' ),
  554. $post_type_object->labels->singular_name, esc_html($post['post_title']) );
  555. if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
  556. echo ': ' . $post_id->get_error_message();
  557. echo '<br />';
  558. continue;
  559. }
  560. if ( $post['is_sticky'] == 1 )
  561. stick_post( $post_id );
  562. }
  563. // map pre-import ID to local ID
  564. $this->processed_posts[intval($post['post_id'])] = (int) $post_id;
  565. if ( ! isset( $post['terms'] ) )
  566. $post['terms'] = array();
  567. $post['terms'] = apply_filters( 'wp_import_post_terms', $post['terms'], $post_id, $post );
  568. // add categories, tags and other terms
  569. if ( ! empty( $post['terms'] ) ) {
  570. $terms_to_set = array();
  571. foreach ( $post['terms'] as $term ) {
  572. // back compat with WXR 1.0 map 'tag' to 'post_tag'
  573. $taxonomy = ( 'tag' == $term['domain'] ) ? 'post_tag' : $term['domain'];
  574. $term_exists = term_exists( $term['slug'], $taxonomy );
  575. $term_id = is_array( $term_exists ) ? $term_exists['term_id'] : $term_exists;
  576. if ( ! $term_id ) {
  577. $t = wp_insert_term( $term['name'], $taxonomy, array( 'slug' => $term['slug'] ) );
  578. if ( ! is_wp_error( $t ) ) {
  579. $term_id = $t['term_id'];
  580. do_action( 'wp_import_insert_term', $t, $term, $post_id, $post );
  581. } else {
  582. printf( __( 'Failed to import %s %s', 'wordpress-importer' ), esc_html($taxonomy), esc_html($term['name']) );
  583. if ( defined('IMPORT_DEBUG') && IMPORT_DEBUG )
  584. echo ': ' . $t->get_error_message();
  585. echo '<br />';
  586. do_action( 'wp_import_insert_term_failed', $t, $term, $post_id, $post );
  587. continue;
  588. }
  589. }
  590. $terms_to_set[$taxonomy][] = intval( $term_id );
  591. }
  592. foreach ( $terms_to_set as $tax => $ids ) {
  593. $tt_ids = wp_set_post_terms( $post_id, $ids, $tax );
  594. do_action( 'wp_import_set_post_terms', $tt_ids, $ids, $tax, $post_id, $post );
  595. }
  596. unset( $post['terms'], $terms_to_set );
  597. }
  598. if ( ! isset( $post['comments'] ) )
  599. $post['comments'] = array();
  600. $post['comments'] = apply_filters( 'wp_import_post_comments', $post['comments'], $post_id, $post );
  601. // add/update comments
  602. if ( ! empty( $post['comments'] ) ) {
  603. $num_comments = 0;
  604. $inserted_comments = array();
  605. foreach ( $post['comments'] as $comment ) {
  606. $comment_id = $comment['comment_id'];
  607. $newcomments[$comment_id]['comment_post_ID'] = $comment_post_ID;
  608. $newcomments[$comment_id]['comment_author'] = $comment['comment_author'];
  609. $newcomments[$comment_id]['comment_author_email'] = $comment['comment_author_email'];
  610. $newcomments[$comment_id]['comment_author_IP'] = $comment['comment_author_IP'];
  611. $newcomments[$comment_id]['comment_author_url'] = $comment['comment_author_url'];
  612. $newcomments[$comment_id]['comment_date'] = $comment['comment_date'];
  613. $newcomments[$comment_id]['comment_date_gmt'] = $comment['comment_date_gmt'];
  614. $newcomments[$comment_id]['comment_content'] = $comment['comment_content'];
  615. $newcomments[$comment_id]['comment_approved'] = $comment['comment_approved'];
  616. $newcomments[$comment_id]['comment_type'] = $comment['comment_type'];
  617. $newcomments[$comment_id]['comment_parent'] = $comment['comment_parent'];
  618. $newcomments[$comment_id]['commentmeta'] = isset( $comment['commentmeta'] ) ? $comment['commentmeta'] : array();
  619. if ( isset( $this->processed_authors[$comment['comment_user_id']] ) )
  620. $newcomments[$comment_id]['user_id'] = $this->processed_authors[$comment['comment_user_id']];
  621. }
  622. ksort( $newcomments );
  623. foreach ( $newcomments as $key => $comment ) {
  624. // if this is a new post we can skip the comment_exists() check
  625. if ( ! $post_exists || ! comment_exists( $comment['comment_author'], $comment['comment_date'] ) ) {
  626. if ( isset( $inserted_comments[$comment['comment_parent']] ) )
  627. $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
  628. $comment = wp_filter_comment( $comment );
  629. $inserted_comments[$key] = wp_insert_comment( $comment );
  630. do_action( 'wp_import_insert_comment', $inserted_comments[$key], $comment, $comment_post_ID, $post );
  631. foreach( $comment['commentmeta'] as $meta ) {
  632. $value = maybe_unserialize( $meta['value'] );
  633. add_comment_meta( $inserted_comments[$key], $meta['key'], $value );
  634. }
  635. $num_comments++;
  636. }
  637. }
  638. unset( $newcomments, $inserted_comments, $post['comments'] );
  639. }
  640. if ( ! isset( $post['postmeta'] ) )
  641. $post['postmeta'] = array();
  642. $post['postmeta'] = apply_filters( 'wp_import_post_meta', $post['postmeta'], $post_id, $post );
  643. // add/update post meta
  644. if ( ! empty( $post['postmeta'] ) ) {
  645. foreach ( $post['postmeta'] as $meta ) {
  646. $key = apply_filters( 'import_post_meta_key', $meta['key'], $post_id, $post );
  647. $value = false;
  648. if ( '_edit_last' == $key ) {
  649. if ( isset( $this->processed_authors[intval($meta['value'])] ) )
  650. $value = $this->processed_authors[intval($meta['value'])];
  651. else
  652. $key = false;
  653. }
  654. if ( $key ) {
  655. // export gets meta straight from the DB so could have a serialized string
  656. if ( ! $value )
  657. $value = maybe_unserialize( $meta['value'] );
  658. add_post_meta( $post_id, $key, $value );
  659. do_action( 'import_post_meta', $post_id, $key, $value );
  660. // if the post has a featured image, take note of this in case of remap
  661. if ( '_thumbnail_id' == $key )
  662. $this->featured_images[$post_id] = (int) $value;
  663. }
  664. }
  665. }
  666. }
  667. unset( $this->posts );
  668. }
  669. /**
  670. * Attempt to create a new menu item from import data
  671. *
  672. * Fails for draft, orphaned menu items and those without an associated nav_menu
  673. * or an invalid nav_menu term. If the post type or term object which the menu item
  674. * represents doesn't exist then the menu item will not be imported (waits until the
  675. * end of the import to retry again before discarding).
  676. *
  677. * @param array $item Menu item details from WXR file
  678. */
  679. function process_menu_item( $item ) {
  680. // skip draft, orphaned menu items
  681. if ( 'draft' == $item['status'] )
  682. return;
  683. $menu_slug = false;
  684. if ( isset($item['terms']) ) {
  685. // loop through terms, assume first nav_menu term is correct menu
  686. foreach ( $item['terms'] as $term ) {
  687. if ( 'nav_menu' == $term['domain'] ) {
  688. $menu_slug = $term['slug'];
  689. break;
  690. }
  691. }
  692. }
  693. // no nav_menu term associated with this menu item
  694. if ( ! $menu_slug ) {
  695. _e( 'Menu item skipped due to missing menu slug', 'wordpress-importer' );
  696. echo '<br />';
  697. return;
  698. }
  699. $menu_id = term_exists( $menu_slug, 'nav_menu' );
  700. if ( ! $menu_id ) {
  701. printf( __( 'Menu item skipped due to invalid menu slug: %s', 'wordpress-importer' ), esc_html( $menu_slug ) );
  702. echo '<br />';
  703. return;
  704. } else {
  705. $menu_id = is_array( $menu_id ) ? $menu_id['term_id'] : $menu_id;
  706. }
  707. foreach ( $item['postmeta'] as $meta ) {
  708. ${$meta['key']} = $meta['value'];
  709. }
  710. if ( 'taxonomy' == $_menu_item_type && isset( $this->processed_terms[intval($_menu_item_object_id)] ) ) {
  711. $_menu_item_object_id = $this->processed_terms[intval($_menu_item_object_id)];
  712. } else if ( 'post_type' == $_menu_item_type && isset( $this->processed_posts[intval($_menu_item_object_id)] ) ) {
  713. $_menu_item_object_id = $this->processed_posts[intval($_menu_item_object_id)];
  714. } else if ( 'custom' != $_menu_item_type ) {
  715. // associated object is missing or not imported yet, we'll retry later
  716. $this->missing_menu_items[] = $item;
  717. return;
  718. }
  719. if ( isset( $this->processed_menu_items[intval($_menu_item_menu_item_parent)] ) ) {
  720. $_menu_item_menu_item_parent = $this->processed_menu_items[intval($_menu_item_menu_item_parent)];
  721. } else if ( $_menu_item_menu_item_parent ) {
  722. $this->menu_item_orphans[intval($item['post_id'])] = (int) $_menu_item_menu_item_parent;
  723. $_menu_item_menu_item_parent = 0;
  724. }
  725. // wp_update_nav_menu_item expects CSS classes as a space separated string
  726. $_menu_item_classes = maybe_unserialize( $_menu_item_classes );
  727. if ( is_array( $_menu_item_classes ) )
  728. $_menu_item_classes = implode( ' ', $_menu_item_classes );
  729. $args = array(
  730. 'menu-item-object-id' => $_menu_item_object_id,
  731. 'menu-item-object' => $_menu_item_object,
  732. 'menu-item-parent-id' => $_menu_item_menu_item_parent,
  733. 'menu-item-position' => intval( $item['menu_order'] ),
  734. 'menu-item-type' => $_menu_item_type,
  735. 'menu-item-title' => $item['post_title'],
  736. 'menu-item-url' => $_menu_item_url,
  737. 'menu-item-description' => $item['post_content'],
  738. 'menu-item-attr-title' => $item['post_excerpt'],
  739. 'menu-item-target' => $_menu_item_target,
  740. 'menu-item-classes' => $_menu_item_classes,
  741. 'menu-item-xfn' => $_menu_item_xfn,
  742. 'menu-item-status' => $item['status']
  743. );
  744. $id = $id = wp_update_nav_menu_item( $menu_id, 0, $args );
  745. if ( $id && ! is_wp_error( $id ) )
  746. $this->processed_menu_items[intval($item['post_id'])] = (int) $id;
  747. foreach ( $item['postmeta'] as $meta ) {
  748. if ( strpos( '_menu', $meta['key'] ) === false ) {
  749. $key = apply_filters( 'import_post_meta_key', $meta['key'], $id, $item );
  750. $value = false;
  751. if ( $key ) {
  752. // export gets meta straight from the DB so could have a serialized string
  753. if ( ! $value )
  754. $value = maybe_unserialize( $meta['value'] );
  755. add_post_meta( $id, $key, $value );
  756. do_action( 'import_post_meta', $id, $key, $value );
  757. }
  758. }
  759. }
  760. }
  761. /**
  762. * If fetching attachments is enabled then attempt to create a new attachment
  763. *
  764. * @param array $post Attachment post details from WXR
  765. * @param string $url URL to fetch attachment from
  766. * @return int|WP_Error Post ID on success, WP_Error otherwise
  767. */
  768. function process_attachment( $post, $url ) {
  769. if ( ! $this->fetch_attachments )
  770. return new WP_Error( 'attachment_processing_error',
  771. __( 'Fetching attachments is not enabled', 'wordpress-importer' ) );
  772. // if the URL is absolute, but does not contain address, then upload it assuming base_site_url
  773. if ( preg_match( '|^/[\w\W]+$|', $url ) )
  774. $url = rtrim( $this->base_url, '/' ) . $url;
  775. $upload = $this->fetch_remote_file( $url, $post );
  776. if ( is_wp_error( $upload ) )
  777. return $upload;
  778. if ( $info = wp_check_filetype( $upload['file'] ) )
  779. $post['post_mime_type'] = $info['type'];
  780. else
  781. return new WP_Error( 'attachment_processing_error', __('Invalid file type', 'wordpress-importer') );
  782. $post['guid'] = $upload['url'];
  783. // as per wp-admin/includes/upload.php
  784. $post_id = wp_insert_attachment( $post, $upload['file'] );
  785. add_filter( 'intermediate_image_sizes_advanced', '__return_false' );
  786. wp_update_attachment_metadata( $post_id, wp_generate_attachment_metadata( $post_id, $upload['file'] ) );
  787. remove_filter( 'intermediate_image_sizes_advanced', '__return_false' );
  788. $this->processed_attachments[] = $post_id;
  789. // remap resized image URLs, works by stripping the extension and remapping the URL stub.
  790. if ( preg_match( '!^image/!', $info['type'] ) ) {
  791. $parts = pathinfo( $url );
  792. $name = basename( $parts['basename'], ".{$parts['extension']}" ); // PATHINFO_FILENAME in PHP 5.2
  793. $parts_new = pathinfo( $upload['url'] );
  794. $name_new = basename( $parts_new['basename'], ".{$parts_new['extension']}" );
  795. $this->url_remap[$parts['dirname'] . '/' . $name] = $parts_new['dirname'] . '/' . $name_new;
  796. }
  797. return $post_id;
  798. }
  799. /**
  800. * Attempt to download a remote file attachment
  801. *
  802. * @param string $url URL of item to fetch
  803. * @param array $post Attachment details
  804. * @return array|WP_Error Local file location details on success, WP_Error otherwise
  805. */
  806. function fetch_remote_file( $url, $post ) {
  807. // extract the file name and extension from the url
  808. $file_name = basename( $url );
  809. // get placeholder file in the upload dir with a unique, sanitized filename
  810. $upload = wp_upload_bits( $file_name, 0, '', $post['upload_date'] );
  811. if ( $upload['error'] )
  812. return new WP_Error( 'upload_dir_error', $upload['error'] );
  813. // fetch the remote url and write it to the placeholder file
  814. $headers = wp_get_http( $url, $upload['file'] );
  815. // request failed
  816. if ( ! $headers ) {
  817. @unlink( $upload['file'] );
  818. return new WP_Error( 'import_file_error', __('Remote server did not respond', 'wordpress-importer') );
  819. }
  820. // make sure the fetch was successful
  821. if ( $headers['response'] != '200' ) {
  822. @unlink( $upload['file'] );
  823. 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']) ) );
  824. }
  825. $filesize = filesize( $upload['file'] );
  826. if ( isset( $headers['content-length'] ) && $filesize != $headers['content-length'] ) {
  827. @unlink( $upload['file'] );
  828. return new WP_Error( 'import_file_error', __('Remote file is incorrect size', 'wordpress-importer') );
  829. }
  830. if ( 0 == $filesize ) {
  831. @unlink( $upload['file'] );
  832. return new WP_Error( 'import_file_error', __('Zero size file downloaded', 'wordpress-importer') );
  833. }
  834. $max_size = (int) $this->max_attachment_size();
  835. if ( ! empty( $max_size ) && $filesize > $max_size ) {
  836. @unlink( $upload['file'] );
  837. return new WP_Error( 'import_file_error', sprintf(__('Remote file is too large, limit is %s', 'wordpress-importer'), size_format($max_size) ) );
  838. }
  839. // keep track of the old and new urls so we can substitute them later
  840. $this->url_remap[$url] = $upload['url'];
  841. $this->url_remap[$post['guid']] = $upload['url']; // r13735, really needed?
  842. // keep track of the destination if the remote url is redirected somewhere else
  843. if ( isset($headers['x-final-location']) && $headers['x-final-location'] != $url )
  844. $this->url_remap[$headers['x-final-location']] = $upload['url'];
  845. return $upload;
  846. }
  847. /**
  848. * Attempt to associate posts and menu items with previously missing parents
  849. *
  850. * An imported post's parent may not have been imported when it was first created
  851. * so try again. Similarly for child menu items and menu items which were missing
  852. * the object (e.g. post) they represent in the menu
  853. */
  854. function backfill_parents() {
  855. global $wpdb;
  856. // find parents for post orphans
  857. foreach ( $this->post_orphans as $child_id => $parent_id ) {
  858. $local_child_id = $local_parent_id = false;
  859. if ( isset( $this->processed_posts[$child_id] ) )
  860. $local_child_id = $this->processed_posts[$child_id];
  861. if ( isset( $this->processed_posts[$parent_id] ) )
  862. $local_parent_id = $this->processed_posts[$parent_id];
  863. if ( $local_child_id && $local_parent_id )
  864. $wpdb->update( $wpdb->posts, array( 'post_parent' => $local_parent_id ), array( 'ID' => $local_child_id ), '%d', '%d' );
  865. }
  866. // all other posts/terms are imported, retry menu items with missing associated object
  867. $missing_menu_items = $this->missing_menu_items;
  868. foreach ( $missing_menu_items as $item )
  869. $this->process_menu_item( $item );
  870. // find parents for menu item orphans
  871. foreach ( $this->menu_item_orphans as $child_id => $parent_id ) {
  872. $local_child_id = $local_parent_id = 0;
  873. if ( isset( $this->processed_menu_items[$child_id] ) )
  874. $local_child_id = $this->processed_menu_items[$child_id];
  875. if ( isset( $this->processed_menu_items[$parent_id] ) )
  876. $local_parent_id = $this->processed_menu_items[$parent_id];
  877. if ( $local_child_id && $local_parent_id )
  878. update_post_meta( $local_child_id, '_menu_item_menu_item_parent', (int) $local_parent_id );
  879. }
  880. }
  881. /**
  882. * Use stored mapping information to update old attachment URLs
  883. *
  884. * With Beaver support - see https://stackoverflow.com/a/3277781/635882
  885. */
  886. function backfill_attachment_urls() {
  887. global $wpdb;
  888. // make sure we do the longest urls first, in case one is a substring of another
  889. uksort( $this->url_remap, array(&$this, 'cmpr_strlen') );
  890. foreach ( $this->url_remap as $from_url => $to_url ) {
  891. // remap urls in post_content
  892. $wpdb->query( $wpdb->prepare("UPDATE {$wpdb->posts} SET post_content = REPLACE(post_content, %s, %s)", $from_url, $to_url) );
  893. // Beaver Builder support - replace serialized data in the postmeta and options tables
  894. $search = 's:' . strlen( $from_url) . ':"' . $from_url . '"';
  895. $replace = 's:' . strlen( $to_url ) . ':"' . $to_url . '"';
  896. $result = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = REPLACE(meta_value, %s, %s)", $search, $replace ) );
  897. $result = $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->options} SET option_value = REPLACE(option_value, %s, %s)", $search, $replace ) );
  898. $meta_to_repace = $wpdb->get_results( $wpdb->prepare( "SELECT meta_id, meta_value FROM {$wpdb->postmeta} WHERE meta_value LIKE '%%%s%%'", $from_url ) );
  899. foreach ( $meta_to_repace as $meta ) {
  900. $meta->meta_value = Vamtam_Import_Search_Replace::recursive_unserialize_replace( $from_url, $to_url, $meta->meta_value );
  901. if ( ! is_string( $meta->meta_value ) ) {
  902. $meta->meta_value = serialize( $meta->meta_value );
  903. }
  904. $wpdb->update(
  905. $wpdb->postmeta,
  906. array(
  907. 'meta_value' => $meta->meta_value,
  908. ),
  909. array( 'meta_id' => $meta->meta_id ),
  910. array(
  911. '%s',
  912. ),
  913. array( '%d' )
  914. );
  915. }
  916. }
  917. }
  918. /**
  919. * Update _thumbnail_id meta to new, imported attachment IDs
  920. */
  921. function remap_featured_images() {
  922. // cycle through posts that have a featured image
  923. foreach ( $this->featured_images as $post_id => $value ) {
  924. if ( isset( $this->processed_posts[$value] ) ) {
  925. $new_id = $this->processed_posts[$value];
  926. // only update if there's a difference
  927. if ( $new_id != $value )
  928. update_post_meta( $post_id, '_thumbnail_id', $new_id );
  929. }
  930. }
  931. }
  932. /**
  933. * Parse a WXR file
  934. *
  935. * @param string $file Path to WXR file for parsing
  936. * @return array Information gathered from the WXR file
  937. */
  938. function parse( $file ) {
  939. $parser = new VAMTAM_WXR_Parser();
  940. return $parser->parse( $file );
  941. }
  942. // Display import page title
  943. function header() {
  944. echo '<div class="wrap">';
  945. echo '<h2>' . __( 'Import WordPress', 'wordpress-importer' ) . '</h2>';
  946. $updates = get_plugin_updates();
  947. $basename = plugin_basename(__FILE__);
  948. if ( isset( $updates[$basename] ) ) {
  949. $update = $updates[$basename];
  950. echo '<div class="error"><p><strong>';
  951. 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 );
  952. echo '</strong></p></div>';
  953. }
  954. }
  955. // Close div.wrap
  956. function footer() {
  957. echo '</div>';
  958. }
  959. /**
  960. * Display introductory text and file upload form
  961. */
  962. function greet() {
  963. echo '<div class="narrow">';
  964. echo '<p>'.__( 'Howdy! Upload your WordPress eXtended RSS (WXR) file and we&#8217;ll import the posts, pages, comments, custom fields, categories, and tags into this site.', 'wordpress-importer' ).'</p>';
  965. echo '<p>'.__( 'Choose a WXR (.xml) file to upload, then click Upload file and import.', 'wordpress-importer' ).'</p>';
  966. wp_import_upload_form( 'admin.php?import=wordpress&amp;step=1' );
  967. echo '</div>';
  968. }
  969. /**
  970. * Decide if the given meta key maps to information we will want to import
  971. *
  972. * @param string $key The meta key to check
  973. * @return string|bool The key if we do want to import, false if not
  974. */
  975. function is_valid_meta_key( $key ) {
  976. // skip attachment metadata since we'll regenerate it from scratch
  977. // skip _edit_lock as not relevant for import
  978. if ( in_array( $key, array( '_wp_attached_file', '_wp_attachment_metadata', '_edit_lock' ) ) )
  979. return false;
  980. return $key;
  981. }
  982. /**
  983. * Decide whether or not the importer is allowed to create users.
  984. * Default is true, can be filtered via import_allow_create_users
  985. *
  986. * @return bool True if creating users is allowed
  987. */
  988. function allow_create_users() {
  989. return apply_filters( 'import_allow_create_users', true );
  990. }
  991. /**
  992. * Decide whether or not the importer should attempt to download attachment files.
  993. * Default is true, can be filtered via import_allow_fetch_attachments. The choice
  994. * made at the import options screen must also be true, false here hides that checkbox.
  995. *
  996. * @return bool True if downloading attachments is allowed
  997. */
  998. function allow_fetch_attachments() {
  999. return apply_filters( 'import_allow_fetch_attachments', true );
  1000. }
  1001. /**
  1002. * Decide what the maximum file size for downloaded attachments is.
  1003. * Default is 0 (unlimited), can be filtered via import_attachment_size_limit
  1004. *
  1005. * @return int Maximum attachment file size to import
  1006. */
  1007. function max_attachment_size() {
  1008. return apply_filters( 'import_attachment_size_limit', 0 );
  1009. }
  1010. /**
  1011. * Added to http_request_timeout filter to force timeout at 60 seconds during import
  1012. * @return int 60
  1013. */
  1014. function bump_request_timeout( $val ) {
  1015. return 60;
  1016. }
  1017. // return the difference in length between two strings
  1018. function cmpr_strlen( $a, $b ) {
  1019. return strlen($b) - strlen($a);
  1020. }
  1021. }
  1022. } // class_exists( 'WP_Importer' )
  1023. function vamtam_importer_init() {
  1024. if ( defined( 'VAMTAM_SAMPLES_DIR' ) ) {
  1025. $GLOBALS['vamtam_import'] = new VAMTAM_Import();
  1026. register_importer( 'wpv', 'Vamtam Demo Content', sprintf( esc_html__( 'Import posts, pages, comments, custom fields, categories, tags and sidebars from a %s dummy content file.', 'wordpress-importer' ), VAMTAM_THEME_NAME ), array( $GLOBALS['vamtam_import'], 'dispatch' ) );
  1027. }
  1028. }
  1029. add_action( 'admin_init', 'vamtam_importer_init' );