colorway_import_file

The ColorWay colorway import file function.

Description

colorway_import_file( $file, (int) $post_id = 0, (string) $import_date = 'file' ); 

Parameters (3)

0. $file
The file.
1. $post_id — Optional. (int)
The post id.
2. $import_date — Optional. (string) => 'file'
The import date.

Usage

  1. if ( !function_exists( 'colorway_import_file' ) ) { 
  2. require_once '/functions.php'; 
  3.  
  4. // The file. 
  5. $file = null; 
  6.  
  7. // The post id. 
  8. $post_id = -1; 
  9.  
  10. // The import date. 
  11. $import_date = 'file'; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = colorway_import_file($file, $post_id, $import_date); 
  15.  

Defined (1)

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

/functions.php  
  1. function colorway_import_file($file, $post_id = 0, $import_date = 'file') { 
  2. set_time_limit(120); 
  3.  
  4. // Initially, Base it on the -current- time. 
  5. $time = current_time('mysql', 1); 
  6. // Next, If it's post to base the upload off: 
  7.  
  8. $time = gmdate('Y-m-d H:i:s', @filemtime($file)); 
  9.  
  10.  
  11. // A writable uploads dir will pass this test. Again, there's no point overriding this one. 
  12. if (!( ( $uploads = wp_upload_dir($time) ) && false === $uploads[error] )) { 
  13. return new WP_Error('upload_error', $uploads[error]); 
  14.  
  15. $wp_filetype = wp_check_filetype($file, null); 
  16.  
  17. extract($wp_filetype); 
  18.  
  19. if ((!$type || !$ext ) && !current_user_can('unfiltered_upload')) { 
  20. return new WP_Error('wrong_file_type', __('Sorry, this file type is not permitted for security reasons.', 'colorway')); //A WP-core string.. 
  21.  
  22. $file_name = str_replace('\\', '/', $file); 
  23.  
  24. if (preg_match('|^' . preg_quote(str_replace('\\', '/', $uploads['basedir'])) . '(.*)$|i', $file_name, $mat)) { 
  25. $filename = basename($file); 
  26. $new_file = $file; 
  27. $url = $uploads['baseurl'] . $mat[1]; 
  28. $attachment = get_posts(array('post_type' => 'attachment', 'meta_key' => '_wp_attached_file', 'meta_value' => ltrim($mat[1], '/'))); 
  29. if (!empty($attachment)) { 
  30. return new WP_Error('file_exists', __('Sorry, That file already exists in the WordPress media library.', 'colorway')); 
  31.  
  32. //Ok, Its in the uploads folder, But NOT in WordPress's media library. 
  33. if ('file' == $import_date) { 
  34. $time = @filemtime($file); 
  35. if (preg_match("|(\d+)/(\d+)|", $mat[1], $datemat)) { //So lets set the date of the import to the date folder its in, IF its in a date folder. 
  36. $hour = $min = $sec = 0; 
  37. $day = 1; 
  38. $year = $datemat[1]; 
  39. $month = $datemat[2]; 
  40.  
  41. // If the files datetime is set, and it's in the same region of upload directory, set the minute details to that too, else, override it. 
  42. if ($time && date('Y-m', $time) == "$year-$month") { 
  43. list($hour, $min, $sec, $day) = explode(';', date('H;i;s;j', $time)); 
  44. $time = mktime($hour, $min, $sec, $month, $day, $year); 
  45. $time = gmdate('Y-m-d H:i:s', $time); 
  46.  
  47. // A new time has been found! Get the new uploads folder: 
  48. // A writable uploads dir will pass this test. Again, there's no point overriding this one. 
  49. if (!( ( $uploads = wp_upload_dir($time) ) && false === $uploads[error] )) 
  50. return new WP_Error('upload_error', $uploads[error]); 
  51. $url = $uploads['baseurl'] . $mat[1]; 
  52. } else { 
  53. $filename = wp_unique_filename($uploads['path'], basename($file)); 
  54. // copy the file to the uploads dir 
  55. $new_file = $uploads['path'] . '/' . $filename; 
  56. if (false === @copy($file, $new_file)) 
  57. return new WP_Error('upload_error', sprintf(__('The selected file could not be copied to %s.', 'colorway'), $uploads['path'])); 
  58.  
  59. // Set correct file permissions 
  60. $stat = stat(dirname($new_file)); 
  61. $perms = $stat['mode'] & 0000666; 
  62. @ chmod($new_file, $perms); 
  63. // Compute the URL 
  64. $url = $uploads['url'] . '/' . $filename; 
  65.  
  66. if ('file' == $import_date) 
  67. $time = gmdate('Y-m-d H:i:s', @filemtime($file)); 
  68.  
  69. //Apply upload filters 
  70. $return = apply_filters('wp_handle_upload', array('file' => $new_file, 'url' => $url, 'type' => $type)); 
  71. $new_file = $return['file']; 
  72. $url = $return['url']; 
  73. $type = $return['type']; 
  74.  
  75. $title = preg_replace('!\.[^.]+$!', '', basename($file)); 
  76. $content = ''; 
  77.  
  78. if ($time) { 
  79. $post_date_gmt = $time; 
  80. $post_date = $time; 
  81. } else { 
  82. $post_date = current_time('mysql'); 
  83. $post_date_gmt = current_time('mysql', 1); 
  84.  
  85. // Construct the attachment array 
  86. $attachment = array( 
  87. 'post_mime_type' => $type,  
  88. 'guid' => $url,  
  89. 'post_parent' => $post_id,  
  90. 'post_title' => $title,  
  91. 'post_name' => $title,  
  92. 'post_content' => $content,  
  93. 'post_date' => $post_date,  
  94. 'post_date_gmt' => $post_date_gmt 
  95. ); 
  96.  
  97. $attachment = apply_filters('afs-import_details', $attachment, $file, $post_id, $import_date); 
  98.  
  99. //Win32 fix: 
  100. $new_file = str_replace(strtolower(str_replace('\\', '/', $uploads['basedir'])), $uploads['basedir'], $new_file); 
  101.  
  102. // Save the data 
  103.  
  104. $id = wp_insert_attachment($attachment, $new_file, $post_id); 
  105. if (!is_wp_error($id)) { 
  106. $data = wp_generate_attachment_metadata($id, $new_file); 
  107. wp_update_attachment_metadata($id, $data); 
  108. //update_post_meta( $id, '_wp_attached_file', $uploads['subdir'] . '/' . $filename ); 
  109.  
  110. return $id;