acf_field_file

The Advanced Custom Fields acf field file class.

Defined (1)

The class is defined in the following location(s).

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