Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 26 additions & 42 deletions src/wp-admin/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,8 @@
'<p>' . __( '<a href="https://wordpress.org/support/forums/">Support forums</a>' ) . '</p>'
);

if ( current_user_can( 'install_plugins' ) ) {
// List of popular importer plugins from the WordPress.org API.
$popular_importers = wp_get_popular_importers();
} else {
$popular_importers = array();
}
// List of popular importer plugins from the WordPress.org API.
$popular_importers = wp_get_popular_importers();

// Detect and redirect invalid importers like 'movabletype', which is registered as 'mt'.
if ( ! empty( $_GET['invalid'] ) && isset( $popular_importers[ $_GET['invalid'] ] ) ) {
Expand Down Expand Up @@ -78,25 +74,8 @@
<p><?php _e( 'If you have posts or comments in another system, WordPress can import those into this site. To get started, choose a system to import from below:' ); ?></p>

<?php
// Registered (already installed) importers. They're stored in the global $wp_importers.
$importers = get_importers();

// If a popular importer is not registered, create a dummy registration that links to the plugin installer.
foreach ( $popular_importers as $pop_importer => $pop_data ) {
if ( isset( $importers[ $pop_importer ] ) ) {
continue;
}
if ( isset( $importers[ $pop_data['importer-id'] ] ) ) {
continue;
}

// Fill the array of registered (already installed) importers with data of the popular importers from the WordPress.org API.
$importers[ $pop_data['importer-id'] ] = array(
$pop_data['name'],
$pop_data['description'],
'install' => $pop_data['plugin-slug'],
);
}
// Registered importers plus popular importer plugins from the WordPress.org API.
$importers = wp_get_available_importers( $popular_importers );

if ( empty( $importers ) ) {
echo '<p>' . __( 'No importers are available.' ) . '</p>'; // TODO: Make more helpful.
Expand All @@ -120,24 +99,29 @@
if ( ! empty( $plugins ) ) {
$keys = array_keys( $plugins );
$plugin_file = $plugin_slug . '/' . $keys[0];
$url = wp_nonce_url(
add_query_arg(
array(
'action' => 'activate',
'plugin' => $plugin_file,
'from' => 'import',

if ( current_user_can( 'activate_plugin', $plugin_file ) ) {
$url = wp_nonce_url(
add_query_arg(
array(
'action' => 'activate',
'plugin' => $plugin_file,
'from' => 'import',
),
admin_url( 'plugins.php' )
),
admin_url( 'plugins.php' )
),
'activate-plugin_' . $plugin_file
);
$action = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
esc_url( $url ),
/* translators: %s: Importer name. */
esc_attr( sprintf( __( 'Run %s' ), $data[0] ) ),
__( 'Run Importer' )
);
'activate-plugin_' . $plugin_file
);
$action = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
esc_url( $url ),
/* translators: %s: Importer name. */
esc_attr( sprintf( __( 'Run %s' ), $data[0] ) ),
__( 'Run Importer' )
);
} else {
$action = __( 'This importer is installed but not active. Please activate it from the Plugins screen.' );
}

$is_plugin_installed = true;
}
Expand Down
41 changes: 41 additions & 0 deletions src/wp-admin/includes/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,53 @@
*/
function get_importers() {
global $wp_importers;

if ( is_array( $wp_importers ) ) {
uasort( $wp_importers, '_usort_by_first_member' );
}

return $wp_importers;
}

/**
* Retrieves the list of available importers for the Import screen.
*
* This includes registered importers and popular importer plugins from WordPress.org
* that can be installed or activated.
*
* @since 7.1.0
*
* @param array|null $popular_importers Optional. Popular importer data keyed by slug.
* Defaults to the result of wp_get_popular_importers().
* @return array Available importers.
*/
function wp_get_available_importers( ?array $popular_importers = null ) {
$importers = get_importers();

if ( null === $popular_importers ) {
$popular_importers = wp_get_popular_importers();
}

// If a popular importer is not registered, create a dummy registration that links to the plugin installer.
foreach ( $popular_importers as $pop_importer => $pop_data ) {
if ( isset( $importers[ $pop_importer ] ) ) {
continue;
}

if ( isset( $importers[ $pop_data['importer-id'] ] ) ) {
continue;
}

$importers[ $pop_data['importer-id'] ] = array(
$pop_data['name'],
$pop_data['description'],
'install' => $pop_data['plugin-slug'],
);
}

return $importers;
}

/**
* Sorts a multidimensional array by first member of each top level member.
*
Expand Down
41 changes: 41 additions & 0 deletions tests/phpunit/tests/import/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Tests_Import_Import extends WP_UnitTestCase {
*/
public function test_ordering_of_importers() {
global $wp_importers;

$_wp_importers = $wp_importers; // Preserve global state.
$wp_importers = array(
'xyz1' => array( 'xyz1' ),
Expand All @@ -27,6 +28,46 @@ public function test_ordering_of_importers() {
),
get_importers()
);

$wp_importers = $_wp_importers; // Restore global state.
}

/**
* @covers ::wp_get_available_importers
*/
public function test_wp_get_available_importers_adds_popular_importers() {
global $wp_importers;

$_wp_importers = $wp_importers; // Preserve global state.
$wp_importers = array(
'xyz1' => array( 'xyz1', 'Registered importer', '__return_null' ),
);

$popular_importers = array(
'wordpress' => array(
'name' => 'WordPress',
'description' => 'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.',
'plugin-slug' => 'wordpress-importer',
'importer-id' => 'wordpress',
),
);

$available_importers = wp_get_available_importers( $popular_importers );

$this->assertArrayHasKey( 'xyz1', $available_importers );
$this->assertSame( array( 'xyz1', 'Registered importer', '__return_null' ), $available_importers['xyz1'] );

// phpcs:ignore WordPress.WP.CapitalPDangit.MisspelledInText -- Importer slugs are lowercase.
$this->assertArrayHasKey( 'wordpress', $available_importers );
$this->assertSame(
array(
'WordPress',
'Import posts, pages, comments, custom fields, categories, and tags from a WordPress export file.',
'install' => 'wordpress-importer',
),
$available_importers['wordpress']
);

$wp_importers = $_wp_importers; // Restore global state.
}
}
Loading