PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/assets/lib/acf/core/fields/file.php

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