class-import-settings.php 2.6 KB

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