RWMB_File_Field

The Evolved RWMB File Field class.

Defined (1)

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

/meta-box/inc/fields/file.php  
  1. class RWMB_File_Field 
  2. /** 
  3. * Enqueue scripts and styles 
  4. * @return void 
  5. */ 
  6. static function admin_enqueue_scripts() 
  7. wp_enqueue_script( 'rwmb-file', RWMB_JS_URL . 'file.js', array( 'jquery', 'wp-ajax-response' ), RWMB_VER, true ); 
  8.  
  9. /** 
  10. * Add actions 
  11. * @return void 
  12. */ 
  13. static function add_actions() 
  14. // Add data encoding type for file uploading 
  15. add_action( 'post_edit_form_tag', array( __CLASS__, 'post_edit_form_tag' ) ); 
  16.  
  17. // Delete file via Ajax 
  18. add_action( 'wp_ajax_rwmb_delete_file', array( __CLASS__, 'wp_ajax_delete_file' ) ); 
  19.  
  20. /** 
  21. * Add data encoding type for file uploading 
  22. * @return void 
  23. */ 
  24. static function post_edit_form_tag() 
  25. echo ' enctype="multipart/form-data"'; 
  26.  
  27. /** 
  28. * Ajax callback for deleting files. 
  29. * Modified from a function used by "Verve Meta Boxes" plugin 
  30. * @link http://goo.gl/LzYSq 
  31. * @return void 
  32. */ 
  33. static function wp_ajax_delete_file() 
  34. $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; 
  35. $field_id = isset( $_POST['field_id'] ) ? $_POST['field_id'] : 0; 
  36. $attachment_id = isset( $_POST['attachment_id'] ) ? intval( $_POST['attachment_id'] ) : 0; 
  37.  
  38. check_admin_referer( "rwmb-delete-file_{$field_id}" ); 
  39.  
  40. $ok = delete_post_meta( $post_id, $field_id, $attachment_id ); 
  41. $ok = $ok && wp_delete_attachment( $attachment_id ); 
  42.  
  43. if ( $ok ) 
  44. RW_Meta_Box::ajax_response( '', 'success' ); 
  45. else 
  46. RW_Meta_Box::ajax_response( __( "Error: Cannot delete file", 'rwmb' ), 'error' ); 
  47.  
  48. /** 
  49. * Get field HTML 
  50. * @param string $html 
  51. * @param mixed $meta 
  52. * @param array $field 
  53. * @return string 
  54. */ 
  55. static function html( $html, $meta, $field ) 
  56. $i18n_msg = _x( 'Uploaded files', 'file upload', 'rwmb' ); 
  57. $i18n_del_file = _x( 'Delete this file', 'file upload', 'rwmb' ); 
  58. $i18n_delete = _x( 'Delete', 'file upload', 'rwmb' ); 
  59. $i18n_title = _x( 'Upload files', 'file upload', 'rwmb' ); 
  60. $i18n_more = _x( '+ Add new file', 'file upload', 'rwmb' ); 
  61.  
  62. $html = wp_nonce_field( "rwmb-delete-file_{$field['id']}", "nonce-delete-file_{$field['id']}", false, false ); 
  63. $html .= "<input type='hidden' class='field-id' value='{$field['id']}' />"; 
  64.  
  65. // Uploaded files 
  66. if ( ! empty( $meta ) ) 
  67. $html .= "<h4>{$i18n_msg}</h4>"; 
  68. $html .= '<ol class="rwmb-uploaded">'; 
  69.  
  70. foreach ( $meta as $attachment_id ) 
  71. $attachment = wp_get_attachment_link( $attachment_id ); 
  72. $html .= "<li>{$attachment} (<a title='{$i18n_del_file}' class='rwmb-delete-file' href='#' rel='{$attachment_id}'>{$i18n_delete}</a>)</li>"; 
  73.  
  74. $html .= '</ol>'; 
  75.  
  76. // Show form upload 
  77. $html .= " 
  78. <h4>{$i18n_title}</h4> 
  79. <div class='new-files'> 
  80. <div class='file-input'><input type='file' name='{$field['id']}[]' /></div> 
  81. <a class='rwmb-add-file' href='#'><strong>{$i18n_more}</strong></a> 
  82. </div> 
  83. "; 
  84.  
  85. return $html; 
  86.  
  87. /** 
  88. * Save file field 
  89. * @param mixed $new 
  90. * @param mixed $old 
  91. * @param int $post_id 
  92. * @param array $field 
  93. */ 
  94. static function save( $new, $old, $post_id, $field ) 
  95. $name = $field['id']; 
  96. if ( empty( $_FILES[ $name ] ) ) 
  97. return; 
  98.  
  99. $files = self::fix_file_array( $_FILES[ $name ] ); 
  100.  
  101. foreach ( $files as $file_item ) 
  102. $file = wp_handle_upload( $file_item, array( 'test_form' => false ) ); 
  103.  
  104. if ( ! isset( $file['file'] ) ) 
  105. continue; 
  106.  
  107. $file_name = $file['file']; 
  108.  
  109. $attachment = array( 
  110. 'post_mime_type' => $file['type'],  
  111. 'guid' => $file['url'],  
  112. 'post_parent' => $post_id,  
  113. 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file_name ) ),  
  114. 'post_content' => '' 
  115. ); 
  116. $id = wp_insert_attachment( $attachment, $file_name, $post_id ); 
  117.  
  118. if ( ! is_wp_error( $id ) ) 
  119. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file_name ) ); 
  120.  
  121. // Save file ID in meta field 
  122. add_post_meta( $post_id, $name, $id, false ); 
  123.  
  124. /** 
  125. * Fixes the odd indexing of multiple file uploads from the format: 
  126. * $_FILES['field']['key']['index'] 
  127. * To the more standard and appropriate: 
  128. * $_FILES['field']['index']['key'] 
  129. * @param array $files 
  130. * @return array 
  131. */ 
  132. static function fix_file_array( $files ) 
  133. $output = array(); 
  134. foreach ( $files as $key => $list ) 
  135. foreach ( $list as $index => $value ) 
  136. $output[$index][$key] = $value; 
  137. return $output;