[ Index ]

PHP Cross Reference of WordPress

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 void|WP_Error Void on success. 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' => sprintf(
  86                  /* translators: 1: php.ini, 2: post_max_size, 3: upload_max_filesize */
  87                  __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your %1$s file or by %2$s being defined as smaller than %3$s in %1$s.' ),
  88                  'php.ini',
  89                  'post_max_size',
  90                  'upload_max_filesize'
  91              ),
  92          );
  93      }
  94  
  95      $overrides                 = array(
  96          'test_form' => false,
  97          'test_type' => false,
  98      );
  99      $_FILES['import']['name'] .= '.txt';
 100      $upload                    = wp_handle_upload( $_FILES['import'], $overrides );
 101  
 102      if ( isset( $upload['error'] ) ) {
 103          return $upload;
 104      }
 105  
 106      // Construct the attachment array.
 107      $attachment = array(
 108          'post_title'     => wp_basename( $upload['file'] ),
 109          'post_content'   => $upload['url'],
 110          'post_mime_type' => $upload['type'],
 111          'guid'           => $upload['url'],
 112          'context'        => 'import',
 113          'post_status'    => 'private',
 114      );
 115  
 116      // Save the data.
 117      $id = wp_insert_attachment( $attachment, $upload['file'] );
 118  
 119      /*
 120       * Schedule a cleanup for one day from now in case of failed
 121       * import or missing wp_import_cleanup() call.
 122       */
 123      wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
 124  
 125      return array(
 126          'file' => $upload['file'],
 127          'id'   => $id,
 128      );
 129  }
 130  
 131  /**
 132   * Returns a list from WordPress.org of popular importer plugins.
 133   *
 134   * @since 3.5.0
 135   *
 136   * @return array Importers with metadata for each.
 137   */
 138  function wp_get_popular_importers() {
 139      // Include an unmodified $wp_version.
 140      require  ABSPATH . WPINC . '/version.php';
 141  
 142      $locale            = get_user_locale();
 143      $cache_key         = 'popular_importers_' . md5( $locale . $wp_version );
 144      $popular_importers = get_site_transient( $cache_key );
 145  
 146      if ( ! $popular_importers ) {
 147          $url     = add_query_arg(
 148              array(
 149                  'locale'  => $locale,
 150                  'version' => $wp_version,
 151              ),
 152              'http://api.wordpress.org/core/importers/1.1/'
 153          );
 154          $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) );
 155  
 156          if ( wp_http_supports( array( 'ssl' ) ) ) {
 157              $url = set_url_scheme( $url, 'https' );
 158          }
 159  
 160          $response          = wp_remote_get( $url, $options );
 161          $popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
 162  
 163          if ( is_array( $popular_importers ) ) {
 164              set_site_transient( $cache_key, $popular_importers, 2 * DAY_IN_SECONDS );
 165          } else {
 166              $popular_importers = false;
 167          }
 168      }
 169  
 170      if ( is_array( $popular_importers ) ) {
 171          // If the data was received as translated, return it as-is.
 172          if ( $popular_importers['translated'] ) {
 173              return $popular_importers['importers'];
 174          }
 175  
 176          foreach ( $popular_importers['importers'] as &$importer ) {
 177              // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
 178              $importer['description'] = translate( $importer['description'] );
 179              if ( 'WordPress' !== $importer['name'] ) {
 180                  // phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
 181                  $importer['name'] = translate( $importer['name'] );
 182              }
 183          }
 184          return $popular_importers['importers'];
 185      }
 186  
 187      return array(
 188          // slug => name, description, plugin slug, and register_importer() slug.
 189          'blogger'     => array(
 190              'name'        => __( 'Blogger' ),
 191              'description' => __( 'Import posts, comments, and users from a Blogger blog.' ),
 192              'plugin-slug' => 'blogger-importer',
 193              'importer-id' => 'blogger',
 194          ),
 195          'wpcat2tag'   => array(
 196              'name'        => __( 'Categories and Tags Converter' ),
 197              'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ),
 198              'plugin-slug' => 'wpcat2tag-importer',
 199              'importer-id' => 'wp-cat2tag',
 200          ),
 201          'livejournal' => array(
 202              'name'        => __( 'LiveJournal' ),
 203              'description' => __( 'Import posts from LiveJournal using their API.' ),
 204              'plugin-slug' => 'livejournal-importer',
 205              'importer-id' => 'livejournal',
 206          ),
 207          'movabletype' => array(
 208              'name'        => __( 'Movable Type and TypePad' ),
 209              'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ),
 210              'plugin-slug' => 'movabletype-importer',
 211              'importer-id' => 'mt',
 212          ),
 213          'rss'         => array(
 214              'name'        => __( 'RSS' ),
 215              'description' => __( 'Import posts from an RSS feed.' ),
 216              'plugin-slug' => 'rss-importer',
 217              'importer-id' => 'rss',
 218          ),
 219          'tumblr'      => array(
 220              'name'        => __( 'Tumblr' ),
 221              'description' => __( 'Import posts &amp; media from Tumblr using their API.' ),
 222              'plugin-slug' => 'tumblr-importer',
 223              'importer-id' => 'tumblr',
 224          ),
 225          'wordpress'   => array(
 226              'name'        => 'WordPress',
 227              'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
 228              'plugin-slug' => 'wordpress-importer',
 229              'importer-id' => 'wordpress',
 230          ),
 231      );
 232  }


Generated: Thu Mar 28 01:00:02 2024 Cross-referenced by PHPXref 0.7.1