/core/fields/file.php

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