MS_Model_Import_File

Base class for all import handlers.

Defined (1)

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

/app/model/import/class-ms-model-import-file.php  
  1. class MS_Model_Import_File extends MS_Model_Import { 
  2.  
  3. /** 
  4. * This function parses the Import source (i.e. an file-upload) and returns 
  5. * true in case the source data is valid. When returning true then the 
  6. * $source property of the model is set to the sanitized import source data. 
  7. * @since 1.0.0 
  8. * @return bool 
  9. */ 
  10. public function prepare() { 
  11. self::_message( 'preview', false ); 
  12.  
  13. if ( empty( $_FILES ) || ! isset( $_FILES['upload'] ) ) { 
  14. self::_message( 'error', __( 'No file was uploaded, please try again.', 'membership2' ) ); 
  15. return false; 
  16.  
  17. $file = $_FILES['upload']; 
  18. if ( empty( $file['name'] ) ) { 
  19. self::_message( 'error', __( 'Please upload an export file.', 'membership2' ) ); 
  20. return false; 
  21. if ( empty( $file['size'] ) ) { 
  22. self::_message( 'error', __( 'The uploaded file is empty, please try again.', 'membership2' ) ); 
  23. return false; 
  24.  
  25. if ( ! is_uploaded_file( $file['tmp_name'] ) ) { 
  26. self::_message( 'error', __( 'Uploaded file not found, please try again.', 'membership2' ) ); 
  27. return false; 
  28.  
  29. $content = file_get_contents( $file['tmp_name'] ); 
  30. try { 
  31. $data = json_decode( $content ); 
  32. } catch( Exception $ex ) { 
  33. $data = (object) array(); 
  34.  
  35. $data = $this->validate_object( $data ); 
  36.  
  37. if ( empty( $data ) ) { 
  38. self::_message( 'error', __( 'No valid export file uploaded, please try again.', 'membership2' ) ); 
  39. return false; 
  40.  
  41. $this->source = $data; 
  42. return true; 
  43.  
  44. /** 
  45. * Returns true if the specific import-source is present and can be used 
  46. * for import. 
  47. * @since 1.0.0 
  48. * @return bool 
  49. */ 
  50. static public function present() { 
  51. return true; 
  52.