class-acf-field-image.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. <?php
  2. if ( ! class_exists( 'acf_field_image' ) ) :
  3. class acf_field_image 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 = 'image';
  19. $this->label = __( 'Image', 'acf' );
  20. $this->category = 'content';
  21. $this->defaults = array(
  22. 'return_format' => 'array',
  23. 'preview_size' => 'medium',
  24. 'library' => 'all',
  25. 'min_width' => 0,
  26. 'min_height' => 0,
  27. 'min_size' => 0,
  28. 'max_width' => 0,
  29. 'max_height' => 0,
  30. 'max_size' => 0,
  31. 'mime_types' => '',
  32. );
  33. // filters
  34. add_filter( 'get_media_item_args', array( $this, 'get_media_item_args' ) );
  35. }
  36. /*
  37. * input_admin_enqueue_scripts
  38. *
  39. * description
  40. *
  41. * @type function
  42. * @date 16/12/2015
  43. * @since 5.3.2
  44. *
  45. * @param $post_id (int)
  46. * @return $post_id (int)
  47. */
  48. function input_admin_enqueue_scripts() {
  49. // localize
  50. acf_localize_text(
  51. array(
  52. 'Select Image' => __( 'Select Image', 'acf' ),
  53. 'Edit Image' => __( 'Edit Image', 'acf' ),
  54. 'Update Image' => __( 'Update Image', 'acf' ),
  55. 'All images' => __( 'All images', 'acf' ),
  56. )
  57. );
  58. }
  59. /**
  60. * Renders the field HTML.
  61. *
  62. * @date 23/01/13
  63. * @since 3.6.0
  64. *
  65. * @param array $field The field settings.
  66. * @return void
  67. */
  68. function render_field( $field ) {
  69. $uploader = acf_get_setting( 'uploader' );
  70. // Enqueue uploader scripts
  71. if ( $uploader === 'wp' ) {
  72. acf_enqueue_uploader();
  73. }
  74. // Elements and attributes.
  75. $value = '';
  76. $div_attrs = array(
  77. 'class' => 'acf-image-uploader',
  78. 'data-preview_size' => $field['preview_size'],
  79. 'data-library' => $field['library'],
  80. 'data-mime_types' => $field['mime_types'],
  81. 'data-uploader' => $uploader,
  82. );
  83. $img_attrs = array(
  84. 'src' => '',
  85. 'alt' => '',
  86. 'data-name' => 'image',
  87. );
  88. // Detect value.
  89. if ( $field['value'] && is_numeric( $field['value'] ) ) {
  90. $image = wp_get_attachment_image_src( $field['value'], $field['preview_size'] );
  91. if ( $image ) {
  92. $value = $field['value'];
  93. $img_attrs['src'] = $image[0];
  94. $img_attrs['alt'] = get_post_meta( $field['value'], '_wp_attachment_image_alt', true );
  95. $div_attrs['class'] .= ' has-value';
  96. }
  97. }
  98. // Add "preview size" max width and height style.
  99. // Apply max-width to wrap, and max-height to img for max compatibility with field widths.
  100. $size = acf_get_image_size( $field['preview_size'] );
  101. $size_w = $size['width'] ? $size['width'] . 'px' : '100%';
  102. $size_h = $size['height'] ? $size['height'] . 'px' : '100%';
  103. $img_attrs['style'] = sprintf( 'max-height: %s;', $size_h );
  104. // Render HTML.
  105. ?>
  106. <div <?php echo acf_esc_attrs( $div_attrs ); ?>>
  107. <?php
  108. acf_hidden_input(
  109. array(
  110. 'name' => $field['name'],
  111. 'value' => $value,
  112. )
  113. );
  114. ?>
  115. <div class="show-if-value image-wrap" style="max-width: <?php echo esc_attr( $size_w ); ?>">
  116. <img <?php echo acf_esc_attrs( $img_attrs ); ?> />
  117. <div class="acf-actions -hover">
  118. <?php if ( $uploader !== 'basic' ) : ?>
  119. <a class="acf-icon -pencil dark" data-name="edit" href="#" title="<?php _e( 'Edit', 'acf' ); ?>"></a>
  120. <?php endif; ?>
  121. <a class="acf-icon -cancel dark" data-name="remove" href="#" title="<?php _e( 'Remove', 'acf' ); ?>"></a>
  122. </div>
  123. </div>
  124. <div class="hide-if-value">
  125. <?php if ( $uploader === 'basic' ) : ?>
  126. <?php if ( $field['value'] && ! is_numeric( $field['value'] ) ) : ?>
  127. <div class="acf-error-message"><p><?php echo acf_esc_html( $field['value'] ); ?></p></div>
  128. <?php endif; ?>
  129. <label class="acf-basic-uploader">
  130. <?php
  131. acf_file_input(
  132. array(
  133. 'name' => $field['name'],
  134. 'id' => $field['id'],
  135. 'key' => $field['key'],
  136. )
  137. );
  138. ?>
  139. </label>
  140. <?php else : ?>
  141. <p><?php _e( 'No image selected', 'acf' ); ?> <a data-name="add" class="acf-button button" href="#"><?php _e( 'Add Image', 'acf' ); ?></a></p>
  142. <?php endif; ?>
  143. </div>
  144. </div>
  145. <?php
  146. }
  147. /*
  148. * render_field_settings()
  149. *
  150. * Create extra options for your field. This is rendered when editing a field.
  151. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  152. *
  153. * @type action
  154. * @since 3.6
  155. * @date 23/01/13
  156. *
  157. * @param $field - an array holding all the field's data
  158. */
  159. function render_field_settings( $field ) {
  160. acf_render_field_setting(
  161. $field,
  162. array(
  163. 'label' => __( 'Return Format', 'acf' ),
  164. 'instructions' => '',
  165. 'type' => 'radio',
  166. 'name' => 'return_format',
  167. 'layout' => 'horizontal',
  168. 'choices' => array(
  169. 'array' => __( 'Image Array', 'acf' ),
  170. 'url' => __( 'Image URL', 'acf' ),
  171. 'id' => __( 'Image ID', 'acf' ),
  172. ),
  173. )
  174. );
  175. acf_render_field_setting(
  176. $field,
  177. array(
  178. 'label' => __( 'Library', 'acf' ),
  179. 'instructions' => __( 'Limit the media library choice', 'acf' ),
  180. 'type' => 'radio',
  181. 'name' => 'library',
  182. 'layout' => 'horizontal',
  183. 'choices' => array(
  184. 'all' => __( 'All', 'acf' ),
  185. 'uploadedTo' => __( 'Uploaded to post', 'acf' ),
  186. ),
  187. )
  188. );
  189. }
  190. /**
  191. * Renders the field settings used in the "Validation" tab.
  192. *
  193. * @since 6.0
  194. *
  195. * @param array $field The field settings array.
  196. * @return void
  197. */
  198. function render_field_validation_settings( $field ) {
  199. // Clear numeric settings.
  200. $clear = array(
  201. 'min_width',
  202. 'min_height',
  203. 'min_size',
  204. 'max_width',
  205. 'max_height',
  206. 'max_size',
  207. );
  208. foreach ( $clear as $k ) {
  209. if ( empty( $field[ $k ] ) ) {
  210. $field[ $k ] = '';
  211. }
  212. }
  213. acf_render_field_setting(
  214. $field,
  215. array(
  216. 'label' => __( 'Minimum', 'acf' ),
  217. 'hint' => __( 'Restrict which images can be uploaded', 'acf' ),
  218. 'type' => 'text',
  219. 'name' => 'min_width',
  220. 'prepend' => __( 'Width', 'acf' ),
  221. 'append' => 'px',
  222. )
  223. );
  224. acf_render_field_setting(
  225. $field,
  226. array(
  227. 'label' => '',
  228. 'type' => 'text',
  229. 'name' => 'min_height',
  230. 'prepend' => __( 'Height', 'acf' ),
  231. 'append' => 'px',
  232. '_append' => 'min_width',
  233. )
  234. );
  235. acf_render_field_setting(
  236. $field,
  237. array(
  238. 'label' => '',
  239. 'type' => 'text',
  240. 'name' => 'min_size',
  241. 'prepend' => __( 'File size', 'acf' ),
  242. 'append' => 'MB',
  243. '_append' => 'min_width',
  244. )
  245. );
  246. acf_render_field_setting(
  247. $field,
  248. array(
  249. 'label' => __( 'Maximum', 'acf' ),
  250. 'hint' => __( 'Restrict which images can be uploaded', 'acf' ),
  251. 'type' => 'text',
  252. 'name' => 'max_width',
  253. 'prepend' => __( 'Width', 'acf' ),
  254. 'append' => 'px',
  255. )
  256. );
  257. acf_render_field_setting(
  258. $field,
  259. array(
  260. 'label' => '',
  261. 'type' => 'text',
  262. 'name' => 'max_height',
  263. 'prepend' => __( 'Height', 'acf' ),
  264. 'append' => 'px',
  265. '_append' => 'max_width',
  266. )
  267. );
  268. acf_render_field_setting(
  269. $field,
  270. array(
  271. 'label' => '',
  272. 'type' => 'text',
  273. 'name' => 'max_size',
  274. 'prepend' => __( 'File size', 'acf' ),
  275. 'append' => 'MB',
  276. '_append' => 'max_width',
  277. )
  278. );
  279. acf_render_field_setting(
  280. $field,
  281. array(
  282. 'label' => __( 'Allowed file types', 'acf' ),
  283. 'instructions' => __( 'Comma separated list. Leave blank for all types', 'acf' ),
  284. 'type' => 'text',
  285. 'name' => 'mime_types',
  286. )
  287. );
  288. }
  289. /**
  290. * Renders the field settings used in the "Presentation" tab.
  291. *
  292. * @since 6.0
  293. *
  294. * @param array $field The field settings array.
  295. * @return void
  296. */
  297. function render_field_presentation_settings( $field ) {
  298. acf_render_field_setting(
  299. $field,
  300. array(
  301. 'label' => __( 'Preview Size', 'acf' ),
  302. 'instructions' => '',
  303. 'type' => 'select',
  304. 'name' => 'preview_size',
  305. 'choices' => acf_get_image_sizes(),
  306. )
  307. );
  308. }
  309. /*
  310. * format_value()
  311. *
  312. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  313. *
  314. * @type filter
  315. * @since 3.6
  316. * @date 23/01/13
  317. *
  318. * @param $value (mixed) the value which was loaded from the database
  319. * @param $post_id (mixed) the $post_id from which the value was loaded
  320. * @param $field (array) the field array holding all the field options
  321. *
  322. * @return $value (mixed) the modified value
  323. */
  324. function format_value( $value, $post_id, $field ) {
  325. // bail early if no value
  326. if ( empty( $value ) ) {
  327. return false;
  328. }
  329. // bail early if not numeric (error message)
  330. if ( ! is_numeric( $value ) ) {
  331. return false;
  332. }
  333. // convert to int
  334. $value = intval( $value );
  335. // format
  336. if ( $field['return_format'] == 'url' ) {
  337. return wp_get_attachment_url( $value );
  338. } elseif ( $field['return_format'] == 'array' ) {
  339. return acf_get_attachment( $value );
  340. }
  341. // return
  342. return $value;
  343. }
  344. /*
  345. * get_media_item_args
  346. *
  347. * description
  348. *
  349. * @type function
  350. * @date 27/01/13
  351. * @since 3.6.0
  352. *
  353. * @param $vars (array)
  354. * @return $vars
  355. */
  356. function get_media_item_args( $vars ) {
  357. $vars['send'] = true;
  358. return( $vars );
  359. }
  360. /*
  361. * update_value()
  362. *
  363. * This filter is appied to the $value before it is updated in the db
  364. *
  365. * @type filter
  366. * @since 3.6
  367. * @date 23/01/13
  368. *
  369. * @param $value - the value which will be saved in the database
  370. * @param $post_id - the $post_id of which the value will be saved
  371. * @param $field - the field array holding all the field options
  372. *
  373. * @return $value - the modified value
  374. */
  375. function update_value( $value, $post_id, $field ) {
  376. return acf_get_field_type( 'file' )->update_value( $value, $post_id, $field );
  377. }
  378. /**
  379. * validate_value
  380. *
  381. * This function will validate a basic file input
  382. *
  383. * @type function
  384. * @date 11/02/2014
  385. * @since 5.0.0
  386. *
  387. * @param $post_id (int)
  388. * @return $post_id (int)
  389. */
  390. function validate_value( $valid, $value, $field, $input ) {
  391. return acf_get_field_type( 'file' )->validate_value( $valid, $value, $field, $input );
  392. }
  393. /**
  394. * Additional validation for the image field when submitted via REST.
  395. *
  396. * @param bool $valid
  397. * @param int $value
  398. * @param array $field
  399. *
  400. * @return bool|WP_Error
  401. */
  402. public function validate_rest_value( $valid, $value, $field ) {
  403. return acf_get_field_type( 'file' )->validate_rest_value( $valid, $value, $field );
  404. }
  405. /**
  406. * Return the schema array for the REST API.
  407. *
  408. * @param array $field
  409. * @return array
  410. */
  411. public function get_rest_schema( array $field ) {
  412. return acf_get_field_type( 'file' )->get_rest_schema( $field );
  413. }
  414. /**
  415. * Apply basic formatting to prepare the value for default REST output.
  416. *
  417. * @param mixed $value
  418. * @param string|int $post_id
  419. * @param array $field
  420. * @return mixed
  421. */
  422. public function format_value_for_rest( $value, $post_id, array $field ) {
  423. return acf_format_numerics( $value );
  424. }
  425. }
  426. // initialize
  427. acf_register_field_type( 'acf_field_image' );
  428. endif; // class_exists check
  429. ?>