1 <?php
2
3 4 5 6 7 8 9
10 class Fieldmanager_Media extends Fieldmanager_Field {
11
12 13 14 15
16 public $field_class = 'media';
17
18 19 20 21
22 public $button_label;
23
24 25 26 27
28 public $modal_button_label;
29
30 31 32 33
34 public $modal_title;
35
36 37 38 39
40 public $thumbnail_class = 'thumbnail';
41
42 43 44 45 46 47
48 public $preview_size = 'thumbnail';
49
50 51 52 53 54 55
56 public $mime_type = 'all';
57
58 59 60 61
62 public static $has_registered_media = false;
63
64 65 66 67 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 84 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 );
93 }
94
95 96 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 107 108 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' ) . ' ';
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 }