wp/wp-admin/includes/import.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    51  * @param callable $callback    Callback to run.
    51  * @param callable $callback    Callback to run.
    52  * @return WP_Error Returns WP_Error when $callback is WP_Error.
    52  * @return WP_Error Returns WP_Error when $callback is WP_Error.
    53  */
    53  */
    54 function register_importer( $id, $name, $description, $callback ) {
    54 function register_importer( $id, $name, $description, $callback ) {
    55 	global $wp_importers;
    55 	global $wp_importers;
    56 	if ( is_wp_error( $callback ) )
    56 	if ( is_wp_error( $callback ) ) {
    57 		return $callback;
    57 		return $callback;
    58 	$wp_importers[$id] = array ( $name, $description, $callback );
    58 	}
       
    59 	$wp_importers[ $id ] = array( $name, $description, $callback );
    59 }
    60 }
    60 
    61 
    61 /**
    62 /**
    62  * Cleanup importer.
    63  * Cleanup importer.
    63  *
    64  *
    79  * @return array Uploaded file's details on success, error message on failure
    80  * @return array Uploaded file's details on success, error message on failure
    80  */
    81  */
    81 function wp_import_handle_upload() {
    82 function wp_import_handle_upload() {
    82 	if ( ! isset( $_FILES['import'] ) ) {
    83 	if ( ! isset( $_FILES['import'] ) ) {
    83 		return array(
    84 		return array(
    84 			'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.' )
    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.' ),
    85 		);
    86 		);
    86 	}
    87 	}
    87 
    88 
    88 	$overrides = array( 'test_form' => false, 'test_type' => false );
    89 	$overrides                 = array(
       
    90 		'test_form' => false,
       
    91 		'test_type' => false,
       
    92 	);
    89 	$_FILES['import']['name'] .= '.txt';
    93 	$_FILES['import']['name'] .= '.txt';
    90 	$upload = wp_handle_upload( $_FILES['import'], $overrides );
    94 	$upload                    = wp_handle_upload( $_FILES['import'], $overrides );
    91 
    95 
    92 	if ( isset( $upload['error'] ) ) {
    96 	if ( isset( $upload['error'] ) ) {
    93 		return $upload;
    97 		return $upload;
    94 	}
    98 	}
    95 
    99 
    96 	// Construct the object array
   100 	// Construct the object array
    97 	$object = array(
   101 	$object = array(
    98 		'post_title' => basename( $upload['file'] ),
   102 		'post_title'     => wp_basename( $upload['file'] ),
    99 		'post_content' => $upload['url'],
   103 		'post_content'   => $upload['url'],
   100 		'post_mime_type' => $upload['type'],
   104 		'post_mime_type' => $upload['type'],
   101 		'guid' => $upload['url'],
   105 		'guid'           => $upload['url'],
   102 		'context' => 'import',
   106 		'context'        => 'import',
   103 		'post_status' => 'private'
   107 		'post_status'    => 'private',
   104 	);
   108 	);
   105 
   109 
   106 	// Save the data
   110 	// Save the data
   107 	$id = wp_insert_attachment( $object, $upload['file'] );
   111 	$id = wp_insert_attachment( $object, $upload['file'] );
   108 
   112 
   110 	 * Schedule a cleanup for one day from now in case of failed
   114 	 * Schedule a cleanup for one day from now in case of failed
   111 	 * import or missing wp_import_cleanup() call.
   115 	 * import or missing wp_import_cleanup() call.
   112 	 */
   116 	 */
   113 	wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
   117 	wp_schedule_single_event( time() + DAY_IN_SECONDS, 'importer_scheduled_cleanup', array( $id ) );
   114 
   118 
   115 	return array( 'file' => $upload['file'], 'id' => $id );
   119 	return array(
       
   120 		'file' => $upload['file'],
       
   121 		'id'   => $id,
       
   122 	);
   116 }
   123 }
   117 
   124 
   118 /**
   125 /**
   119  * Returns a list from WordPress.org of popular importer plugins.
   126  * Returns a list from WordPress.org of popular importer plugins.
   120  *
   127  *
   123  * @return array Importers with metadata for each.
   130  * @return array Importers with metadata for each.
   124  */
   131  */
   125 function wp_get_popular_importers() {
   132 function wp_get_popular_importers() {
   126 	include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
   133 	include( ABSPATH . WPINC . '/version.php' ); // include an unmodified $wp_version
   127 
   134 
   128 	$locale = get_user_locale();
   135 	$locale            = get_user_locale();
   129 	$cache_key = 'popular_importers_' . md5( $locale . $wp_version );
   136 	$cache_key         = 'popular_importers_' . md5( $locale . $wp_version );
   130 	$popular_importers = get_site_transient( $cache_key );
   137 	$popular_importers = get_site_transient( $cache_key );
   131 
   138 
   132 	if ( ! $popular_importers ) {
   139 	if ( ! $popular_importers ) {
   133 		$url = add_query_arg( array(
   140 		$url     = add_query_arg(
   134 			'locale'  => $locale,
   141 			array(
   135 			'version' => $wp_version,
   142 				'locale'  => $locale,
   136 		), 'http://api.wordpress.org/core/importers/1.1/' );
   143 				'version' => $wp_version,
       
   144 			),
       
   145 			'http://api.wordpress.org/core/importers/1.1/'
       
   146 		);
   137 		$options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) );
   147 		$options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url( '/' ) );
   138 
   148 
   139 		if ( wp_http_supports( array( 'ssl' ) ) ) {
   149 		if ( wp_http_supports( array( 'ssl' ) ) ) {
   140 			$url = set_url_scheme( $url, 'https' );
   150 			$url = set_url_scheme( $url, 'https' );
   141 		}
   151 		}
   142 
   152 
   143 		$response = wp_remote_get( $url, $options );
   153 		$response          = wp_remote_get( $url, $options );
   144 		$popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
   154 		$popular_importers = json_decode( wp_remote_retrieve_body( $response ), true );
   145 
   155 
   146 		if ( is_array( $popular_importers ) ) {
   156 		if ( is_array( $popular_importers ) ) {
   147 			set_site_transient( $cache_key, $popular_importers, 2 * DAY_IN_SECONDS );
   157 			set_site_transient( $cache_key, $popular_importers, 2 * DAY_IN_SECONDS );
   148 		} else {
   158 		} else {
   150 		}
   160 		}
   151 	}
   161 	}
   152 
   162 
   153 	if ( is_array( $popular_importers ) ) {
   163 	if ( is_array( $popular_importers ) ) {
   154 		// If the data was received as translated, return it as-is.
   164 		// If the data was received as translated, return it as-is.
   155 		if ( $popular_importers['translated'] )
   165 		if ( $popular_importers['translated'] ) {
   156 			return $popular_importers['importers'];
   166 			return $popular_importers['importers'];
       
   167 		}
   157 
   168 
   158 		foreach ( $popular_importers['importers'] as &$importer ) {
   169 		foreach ( $popular_importers['importers'] as &$importer ) {
       
   170 			// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
   159 			$importer['description'] = translate( $importer['description'] );
   171 			$importer['description'] = translate( $importer['description'] );
   160 			if ( $importer['name'] != 'WordPress' )
   172 			if ( $importer['name'] != 'WordPress' ) {
       
   173 				// phpcs:ignore WordPress.WP.I18n.LowLevelTranslationFunction,WordPress.WP.I18n.NonSingularStringLiteralText
   161 				$importer['name'] = translate( $importer['name'] );
   174 				$importer['name'] = translate( $importer['name'] );
       
   175 			}
   162 		}
   176 		}
   163 		return $popular_importers['importers'];
   177 		return $popular_importers['importers'];
   164 	}
   178 	}
   165 
   179 
   166 	return array(
   180 	return array(
   167 		// slug => name, description, plugin slug, and register_importer() slug
   181 		// slug => name, description, plugin slug, and register_importer() slug
   168 		'blogger' => array(
   182 		'blogger'     => array(
   169 			'name' => __( 'Blogger' ),
   183 			'name'        => __( 'Blogger' ),
   170 			'description' => __( 'Import posts, comments, and users from a Blogger blog.' ),
   184 			'description' => __( 'Import posts, comments, and users from a Blogger blog.' ),
   171 			'plugin-slug' => 'blogger-importer',
   185 			'plugin-slug' => 'blogger-importer',
   172 			'importer-id' => 'blogger',
   186 			'importer-id' => 'blogger',
   173 		),
   187 		),
   174 		'wpcat2tag' => array(
   188 		'wpcat2tag'   => array(
   175 			'name' => __( 'Categories and Tags Converter' ),
   189 			'name'        => __( 'Categories and Tags Converter' ),
   176 			'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ),
   190 			'description' => __( 'Convert existing categories to tags or tags to categories, selectively.' ),
   177 			'plugin-slug' => 'wpcat2tag-importer',
   191 			'plugin-slug' => 'wpcat2tag-importer',
   178 			'importer-id' => 'wp-cat2tag',
   192 			'importer-id' => 'wp-cat2tag',
   179 		),
   193 		),
   180 		'livejournal' => array(
   194 		'livejournal' => array(
   181 			'name' => __( 'LiveJournal' ),
   195 			'name'        => __( 'LiveJournal' ),
   182 			'description' => __( 'Import posts from LiveJournal using their API.' ),
   196 			'description' => __( 'Import posts from LiveJournal using their API.' ),
   183 			'plugin-slug' => 'livejournal-importer',
   197 			'plugin-slug' => 'livejournal-importer',
   184 			'importer-id' => 'livejournal',
   198 			'importer-id' => 'livejournal',
   185 		),
   199 		),
   186 		'movabletype' => array(
   200 		'movabletype' => array(
   187 			'name' => __( 'Movable Type and TypePad' ),
   201 			'name'        => __( 'Movable Type and TypePad' ),
   188 			'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ),
   202 			'description' => __( 'Import posts and comments from a Movable Type or TypePad blog.' ),
   189 			'plugin-slug' => 'movabletype-importer',
   203 			'plugin-slug' => 'movabletype-importer',
   190 			'importer-id' => 'mt',
   204 			'importer-id' => 'mt',
   191 		),
   205 		),
   192 		'opml' => array(
   206 		'opml'        => array(
   193 			'name' => __( 'Blogroll' ),
   207 			'name'        => __( 'Blogroll' ),
   194 			'description' => __( 'Import links in OPML format.' ),
   208 			'description' => __( 'Import links in OPML format.' ),
   195 			'plugin-slug' => 'opml-importer',
   209 			'plugin-slug' => 'opml-importer',
   196 			'importer-id' => 'opml',
   210 			'importer-id' => 'opml',
   197 		),
   211 		),
   198 		'rss' => array(
   212 		'rss'         => array(
   199 			'name' => __( 'RSS' ),
   213 			'name'        => __( 'RSS' ),
   200 			'description' => __( 'Import posts from an RSS feed.' ),
   214 			'description' => __( 'Import posts from an RSS feed.' ),
   201 			'plugin-slug' => 'rss-importer',
   215 			'plugin-slug' => 'rss-importer',
   202 			'importer-id' => 'rss',
   216 			'importer-id' => 'rss',
   203 		),
   217 		),
   204 		'tumblr' => array(
   218 		'tumblr'      => array(
   205 			'name' => __( 'Tumblr' ),
   219 			'name'        => __( 'Tumblr' ),
   206 			'description' => __( 'Import posts & media from Tumblr using their API.' ),
   220 			'description' => __( 'Import posts & media from Tumblr using their API.' ),
   207 			'plugin-slug' => 'tumblr-importer',
   221 			'plugin-slug' => 'tumblr-importer',
   208 			'importer-id' => 'tumblr',
   222 			'importer-id' => 'tumblr',
   209 		),
   223 		),
   210 		'wordpress' => array(
   224 		'wordpress'   => array(
   211 			'name' => 'WordPress',
   225 			'name'        => 'WordPress',
   212 			'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
   226 			'description' => __( 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
   213 			'plugin-slug' => 'wordpress-importer',
   227 			'plugin-slug' => 'wordpress-importer',
   214 			'importer-id' => 'wordpress',
   228 			'importer-id' => 'wordpress',
   215 		),
   229 		),
   216 	);
   230 	);