base.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. abstract class WP_Import_UnitTestCase extends WP_UnitTestCase {
  3. /**
  4. * Import a WXR file.
  5. *
  6. * The $users parameter provides information on how users specified in the import
  7. * file should be imported. Each key is a user login name and indicates if the user
  8. * should be mapped to an existing user, created as a new user with a particular login
  9. * or imported with the information held in the WXR file. An example of this:
  10. *
  11. * <code>
  12. * $users = array(
  13. * 'alice' => 1, // alice will be mapped to user ID 1
  14. * 'bob' => 'john', // bob will be transformed into john
  15. * 'eve' => false // eve will be imported as is
  16. * );</code>
  17. *
  18. * @param string $filename Full path of the file to import
  19. * @param array $users User import settings
  20. * @param bool $fetch_files Whether or not do download remote attachments
  21. */
  22. protected function _import_wp( $filename, $users = array(), $fetch_files = true ) {
  23. $importer = new WP_Import();
  24. $file = realpath( $filename );
  25. assert('!empty($file)');
  26. assert('is_file($file)');
  27. $authors = $mapping = $new = array();
  28. $i = 0;
  29. // each user is either mapped to a given ID, mapped to a new user
  30. // with given login or imported using details in WXR file
  31. foreach ( $users as $user => $map ) {
  32. $authors[$i] = $user;
  33. if ( is_int( $map ) )
  34. $mapping[$i] = $map;
  35. else if ( is_string( $map ) )
  36. $new[$i] = $map;
  37. $i++;
  38. }
  39. $_POST = array( 'imported_authors' => $authors, 'user_map' => $mapping, 'user_new' => $new );
  40. ob_start();
  41. $importer->fetch_attachments = $fetch_files;
  42. $importer->import( $file );
  43. ob_end_clean();
  44. $_POST = array();
  45. }
  46. }