| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* Manages the compatibility with WordPress Importer. |
| 8 |
* |
| 9 |
* @since 2.8 |
| 10 |
*/ |
| 11 |
class PLL_WordPress_Importer { |
| 12 |
|
| 13 |
/** |
| 14 |
* Setups filters. |
| 15 |
* |
| 16 |
* @since 2.8 |
| 17 |
*/ |
| 18 |
public function __construct() { |
| 19 |
add_action( 'init', array( $this, 'maybe_wordpress_importer' ) ); |
| 20 |
add_filter( 'wp_import_terms', array( $this, 'wp_import_terms' ) ); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* If WordPress Importer is active, replace the wordpress_importer_init function. |
| 25 |
* |
| 26 |
* @since 1.2 |
| 27 |
*/ |
| 28 |
public function maybe_wordpress_importer() { |
| 29 |
if ( defined( 'WP_LOAD_IMPORTERS' ) && class_exists( 'WP_Import' ) ) { |
| 30 |
remove_action( 'admin_init', 'wordpress_importer_init' ); |
| 31 |
add_action( 'admin_init', array( $this, 'wordpress_importer_init' ) ); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Loads our child class PLL_WP_Import instead of WP_Import. |
| 37 |
* |
| 38 |
* @since 1.2 |
| 39 |
*/ |
| 40 |
public function wordpress_importer_init() { |
| 41 |
$class = new ReflectionClass( 'WP_Import' ); |
| 42 |
load_plugin_textdomain( 'wordpress-importer', false, basename( dirname( $class->getFileName() ) ) . '/languages' ); |
| 43 |
|
| 44 |
$GLOBALS['wp_import'] = new PLL_WP_Import(); |
| 45 |
register_importer( 'wordpress', 'WordPress', __( 'Import <strong>posts, pages, comments, custom fields, categories, and tags</strong> from a WordPress export file.', 'polylang' ), array( $GLOBALS['wp_import'], 'dispatch' ) ); // phpcs:ignore WordPress.WP.CapitalPDangit.Misspelled |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Sets the flag when importing a language and the file has been exported with Polylang < 1.8. |
| 50 |
* |
| 51 |
* @since 1.8 |
| 52 |
* |
| 53 |
* @param array $terms An array of arrays containing terms information form the WXR file. |
| 54 |
* @return array |
| 55 |
*/ |
| 56 |
public function wp_import_terms( $terms ) { |
| 57 |
$languages = include POLYLANG_DIR . '/settings/languages.php'; |
| 58 |
|
| 59 |
foreach ( $terms as $key => $term ) { |
| 60 |
if ( 'language' === $term['term_taxonomy'] ) { |
| 61 |
$description = maybe_unserialize( $term['term_description'] ); |
| 62 |
if ( empty( $description['flag_code'] ) && isset( $languages[ $description['locale'] ] ) ) { |
| 63 |
$description['flag_code'] = $languages[ $description['locale'] ]['flag']; |
| 64 |
$terms[ $key ]['term_description'] = maybe_serialize( $description ); |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
return $terms; |
| 69 |
} |
| 70 |
} |
| 71 |
|