Fieldmanager
  • Package
  • Class
  • Tree
  • Todo

Packages

  • Fieldmanager
    • Context
    • Datasource
    • Field
    • Util
  • None

Classes

  • Fieldmanager_Autocomplete
  • Fieldmanager_Checkbox
  • Fieldmanager_Checkboxes
  • Fieldmanager_Colorpicker
  • Fieldmanager_Datepicker
  • Fieldmanager_DraggablePost
  • Fieldmanager_Field
  • Fieldmanager_Grid
  • Fieldmanager_Group
  • Fieldmanager_Hidden
  • Fieldmanager_Link
  • Fieldmanager_Media
  • Fieldmanager_Options
  • Fieldmanager_Password
  • Fieldmanager_Radios
  • Fieldmanager_RichTextArea
  • Fieldmanager_Select
  • Fieldmanager_TextArea
  • Fieldmanager_TextField
  1 <?php
  2 
  3 /**
  4  * A field to select an attachment via the WordPress Media Manager.
  5  *
  6  * This field submits the selected attachment as an attachment ID (post ID).
  7  *
  8  * @package Fieldmanager_Field
  9  */
 10 class Fieldmanager_Media extends Fieldmanager_Field {
 11 
 12     /**
 13      * @var string
 14      * Override field_class
 15      */
 16     public $field_class = 'media';
 17 
 18     /**
 19      * @var string
 20      * Button Label
 21      */
 22     public $button_label;
 23 
 24     /**
 25      * @var string
 26      * Button label in the media modal popup
 27      */
 28     public $modal_button_label;
 29 
 30     /**
 31      * @var string
 32      * Title of the media modal popup
 33      */
 34     public $modal_title;
 35 
 36     /**
 37      * @var string
 38      * Class to attach to thumbnail media display
 39      */
 40     public $thumbnail_class = 'thumbnail';
 41 
 42     /**
 43      * @var string
 44      * Which size a preview image should be.
 45      * Should be a string (e.g. "thumbnail", "large", or some size created with add_image_size)
 46      * You can use an array here
 47      */
 48     public $preview_size = 'thumbnail';
 49 
 50     /**
 51      * @var string
 52      * What mime types are available to choose from.
 53      * Valid options are "all" or a partial or full mimetype (e.g. "image" or
 54      * "application/pdf").
 55      */
 56     public $mime_type = 'all';
 57 
 58     /**
 59      * @var boolean
 60      * Static variable so we only load media JS once
 61      */
 62     public static $has_registered_media = false;
 63 
 64     /**
 65      * Construct default attributes
 66      * @param string $label
 67      * @param array $options
 68      */
 69     public function __construct( $label = '', $options = array() ) {
 70         $this->button_label       = __( 'Attach a File', 'fieldmanager' );
 71         $this->modal_button_label = __( 'Select Attachment', 'fieldmanager' );
 72         $this->modal_title        = __( 'Choose an Attachment', 'fieldmanager' );
 73 
 74         add_action( 'admin_print_scripts', array( $this, 'admin_print_scripts' ) );
 75         if ( ! self::$has_registered_media ) {
 76             fm_add_script( 'fm_media', 'js/media/fieldmanager-media.js', array( 'jquery' ), '1.0.3' );
 77             self::$has_registered_media = true;
 78         }
 79         parent::__construct( $label, $options );
 80     }
 81 
 82     /**
 83      * Hook into admin_print_scripts action to enqueue the media for the current
 84      * post
 85      */
 86     public function admin_print_scripts() {
 87         $post = get_post();
 88         $args = array();
 89         if ( isset( $post ) && $post->ID ) {
 90             $args['post'] = $post->ID;
 91         }
 92         wp_enqueue_media( $args ); // generally on post pages this will not have an impact.
 93     }
 94 
 95     /**
 96      * Presave; ensure that the value is an absolute integer
 97      */
 98     public function presave( $value, $current_value = array() ) {
 99         if ( $value == 0 || !is_numeric( $value ) ) {
100             return NULL;
101         }
102         return absint( $value );
103     }
104 
105     /**
106      * Form element
107      * @param mixed $value
108      * @return string HTML
109      */
110     public function form_element( $value = array() ) {
111         if ( is_numeric( $value ) && $value > 0 ) {
112             $attachment = get_post( $value );
113             if ( strpos( $attachment->post_mime_type, 'image/' ) === 0 ) {
114                 $preview = esc_html__( 'Uploaded image:', 'fieldmanager' ) . '<br />';
115                 $preview .= '<a href="#">' . wp_get_attachment_image( $value, $this->preview_size, false, array( 'class' => $this->thumbnail_class ) ) . '</a>';
116             } else {
117                 $preview = esc_html__( 'Uploaded file:', 'fieldmanager' ) . '&nbsp;';
118                 $preview .= wp_get_attachment_link( $value, $this->preview_size, True, True, $attachment->post_title );
119             }
120             $preview .= sprintf( '<br /><a href="#" class="fm-media-remove fm-delete">%s</a>', esc_html__( 'remove', 'fieldmanager' ) );
121             $preview = apply_filters( 'fieldmanager_media_preview', $preview, $value, $attachment );
122         } else {
123             $preview = '';
124         }
125         return sprintf(
126             '<input type="button" class="fm-media-button button-secondary fm-incrementable" id="%1$s" value="%3$s" data-choose="%7$s" data-update="%8$s" data-preview-size="%6$s" data-mime-type="%9$s" %10$s />
127             <input type="hidden" name="%2$s" value="%4$s" class="fm-element fm-media-id" />
128             <div class="media-wrapper">%5$s</div>',
129             esc_attr( $this->get_element_id() ),
130             esc_attr( $this->get_form_name() ),
131             esc_attr( $this->button_label ),
132             esc_attr( $value ),
133             $preview,
134             esc_attr( $this->preview_size ),
135             esc_attr( $this->modal_title ),
136             esc_attr( $this->modal_button_label ),
137             esc_attr( $this->mime_type ),
138             $this->get_element_attributes()
139         );
140     }
141 
142 }
Fieldmanager API documentation generated by ApiGen 2.8.0