class-acf-field-gallery.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982
  1. <?php
  2. if ( ! class_exists( 'acf_field_gallery' ) ) :
  3. class acf_field_gallery extends acf_field {
  4. /*
  5. * __construct
  6. *
  7. * This function will setup the field type data
  8. *
  9. * @type function
  10. * @date 5/03/2014
  11. * @since 5.0.0
  12. *
  13. * @param n/a
  14. * @return n/a
  15. */
  16. function initialize() {
  17. // vars
  18. $this->name = 'gallery';
  19. $this->label = __( 'Gallery', 'acf' );
  20. $this->category = 'content';
  21. $this->defaults = array(
  22. 'return_format' => 'array',
  23. 'preview_size' => 'medium',
  24. 'insert' => 'append',
  25. 'library' => 'all',
  26. 'min' => 0,
  27. 'max' => 0,
  28. 'min_width' => 0,
  29. 'min_height' => 0,
  30. 'min_size' => 0,
  31. 'max_width' => 0,
  32. 'max_height' => 0,
  33. 'max_size' => 0,
  34. 'mime_types' => '',
  35. );
  36. // actions
  37. add_action( 'wp_ajax_acf/fields/gallery/get_attachment', array( $this, 'ajax_get_attachment' ) );
  38. add_action( 'wp_ajax_nopriv_acf/fields/gallery/get_attachment', array( $this, 'ajax_get_attachment' ) );
  39. add_action( 'wp_ajax_acf/fields/gallery/update_attachment', array( $this, 'ajax_update_attachment' ) );
  40. add_action( 'wp_ajax_nopriv_acf/fields/gallery/update_attachment', array( $this, 'ajax_update_attachment' ) );
  41. add_action( 'wp_ajax_acf/fields/gallery/get_sort_order', array( $this, 'ajax_get_sort_order' ) );
  42. add_action( 'wp_ajax_nopriv_acf/fields/gallery/get_sort_order', array( $this, 'ajax_get_sort_order' ) );
  43. }
  44. /*
  45. * input_admin_enqueue_scripts
  46. *
  47. * description
  48. *
  49. * @type function
  50. * @date 16/12/2015
  51. * @since 5.3.2
  52. *
  53. * @param $post_id (int)
  54. * @return $post_id (int)
  55. */
  56. function input_admin_enqueue_scripts() {
  57. // localize
  58. acf_localize_text(
  59. array(
  60. 'Add Image to Gallery' => __( 'Add Image to Gallery', 'acf' ),
  61. 'Maximum selection reached' => __( 'Maximum selection reached', 'acf' ),
  62. )
  63. );
  64. }
  65. /*
  66. * ajax_get_attachment
  67. *
  68. * description
  69. *
  70. * @type function
  71. * @date 13/12/2013
  72. * @since 5.0.0
  73. *
  74. * @param $post_id (int)
  75. * @return $post_id (int)
  76. */
  77. function ajax_get_attachment() {
  78. // Validate requrest.
  79. if ( ! acf_verify_ajax() ) {
  80. die();
  81. }
  82. // Get args.
  83. $args = acf_request_args(
  84. array(
  85. 'id' => 0,
  86. 'field_key' => '',
  87. )
  88. );
  89. // Cast args.
  90. $args['id'] = (int) $args['id'];
  91. // Bail early if no id.
  92. if ( ! $args['id'] ) {
  93. die();
  94. }
  95. // Load field.
  96. $field = acf_get_field( $args['field_key'] );
  97. if ( ! $field ) {
  98. die();
  99. }
  100. // Render.
  101. $this->render_attachment( $args['id'], $field );
  102. die;
  103. }
  104. /*
  105. * ajax_update_attachment
  106. *
  107. * description
  108. *
  109. * @type function
  110. * @date 13/12/2013
  111. * @since 5.0.0
  112. *
  113. * @param $post_id (int)
  114. * @return $post_id (int)
  115. */
  116. function ajax_update_attachment() {
  117. // validate nonce
  118. if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['nonce'] ) ), 'acf_nonce' ) ) {
  119. wp_send_json_error();
  120. }
  121. // bail early if no attachments
  122. if ( empty( $_POST['attachments'] ) ) {
  123. wp_send_json_error();
  124. }
  125. // loop over attachments
  126. foreach ( $_POST['attachments'] as $id => $changes ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Sanitized by WP core when saved.
  127. if ( ! current_user_can( 'edit_post', $id ) ) {
  128. wp_send_json_error();
  129. }
  130. $post = get_post( $id, ARRAY_A );
  131. if ( 'attachment' != $post['post_type'] ) {
  132. wp_send_json_error();
  133. }
  134. if ( isset( $changes['title'] ) ) {
  135. $post['post_title'] = $changes['title'];
  136. }
  137. if ( isset( $changes['caption'] ) ) {
  138. $post['post_excerpt'] = $changes['caption'];
  139. }
  140. if ( isset( $changes['description'] ) ) {
  141. $post['post_content'] = $changes['description'];
  142. }
  143. if ( isset( $changes['alt'] ) ) {
  144. $alt = wp_unslash( $changes['alt'] );
  145. if ( $alt != get_post_meta( $id, '_wp_attachment_image_alt', true ) ) {
  146. $alt = wp_strip_all_tags( $alt, true );
  147. update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) );
  148. }
  149. }
  150. // save post
  151. wp_update_post( $post );
  152. /** This filter is documented in wp-admin/includes/media.php */
  153. // - seems off to run this filter AFTER the update_post function, but there is a reason
  154. // - when placed BEFORE, an empty post_title will be populated by WP
  155. // - this filter will still allow 3rd party to save extra image data!
  156. $post = apply_filters( 'attachment_fields_to_save', $post, $changes );
  157. // save meta
  158. acf_save_post( $id );
  159. }
  160. // return
  161. wp_send_json_success();
  162. }
  163. /*
  164. * ajax_get_sort_order
  165. *
  166. * description
  167. *
  168. * @type function
  169. * @date 13/12/2013
  170. * @since 5.0.0
  171. *
  172. * @param $post_id (int)
  173. * @return $post_id (int)
  174. */
  175. function ajax_get_sort_order() {
  176. // vars
  177. $r = array();
  178. $order = 'DESC';
  179. $args = acf_parse_args(
  180. $_POST, // phpcs:ignore WordPress.Security.NonceVerification.Missing -- Verified below.
  181. array(
  182. 'ids' => 0,
  183. 'sort' => 'date',
  184. 'field_key' => '',
  185. 'nonce' => '',
  186. )
  187. );
  188. // validate
  189. if ( ! wp_verify_nonce( $args['nonce'], 'acf_nonce' ) ) {
  190. wp_send_json_error();
  191. }
  192. // reverse
  193. if ( $args['sort'] == 'reverse' ) {
  194. $ids = array_reverse( $args['ids'] );
  195. wp_send_json_success( $ids );
  196. }
  197. if ( $args['sort'] == 'title' ) {
  198. $order = 'ASC';
  199. }
  200. // find attachments (DISTINCT POSTS)
  201. $ids = get_posts(
  202. array(
  203. 'post_type' => 'attachment',
  204. 'numberposts' => -1,
  205. 'post_status' => 'any',
  206. 'post__in' => $args['ids'],
  207. 'order' => $order,
  208. 'orderby' => $args['sort'],
  209. 'fields' => 'ids',
  210. )
  211. );
  212. // success
  213. if ( ! empty( $ids ) ) {
  214. wp_send_json_success( $ids );
  215. }
  216. // failure
  217. wp_send_json_error();
  218. }
  219. /**
  220. * Renders the sidebar HTML shown when selecting an attachmemnt.
  221. *
  222. * @date 13/12/2013
  223. * @since 5.0.0
  224. *
  225. * @param int $id The attachment ID.
  226. * @param array $field The field array.
  227. * @return void
  228. */
  229. function render_attachment( $id, $field ) {
  230. // Load attachmenet data.
  231. $attachment = wp_prepare_attachment_for_js( $id );
  232. $compat = get_compat_media_markup( $id );
  233. // Get attachment thumbnail (video).
  234. if ( isset( $attachment['thumb']['src'] ) ) {
  235. $thumb = $attachment['thumb']['src'];
  236. // Look for thumbnail size (image).
  237. } elseif ( isset( $attachment['sizes']['thumbnail']['url'] ) ) {
  238. $thumb = $attachment['sizes']['thumbnail']['url'];
  239. // Use url for svg.
  240. } elseif ( $attachment['type'] === 'image' ) {
  241. $thumb = $attachment['url'];
  242. // Default to icon.
  243. } else {
  244. $thumb = wp_mime_type_icon( $id );
  245. }
  246. // Get attachment dimensions / time / size.
  247. $dimensions = '';
  248. if ( $attachment['type'] === 'audio' ) {
  249. $dimensions = __( 'Length', 'acf' ) . ': ' . $attachment['fileLength'];
  250. } elseif ( ! empty( $attachment['width'] ) ) {
  251. $dimensions = $attachment['width'] . ' x ' . $attachment['height'];
  252. }
  253. if ( ! empty( $attachment['filesizeHumanReadable'] ) ) {
  254. $dimensions .= ' (' . $attachment['filesizeHumanReadable'] . ')';
  255. }
  256. ?>
  257. <div class="acf-gallery-side-info">
  258. <img src="<?php echo esc_attr( $thumb ); ?>" alt="<?php echo esc_attr( $attachment['alt'] ); ?>" />
  259. <p class="filename"><strong><?php echo esc_html( $attachment['filename'] ); ?></strong></p>
  260. <p class="uploaded"><?php echo esc_html( $attachment['dateFormatted'] ); ?></p>
  261. <p class="dimensions"><?php echo esc_html( $dimensions ); ?></p>
  262. <p class="actions">
  263. <a href="#" class="acf-gallery-edit" data-id="<?php echo esc_attr( $id ); ?>"><?php _e( 'Edit', 'acf' ); ?></a>
  264. <a href="#" class="acf-gallery-remove" data-id="<?php echo esc_attr( $id ); ?>"><?php _e( 'Remove', 'acf' ); ?></a>
  265. </p>
  266. </div>
  267. <table class="form-table">
  268. <tbody>
  269. <?php
  270. // Render fields.
  271. $prefix = 'attachments[' . $id . ']';
  272. acf_render_field_wrap(
  273. array(
  274. // 'key' => "{$field['key']}-title",
  275. 'name' => 'title',
  276. 'prefix' => $prefix,
  277. 'type' => 'text',
  278. 'label' => __( 'Title', 'acf' ),
  279. 'value' => $attachment['title'],
  280. ),
  281. 'tr'
  282. );
  283. acf_render_field_wrap(
  284. array(
  285. // 'key' => "{$field['key']}-caption",
  286. 'name' => 'caption',
  287. 'prefix' => $prefix,
  288. 'type' => 'textarea',
  289. 'label' => __( 'Caption', 'acf' ),
  290. 'value' => $attachment['caption'],
  291. ),
  292. 'tr'
  293. );
  294. acf_render_field_wrap(
  295. array(
  296. // 'key' => "{$field['key']}-alt",
  297. 'name' => 'alt',
  298. 'prefix' => $prefix,
  299. 'type' => 'text',
  300. 'label' => __( 'Alt Text', 'acf' ),
  301. 'value' => $attachment['alt'],
  302. ),
  303. 'tr'
  304. );
  305. acf_render_field_wrap(
  306. array(
  307. // 'key' => "{$field['key']}-description",
  308. 'name' => 'description',
  309. 'prefix' => $prefix,
  310. 'type' => 'textarea',
  311. 'label' => __( 'Description', 'acf' ),
  312. 'value' => $attachment['description'],
  313. ),
  314. 'tr'
  315. );
  316. ?>
  317. </tbody>
  318. </table>
  319. <?php
  320. // Display compat fields.
  321. echo $compat['item'];
  322. }
  323. /*
  324. * render_field()
  325. *
  326. * Create the HTML interface for your field
  327. *
  328. * @param $field - an array holding all the field's data
  329. *
  330. * @type action
  331. * @since 3.6
  332. * @date 23/01/13
  333. */
  334. function render_field( $field ) {
  335. // Enqueue uploader assets.
  336. acf_enqueue_uploader();
  337. // Control attributes.
  338. $attrs = array(
  339. 'id' => $field['id'],
  340. 'class' => "acf-gallery {$field['class']}",
  341. 'data-library' => $field['library'],
  342. 'data-preview_size' => $field['preview_size'],
  343. 'data-min' => $field['min'],
  344. 'data-max' => $field['max'],
  345. 'data-mime_types' => $field['mime_types'],
  346. 'data-insert' => $field['insert'],
  347. 'data-columns' => 4,
  348. );
  349. // Set gallery height with deafult of 400px and minimum of 200px.
  350. $height = acf_get_user_setting( 'gallery_height', 400 );
  351. $height = max( $height, 200 );
  352. $attrs['style'] = "height:{$height}px";
  353. // Load attachments.
  354. $attachments = array();
  355. if ( $field['value'] ) {
  356. // Clean value into an array of IDs.
  357. $attachment_ids = array_map( 'intval', acf_array( $field['value'] ) );
  358. // Find posts in database (ensures all results are real).
  359. $posts = acf_get_posts(
  360. array(
  361. 'post_type' => 'attachment',
  362. 'post__in' => $attachment_ids,
  363. 'update_post_meta_cache' => true,
  364. 'update_post_term_cache' => false,
  365. )
  366. );
  367. // Load attatchment data for each post.
  368. $attachments = array_map( 'acf_get_attachment', $posts );
  369. }
  370. ?>
  371. <div <?php echo acf_esc_attrs( $attrs ); ?>>
  372. <input type="hidden" name="<?php echo esc_attr( $field['name'] ); ?>" value="" />
  373. <div class="acf-gallery-main">
  374. <div class="acf-gallery-attachments">
  375. <?php if ( $attachments ) : ?>
  376. <?php
  377. foreach ( $attachments as $i => $attachment ) :
  378. // Vars
  379. $a_id = $attachment['ID'];
  380. $a_title = $attachment['title'];
  381. $a_type = $attachment['type'];
  382. $a_filename = $attachment['filename'];
  383. $a_class = "acf-gallery-attachment -{$a_type}";
  384. // Get thumbnail.
  385. $a_thumbnail = acf_get_post_thumbnail( $a_id, $field['preview_size'] );
  386. $a_class .= ( $a_thumbnail['type'] === 'icon' ) ? ' -icon' : '';
  387. ?>
  388. <div class="<?php echo esc_attr( $a_class ); ?>" data-id="<?php echo esc_attr( $a_id ); ?>">
  389. <input type="hidden" name="<?php echo esc_attr( $field['name'] ); ?>[]" value="<?php echo esc_attr( $a_id ); ?>" />
  390. <div class="margin">
  391. <div class="thumbnail">
  392. <img src="<?php echo esc_url( $a_thumbnail['url'] ); ?>" alt="" />
  393. </div>
  394. <?php if ( $a_type !== 'image' ) : ?>
  395. <div class="filename"><?php echo acf_get_truncated( $a_filename, 30 ); ?></div>
  396. <?php endif; ?>
  397. </div>
  398. <div class="actions">
  399. <a class="acf-icon -cancel dark acf-gallery-remove" href="#" data-id="<?php echo esc_attr( $a_id ); ?>" title="<?php _e( 'Remove', 'acf' ); ?>"></a>
  400. </div>
  401. </div>
  402. <?php endforeach; ?>
  403. <?php endif; ?>
  404. </div>
  405. <div class="acf-gallery-toolbar">
  406. <ul class="acf-hl">
  407. <li>
  408. <a href="#" class="acf-button button button-primary acf-gallery-add"><?php _e( 'Add to gallery', 'acf' ); ?></a>
  409. </li>
  410. <li class="acf-fr">
  411. <select class="acf-gallery-sort">
  412. <option value=""><?php _e( 'Bulk actions', 'acf' ); ?></option>
  413. <option value="date"><?php _e( 'Sort by date uploaded', 'acf' ); ?></option>
  414. <option value="modified"><?php _e( 'Sort by date modified', 'acf' ); ?></option>
  415. <option value="title"><?php _e( 'Sort by title', 'acf' ); ?></option>
  416. <option value="reverse"><?php _e( 'Reverse current order', 'acf' ); ?></option>
  417. </select>
  418. </li>
  419. </ul>
  420. </div>
  421. </div>
  422. <div class="acf-gallery-side">
  423. <div class="acf-gallery-side-inner">
  424. <div class="acf-gallery-side-data"></div>
  425. <div class="acf-gallery-toolbar">
  426. <ul class="acf-hl">
  427. <li>
  428. <a href="#" class="acf-button button acf-gallery-close"><?php _e( 'Close', 'acf' ); ?></a>
  429. </li>
  430. <li class="acf-fr">
  431. <a class="acf-button button button-primary acf-gallery-update" href="#"><?php _e( 'Update', 'acf' ); ?></a>
  432. </li>
  433. </ul>
  434. </div>
  435. </div>
  436. </div>
  437. </div>
  438. <?php
  439. }
  440. /*
  441. * render_field_settings()
  442. *
  443. * Create extra options for your field. This is rendered when editing a field.
  444. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  445. *
  446. * @type action
  447. * @since 3.6
  448. * @date 23/01/13
  449. *
  450. * @param $field - an array holding all the field's data
  451. */
  452. function render_field_settings( $field ) {
  453. acf_render_field_setting(
  454. $field,
  455. array(
  456. 'label' => __( 'Return Format', 'acf' ),
  457. 'instructions' => '',
  458. 'type' => 'radio',
  459. 'name' => 'return_format',
  460. 'layout' => 'horizontal',
  461. 'choices' => array(
  462. 'array' => __( 'Image Array', 'acf' ),
  463. 'url' => __( 'Image URL', 'acf' ),
  464. 'id' => __( 'Image ID', 'acf' ),
  465. ),
  466. )
  467. );
  468. acf_render_field_setting(
  469. $field,
  470. array(
  471. 'label' => __( 'Library', 'acf' ),
  472. 'instructions' => __( 'Limit the media library choice', 'acf' ),
  473. 'type' => 'radio',
  474. 'name' => 'library',
  475. 'layout' => 'horizontal',
  476. 'choices' => array(
  477. 'all' => __( 'All', 'acf' ),
  478. 'uploadedTo' => __( 'Uploaded to post', 'acf' ),
  479. ),
  480. )
  481. );
  482. }
  483. /**
  484. * Renders the field settings used in the "Validation" tab.
  485. *
  486. * @since 6.0
  487. *
  488. * @param array $field The field settings array.
  489. * @return void
  490. */
  491. function render_field_validation_settings( $field ) {
  492. // Clear numeric settings.
  493. $clear = array(
  494. 'min',
  495. 'max',
  496. 'min_width',
  497. 'min_height',
  498. 'min_size',
  499. 'max_width',
  500. 'max_height',
  501. 'max_size',
  502. );
  503. foreach ( $clear as $k ) {
  504. if ( empty( $field[ $k ] ) ) {
  505. $field[ $k ] = '';
  506. }
  507. }
  508. acf_render_field_setting(
  509. $field,
  510. array(
  511. 'label' => __( 'Minimum Selection', 'acf' ),
  512. 'instructions' => '',
  513. 'type' => 'number',
  514. 'name' => 'min',
  515. )
  516. );
  517. acf_render_field_setting(
  518. $field,
  519. array(
  520. 'label' => __( 'Maximum Selection', 'acf' ),
  521. 'instructions' => '',
  522. 'type' => 'number',
  523. 'name' => 'max',
  524. )
  525. );
  526. acf_render_field_setting(
  527. $field,
  528. array(
  529. 'label' => __( 'Minimum', 'acf' ),
  530. 'hint' => __( 'Restrict which images can be uploaded', 'acf' ),
  531. 'type' => 'text',
  532. 'name' => 'min_width',
  533. 'prepend' => __( 'Width', 'acf' ),
  534. 'append' => 'px',
  535. )
  536. );
  537. acf_render_field_setting(
  538. $field,
  539. array(
  540. 'label' => '',
  541. 'type' => 'text',
  542. 'name' => 'min_height',
  543. 'prepend' => __( 'Height', 'acf' ),
  544. 'append' => 'px',
  545. '_append' => 'min_width',
  546. )
  547. );
  548. acf_render_field_setting(
  549. $field,
  550. array(
  551. 'label' => '',
  552. 'type' => 'text',
  553. 'name' => 'min_size',
  554. 'prepend' => __( 'File size', 'acf' ),
  555. 'append' => 'MB',
  556. '_append' => 'min_width',
  557. )
  558. );
  559. acf_render_field_setting(
  560. $field,
  561. array(
  562. 'label' => __( 'Maximum', 'acf' ),
  563. 'hint' => __( 'Restrict which images can be uploaded', 'acf' ),
  564. 'type' => 'text',
  565. 'name' => 'max_width',
  566. 'prepend' => __( 'Width', 'acf' ),
  567. 'append' => 'px',
  568. )
  569. );
  570. acf_render_field_setting(
  571. $field,
  572. array(
  573. 'label' => '',
  574. 'type' => 'text',
  575. 'name' => 'max_height',
  576. 'prepend' => __( 'Height', 'acf' ),
  577. 'append' => 'px',
  578. '_append' => 'max_width',
  579. )
  580. );
  581. acf_render_field_setting(
  582. $field,
  583. array(
  584. 'label' => '',
  585. 'type' => 'text',
  586. 'name' => 'max_size',
  587. 'prepend' => __( 'File size', 'acf' ),
  588. 'append' => 'MB',
  589. '_append' => 'max_width',
  590. )
  591. );
  592. acf_render_field_setting(
  593. $field,
  594. array(
  595. 'label' => __( 'Allowed file types', 'acf' ),
  596. 'hint' => __( 'Comma separated list. Leave blank for all types', 'acf' ),
  597. 'type' => 'text',
  598. 'name' => 'mime_types',
  599. )
  600. );
  601. }
  602. /**
  603. * Renders the field settings used in the "Presentation" tab.
  604. *
  605. * @since 6.0
  606. *
  607. * @param array $field The field settings array.
  608. * @return void
  609. */
  610. function render_field_presentation_settings( $field ) {
  611. acf_render_field_setting(
  612. $field,
  613. array(
  614. 'label' => __( 'Insert', 'acf' ),
  615. 'instructions' => __( 'Specify where new attachments are added', 'acf' ),
  616. 'type' => 'select',
  617. 'name' => 'insert',
  618. 'choices' => array(
  619. 'append' => __( 'Append to the end', 'acf' ),
  620. 'prepend' => __( 'Prepend to the beginning', 'acf' ),
  621. ),
  622. )
  623. );
  624. acf_render_field_setting(
  625. $field,
  626. array(
  627. 'label' => __( 'Preview Size', 'acf' ),
  628. 'instructions' => '',
  629. 'type' => 'select',
  630. 'name' => 'preview_size',
  631. 'choices' => acf_get_image_sizes(),
  632. )
  633. );
  634. }
  635. /*
  636. * format_value()
  637. *
  638. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  639. *
  640. * @type filter
  641. * @since 3.6
  642. * @date 23/01/13
  643. *
  644. * @param $value (mixed) the value which was loaded from the database
  645. * @param $post_id (mixed) the $post_id from which the value was loaded
  646. * @param $field (array) the field array holding all the field options
  647. *
  648. * @return $value (mixed) the modified value
  649. */
  650. function format_value( $value, $post_id, $field ) {
  651. // Bail early if no value.
  652. if ( ! $value ) {
  653. return false;
  654. }
  655. // Clean value into an array of IDs.
  656. $attachment_ids = array_map( 'intval', acf_array( $value ) );
  657. // Find posts in database (ensures all results are real).
  658. $posts = acf_get_posts(
  659. array(
  660. 'post_type' => 'attachment',
  661. 'post__in' => $attachment_ids,
  662. 'update_post_meta_cache' => true,
  663. 'update_post_term_cache' => false,
  664. )
  665. );
  666. // Bail early if no posts found.
  667. if ( ! $posts ) {
  668. return false;
  669. }
  670. // Format values using field settings.
  671. $value = array();
  672. foreach ( $posts as $post ) {
  673. // Return object.
  674. if ( $field['return_format'] == 'object' ) {
  675. $item = $post;
  676. // Return array.
  677. } elseif ( $field['return_format'] == 'array' ) {
  678. $item = acf_get_attachment( $post );
  679. // Return URL.
  680. } elseif ( $field['return_format'] == 'url' ) {
  681. $item = wp_get_attachment_url( $post->ID );
  682. // Return ID.
  683. } else {
  684. $item = $post->ID;
  685. }
  686. // Append item.
  687. $value[] = $item;
  688. }
  689. // Return.
  690. return $value;
  691. }
  692. /*
  693. * validate_value
  694. *
  695. * description
  696. *
  697. * @type function
  698. * @date 11/02/2014
  699. * @since 5.0.0
  700. *
  701. * @param $post_id (int)
  702. * @return $post_id (int)
  703. */
  704. function validate_value( $valid, $value, $field, $input ) {
  705. if ( empty( $value ) || ! is_array( $value ) ) {
  706. $value = array();
  707. }
  708. if ( count( $value ) < $field['min'] ) {
  709. $valid = _n( '%1$s requires at least %2$s selection', '%1$s requires at least %2$s selections', $field['min'], 'acf' );
  710. $valid = sprintf( $valid, $field['label'], $field['min'] );
  711. }
  712. return $valid;
  713. }
  714. /*
  715. * update_value()
  716. *
  717. * This filter is appied to the $value before it is updated in the db
  718. *
  719. * @type filter
  720. * @since 3.6
  721. * @date 23/01/13
  722. *
  723. * @param $value - the value which will be saved in the database
  724. * @param $post_id - the $post_id of which the value will be saved
  725. * @param $field - the field array holding all the field options
  726. *
  727. * @return $value - the modified value
  728. */
  729. function update_value( $value, $post_id, $field ) {
  730. // Bail early if no value.
  731. if ( empty( $value ) ) {
  732. return $value;
  733. }
  734. // Convert to array.
  735. $value = acf_array( $value );
  736. // Format array of values.
  737. // - ensure each value is an id.
  738. // - Parse each id as string for SQL LIKE queries.
  739. $value = array_map( 'acf_idval', $value );
  740. $value = array_map( 'strval', $value );
  741. // Return value.
  742. return $value;
  743. }
  744. /**
  745. * Validates file fields updated via the REST API.
  746. *
  747. * @param bool $valid
  748. * @param int $value
  749. * @param array $field
  750. *
  751. * @return bool|WP_Error
  752. */
  753. public function validate_rest_value( $valid, $value, $field ) {
  754. if ( ! $valid || ! is_array( $value ) ) {
  755. return $valid;
  756. }
  757. foreach ( $value as $attachment_id ) {
  758. $file_valid = acf_get_field_type( 'file' )->validate_rest_value( $valid, $attachment_id, $field );
  759. if ( is_wp_error( $file_valid ) ) {
  760. return $file_valid;
  761. }
  762. }
  763. return $valid;
  764. }
  765. /**
  766. * Return the schema array for the REST API.
  767. *
  768. * @param array $field
  769. * @return array
  770. */
  771. public function get_rest_schema( array $field ) {
  772. $schema = array(
  773. 'type' => array( 'array', 'null' ),
  774. 'required' => ! empty( $field['required'] ),
  775. 'items' => array(
  776. 'type' => 'number',
  777. ),
  778. );
  779. if ( ! empty( $field['min'] ) ) {
  780. $schema['minItems'] = (int) $field['min'];
  781. }
  782. if ( ! empty( $field['max'] ) ) {
  783. $schema['maxItems'] = (int) $field['max'];
  784. }
  785. return $schema;
  786. }
  787. /**
  788. * @see \acf_field::get_rest_links()
  789. * @param mixed $value The raw (unformatted) field value.
  790. * @param int|string $post_id
  791. * @param array $field
  792. * @return array
  793. */
  794. public function get_rest_links( $value, $post_id, array $field ) {
  795. $links = array();
  796. if ( empty( $value ) ) {
  797. return $links;
  798. }
  799. foreach ( (array) $value as $object_id ) {
  800. $links[] = array(
  801. 'rel' => 'acf:attachment',
  802. 'href' => rest_url( '/wp/v2/media/' . $object_id ),
  803. 'embeddable' => true,
  804. );
  805. }
  806. return $links;
  807. }
  808. /**
  809. * Apply basic formatting to prepare the value for default REST output.
  810. *
  811. * @param mixed $value
  812. * @param string|int $post_id
  813. * @param array $field
  814. * @return mixed
  815. */
  816. public function format_value_for_rest( $value, $post_id, array $field ) {
  817. return acf_format_numerics( $value );
  818. }
  819. }
  820. // initialize
  821. acf_register_field_type( 'acf_field_gallery' );
  822. endif; // class_exists check
  823. ?>