file.php
| 1 | <?php |
| 2 | |
| 3 | class acf_File extends acf_Field |
| 4 | { |
| 5 | /*-------------------------------------------------------------------------------------- |
| 6 | * |
| 7 | * Constructor |
| 8 | * |
| 9 | * @author Elliot Condon |
| 10 | * @since 1.0.0 |
| 11 | * @updated 2.2.0 |
| 12 | * |
| 13 | *-------------------------------------------------------------------------------------*/ |
| 14 | |
| 15 | function __construct($parent) |
| 16 | { |
| 17 | parent::__construct($parent); |
| 18 | |
| 19 | $this->name = 'file'; |
| 20 | $this->title = __('File','acf'); |
| 21 | |
| 22 | add_action('admin_head-media-upload-popup', array($this, 'popup_head')); |
| 23 | add_action('wp_ajax_acf_select_file', array($this, 'select_file')); |
| 24 | add_filter('get_media_item_args', array($this, 'allow_file_insertion')); |
| 25 | } |
| 26 | |
| 27 | |
| 28 | /*-------------------------------------------------------------------------------------- |
| 29 | * |
| 30 | * select_file |
| 31 | * |
| 32 | * @description ajax function to provide url of selected file |
| 33 | * @author Elliot Condon |
| 34 | * @since 3.1.5 |
| 35 | * |
| 36 | *-------------------------------------------------------------------------------------*/ |
| 37 | |
| 38 | function select_file() |
| 39 | { |
| 40 | $id = isset($_POST['id']) ? $_POST['id'] : false; |
| 41 | |
| 42 | |
| 43 | // attachment ID is required |
| 44 | if(!$id) |
| 45 | { |
| 46 | echo ""; |
| 47 | die(); |
| 48 | } |
| 49 | |
| 50 | |
| 51 | $file_src = wp_get_attachment_url($id); |
| 52 | |
| 53 | echo $file_src; |
| 54 | die(); |
| 55 | } |
| 56 | |
| 57 | |
| 58 | /*-------------------------------------------------------------------------------------- |
| 59 | * |
| 60 | * allow_file_insertion |
| 61 | * |
| 62 | * @author Elliot Condon |
| 63 | * @since 3.0.1 |
| 64 | * |
| 65 | *-------------------------------------------------------------------------------------*/ |
| 66 | |
| 67 | function allow_file_insertion($vars) |
| 68 | { |
| 69 | $vars['send'] = true; |
| 70 | return($vars); |
| 71 | } |
| 72 | |
| 73 | |
| 74 | /*-------------------------------------------------------------------------------------- |
| 75 | * |
| 76 | * admin_print_scripts / admin_print_styles |
| 77 | * |
| 78 | * @author Elliot Condon |
| 79 | * @since 3.0.0 |
| 80 | * |
| 81 | *-------------------------------------------------------------------------------------*/ |
| 82 | |
| 83 | function admin_print_scripts() |
| 84 | { |
| 85 | wp_enqueue_script(array( |
| 86 | 'jquery', |
| 87 | 'jquery-ui-core', |
| 88 | 'jquery-ui-tabs', |
| 89 | |
| 90 | 'thickbox', |
| 91 | 'media-upload', |
| 92 | )); |
| 93 | } |
| 94 | |
| 95 | function admin_print_styles() |
| 96 | { |
| 97 | wp_enqueue_style(array( |
| 98 | 'thickbox', |
| 99 | )); |
| 100 | } |
| 101 | |
| 102 | |
| 103 | /*-------------------------------------------------------------------------------------- |
| 104 | * |
| 105 | * create_field |
| 106 | * |
| 107 | * @author Elliot Condon |
| 108 | * @since 2.0.5 |
| 109 | * @updated 2.2.0 |
| 110 | * |
| 111 | *-------------------------------------------------------------------------------------*/ |
| 112 | |
| 113 | function create_field($field) |
| 114 | { |
| 115 | // vars |
| 116 | $class = ""; |
| 117 | $file_src = ""; |
| 118 | |
| 119 | // get file url |
| 120 | if($field['value'] != '' && is_numeric($field['value'])) |
| 121 | { |
| 122 | $file_src = wp_get_attachment_url($field['value']); |
| 123 | if($file_src) $class = "active"; |
| 124 | } |
| 125 | |
| 126 | // html |
| 127 | echo '<div class="acf_file_uploader ' . $class . '">'; |
| 128 | echo '<p class="file"><span class="file_url">'.$file_src.'</span> <input type="button" class="button" value="'.__('Remove File','acf').'" /></p>'; |
| 129 | echo '<input class="value" type="hidden" name="' . $field['name'] . '" value="' . $field['value'] . '" />'; |
| 130 | echo '<p class="no_file">'.__('No File selected','acf').'. <input type="button" class="button" value="'.__('Add File','acf').'" /></p>'; |
| 131 | echo '</div>'; |
| 132 | |
| 133 | } |
| 134 | |
| 135 | |
| 136 | |
| 137 | /*-------------------------------------------------------------------------------------- |
| 138 | * |
| 139 | * create_options |
| 140 | * |
| 141 | * @author Elliot Condon |
| 142 | * @since 2.0.6 |
| 143 | * @updated 2.2.0 |
| 144 | * |
| 145 | *-------------------------------------------------------------------------------------*/ |
| 146 | |
| 147 | function create_options($key, $field) |
| 148 | { |
| 149 | // vars |
| 150 | $field['save_format'] = isset($field['save_format']) ? $field['save_format'] : 'url'; |
| 151 | |
| 152 | ?> |
| 153 | <tr class="field_option field_option_<?php echo $this->name; ?>"> |
| 154 | <td class="label"> |
| 155 | <label><?php _e("Return Value",'acf'); ?></label> |
| 156 | </td> |
| 157 | <td> |
| 158 | <?php |
| 159 | $this->parent->create_field(array( |
| 160 | 'type' => 'radio', |
| 161 | 'name' => 'fields['.$key.'][save_format]', |
| 162 | 'value' => $field['save_format'], |
| 163 | 'layout' => 'horizontal', |
| 164 | 'choices' => array( |
| 165 | 'url' => 'File URL', |
| 166 | 'id' => 'Attachment ID' |
| 167 | ) |
| 168 | )); |
| 169 | ?> |
| 170 | </td> |
| 171 | </tr> |
| 172 | |
| 173 | <?php |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /*--------------------------------------------------------------------------------------------- |
| 178 | * popup_head - STYLES MEDIA THICKBOX |
| 179 | * |
| 180 | * @author Elliot Condon |
| 181 | * @since 1.1.4 |
| 182 | * |
| 183 | ---------------------------------------------------------------------------------------------*/ |
| 184 | function popup_head() |
| 185 | { |
| 186 | if(isset($_GET["acf_type"]) && $_GET['acf_type'] == 'file') |
| 187 | { |
| 188 | $tab = isset($_GET['tab']) ? $_GET['tab'] : "type"; // "type" is the upload tab |
| 189 | |
| 190 | ?><style type="text/css"> |
| 191 | #media-upload-header #sidemenu li#tab-type_url, |
| 192 | #media-upload-header #sidemenu li#tab-gallery, |
| 193 | #media-items .media-item table.slidetoggle, |
| 194 | #media-items .media-item a.toggle { |
| 195 | display: none !important; |
| 196 | } |
| 197 | |
| 198 | #media-items .media-item { |
| 199 | min-height: 68px; |
| 200 | } |
| 201 | |
| 202 | #media-items .media-item .acf-checkbox { |
| 203 | float: left; |
| 204 | margin: 28px 10px 0; |
| 205 | } |
| 206 | |
| 207 | #media-items .media-item .pinkynail { |
| 208 | max-width: 64px; |
| 209 | max-height: 64px; |
| 210 | display: block !important; |
| 211 | } |
| 212 | |
| 213 | #media-items .media-item .filename.new { |
| 214 | min-height: 0; |
| 215 | padding: 25px 10px 10px; |
| 216 | line-height: 14px; |
| 217 | |
| 218 | } |
| 219 | |
| 220 | #media-items .media-item .title { |
| 221 | line-height: 14px; |
| 222 | } |
| 223 | |
| 224 | #media-items .media-item .button { |
| 225 | float: right; |
| 226 | margin: -2px 0 0 10px; |
| 227 | } |
| 228 | |
| 229 | #media-upload .ml-submit { |
| 230 | display: none !important; |
| 231 | } |
| 232 | |
| 233 | #media-upload .acf-submit { |
| 234 | margin: 1em 0; |
| 235 | padding: 1em 0; |
| 236 | position: relative; |
| 237 | overflow: hidden; |
| 238 | display: none; /* default is hidden */ |
| 239 | } |
| 240 | |
| 241 | #media-upload .acf-submit a { |
| 242 | float: left; |
| 243 | margin: 0 10px 0 0; |
| 244 | } |
| 245 | |
| 246 | </style> |
| 247 | <script type="text/javascript"> |
| 248 | (function($){ |
| 249 | |
| 250 | //console.log(window.plupload); |
| 251 | $('#media-items .media-item .filename a.acf-select').live('click', function(){ |
| 252 | |
| 253 | var id = $(this).attr('href'); |
| 254 | |
| 255 | var data = { |
| 256 | action: 'acf_select_file', |
| 257 | id: id |
| 258 | }; |
| 259 | |
| 260 | // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php |
| 261 | $.post(ajaxurl, data, function(html) { |
| 262 | |
| 263 | if(!html || html == "") |
| 264 | { |
| 265 | return false; |
| 266 | } |
| 267 | |
| 268 | self.parent.acf_div.find('input.value').val(id); |
| 269 | self.parent.acf_div.find('span.file_url').text(html); |
| 270 | self.parent.acf_div.addClass('active'); |
| 271 | |
| 272 | // validation |
| 273 | self.parent.acf_div.closest('.field').removeClass('error'); |
| 274 | |
| 275 | // reset acf_div and return false |
| 276 | self.parent.acf_div = null; |
| 277 | self.parent.tb_remove(); |
| 278 | |
| 279 | }); |
| 280 | |
| 281 | return false; |
| 282 | }); |
| 283 | |
| 284 | |
| 285 | $('#acf-add-selected').live('click', function(){ |
| 286 | |
| 287 | // check total |
| 288 | var total = $('#media-items .media-item .acf-checkbox:checked').length; |
| 289 | if(total == 0) |
| 290 | { |
| 291 | alert("<?php _e("No Files Selected",'acf'); ?>"); |
| 292 | return false; |
| 293 | } |
| 294 | |
| 295 | $('#media-items .media-item .acf-checkbox:checked').each(function(i){ |
| 296 | |
| 297 | var id = $(this).val(); |
| 298 | |
| 299 | var data = { |
| 300 | action: 'acf_select_image', |
| 301 | id: id |
| 302 | }; |
| 303 | |
| 304 | // since 2.8 ajaxurl is always defined in the admin header and points to admin-ajax.php |
| 305 | $.post(ajaxurl, data, function(html) { |
| 306 | |
| 307 | if(!html || html == "") |
| 308 | { |
| 309 | return false; |
| 310 | } |
| 311 | |
| 312 | self.parent.acf_div.find('input.value').val(id); |
| 313 | self.parent.acf_div.find('span.file_url').text(html); |
| 314 | self.parent.acf_div.addClass('active'); |
| 315 | |
| 316 | // validation |
| 317 | self.parent.acf_div.closest('.field').removeClass('error'); |
| 318 | |
| 319 | if((i+1) < total) |
| 320 | { |
| 321 | // add row |
| 322 | self.parent.acf_div.closest('.repeater').find('.table_footer #r_add_row').trigger('click'); |
| 323 | |
| 324 | // set acf_div to new row image |
| 325 | self.parent.acf_div = self.parent.acf_div.closest('.repeater').find('table tbody tr.row:last-child .acf_file_uploader'); |
| 326 | } |
| 327 | else |
| 328 | { |
| 329 | // reset acf_div and return false |
| 330 | self.parent.acf_div = null; |
| 331 | self.parent.tb_remove(); |
| 332 | } |
| 333 | |
| 334 | |
| 335 | }); |
| 336 | |
| 337 | }); |
| 338 | |
| 339 | |
| 340 | return false; |
| 341 | |
| 342 | }); |
| 343 | |
| 344 | |
| 345 | // set a interval function to add buttons to media items |
| 346 | function acf_add_buttons() |
| 347 | { |
| 348 | // vars |
| 349 | var is_sub_field = (self.parent.acf_div && self.parent.acf_div.closest('.repeater').length > 0) ? true : false; |
| 350 | |
| 351 | |
| 352 | // add submit after media items (on for sub fields) |
| 353 | if($('.acf-submit').length == 0 && is_sub_field) |
| 354 | { |
| 355 | $('#media-items').after('<div class="acf-submit"><a id="acf-add-selected" class="button"><?php _e("Add Selected Files",'acf'); ?></a></div>'); |
| 356 | } |
| 357 | |
| 358 | |
| 359 | // add buttons to media items |
| 360 | $('#media-items .media-item:not(.acf-active)').each(function(){ |
| 361 | |
| 362 | // show the add all button |
| 363 | $('.acf-submit').show(); |
| 364 | |
| 365 | // needs attachment ID |
| 366 | if($(this).children('input[id*="type-of-"]').length == 0){ return false; } |
| 367 | |
| 368 | // only once! |
| 369 | $(this).addClass('acf-active'); |
| 370 | |
| 371 | // find id |
| 372 | var id = $(this).children('input[id*="type-of-"]').attr('id').replace('type-of-', ''); |
| 373 | |
| 374 | // if inside repeater, add checkbox |
| 375 | if(is_sub_field) |
| 376 | { |
| 377 | $(this).prepend('<input type="checkbox" class="acf-checkbox" value="' + id + '" <?php if($tab == "type"){echo 'checked="checked"';} ?> />'); |
| 378 | } |
| 379 | |
| 380 | // change text of insert button, and add new button |
| 381 | $(this).find('.filename.new').append('<a href="' + id + '" class="button acf-select"><?php _e("Select File",'acf'); ?></a>'); |
| 382 | |
| 383 | }); |
| 384 | } |
| 385 | <?php |
| 386 | |
| 387 | // run the acf_add_buttons ever 500ms when on the image upload tab |
| 388 | if($tab == 'type'): ?> |
| 389 | var acf_t = setInterval(function(){ |
| 390 | acf_add_buttons(); |
| 391 | }, 500); |
| 392 | <?php endif; ?> |
| 393 | |
| 394 | |
| 395 | // add acf input filters to allow for tab navigation |
| 396 | $(document).ready(function(){ |
| 397 | |
| 398 | setTimeout(function(){ |
| 399 | acf_add_buttons(); |
| 400 | }, 1); |
| 401 | |
| 402 | |
| 403 | $('form#filter, form#image-form').each(function(){ |
| 404 | |
| 405 | $(this).append('<input type="hidden" name="acf_type" value="file" />'); |
| 406 | |
| 407 | }); |
| 408 | }); |
| 409 | |
| 410 | })(jQuery); |
| 411 | </script><?php |
| 412 | |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | |
| 417 | /*-------------------------------------------------------------------------------------- |
| 418 | * |
| 419 | * get_value_for_api |
| 420 | * |
| 421 | * @author Elliot Condon |
| 422 | * @since 3.0.0 |
| 423 | * |
| 424 | *-------------------------------------------------------------------------------------*/ |
| 425 | |
| 426 | function get_value_for_api($post_id, $field) |
| 427 | { |
| 428 | // vars |
| 429 | $format = isset($field['save_format']) ? $field['save_format'] : 'url'; |
| 430 | |
| 431 | $value = parent::get_value($post_id, $field); |
| 432 | |
| 433 | if($format == 'url') |
| 434 | { |
| 435 | $value = wp_get_attachment_url($value); |
| 436 | } |
| 437 | |
| 438 | return $value; |
| 439 | } |
| 440 | |
| 441 | } |
| 442 | |
| 443 | ?> |