PageRenderTime 28ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/wp-overrides.php

https://gitlab.com/aristath/timber
PHP | 253 lines | 151 code | 54 blank | 48 comment | 57 complexity | 381019cbd6d948c24eff126be9941544 MD5 | raw file
  1. <?php
  2. class WP_Overrides {
  3. public static function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrides = array( 'test_form' => false ) ) {
  4. $time = current_time( 'mysql' );
  5. if ( $post = get_post( $post_id ) ) {
  6. if ( substr( $post->post_date, 0, 4 ) > 0 )
  7. $time = $post->post_date;
  8. }
  9. $name = $_FILES[$file_id]['name'];
  10. $file = self::wp_handle_upload( $_FILES[$file_id], $overrides, $time );
  11. if ( isset( $file['error'] ) )
  12. return new WP_Error( 'upload_error', $file['error'] );
  13. $name_parts = pathinfo( $name );
  14. $name = trim( substr( $name, 0, -( 1 + strlen( $name_parts['extension'] ) ) ) );
  15. $url = $file['url'];
  16. $type = $file['type'];
  17. $file = $file['file'];
  18. $title = $name;
  19. $content = '';
  20. if ( preg_match( '#^audio#', $type ) ) {
  21. $meta = wp_read_audio_metadata( $file );
  22. if ( ! empty( $meta['title'] ) )
  23. $title = $meta['title'];
  24. $content = '';
  25. if ( ! empty( $title ) ) {
  26. if ( ! empty( $meta['album'] ) && ! empty( $meta['artist'] ) ) {
  27. /* translators: 1: audio track title, 2: album title, 3: artist name */
  28. $content .= sprintf( __( '"%1$s" from %2$s by %3$s.' ), $title, $meta['album'], $meta['artist'] );
  29. } else if ( ! empty( $meta['album'] ) ) {
  30. /* translators: 1: audio track title, 2: album title */
  31. $content .= sprintf( __( '"%1$s" from %2$s.' ), $title, $meta['album'] );
  32. } else if ( ! empty( $meta['artist'] ) ) {
  33. /* translators: 1: audio track title, 2: artist name */
  34. $content .= sprintf( __( '"%1$s" by %2$s.' ), $title, $meta['artist'] );
  35. } else {
  36. $content .= sprintf( __( '"%s".' ), $title );
  37. }
  38. } else if ( ! empty( $meta['album'] ) ) {
  39. if ( ! empty( $meta['artist'] ) ) {
  40. /* translators: 1: audio album title, 2: artist name */
  41. $content .= sprintf( __( '%1$s by %2$s.' ), $meta['album'], $meta['artist'] );
  42. } else {
  43. $content .= $meta['album'] . '.';
  44. }
  45. } else if ( ! empty( $meta['artist'] ) ) {
  46. $content .= $meta['artist'] . '.';
  47. }
  48. if ( ! empty( $meta['year'] ) )
  49. $content .= ' ' . sprintf( __( 'Released: %d.' ), $meta['year'] );
  50. if ( ! empty( $meta['track_number'] ) ) {
  51. $track_number = explode( '/', $meta['track_number'] );
  52. if ( isset( $track_number[1] ) )
  53. $content .= ' ' . sprintf( __( 'Track %1$s of %2$s.' ), number_format_i18n( $track_number[0] ), number_format_i18n( $track_number[1] ) );
  54. else
  55. $content .= ' ' . sprintf( __( 'Track %1$s.' ), number_format_i18n( $track_number[0] ) );
  56. }
  57. if ( ! empty( $meta['genre'] ) )
  58. $content .= ' ' . sprintf( __( 'Genre: %s.' ), $meta['genre'] );
  59. // use image exif/iptc data for title and caption defaults if possible
  60. } elseif ( $image_meta = @wp_read_image_metadata( $file ) ) {
  61. if ( trim( $image_meta['title'] ) && ! is_numeric( sanitize_title( $image_meta['title'] ) ) )
  62. $title = $image_meta['title'];
  63. if ( trim( $image_meta['caption'] ) )
  64. $content = $image_meta['caption'];
  65. }
  66. // Construct the attachment array
  67. $attachment = array_merge( array(
  68. 'post_mime_type' => $type,
  69. 'guid' => $url,
  70. 'post_parent' => $post_id,
  71. 'post_title' => $title,
  72. 'post_content' => $content,
  73. ), $post_data );
  74. // This should never be set as it would then overwrite an existing attachment.
  75. if ( isset( $attachment['ID'] ) )
  76. unset( $attachment['ID'] );
  77. // Save the data
  78. $id = wp_insert_attachment( $attachment, $file, $post_id );
  79. if ( !is_wp_error( $id ) ) {
  80. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );
  81. }
  82. return $id;
  83. }
  84. static function wp_handle_upload( &$file, $overrides = false, $time = null ) {
  85. // The default error handler.
  86. if ( ! function_exists( 'wp_handle_upload_error' ) ) {
  87. function wp_handle_upload_error( &$file, $message ) {
  88. return array( 'error'=>$message );
  89. }
  90. }
  91. /**
  92. * Filter data for the current file to upload.
  93. *
  94. * @since 2.9.0
  95. *
  96. * @param array $file An array of data for a single file.
  97. */
  98. $file = apply_filters( 'wp_handle_upload_prefilter', $file );
  99. // You may define your own function and pass the name in $overrides['upload_error_handler']
  100. $upload_error_handler = 'wp_handle_upload_error';
  101. // You may have had one or more 'wp_handle_upload_prefilter' functions error out the file. Handle that gracefully.
  102. if ( isset( $file['error'] ) && !is_numeric( $file['error'] ) && $file['error'] )
  103. return $upload_error_handler( $file, $file['error'] );
  104. // You may define your own function and pass the name in $overrides['unique_filename_callback']
  105. $unique_filename_callback = null;
  106. // $_POST['action'] must be set and its value must equal $overrides['action'] or this:
  107. $action = 'wp_handle_upload';
  108. // Courtesy of php.net, the strings that describe the error indicated in $_FILES[{form field}]['error'].
  109. $upload_error_strings = array( false,
  110. __( "The uploaded file exceeds the upload_max_filesize directive in php.ini." ),
  111. __( "The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form." ),
  112. __( "The uploaded file was only partially uploaded." ),
  113. __( "No file was uploaded." ),
  114. '',
  115. __( "Missing a temporary folder." ),
  116. __( "Failed to write file to disk." ),
  117. __( "File upload stopped by extension." ) );
  118. // All tests are on by default. Most can be turned off by $overrides[{test_name}] = false;
  119. $test_form = true;
  120. $test_size = true;
  121. $test_upload = true;
  122. // If you override this, you must provide $ext and $type!!!!
  123. $test_type = true;
  124. $mimes = false;
  125. // Install user overrides. Did we mention that this voids your warranty?
  126. if ( is_array( $overrides ) )
  127. extract( $overrides, EXTR_OVERWRITE );
  128. // A correct form post will pass this test.
  129. if ( $test_form && ( !isset( $_POST['action'] ) || ( $_POST['action'] != $action ) ) )
  130. return call_user_func( $upload_error_handler, $file, __( 'Invalid form submission.' ) );
  131. // A successful upload will pass this test. It makes no sense to override this one.
  132. if ( isset( $file['error'] ) && $file['error'] > 0 ) {
  133. return call_user_func( $upload_error_handler, $file, $upload_error_strings[ $file['error'] ] );
  134. }
  135. // A non-empty file will pass this test.
  136. if ( $test_size && !( $file['size'] > 0 ) ) {
  137. if ( is_multisite() )
  138. $error_msg = __( 'File is empty. Please upload something more substantial.' );
  139. else
  140. $error_msg = __( 'File is empty. Please upload something more substantial. This error could also be caused by uploads being disabled in your php.ini or by post_max_size being defined as smaller than upload_max_filesize in php.ini.' );
  141. return call_user_func( $upload_error_handler, $file, $error_msg );
  142. }
  143. // A properly uploaded file will pass this test. There should be no reason to override this one.
  144. if ( $test_upload && ! @ file_exists( $file['tmp_name'] ) )
  145. return call_user_func( $upload_error_handler, $file, __( 'Specified file failed upload test.' ) );
  146. // A correct MIME type will pass this test. Override $mimes or use the upload_mimes filter.
  147. if ( $test_type ) {
  148. $wp_filetype = wp_check_filetype_and_ext( $file['tmp_name'], $file['name'], $mimes );
  149. extract( $wp_filetype );
  150. // Check to see if wp_check_filetype_and_ext() determined the filename was incorrect
  151. if ( $proper_filename )
  152. $file['name'] = $proper_filename;
  153. if ( ( !$type || !$ext ) && !current_user_can( 'unfiltered_upload' ) )
  154. return call_user_func( $upload_error_handler, $file, __( 'Sorry, this file type is not permitted for security reasons.' ) );
  155. if ( !$ext )
  156. $ext = ltrim( strrchr( $file['name'], '.' ), '.' );
  157. if ( !$type )
  158. $type = $file['type'];
  159. } else {
  160. $type = '';
  161. }
  162. // A writable uploads dir will pass this test. Again, there's no point overriding this one.
  163. if ( ! ( ( $uploads = wp_upload_dir( $time ) ) && false === $uploads['error'] ) )
  164. return call_user_func( $upload_error_handler, $file, $uploads['error'] );
  165. $filename = wp_unique_filename( $uploads['path'], $file['name'], $unique_filename_callback );
  166. // Move the file to the uploads dir
  167. $new_file = $uploads['path'] . "/$filename";
  168. if ( false === @ copy( $file['tmp_name'], $new_file ) ) {
  169. if ( 0 === strpos( $uploads['basedir'], ABSPATH ) )
  170. $error_path = str_replace( ABSPATH, '', $uploads['basedir'] ) . $uploads['subdir'];
  171. else
  172. $error_path = basename( $uploads['basedir'] ) . $uploads['subdir'];
  173. return $upload_error_handler( $file, sprintf( __( 'The uploaded file could not be moved to %s.' ), $error_path ) );
  174. }
  175. // Set correct file permissions
  176. $stat = stat( dirname( $new_file ) );
  177. $perms = $stat['mode'] & 0000666;
  178. @ chmod( $new_file, $perms );
  179. // Compute the URL
  180. $url = $uploads['url'] . "/$filename";
  181. if ( is_multisite() )
  182. delete_transient( 'dirsize_cache' );
  183. /**
  184. * Filter the data array for the uploaded file.
  185. *
  186. * @since 2.1.0
  187. *
  188. * @param array $upload {
  189. * Array of upload data.
  190. *
  191. * @type string $file Filename of the newly-uploaded file.
  192. * @type string $url URL of the uploaded file.
  193. * @type string $type File type.
  194. * }
  195. * @param string $context The type of upload action. Accepts 'upload' or 'sideload'.
  196. */
  197. return apply_filters( 'wp_handle_upload', array( 'file' => $new_file, 'url' => $url, 'type' => $type ), 'upload' );
  198. }
  199. }