Make WordPress Core

Changeset 22632


Ignore:
Timestamp:
11/17/2012 07:20:04 AM (13 years ago)
Author:
nacin
Message:

Pull the list of popular importers from WordPress.org.

These are the importers we suggest on import.php, prompting the user to
install the relevant plugin for the import they want to go through.

If the API is inaccessible, it falls back to a hard-coded list that should
be kept sync'd with the API with each major version of WordPress. This API
enables us to add new importers between releases, as they are completed or
if services gain quick adoption. As a last resort, we can also temporarily
disable importers that are broken (due to API changes, for example).

The importer currently returns English strings (which are then run through
translate() for existing strings), but the locale is passed to the API,
allowing us to ship translated strings if we wish to be adventurous.

props dllh for the assist.
fixes #18977.

Location:
trunk/wp-admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/import.php

    r19712 r22632  
    3030);
    3131
    32 $popular_importers = array();
    33 if ( current_user_can('install_plugins') )
    34     $popular_importers = array(
    35         'blogger' => array( __('Blogger'), __('Install the Blogger importer to import posts, comments, and users from a Blogger blog.'), 'install' ),
    36         'wpcat2tag' => array(__('Categories and Tags Converter'), __('Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.'), 'install', 'wp-cat2tag' ),
    37         'livejournal' => array( __( 'LiveJournal' ), __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ), 'install' ),
    38         'movabletype' => array( __('Movable Type and TypePad'), __('Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.'), 'install', 'mt' ),
    39         'opml' => array( __('Blogroll'), __('Install the blogroll importer to import links in OPML format.'), 'install' ),
    40         'rss' => array( __('RSS'), __('Install the RSS importer to import posts from an RSS feed.'), 'install' ),
    41         'tumblr' => array( __('Tumblr'), __('Install the Tumblr importer to import posts & media from Tumblr using their API.'), 'install' ),
    42         'wordpress' => array( 'WordPress', __('Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.'), 'install' )
    43     );
     32if ( current_user_can( 'install_plugins' ) )
     33    $popular_importers = wp_get_popular_importers();
     34else
     35    $popular_importers = array();
    4436
    45 if ( ! empty( $_GET['invalid'] ) && !empty($popular_importers[$_GET['invalid']][3]) ) {
    46     wp_redirect( admin_url('import.php?import=' . $popular_importers[$_GET['invalid']][3]) );
    47     exit;
     37// Detect and redirect invalid importers like 'movabletype', which is registered as 'mt'
     38if ( ! empty( $_GET['invalid'] ) && isset( $popular_importers[ $_GET['invalid'] ] ) ) {
     39    $importer_id = $popular_importers[ $_GET['invalid'] ]['importer-id'];
     40    if ( $importer_id != $_GET['invalid'] ) { // Prevent redirect loops.
     41        wp_redirect( admin_url( 'admin.php?import=' . $importer_id ) );
     42        exit;
     43    }
     44    unset( $importer_id );
    4845}
    4946
     
    6966// If a popular importer is not registered, create a dummy registration that links to the plugin installer.
    7067foreach ( $popular_importers as $pop_importer => $pop_data ) {
    71     if ( isset( $importers[$pop_importer] ) )
     68    if ( isset( $importers[ $pop_importer ] ) )
    7269        continue;
    73     if ( isset( $pop_data[3] ) && isset( $importers[ $pop_data[3] ] ) )
     70    if ( isset( $importers[ $pop_data['importer-id'] ] ) )
    7471        continue;
    75 
    76     $importers[$pop_importer] = $popular_importers[$pop_importer];
     72    $importers[ $pop_data['importer-id'] ] = array( $pop_data['name'], $pop_data['description'], 'install' => $pop_data['plugin-slug'] );
    7773}
    7874
    79 if ( empty($importers) ) {
    80     echo '<p>'.__('No importers are available.').'</p>'; // TODO: make more helpful
     75if ( empty( $importers ) ) {
     76    echo '<p>' . __('No importers are available.') . '</p>'; // TODO: make more helpful
    8177} else {
    82     uasort($importers, create_function('$a, $b', 'return strcmp($a[0], $b[0]);'));
     78    uasort($importers, create_function('$a, $b', 'return strnatcasecmp($a[0], $b[0]);'));
    8379?>
    8480<table class="widefat importers" cellspacing="0">
    8581
    8682<?php
    87     $style = '';
    88     foreach ($importers as $id => $data) {
    89         $style = ('class="alternate"' == $style || 'class="alternate active"' == $style) ? '' : 'alternate';
     83    $alt = '';
     84    foreach ($importers as $importer_id => $data) {
    9085        $action = '';
    91         if ( 'install' == $data[2] ) {
    92             $plugin_slug = $id . '-importer';
     86        if ( isset( $data['install'] ) ) {
     87            $plugin_slug = $data['install'];
    9388            if ( file_exists( WP_PLUGIN_DIR . '/' . $plugin_slug ) ) {
    9489                // Looks like Importer is installed, But not active
     
    112107            }
    113108        } else {
    114             $action = "<a href='" . esc_url("admin.php?import=$id") . "' title='" . esc_attr( wptexturize(strip_tags($data[1])) ) ."'>{$data[0]}</a>";
     109            $action = "<a href='" . esc_url( "admin.php?import=$importer_id" ) . "' title='" . esc_attr( wptexturize( strip_tags( $data[1] ) ) ) ."'>{$data[0]}</a>";
    115110        }
    116111
    117         if ($style != '')
    118             $style = 'class="'.$style.'"';
     112        $alt = $alt ? '' : ' class="alternate"';
    119113        echo "
    120             <tr $style>
     114            <tr$alt>
    121115                <td class='import-system row-title'>$action</td>
    122116                <td class='desc'>{$data[1]}</td>
  • trunk/wp-admin/includes/import.php

    r21996 r22632  
    9494    return array( 'file' => $file, 'id' => $id );
    9595}
     96
     97/**
     98 * Returns a list from WordPress.org of popular importer plugins.
     99 *
     100 * @since 3.5.0
     101 *
     102 * @return array Importers with metadata for each.
     103 */
     104function wp_get_popular_importers() {
     105    include ABSPATH . WPINC . '/version.php'; // include an unmodified $wp_version
     106
     107    $locale = get_locale();
     108    $popular_importers = get_site_transient( 'popular_importers_' . $locale );
     109
     110    if ( ! $popular_importers ) {
     111        $url = add_query_arg( 'locale', get_locale(), 'http://api.wordpress.org/core/importers/1.0/' );
     112        $options = array( 'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url() );
     113        $popular_importers = maybe_unserialize( wp_remote_retrieve_body( wp_remote_get( $url, $options ) ) );
     114
     115        if ( is_array( $popular_importers ) )
     116            set_site_transient( 'popular_importers_' . $locale, $popular_importers, 2 * DAY_IN_SECONDS );
     117        else
     118            $popular_importers = false;
     119    }
     120
     121    if ( is_array( $popular_importers ) ) {
     122        // If the data was received as translated, return it as-is.
     123        if ( $popular_importers['translated'] )
     124            return $popular_importers['importers'];
     125
     126        foreach ( $popular_importers['importers'] as &$importer ) {
     127            $importer['description'] = translate( $importer['description'] );
     128            if ( $importer['name'] != 'WordPress' )
     129                $importer['name'] = translate( $importer['name'] );
     130        }
     131        return $popular_importers['importers'];
     132    }
     133
     134    return array(
     135        // slug => name, description, plugin slug, and register_importer() slug
     136        'blogger' => array(
     137            'name' => __( 'Blogger' ),
     138            'description' => __( 'Install the Blogger importer to import posts, comments, and users from a Blogger blog.' ),
     139            'plugin-slug' => 'blogger-importer',
     140            'importer-id' => 'blogger',
     141        ),
     142        'wpcat2tag' => array(
     143            'name' => __( 'Categories and Tags Converter' ),
     144            'description' => __( 'Install the category/tag converter to convert existing categories to tags or tags to categories, selectively.' ),
     145            'plugin-slug' => 'wpcat2tag-importer',
     146            'importer-id' => 'wp-cat2tag',
     147        ),
     148        'livejournal' => array(
     149            'name' => __( 'LiveJournal' ),
     150            'description' => __( 'Install the LiveJournal importer to import posts from LiveJournal using their API.' ),
     151            'plugin-slug' => 'livejournal-importer',
     152            'importer-id' => 'livejournal',
     153        ),
     154        'movabletype' => array(
     155            'name' => __( 'Movable Type and TypePad' ),
     156            'description' => __( 'Install the Movable Type importer to import posts and comments from a Movable Type or TypePad blog.' ),
     157            'plugin-slug' => 'movabletype-importer',
     158            'importer-id' => 'mt',
     159        ),
     160        'opml' => array(
     161            'name' => __( 'Blogroll' ),
     162            'description' => __( 'Install the blogroll importer to import links in OPML format.' ),
     163            'plugin-slug' => 'opml-importer',
     164            'importer-id' => 'opml',
     165        ),
     166        'rss' => array(
     167            'name' => __( 'RSS' ),
     168            'description' => __( 'Install the RSS importer to import posts from an RSS feed.' ),
     169            'plugin-slug' => 'rss-importer',
     170            'importer-id' => 'rss',
     171        ),
     172        'tumblr' => array(
     173            'name' => __( 'Tumblr' ),
     174            'description' => __( 'Install the Tumblr importer to import posts &amp; media from Tumblr using their API.' ),
     175            'plugin-slug' => 'tumblr-importer',
     176            'importer-id' => 'tumblr',
     177        ),
     178        'wordpress' => array(
     179            'name' => 'WordPress',
     180            'description' => __( 'Install the WordPress importer to import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.' ),
     181            'plugin-slug' => 'wordpress-importer',
     182            'importer-id' => 'wordpress',
     183        ),
     184    );
     185}
Note: See TracChangeset for help on using the changeset viewer.