file.php
| 1 | <?php |
| 2 | |
| 3 | class acf_field_file extends acf_field |
| 4 | { |
| 5 | |
| 6 | /* |
| 7 | * __construct |
| 8 | * |
| 9 | * Set name / label needed for actions / filters |
| 10 | * |
| 11 | * @since 3.6 |
| 12 | * @date 23/01/13 |
| 13 | */ |
| 14 | |
| 15 | function __construct() |
| 16 | { |
| 17 | // vars |
| 18 | $this->name = 'file'; |
| 19 | $this->label = __("File",'acf'); |
| 20 | $this->category = __("Content",'acf'); |
| 21 | $this->defaults = array( |
| 22 | 'save_format' => 'object', |
| 23 | 'library' => 'all' |
| 24 | ); |
| 25 | $this->l10n = array( |
| 26 | 'select' => __("Select File",'acf'), |
| 27 | 'edit' => __("Edit File",'acf'), |
| 28 | 'update' => __("Update File",'acf'), |
| 29 | 'uploadedTo' => __("Uploaded to this post",'acf'), |
| 30 | ); |
| 31 | |
| 32 | |
| 33 | // do not delete! |
| 34 | parent::__construct(); |
| 35 | |
| 36 | |
| 37 | // filters |
| 38 | add_filter('get_media_item_args', array($this, 'get_media_item_args')); |
| 39 | add_filter('wp_prepare_attachment_for_js', array($this, 'wp_prepare_attachment_for_js'), 10, 3); |
| 40 | |
| 41 | |
| 42 | // JSON |
| 43 | add_action('wp_ajax_acf/fields/file/get_files', array($this, 'ajax_get_files')); |
| 44 | add_action('wp_ajax_nopriv_acf/fields/file/get_files', array($this, 'ajax_get_files'), 10, 1); |
| 45 | } |
| 46 | |
| 47 | |
| 48 | /* |
| 49 | * create_field() |
| 50 | * |
| 51 | * Create the HTML interface for your field |
| 52 | * |
| 53 | * @param $field - an array holding all the field's data |
| 54 | * |
| 55 | * @type action |
| 56 | * @since 3.6 |
| 57 | * @date 23/01/13 |
| 58 | */ |
| 59 | |
| 60 | function create_field( $field ) |
| 61 | { |
| 62 | // vars |
| 63 | $o = array( |
| 64 | 'class' => '', |
| 65 | 'icon' => '', |
| 66 | 'title' => '', |
| 67 | 'size' => '', |
| 68 | 'url' => '', |
| 69 | 'name' => '', |
| 70 | ); |
| 71 | |
| 72 | if( $field['value'] && is_numeric($field['value']) ) |
| 73 | { |
| 74 | $file = get_post( $field['value'] ); |
| 75 | |
| 76 | if( $file ) |
| 77 | { |
| 78 | $o['class'] = 'active'; |
| 79 | $o['icon'] = wp_mime_type_icon( $file->ID ); |
| 80 | $o['title'] = $file->post_title; |
| 81 | $o['size'] = size_format(filesize( get_attached_file( $file->ID ) )); |
| 82 | $o['url'] = wp_get_attachment_url( $file->ID ); |
| 83 | |
| 84 | $explode = explode('/', $o['url']); |
| 85 | $o['name'] = end( $explode ); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | ?> |
| 91 | <div class="acf-file-uploader clearfix <?php echo $o['class']; ?>" data-library="<?php echo $field['library']; ?>"> |
| 92 | <input class="acf-file-value" type="hidden" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>" /> |
| 93 | <div class="has-file"> |
| 94 | <ul class="hl clearfix"> |
| 95 | <li> |
| 96 | <img class="acf-file-icon" src="<?php echo $o['icon']; ?>" alt=""/> |
| 97 | <div class="hover"> |
| 98 | <ul class="bl"> |
| 99 | <li><a href="#" class="acf-button-delete ir">Remove</a></li> |
| 100 | <li><a href="#" class="acf-button-edit ir">Edit</a></li> |
| 101 | </ul> |
| 102 | </div> |
| 103 | </li> |
| 104 | <li> |
| 105 | <p> |
| 106 | <strong class="acf-file-title"><?php echo $o['title']; ?></strong> |
| 107 | </p> |
| 108 | <p> |
| 109 | <strong><?php _e('Name', 'acf'); ?>:</strong> |
| 110 | <a class="acf-file-name" href="<?php echo $o['url']; ?>" target="_blank"><?php echo $o['name']; ?></a> |
| 111 | </p> |
| 112 | <p> |
| 113 | <strong><?php _e('Size', 'acf'); ?>:</strong> |
| 114 | <span class="acf-file-size"><?php echo $o['size']; ?></span> |
| 115 | </p> |
| 116 | |
| 117 | </li> |
| 118 | </ul> |
| 119 | </div> |
| 120 | <div class="no-file"> |
| 121 | <ul class="hl clearfix"> |
| 122 | <li> |
| 123 | <span><?php _e('No File Selected','acf'); ?></span>. <a href="#" class="button add-file"><?php _e('Add File','acf'); ?></a> |
| 124 | </li> |
| 125 | </ul> |
| 126 | </div> |
| 127 | </div> |
| 128 | <?php |
| 129 | } |
| 130 | |
| 131 | |
| 132 | /* |
| 133 | * create_options() |
| 134 | * |
| 135 | * Create extra options for your field. This is rendered when editing a field. |
| 136 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
| 137 | * |
| 138 | * @type action |
| 139 | * @since 3.6 |
| 140 | * @date 23/01/13 |
| 141 | * |
| 142 | * @param $field - an array holding all the field's data |
| 143 | */ |
| 144 | |
| 145 | function create_options( $field ) |
| 146 | { |
| 147 | // vars |
| 148 | $key = $field['name']; |
| 149 | |
| 150 | ?> |
| 151 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 152 | <td class="label"> |
| 153 | <label><?php _e("Return Value",'acf'); ?></label> |
| 154 | </td> |
| 155 | <td> |
| 156 | <?php |
| 157 | |
| 158 | do_action('acf/create_field', array( |
| 159 | 'type' => 'radio', |
| 160 | 'name' => 'fields['.$key.'][save_format]', |
| 161 | 'value' => $field['save_format'], |
| 162 | 'layout' => 'horizontal', |
| 163 | 'choices' => array( |
| 164 | 'object' => __("File Object",'acf'), |
| 165 | 'url' => __("File URL",'acf'), |
| 166 | 'id' => __("File ID",'acf') |
| 167 | ) |
| 168 | )); |
| 169 | |
| 170 | ?> |
| 171 | </td> |
| 172 | </tr> |
| 173 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 174 | <td class="label"> |
| 175 | <label><?php _e("Library",'acf'); ?></label> |
| 176 | </td> |
| 177 | <td> |
| 178 | <?php |
| 179 | |
| 180 | do_action('acf/create_field', array( |
| 181 | 'type' => 'radio', |
| 182 | 'name' => 'fields['.$key.'][library]', |
| 183 | 'value' => $field['library'], |
| 184 | 'layout' => 'horizontal', |
| 185 | 'choices' => array( |
| 186 | 'all' => __('All', 'acf'), |
| 187 | 'uploadedTo' => __('Uploaded to post', 'acf') |
| 188 | ) |
| 189 | )); |
| 190 | |
| 191 | ?> |
| 192 | </td> |
| 193 | </tr> |
| 194 | <?php |
| 195 | |
| 196 | } |
| 197 | |
| 198 | |
| 199 | /* |
| 200 | * format_value_for_api() |
| 201 | * |
| 202 | * This filter is appied to the $value after it is loaded from the db and before it is passed back to the api functions such as the_field |
| 203 | * |
| 204 | * @type filter |
| 205 | * @since 3.6 |
| 206 | * @date 23/01/13 |
| 207 | * |
| 208 | * @param $value - the value which was loaded from the database |
| 209 | * @param $post_id - the $post_id from which the value was loaded |
| 210 | * @param $field - the field array holding all the field options |
| 211 | * |
| 212 | * @return $value - the modified value |
| 213 | */ |
| 214 | |
| 215 | function format_value_for_api( $value, $post_id, $field ) |
| 216 | { |
| 217 | |
| 218 | // validate |
| 219 | if( !$value ) |
| 220 | { |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | // format |
| 226 | if( $field['save_format'] == 'url' ) |
| 227 | { |
| 228 | $value = wp_get_attachment_url($value); |
| 229 | } |
| 230 | elseif( $field['save_format'] == 'object' ) |
| 231 | { |
| 232 | $attachment = get_post( $value ); |
| 233 | |
| 234 | |
| 235 | // validate |
| 236 | if( !$attachment ) |
| 237 | { |
| 238 | return false; |
| 239 | } |
| 240 | |
| 241 | |
| 242 | // create array to hold value data |
| 243 | $value = array( |
| 244 | 'id' => $attachment->ID, |
| 245 | 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true), |
| 246 | 'title' => $attachment->post_title, |
| 247 | 'caption' => $attachment->post_excerpt, |
| 248 | 'description' => $attachment->post_content, |
| 249 | 'mime_type' => $attachment->post_mime_type, |
| 250 | 'url' => wp_get_attachment_url( $attachment->ID ), |
| 251 | ); |
| 252 | } |
| 253 | |
| 254 | return $value; |
| 255 | } |
| 256 | |
| 257 | |
| 258 | /* |
| 259 | * get_media_item_args |
| 260 | * |
| 261 | * @description: |
| 262 | * @since: 3.6 |
| 263 | * @created: 27/01/13 |
| 264 | */ |
| 265 | |
| 266 | function get_media_item_args( $vars ) |
| 267 | { |
| 268 | $vars['send'] = true; |
| 269 | return($vars); |
| 270 | } |
| 271 | |
| 272 | |
| 273 | /* |
| 274 | * ajax_get_files |
| 275 | * |
| 276 | * @description: |
| 277 | * @since: 3.5.7 |
| 278 | * @created: 13/01/13 |
| 279 | */ |
| 280 | |
| 281 | function ajax_get_files() |
| 282 | { |
| 283 | // vars |
| 284 | $options = array( |
| 285 | 'nonce' => '', |
| 286 | 'files' => array() |
| 287 | ); |
| 288 | $return = array(); |
| 289 | |
| 290 | |
| 291 | // load post options |
| 292 | $options = array_merge($options, $_POST); |
| 293 | |
| 294 | |
| 295 | // verify nonce |
| 296 | if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') ) |
| 297 | { |
| 298 | die(0); |
| 299 | } |
| 300 | |
| 301 | |
| 302 | if( $options['files'] ) |
| 303 | { |
| 304 | foreach( $options['files'] as $id ) |
| 305 | { |
| 306 | $o = array(); |
| 307 | $file = get_post( $id ); |
| 308 | |
| 309 | $o['id'] = $file->ID; |
| 310 | $o['icon'] = wp_mime_type_icon( $file->ID ); |
| 311 | $o['title'] = $file->post_title; |
| 312 | $o['size'] = size_format(filesize( get_attached_file( $file->ID ) )); |
| 313 | $o['url'] = wp_get_attachment_url( $file->ID ); |
| 314 | $o['name'] = end(explode('/', $o['url'])); |
| 315 | |
| 316 | $return[] = $o; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | |
| 321 | // return json |
| 322 | echo json_encode( $return ); |
| 323 | die; |
| 324 | |
| 325 | } |
| 326 | |
| 327 | |
| 328 | /* |
| 329 | * update_value() |
| 330 | * |
| 331 | * This filter is appied to the $value before it is updated in the db |
| 332 | * |
| 333 | * @type filter |
| 334 | * @since 3.6 |
| 335 | * @date 23/01/13 |
| 336 | * |
| 337 | * @param $value - the value which will be saved in the database |
| 338 | * @param $post_id - the $post_id of which the value will be saved |
| 339 | * @param $field - the field array holding all the field options |
| 340 | * |
| 341 | * @return $value - the modified value |
| 342 | */ |
| 343 | |
| 344 | function update_value( $value, $post_id, $field ) |
| 345 | { |
| 346 | // array? |
| 347 | if( is_array($value) && isset($value['id']) ) |
| 348 | { |
| 349 | $value = $value['id']; |
| 350 | } |
| 351 | |
| 352 | // object? |
| 353 | if( is_object($value) && isset($value->ID) ) |
| 354 | { |
| 355 | $value = $value->ID; |
| 356 | } |
| 357 | |
| 358 | return $value; |
| 359 | } |
| 360 | |
| 361 | |
| 362 | /* |
| 363 | * wp_prepare_attachment_for_js |
| 364 | * |
| 365 | * this filter allows ACF to add in extra data to an attachment JS object |
| 366 | * |
| 367 | * @type function |
| 368 | * @date 1/06/13 |
| 369 | * |
| 370 | * @param {int} $post_id |
| 371 | * @return {int} $post_id |
| 372 | */ |
| 373 | |
| 374 | function wp_prepare_attachment_for_js( $response, $attachment, $meta ) |
| 375 | { |
| 376 | // default |
| 377 | $fs = '0 kb'; |
| 378 | |
| 379 | |
| 380 | // supress PHP warnings caused by corrupt images |
| 381 | if( $i = @filesize( get_attached_file( $attachment->ID ) ) ) |
| 382 | { |
| 383 | $fs = size_format( $i ); |
| 384 | } |
| 385 | |
| 386 | |
| 387 | // update JSON |
| 388 | $response['filesize'] = $fs; |
| 389 | |
| 390 | |
| 391 | // return |
| 392 | return $response; |
| 393 | } |
| 394 | |
| 395 | } |
| 396 | |
| 397 | new acf_field_file(); |
| 398 | |
| 399 | ?> |