WPSEO_Import

Class WPSEO_Import.

Defined (1)

The class is defined in the following location(s).

/admin/class-import.php  
  1. class WPSEO_Import { 
  2.  
  3. /** 
  4. * Message about the import 
  5. * @var string 
  6. */ 
  7. public $msg = ''; 
  8.  
  9. /** @var bool $success If import was a success flag. */ 
  10. public $success = false; 
  11.  
  12. /** 
  13. * @var array 
  14. */ 
  15. private $file; 
  16.  
  17. /** 
  18. * @var string 
  19. */ 
  20. private $filename; 
  21.  
  22. /** 
  23. * @var string 
  24. */ 
  25. private $old_wpseo_version = null; 
  26.  
  27. /** 
  28. * @var string 
  29. */ 
  30. private $path; 
  31.  
  32. /** 
  33. * @var array 
  34. */ 
  35. private $upload_dir; 
  36.  
  37. /** 
  38. * Class constructor 
  39. */ 
  40. public function __construct() { 
  41. if ( ! $this->handle_upload() ) { 
  42. return; 
  43.  
  44. $this->determine_path(); 
  45.  
  46. if ( ! $this->unzip_file() ) { 
  47. $this->clean_up(); 
  48.  
  49. return; 
  50.  
  51. $this->parse_options(); 
  52.  
  53. $this->clean_up(); 
  54.  
  55. /** 
  56. * Handle the file upload 
  57. * @return boolean 
  58. */ 
  59. private function handle_upload() { 
  60. $overrides = array( 'mimes' => array( 'zip' => 'application/zip' ) ); // Explicitly allow zip in multisite. 
  61. $this->file = wp_handle_upload( $_FILES['settings_import_file'], $overrides ); 
  62.  
  63. if ( is_wp_error( $this->file ) ) { 
  64. $this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . $this->file->get_error_message(); 
  65.  
  66. return false; 
  67.  
  68. if ( is_array( $this->file ) && isset( $this->file['error'] ) ) { 
  69. $this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . $this->file['error']; 
  70.  
  71. return false; 
  72.  
  73. if ( ! isset( $this->file['file'] ) ) { 
  74. $this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . __( 'Upload failed.', 'wordpress-seo' ); 
  75.  
  76. return false; 
  77.  
  78. return true; 
  79.  
  80. /** 
  81. * Determine the path to the import file 
  82. */ 
  83. private function determine_path() { 
  84. $this->upload_dir = wp_upload_dir(); 
  85.  
  86. if ( ! defined( 'DIRECTORY_SEPARATOR' ) ) { 
  87. define( 'DIRECTORY_SEPARATOR', '/' ); 
  88. $this->path = $this->upload_dir['basedir'] . DIRECTORY_SEPARATOR . 'wpseo-import' . DIRECTORY_SEPARATOR; 
  89.  
  90. if ( ! isset( $GLOBALS['wp_filesystem'] ) || ! is_object( $GLOBALS['wp_filesystem'] ) ) { 
  91. WP_Filesystem(); 
  92.  
  93. /** 
  94. * Unzip the file 
  95. * @return boolean 
  96. */ 
  97. private function unzip_file() { 
  98. $unzipped = unzip_file( $this->file['file'], $this->path ); 
  99. if ( is_wp_error( $unzipped ) ) { 
  100. $this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . sprintf( __( 'Unzipping failed with error "%s".', 'wordpress-seo' ), $unzipped->get_error_message() ); 
  101.  
  102. return false; 
  103.  
  104. $this->filename = $this->path . 'settings.ini'; 
  105. if ( ! is_file( $this->filename ) || ! is_readable( $this->filename ) ) { 
  106. $this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . __( 'Unzipping failed - file settings.ini not found.', 'wordpress-seo' ); 
  107.  
  108. return false; 
  109.  
  110. return true; 
  111.  
  112. /** 
  113. * Parse the option file 
  114. */ 
  115. private function parse_options() { 
  116. $options = parse_ini_file( $this->filename, true ); 
  117.  
  118. if ( is_array( $options ) && $options !== array() ) { 
  119. if ( isset( $options['wpseo']['version'] ) && $options['wpseo']['version'] !== '' ) { 
  120. $this->old_wpseo_version = $options['wpseo']['version']; 
  121. foreach ( $options as $name => $opt_group ) { 
  122. $this->parse_option_group( $name, $opt_group, $options ); 
  123. $this->msg = __( 'Settings successfully imported.', 'wordpress-seo' ); 
  124. $this->success = true; 
  125. else { 
  126. $this->msg = __( 'Settings could not be imported:', 'wordpress-seo' ) . ' ' . __( 'No settings found in file.', 'wordpress-seo' ); 
  127.  
  128. /** 
  129. * Parse the option group and import it 
  130. * @param string $name Name string. 
  131. * @param array $opt_group Option group data. 
  132. * @param array $options Options data. 
  133. */ 
  134. private function parse_option_group( $name, $opt_group, $options ) { 
  135. if ( $name === 'wpseo_taxonomy_meta' ) { 
  136. $opt_group = json_decode( urldecode( $opt_group['wpseo_taxonomy_meta'] ), true ); 
  137.  
  138. // Make sure that the imported options are cleaned/converted on import. 
  139. $option_instance = WPSEO_Options::get_option_instance( $name ); 
  140. if ( is_object( $option_instance ) && method_exists( $option_instance, 'import' ) ) { 
  141. $option_instance->import( $opt_group, $this->old_wpseo_version, $options ); 
  142. elseif ( WP_DEBUG === true || ( defined( 'WPSEO_DEBUG' ) && WPSEO_DEBUG === true ) ) { 
  143. $this->msg = sprintf( __( 'Setting "%s" is no longer used and has been discarded.', 'wordpress-seo' ), $name ); 
  144.  
  145. /** 
  146. * Remove the files 
  147. */ 
  148. private function clean_up() { 
  149. if ( file_exists( $this->filename ) && is_writable( $this->filename ) ) { 
  150. unlink( $this->filename ); 
  151. if ( ! empty( $this->file['file'] ) && file_exists( $this->file['file'] ) && is_writable( $this->file['file'] ) ) { 
  152. unlink( $this->file['file'] ); 
  153. if ( file_exists( $this->path ) && is_writable( $this->path ) ) { 
  154. $wp_file = new WP_Filesystem_Direct( $this->path ); 
  155. $wp_file->rmdir( $this->path, true );