[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-admin/includes/ -> import.php (source)

   1  <?php
   2  /**
   3   * WordPress Administration Importer API.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Retrieve list of importers.
  11   *
  12   * @since 2.0.0
  13   *
  14   * @global array $wp_importers
  15   * @return array
  16   */
  17  function get_importers() {
  18      global $wp_importers;
  19      if ( is_array( $wp_importers ) ) {
  20          uasort( $wp_importers, '_usort_by_first_member' );
  21      }
  22      return $wp_importers;
  23  }
  24  
  25  /**
  26   * Sorts a multidimensional array by first member of each top level member
  27   *
  28   * Used by uasort() as a callback, should not be used directly.
  29   *
  30   * @since 2.9.0
  31   * @access private
  32   *
  33   * @param array $a
  34   * @param array $b
  35   * @return int
  36   */
  37  function _usort_by_first_member( $a, $b ) {
  38      return strnatcasecmp( $a[0], $b[0] );
  39  }
  40  
  41  /**
  42   * Register importer for WordPress.
  43   *
  44   * @since 2.0.0
  45   *
  46   * @global array $wp_importers
  47   *
  48   * @param string   $id          Importer tag. Used to uniquely identify importer.
  49   * @param string   $name        Importer name and title.
  50   * @param string   $description Importer description.
  51   * @param callable $callback    Callback to run.
  52   * @return WP_Error Returns WP_Error when $callback is WP_Error.
  53   */
  54  function register_importer( $id, $name, $description, $callback ) {
  55      global $wp_importers;
  56      if ( is_wp_error( $callback ) ) {
  57          return $callback;
  58      }
  59      $wp_importers[ $id ] = array( $name, $description, $callback );
  60  }
  61  
  62  /**
  63   * Cleanup importer.
  64   *
  65   * Removes attachment based on ID.
  66   *
  67   * @since 2.0.0
  68   *
  69   * @param string $id Importer ID.
  70   */
  71  function wp_import_cleanup( $id ) {
  72      wp_delete_attachment( $id );
  73  }
  74  
  75  /**
  76   * Handle importer uploading and add attachment.
  77   *
  78   * @since 2.0.0
  79   *
  80   * @return array Uploaded file's details on success, error message on failure
  81   */
  82  function wp_import_handle_upload() {
  83      if ( ! isset( $_FILES['import'] ) ) {
  84          return array(
  85              'error' => __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' ),
  86          );
  87      }
  88  
  89      $overrides                 = array(
  90          'test_form' => false,
  91          'test_type' => false,
  92      );
  93      $_FILES['import']['name'] .= '.txt';
  94      $upload                    = wp_handle_upload( $_FILES['import'], $overrides );
  95  
  96      if ( isset( $upload['error'] ) ) {
  97          return $upload;
  98      }
  99  
 100      // Construct the object array
 101      $object = array(
 102          'post_title'     => wp_basename( $upload['file'] ),
 103          'post_content'   => $upload['url'],
 104          'post_mime_type' => $upload['type'],
 105          'guid'           => $upload['url'],
 106          'context'        => 'import',
 107          'post_status'    => 'private',
 108      );
 109  
 110      // Save the data
 111      $id = wp_insert_attachment( $object, $upload['file'] );
 112  
 113      /*
 114       * Schedule a cleanup for one day from now in case of failed
 115       * import or missing wp_import_cleanup() call.
 116       */
 117      wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
 118  
 119      return array(
 120          'file' => $upload['file'],
 121          'id'   => $id,
 122      );
 123  }
 124  
 125  /**
 126   * Returns a list from WordPress.org of popular importer plugins.
 127   *
 128   * @since 3.5.0
 129   *
 130   * @return array Importers with metadata for each.
 131   */
 132  function wp_get_popular_importers() {
 133      include ( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
 134  
 135      $locale            = get_user_locale();
 136      $cache_key         = 'popular_importers_' . md5( $locale . $wp_version );
 137      $popular_importers = get_site_transient( $cache_key );
 138  
 139      if ( ! $popular_importers ) {
 140          $url     = add_query_arg(
 141              array(
 142                  'locale'  => $locale,
 143                  'version' => $wp_version,
 144              ),
 145              'http://api.wordpress.org/core/importers/1.1/'
 146          );
 147          $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) );
 148  
 149          if ( wp_http_supports( array( 'ssl' ) ) ) {
 150              $url = set_url_scheme( $url, 'https' );
 151          }
 152  
 153          $response          = wp_remote_get( $url, $options );
 154          $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
 155  
 156          if ( is_array( $popular_importers ) ) {
 157              set_site_transient( $cache_key, $popular_importers, 2 * DAY_IN_SECONDS );
 158          } else {
 159              $popular_importers = false;
 160          }
 161      }
 162  
 163      if ( is_array( $popular_importers ) ) {
 164          // If the data was received as translated, return it as-is.
 165          if ( $popular_importers['translated'] ) {
 166              return $popular_importers['importers'];
 167          }
 168  
 169          foreach ( $popular_importers['importers'] as &$importer ) {
 170              // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
 171              $importer['description'] = translate( $importer['description'] );
 172              if ( $importer['name'] != 'WordPress' ) {
 173                  // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
 174                  $importer['name'] = translate( $importer['name'] );
 175              }
 176          }
 177          return $popular_importers['importers'];
 178      }
 179  
 180      return array(
 181          // slug => name, description, plugin slug, and register_importer() slug
 182          'blogger'     => array(
 183              'name'        => __( 'Blogger' ),
 184              'description' => __( 'Import posts, comments, and users from a Blogger blog.' ),
 185              'plugin-slug' => 'blogger-importer',
 186              'importer-id' => 'blogger',
 187          ),
 188          'wpcat2tag'   => array(
 189              'name'        => __( 'Categories and Tags Converter' ),
 190              'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ),
 191              'plugin-slug' => 'wpcat2tag-importer',
 192              'importer-id' => 'wp-cat2tag',
 193          ),
 194          'livejournal' => array(
 195              'name'        => __( 'LiveJournal' ),
 196              'description' => __( 'Import posts from LiveJournal using their API.' ),
 197              'plugin-slug' => 'livejournal-importer',
 198              'importer-id' => 'livejournal',
 199          ),
 200          'movabletype' => array(
 201              'name'        => __( 'Movable Type and TypePad' ),
 202              'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ),
 203              'plugin-slug' => 'movabletype-importer',
 204              'importer-id' => 'mt',
 205          ),
 206          'opml'        => array(
 207              'name'        => __( 'Blogroll' ),
 208              'description' => __( 'Import links in OPML format.' ),
 209              'plugin-slug' => 'opml-importer',
 210              'importer-id' => 'opml',
 211          ),
 212          'rss'         => array(
 213              'name'        => __( 'RSS' ),
 214              'description' => __( 'Import posts from an RSS feed.' ),
 215              'plugin-slug' => 'rss-importer',
 216              'importer-id' => 'rss',
 217          ),
 218          'tumblr'      => array(
 219              'name'        => __( 'Tumblr' ),
 220              'description' => __( 'Import posts &amp; media from Tumblr using their API.' ),
 221              'plugin-slug' => 'tumblr-importer',
 222              'importer-id' => 'tumblr',
 223          ),
 224          'wordpress'   => array(
 225              'name'        => 'WordPress',
 226              'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
 227              'plugin-slug' => 'wordpress-importer',
 228              'importer-id' => 'wordpress',
 229          ),
 230      );
 231  }


Generated: Wed Oct 23 08:20:01 2019 Cross-referenced by PHPXref 0.7