PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/advanced-custom-fields/core/fields/file.php

https://bitbucket.org/geetharani/gordon
PHP | 702 lines | 364 code | 178 blank | 160 comment | 30 complexity | b5ddb2c4dbb87aa64fa8ac25fb5b4952 MD5 | raw file
Possible License(s): MIT
  1. <?php
  2. class acf_File extends acf_Field
  3. {
  4. /*--------------------------------------------------------------------------------------
  5. *
  6. * Constructor
  7. *
  8. * @author Elliot Condon
  9. * @since 1.0.0
  10. * @updated 2.2.0
  11. *
  12. *-------------------------------------------------------------------------------------*/
  13. function __construct($parent)
  14. {
  15. parent::__construct($parent);
  16. $this->name = 'file';
  17. $this->title = __('File','acf');
  18. add_action('admin_head-media-upload-popup', array($this, 'popup_head'));
  19. add_filter('get_media_item_args', array($this, 'allow_file_insertion'));
  20. add_action('acf_head-update_attachment-file', array($this, 'acf_head_update_attachment'));
  21. add_action('wp_ajax_acf/fields/file/get_files', array($this, 'ajax_get_files'));
  22. }
  23. /*
  24. * acf_head_update_attachment
  25. *
  26. * @description:
  27. * @since: 3.2.7
  28. * @created: 4/07/12
  29. */
  30. function acf_head_update_attachment()
  31. {
  32. ?>
  33. <script type="text/javascript">
  34. (function($){
  35. // vars
  36. var div = self.parent.acf.media.div;
  37. // add message
  38. self.parent.acf.helpers.add_message("<?php _e("File Updated.",'acf'); ?>", div);
  39. })(jQuery);
  40. </script>
  41. <?php
  42. }
  43. /*
  44. * ajax_get_files
  45. *
  46. * @description:
  47. * @since: 3.5.7
  48. * @created: 13/01/13
  49. */
  50. function ajax_get_files()
  51. {
  52. // vars
  53. $options = array(
  54. 'nonce' => '',
  55. 'files' => array()
  56. );
  57. $return = array();
  58. // load post options
  59. $options = array_merge($options, $_POST);
  60. // verify nonce
  61. if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') )
  62. {
  63. die(0);
  64. }
  65. if( $options['files'] )
  66. {
  67. foreach( $options['files'] as $id )
  68. {
  69. $file_src = wp_get_attachment_url( $id );
  70. preg_match("~[^/]*$~", $file_src, $file_name);
  71. // vars
  72. $return[] = array(
  73. 'id' => $id,
  74. 'icon' => wp_mime_type_icon( $id ),
  75. 'name' => $file_name[0]
  76. );
  77. }
  78. }
  79. // return json
  80. echo json_encode( $return );
  81. die;
  82. }
  83. /*--------------------------------------------------------------------------------------
  84. *
  85. * allow_file_insertion
  86. *
  87. * @author Elliot Condon
  88. * @since 3.0.1
  89. *
  90. *-------------------------------------------------------------------------------------*/
  91. function allow_file_insertion($vars)
  92. {
  93. $vars['send'] = true;
  94. return($vars);
  95. }
  96. /*--------------------------------------------------------------------------------------
  97. *
  98. * create_field
  99. *
  100. * @author Elliot Condon
  101. * @since 2.0.5
  102. * @updated 2.2.0
  103. *
  104. *-------------------------------------------------------------------------------------*/
  105. function create_field($field)
  106. {
  107. // vars
  108. $options = array(
  109. 'class' => '',
  110. 'icon' => '',
  111. 'file_name' => ''
  112. );
  113. if( $field['value'] )
  114. {
  115. $file_src = wp_get_attachment_url( $field['value'] );
  116. preg_match("~[^/]*$~", $file_src, $file_name);
  117. $options['class'] = 'active';
  118. $options['icon'] = wp_mime_type_icon( $field['value'] );
  119. $options['file_name'] = $file_name[0];
  120. }
  121. ?>
  122. <div class="acf-file-uploader <?php echo $options['class']; ?>">
  123. <input class="acf-file-value" type="hidden" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>" />
  124. <div class="has-file">
  125. <ul class="hl clearfix">
  126. <li>
  127. <img class="acf-file-icon" src="<?php echo $options['icon']; ?>" alt=""/>
  128. </li>
  129. <li>
  130. <span class="acf-file-name"><?php echo $options['file_name']; ?></span><br />
  131. <a href="#" class="edit-file"><?php _e('Edit','acf'); ?></a>
  132. <a href="#" class="remove-file"><?php _e('Remove','acf'); ?></a>
  133. </li>
  134. </ul>
  135. </div>
  136. <div class="no-file">
  137. <ul class="hl clearfix">
  138. <li>
  139. <span><?php _e('No File Selected','acf'); ?></span>. <a href="#" class="button add-file"><?php _e('Add File','acf'); ?></a>
  140. </li>
  141. </ul>
  142. </div>
  143. </div>
  144. <?php
  145. }
  146. /*--------------------------------------------------------------------------------------
  147. *
  148. * create_options
  149. *
  150. * @author Elliot Condon
  151. * @since 2.0.6
  152. * @updated 2.2.0
  153. *
  154. *-------------------------------------------------------------------------------------*/
  155. function create_options($key, $field)
  156. {
  157. // vars
  158. $defaults = array(
  159. 'save_format' => 'id',
  160. );
  161. $field = array_merge($defaults, $field);
  162. ?>
  163. <tr class="field_option field_option_<?php echo $this->name; ?>">
  164. <td class="label">
  165. <label><?php _e("Return Value",'acf'); ?></label>
  166. </td>
  167. <td>
  168. <?php
  169. do_action('acf/create_field', array(
  170. 'type' => 'radio',
  171. 'name' => 'fields['.$key.'][save_format]',
  172. 'value' => $field['save_format'],
  173. 'layout' => 'horizontal',
  174. 'choices' => array(
  175. 'object' => __("File Object",'acf'),
  176. 'url' => __("File URL",'acf'),
  177. 'id' => __("File ID",'acf')
  178. )
  179. ));
  180. ?>
  181. </td>
  182. </tr>
  183. <?php
  184. }
  185. /*
  186. * popup_head
  187. *
  188. * @description: css + js for thickbox
  189. * @since: 1.1.4
  190. * @created: 7/12/12
  191. */
  192. function popup_head()
  193. {
  194. // options
  195. $defaults = array(
  196. 'acf_type' => '',
  197. 'tab' => 'type',
  198. );
  199. $options = array_merge($defaults, $_GET);
  200. // validate
  201. if( $options['acf_type'] != 'file' )
  202. {
  203. return;
  204. }
  205. // update attachment
  206. if( isset($_POST["attachments"]) )
  207. {
  208. echo '<div class="updated"><p>' . __("Media attachment updated.") . '</p></div>';
  209. }
  210. ?><style type="text/css">
  211. #media-upload-header #sidemenu li#tab-type_url,
  212. #media-items .media-item a.toggle,
  213. #media-items .media-item tr.image-size,
  214. #media-items .media-item tr.align,
  215. #media-items .media-item tr.url,
  216. #media-items .media-item .slidetoggle {
  217. display: none !important;
  218. }
  219. #media-items .media-item {
  220. position: relative;
  221. overflow: hidden;
  222. }
  223. #media-items .media-item .acf-checkbox {
  224. float: left;
  225. margin: 28px 10px 0;
  226. }
  227. #media-items .media-item .pinkynail {
  228. max-width: 64px;
  229. max-height: 64px;
  230. display: block !important;
  231. margin: 2px;
  232. }
  233. #media-items .media-item .filename.new {
  234. min-height: 0;
  235. padding: 10px;
  236. line-height: 15px;
  237. }
  238. #media-items .media-item .title {
  239. line-height: 14px;
  240. }
  241. #media-items .media-item .acf-select {
  242. float: right;
  243. margin: 22px 12px 0 10px;
  244. }
  245. #media-upload .ml-submit {
  246. display: none !important;
  247. }
  248. #media-upload .acf-submit {
  249. margin: 1em 0;
  250. padding: 1em 0;
  251. position: relative;
  252. overflow: hidden;
  253. display: none; /* default is hidden */
  254. clear: both;
  255. }
  256. #media-upload .acf-submit a {
  257. float: left;
  258. margin: 0 10px 0 0;
  259. }
  260. #media-items .media-item .acf-filename {
  261. color: #999;
  262. font-size: 11px;
  263. margin: 0 0 3px;
  264. display: block;
  265. }
  266. <?php if( $options['tab'] == 'gallery' ): ?>
  267. #sort-buttons,
  268. #gallery-form > .widefat,
  269. #media-items .menu_order,
  270. #gallery-settings {
  271. display: none !important;
  272. }
  273. <?php endif; ?>
  274. </style>
  275. <script type="text/javascript">
  276. (function($){
  277. /*
  278. * Select File
  279. *
  280. * @description:
  281. * @since: 2.0.4
  282. * @created: 11/12/12
  283. */
  284. $('#media-items .media-item a.acf-select').live('click', function(){
  285. var id = $(this).attr('href');
  286. // IE7 Fix
  287. if( id.indexOf("/") != -1 )
  288. {
  289. var split = id.split("/");
  290. id = split[split.length-1];
  291. }
  292. var ajax_data = {
  293. action : 'acf/fields/file/get_files',
  294. nonce : self.parent.acf.nonce,
  295. files : [ id ]
  296. };
  297. // ajax
  298. $.ajax({
  299. url: ajaxurl,
  300. type: 'post',
  301. data : ajax_data,
  302. cache: false,
  303. dataType: "json",
  304. success: function( json ) {
  305. // validate
  306. if( !json )
  307. {
  308. return false;
  309. }
  310. // add file
  311. self.parent.acf.fields.file.add( json[0] );
  312. self.parent.tb_remove();
  313. }
  314. });
  315. return false;
  316. });
  317. /*
  318. * Select Files
  319. *
  320. * @description:
  321. * @since: 2.0.4
  322. * @created: 11/12/12
  323. */
  324. $('#acf-add-selected').live('click', function(){
  325. // check total
  326. var total = $('#media-items .media-item .acf-checkbox:checked').length;
  327. if( total == 0 )
  328. {
  329. alert("<?php _e("No files selected",'acf'); ?>");
  330. return false;
  331. }
  332. var ajax_data = {
  333. action : 'acf/fields/file/get_files',
  334. nonce : self.parent.acf.nonce,
  335. files : []
  336. };
  337. // add to id array
  338. $('#media-items .media-item .acf-checkbox:checked').each(function(){
  339. ajax_data.files.push( $(this).val() );
  340. });
  341. // ajax
  342. $.ajax({
  343. url: ajaxurl,
  344. type: 'post',
  345. data : ajax_data,
  346. cache: false,
  347. dataType: "json",
  348. success: function( json ) {
  349. // validate
  350. if( !json )
  351. {
  352. return false;
  353. }
  354. var selection = json,
  355. i = 0;
  356. $.each( json, function( k, file ){
  357. // counter
  358. i++;
  359. // vars
  360. var div = self.parent.acf.media.div;
  361. // add image to field
  362. self.parent.acf.fields.file.add( file );
  363. // select / add another file field?
  364. if( i < selection.length )
  365. {
  366. var tr = div.closest('tr'),
  367. repeater = tr.closest('.repeater');
  368. if( tr.next('.row').exists() )
  369. {
  370. self.parent.acf.media.div = tr.next('.row').find('.acf-file-uploader');
  371. }
  372. else
  373. {
  374. // add row
  375. repeater.find('.add-row-end').trigger('click');
  376. // set div to new row file
  377. self.parent.acf.media.div = repeater.find('> table > tbody > tr.row:last .acf-file-uploader');
  378. }
  379. }
  380. });
  381. self.parent.tb_remove();
  382. }
  383. });
  384. return false;
  385. });
  386. /*
  387. * Edit Attachment Toggle
  388. *
  389. * @description:
  390. * @since: 2.0.4
  391. * @created: 11/12/12
  392. */
  393. $('#media-items .media-item a.acf-toggle-edit').live('click', function(){
  394. // vars
  395. var a = $(this),
  396. item = a.closest('.media-item');
  397. // toggle
  398. if( a.hasClass('active') )
  399. {
  400. a.removeClass('active');
  401. item.find('.slidetoggle').attr('style', 'display: none !important');
  402. }
  403. else
  404. {
  405. a.addClass('active');
  406. item.find('.slidetoggle').attr('style', 'display: table !important');
  407. }
  408. // return
  409. return false;
  410. });
  411. /*
  412. * add_buttons
  413. *
  414. * @description:
  415. * @since: 2.0.4
  416. * @created: 11/12/12
  417. */
  418. function add_buttons()
  419. {
  420. // vars
  421. var is_sub_field = (self.parent.acf.media.div.closest('.repeater').length > 0) ? true : false;
  422. // add submit after media items (on for sub fields)
  423. if($('.acf-submit').length == 0 && is_sub_field)
  424. {
  425. $('#media-items').after('<div class="acf-submit"><a id="acf-add-selected" class="button"><?php _e("Add Selected Files",'acf'); ?></a></div>');
  426. }
  427. // add buttons to media items
  428. $('#media-items .media-item:not(.acf-active)').each(function(){
  429. // show the add all button
  430. $('.acf-submit').show();
  431. // needs attachment ID
  432. if($(this).children('input[id*="type-of-"]').length == 0){ return false; }
  433. // only once!
  434. $(this).addClass('acf-active');
  435. // find id
  436. var id = $(this).children('input[id*="type-of-"]').attr('id').replace('type-of-', '');
  437. // if inside repeater, add checkbox
  438. if(is_sub_field)
  439. {
  440. $(this).prepend('<input type="checkbox" class="acf-checkbox" value="' + id + '" <?php if( $options['tab'] == 'type' ){echo 'checked="checked"';} ?> />');
  441. }
  442. // find file url
  443. var file_url = $(this).find('.slidetoggle tr.url .urlfile').attr('data-link-url');
  444. $(this).find('.filename.new').append('<span class="acf-filename">' + file_url + '</span>');
  445. // Add edit button
  446. $(this).find('.filename.new').append('<a href="#" class="acf-toggle-edit">Edit</a>');
  447. // Add select button
  448. $(this).find('.filename.new').before('<a href="' + id + '" class="button acf-select"><?php _e("Select File",'acf'); ?></a>');
  449. // add save changes button
  450. $(this).find('tr.submit input.button').hide().before('<input type="submit" value="<?php _e("Update File",'acf'); ?>" class="button savebutton" />');
  451. });
  452. }
  453. <?php
  454. // run the acf_add_buttons ever 500ms when on the file upload tab
  455. if( $options['tab'] == 'type' ): ?>
  456. var acf_t = setInterval(function(){
  457. add_buttons();
  458. }, 500);
  459. <?php endif; ?>
  460. // add acf input filters to allow for tab navigation
  461. $(document).ready(function(){
  462. setTimeout(function(){
  463. add_buttons();
  464. }, 1);
  465. $('form#filter').each(function(){
  466. $(this).append('<input type="hidden" name="acf_type" value="file" />');
  467. });
  468. $('form#image-form, form#library-form').each(function(){
  469. var action = $(this).attr('action');
  470. action += "&acf_type=file";
  471. $(this).attr('action', action);
  472. });
  473. });
  474. })(jQuery);
  475. </script><?php
  476. }
  477. /*--------------------------------------------------------------------------------------
  478. *
  479. * get_value_for_api
  480. *
  481. * @author Elliot Condon
  482. * @since 3.0.0
  483. *
  484. *-------------------------------------------------------------------------------------*/
  485. function get_value_for_api($post_id, $field)
  486. {
  487. // vars
  488. $defaults = array(
  489. 'save_format' => 'object',
  490. );
  491. $field = array_merge($defaults, $field);
  492. $value = parent::get_value($post_id, $field);
  493. // validate
  494. if( !$value )
  495. {
  496. return false;
  497. }
  498. // format
  499. if( $field['save_format'] == 'url' )
  500. {
  501. $value = wp_get_attachment_url($value);
  502. }
  503. elseif( $field['save_format'] == 'object' )
  504. {
  505. $attachment = get_post( $value );
  506. // validate
  507. if( !$attachment )
  508. {
  509. return false;
  510. }
  511. // create array to hold value data
  512. $value = array(
  513. 'id' => $attachment->ID,
  514. 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),
  515. 'title' => $attachment->post_title,
  516. 'caption' => $attachment->post_excerpt,
  517. 'description' => $attachment->post_content,
  518. 'url' => wp_get_attachment_url( $attachment->ID ),
  519. );
  520. }
  521. return $value;
  522. }
  523. }
  524. ?>