| 1 |
<?php |
| 2 |
/** |
| 3 |
* WPSEO plugin file. |
| 4 |
* |
| 5 |
* @package WPSEO\Admin\Import |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Class WPSEO_Import_Settings. |
| 10 |
* |
| 11 |
* Class with functionality to import the Yoast SEO settings. |
| 12 |
*/ |
| 13 |
class WPSEO_Import_Settings { |
| 14 |
|
| 15 |
/** |
| 16 |
* Nonce action key. |
| 17 |
* |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
public const NONCE_ACTION = 'wpseo-import-settings'; |
| 21 |
|
| 22 |
/** |
| 23 |
* Holds the import status instance. |
| 24 |
* |
| 25 |
* @var WPSEO_Import_Status |
| 26 |
*/ |
| 27 |
public $status; |
| 28 |
|
| 29 |
/** |
| 30 |
* Holds the old WPSEO version. |
| 31 |
* |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $old_wpseo_version; |
| 35 |
|
| 36 |
/** |
| 37 |
* Class constructor. |
| 38 |
*/ |
| 39 |
public function __construct() { |
| 40 |
$this->status = new WPSEO_Import_Status( 'import', false ); |
| 41 |
} |
| 42 |
|
| 43 |
/** |
| 44 |
* Imports the data submitted by the user. |
| 45 |
* |
| 46 |
* @return void |
| 47 |
*/ |
| 48 |
public function import() { |
| 49 |
check_admin_referer( self::NONCE_ACTION ); |
| 50 |
|
| 51 |
if ( ! WPSEO_Capability_Utils::current_user_can( 'wpseo_manage_options' ) ) { |
| 52 |
return; |
| 53 |
} |
| 54 |
|
| 55 |
if ( ! isset( $_POST['settings_import'] ) || ! is_string( $_POST['settings_import'] ) ) { |
| 56 |
return; |
| 57 |
} |
| 58 |
|
| 59 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: The raw content will be parsed afterwards. |
| 60 |
$content = wp_unslash( $_POST['settings_import'] ); |
| 61 |
|
| 62 |
if ( empty( $content ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$this->parse_options( $content ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Parse the options. |
| 71 |
* |
| 72 |
* @param string $raw_options The content to parse. |
| 73 |
* |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
protected function parse_options( $raw_options ) { |
| 77 |
$options = parse_ini_string( $raw_options, true, INI_SCANNER_RAW ); |
| 78 |
|
| 79 |
if ( is_array( $options ) && $options !== [] ) { |
| 80 |
$this->import_options( $options ); |
| 81 |
|
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
$this->status->set_msg( __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . __( 'No settings found.', 'wordpress-seo' ) ); |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Parse the option group and import it. |
| 90 |
* |
| 91 |
* @param string $name Name string. |
| 92 |
* @param array $option_group Option group data. |
| 93 |
* @param array $options Options data. |
| 94 |
* |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
protected function parse_option_group( $name, $option_group, $options ) { |
| 98 |
// Make sure that the imported options are cleaned/converted on import. |
| 99 |
$option_instance = WPSEO_Options::get_option_instance( $name ); |
| 100 |
if ( is_object( $option_instance ) && method_exists( $option_instance, 'import' ) ) { |
| 101 |
$option_instance->import( $option_group, $this->old_wpseo_version, $options ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Imports the options if found. |
| 107 |
* |
| 108 |
* @param array $options The options parsed from the provided settings. |
| 109 |
* |
| 110 |
* @return void |
| 111 |
*/ |
| 112 |
protected function import_options( $options ) { |
| 113 |
if ( isset( $options['wpseo']['version'] ) && $options['wpseo']['version'] !== '' ) { |
| 114 |
$this->old_wpseo_version = $options['wpseo']['version']; |
| 115 |
} |
| 116 |
|
| 117 |
foreach ( $options as $name => $option_group ) { |
| 118 |
$this->parse_option_group( $name, $option_group, $options ); |
| 119 |
} |
| 120 |
|
| 121 |
$this->status->set_msg( __( 'Settings successfully imported.', 'wordpress-seo' ) ); |
| 122 |
$this->status->set_status( true ); |
| 123 |
|
| 124 |
// Reset the cached option values. |
| 125 |
WPSEO_Options::clear_cache(); |
| 126 |
} |
| 127 |
} |
| 128 |
|