RWMB_Image_Field

The Evolved RWMB Image Field class.

Defined (1)

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

/meta-box/inc/fields/image.php  
  1. class RWMB_Image_Field extends RWMB_File_Field 
  2. /** 
  3. * Enqueue scripts and styles 
  4. * @return void 
  5. */ 
  6. static function admin_enqueue_scripts() 
  7. // Enqueue same scripts and styles as for file field 
  8. parent::admin_enqueue_scripts(); 
  9.  
  10. wp_enqueue_style( 'rwmb-image', RWMB_CSS_URL . 'image.css', array(), RWMB_VER ); 
  11.  
  12. wp_enqueue_script( 'rwmb-image', RWMB_JS_URL . 'image.js', array( 'jquery-ui-sortable', 'wp-ajax-response' ), RWMB_VER, true ); 
  13.  
  14. /** 
  15. * Add actions 
  16. * @return void 
  17. */ 
  18. static function add_actions() 
  19. // Do same actions as file field 
  20. parent::add_actions(); 
  21.  
  22. // Reorder images via Ajax 
  23. add_action( 'wp_ajax_rwmb_reorder_images', array( __CLASS__, 'wp_ajax_reorder_images' ) ); 
  24.  
  25. /** 
  26. * Ajax callback for reordering images 
  27. * @return void 
  28. */ 
  29. static function wp_ajax_reorder_images() 
  30. $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; 
  31. $field_id = isset( $_POST['field_id'] ) ? $_POST['field_id'] : 0; 
  32. $order = isset( $_POST['order'] ) ? $_POST['order'] : 0; 
  33.  
  34. check_admin_referer( "rwmb-reorder-images_{$field_id}" ); 
  35.  
  36. parse_str( $order, $items ); 
  37. $items = $items['item']; 
  38. $order = 1; 
  39.  
  40. // Delete old meta values 
  41. delete_post_meta( $post_id, $field_id ); 
  42. foreach ( $items as $item ) 
  43. wp_update_post( array( 
  44. 'ID' => $item,  
  45. 'post_parent' => $post_id,  
  46. 'menu_order' => $order ++ 
  47. ) ); 
  48.  
  49. // Save images in that order to meta field 
  50. // That helps retrieving values easier 
  51. add_post_meta( $post_id, $field_id, $item, false ); 
  52.  
  53. RW_Meta_Box::ajax_response( __( 'Order saved', 'rwmb' ), 'success' ); 
  54.  
  55. /** 
  56. * Get field HTML 
  57. * @param string $html 
  58. * @param mixed $meta 
  59. * @param array $field 
  60. * @return string 
  61. */ 
  62. static function html( $html, $meta, $field ) 
  63. global $wpdb; 
  64.  
  65. $i18n_msg = _x( 'Uploaded files', 'image upload', 'rwmb' ); 
  66. $i18n_del_file = _x( 'Delete this file', 'image upload', 'rwmb' ); 
  67. $i18n_delete = _x( 'Delete', 'image upload', 'rwmb' ); 
  68. $i18n_edit = _x( 'Edit', 'image upload', 'rwmb' ); 
  69. $i18n_title = _x( 'Upload files', 'image upload', 'rwmb' ); 
  70. $i18n_more = _x( '+ Add new image', 'image upload', 'rwmb' ); 
  71.  
  72. $html = wp_nonce_field( "rwmb-delete-file_{$field['id']}", "nonce-delete-file_{$field['id']}", false, false ); 
  73. $html .= wp_nonce_field( "rwmb-reorder-images_{$field['id']}", "nonce-reorder-images_{$field['id']}", false, false ); 
  74. $html .= "<input type='hidden' class='field-id' value='{$field['id']}' />"; 
  75.  
  76. // Uploaded images 
  77. if ( ! empty( $meta ) ) 
  78. $html .= "<h4>{$i18n_msg}</h4>"; 
  79. $html .= "<ul class='rwmb-images rwmb-uploaded'>"; 
  80.  
  81. foreach ( $meta as $image ) 
  82. $src = wp_get_attachment_image_src( $image, 'thumbnail' ); 
  83. $src = $src[0]; 
  84. $link = get_edit_post_link( $image ); 
  85.  
  86. $html .= "<li id='item_{$image}'> 
  87. <img src='{$src}' /> 
  88. <div class='rwmb-image-bar'> 
  89. <a title='{$i18n_edit}' class='rwmb-edit-file' href='{$link}' target='_blank'>{$i18n_edit}</a> | 
  90. <a title='{$i18n_del_file}' class='rwmb-delete-file' href='#' rel='{$image}'>{$i18n_delete}</a> 
  91. </div> 
  92. </li>"; 
  93.  
  94. $html .= '</ul>'; 
  95.  
  96. // Show form upload 
  97. $html .= " 
  98. <h4>{$i18n_title}</h4> 
  99. <div class='new-files'> 
  100. <div class='file-input'><input type='file' name='{$field['id']}[]' /></div> 
  101. <a class='rwmb-add-file' href='#'><strong>{$i18n_more}</strong></a> 
  102. </div>"; 
  103.  
  104. return $html;