RWMB_Plupload_Image_Field

The Evolved RWMB Plupload Image Field class.

Defined (1)

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

/meta-box/inc/fields/plupload-image.php  
  1. class RWMB_Plupload_Image_Field extends RWMB_Image_Field 
  2. /** 
  3. * Add field actions 
  4. * @return void 
  5. */ 
  6. static function add_actions() 
  7. parent::add_actions(); 
  8. add_action( 'wp_ajax_plupload_image_upload', array( __CLASS__ , 'handle_upload' ) ); 
  9.  
  10. /** 
  11. * Upload 
  12. * Ajax callback function 
  13. * @return error or (XML-)response 
  14. */ 
  15. static function handle_upload () 
  16. check_admin_referer( 'rwmb-upload-images_' . $_REQUEST['field_id'] ); 
  17.  
  18. $post_id = 0; 
  19. if ( is_numeric( $_REQUEST['post_id'] ) ) 
  20. $post_id = (int) $_REQUEST['post_id']; 
  21.  
  22. // You can use WP's wp_handle_upload() function: 
  23. $file = $_FILES['async-upload']; 
  24. $file_attr = wp_handle_upload( $file, array( 
  25. 'test_form'=> true,  
  26. 'action' => 'plupload_image_upload' 
  27. ) ); 
  28. $attachment = array( 
  29. 'post_mime_type' => $file_attr['type'],  
  30. 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ),  
  31. 'post_content' => '',  
  32. 'post_status' => 'inherit' 
  33. ); 
  34.  
  35. // Adds file as attachment to WordPress 
  36. $id = wp_insert_attachment( $attachment, $file_attr['file'], $post_id ); 
  37. if ( ! is_wp_error( $id ) ) 
  38. $response = new WP_Ajax_Response(); 
  39. wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file_attr['file'] ) ); 
  40. if ( isset( $_REQUEST['field_id'] ) ) 
  41. // Save file ID in meta field 
  42. add_post_meta( $post_id, $_REQUEST['field_id'], $id, false ); 
  43. $response->add( array( 
  44. 'what' => 'rwmb_image_response',  
  45. 'data' => self::img_html( $id ) 
  46. ) ); 
  47. $response->send(); 
  48. // Faster than die(); 
  49. exit; 
  50.  
  51. /** 
  52. * Enqueue scripts and styles 
  53. * @return void 
  54. */ 
  55. static function admin_enqueue_scripts() 
  56. // Enqueue same scripts and styles as for file field 
  57. parent::admin_enqueue_scripts(); 
  58. wp_enqueue_style( 'rwmb-plupload-image', RWMB_CSS_URL . 'plupload-image.css', array(), RWMB_VER ); 
  59. wp_enqueue_script( 'rwmb-plupload-image', RWMB_JS_URL . 'plupload-image.js', array( 'jquery-ui-sortable', 'wp-ajax-response', 'plupload-all' ), RWMB_VER, true ); 
  60. wp_localize_script( 'rwmb-plupload-image', 'RWMB', array('url' => RWMB_URL) ); 
  61. wp_localize_script( 'rwmb-plupload-image', 'rwmb_plupload_defaults', array( 
  62. 'runtimes' => 'html5, silverlight, flash, html4',  
  63. 'file_data_name' => 'async-upload',  
  64. 'multiple_queues' => true,  
  65. 'max_file_size' => wp_max_upload_size() . 'b',  
  66. 'url' => admin_url( 'admin-ajax.php' ),  
  67. 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ),  
  68. 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ),  
  69. 'filters' => array( array( 
  70. 'title' => _x( 'Allowed Image Files', 'image upload', 'rwmb' ),  
  71. 'extensions' => 'jpg, jpeg, gif, png' 
  72. ) ),  
  73. 'multipart' => true,  
  74. 'urlstream_upload' => true,  
  75. ) ); 
  76.  
  77. /** 
  78. * Get image html 
  79. * @param int $img_id 
  80. * @return string 
  81. */ 
  82. static function img_html( $img_id ) 
  83. $i18n_del_file = _x( 'Delete this file', 'image upload', 'rwmb' ); 
  84. $i18n_delete = _x( 'Delete', 'image upload', 'rwmb' ); 
  85. $i18n_edit = _x( 'Edit', 'image upload', 'rwmb' ); 
  86.  
  87. $src = wp_get_attachment_image_src( $img_id, 'thumbnail' ); 
  88. $src = $src[0]; 
  89. $link = get_edit_post_link( $img_id ); 
  90.  
  91. $html = <<<HTML 
  92. <li id='item_{$img_id}'> 
  93. <img src='{$src}' /> 
  94. <div class='rwmb-image-bar'> 
  95. <a title='{$i18n_edit}' class='rwmb-edit-file' href='{$link}' target='_blank'>{$i18n_edit}</a> | 
  96. <a title='{$i18n_del_file}' class='rwmb-delete-file' href='#' rel='{$img_id}'>{$i18n_delete}</a> 
  97. </div> 
  98. </li> 
  99. HTML; 
  100. return $html; 
  101.  
  102. /** 
  103. * Get field HTML 
  104. * @param string $html 
  105. * @param mixed $meta 
  106. * @param array $field 
  107. * @return string 
  108. */ 
  109. static function html( $html, $meta, $field ) 
  110. if ( ! is_array( $meta ) ) 
  111. $meta = (array) $meta; 
  112.  
  113. $i18n_msg = _x( 'Uploaded files', 'image upload', 'rwmb' ); 
  114. $i18n_title = _x( 'Upload files', 'image upload', 'rwmb' ); 
  115.  
  116. // Filter to change the drag & drop box background string 
  117. $i18n_drop = apply_filters( 'rwmb_upload_drop_string', _x( 'Drop images here', 'image upload', 'rwmb' ) ); 
  118. $i18n_or = _x( 'or', 'image upload', 'rwmb' ); 
  119. $i18n_select = _x( 'Select Files', 'image upload', 'rwmb' ); 
  120. $img_prefix = $field['id']; 
  121.  
  122. $html = wp_nonce_field( "rwmb-delete-file_{$field['id']}", "nonce-delete-file_{$field['id']}", false, false ); 
  123. $html .= wp_nonce_field( "rwmb-reorder-images_{$field['id']}", "nonce-reorder-images_{$field['id']}", false, false ); 
  124. $html .= wp_nonce_field( "rwmb-upload-images_{$field['id']}", "nonce-upload-images_{$field['id']}", false, false ); 
  125. $html .= "<input type='hidden' class='field-id rwmb-image-prefix' value='{$field['id']}' />"; 
  126.  
  127. // Uploaded images 
  128. $html .= "<div id='{$img_prefix}-container'>"; 
  129.  
  130. // Check for max_file_uploads 
  131. $class = 'rwmb-drag-drop hide-if-no-js'; 
  132. if ( array_key_exists( 'max_file_uploads', $field ) ) 
  133. $max_file_uploads = $field['max_file_uploads']; 
  134. $html .= "<input class='max_file_uploads' type='hidden' value='{$max_file_uploads}' />"; 
  135. if ( count( $meta ) >= $max_file_uploads ) 
  136. $class = RW_Meta_Box::add_cssclass( 'hidden', $class ); 
  137.  
  138. $html .= "<h4 class='rwmb-uploaded-title'>{$i18n_msg}</h4>"; 
  139. $html .= "<ul class='rwmb-images rwmb-uploaded'>"; 
  140. foreach ( $meta as $image ) 
  141. $html .= self::img_html($image); 
  142. $html .= '</ul>'; 
  143.  
  144. // Show form upload 
  145. $html .= " 
  146. <h4>{$i18n_title}</h4> 
  147. <div id='{$img_prefix}-dragdrop' class='{$class}'> 
  148. <div class = 'rwmb-drag-drop-inside'> 
  149. <p>{$i18n_drop}</p> 
  150. <p>{$i18n_or}</p> 
  151. <p><input id='{$img_prefix}-browse-button' type='button' value='{$i18n_select}' class='button' /></p> 
  152. </div> 
  153. </div> 
  154. "; 
  155.  
  156. $html .= "</div>"; 
  157.  
  158. return $html;