No Description

class-acf-field-image.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. )
  136. );
  137. ?>
  138. </label>
  139. <?php else : ?>
  140. <p><?php _e( 'No image selected', 'acf' ); ?> <a data-name="add" class="acf-button button" href="#"><?php _e( 'Add Image', 'acf' ); ?></a></p>
  141. <?php endif; ?>
  142. </div>
  143. </div>
  144. <?php
  145. }
  146. /*
  147. * render_field_settings()
  148. *
  149. * Create extra options for your field. This is rendered when editing a field.
  150. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  151. *
  152. * @type action
  153. * @since 3.6
  154. * @date 23/01/13
  155. *
  156. * @param $field - an array holding all the field's data
  157. */
  158. function render_field_settings( $field ) {
  159. // clear numeric settings
  160. $clear = array(
  161. 'min_width',
  162. 'min_height',
  163. 'min_size',
  164. 'max_width',
  165. 'max_height',
  166. 'max_size',
  167. );
  168. foreach ( $clear as $k ) {
  169. if ( empty( $field[ $k ] ) ) {
  170. $field[ $k ] = '';
  171. }
  172. }
  173. // return_format
  174. acf_render_field_setting(
  175. $field,
  176. array(
  177. 'label' => __( 'Return Format', 'acf' ),
  178. 'instructions' => '',
  179. 'type' => 'radio',
  180. 'name' => 'return_format',
  181. 'layout' => 'horizontal',
  182. 'choices' => array(
  183. 'array' => __( 'Image Array', 'acf' ),
  184. 'url' => __( 'Image URL', 'acf' ),
  185. 'id' => __( 'Image ID', 'acf' ),
  186. ),
  187. )
  188. );
  189. // preview_size
  190. acf_render_field_setting(
  191. $field,
  192. array(
  193. 'label' => __( 'Preview Size', 'acf' ),
  194. 'instructions' => '',
  195. 'type' => 'select',
  196. 'name' => 'preview_size',
  197. 'choices' => acf_get_image_sizes(),
  198. )
  199. );
  200. // library
  201. acf_render_field_setting(
  202. $field,
  203. array(
  204. 'label' => __( 'Library', 'acf' ),
  205. 'instructions' => __( 'Limit the media library choice', 'acf' ),
  206. 'type' => 'radio',
  207. 'name' => 'library',
  208. 'layout' => 'horizontal',
  209. 'choices' => array(
  210. 'all' => __( 'All', 'acf' ),
  211. 'uploadedTo' => __( 'Uploaded to post', 'acf' ),
  212. ),
  213. )
  214. );
  215. // min
  216. acf_render_field_setting(
  217. $field,
  218. array(
  219. 'label' => __( 'Minimum', 'acf' ),
  220. 'instructions' => __( 'Restrict which images can be uploaded', 'acf' ),
  221. 'type' => 'text',
  222. 'name' => 'min_width',
  223. 'prepend' => __( 'Width', 'acf' ),
  224. 'append' => 'px',
  225. )
  226. );
  227. acf_render_field_setting(
  228. $field,
  229. array(
  230. 'label' => '',
  231. 'type' => 'text',
  232. 'name' => 'min_height',
  233. 'prepend' => __( 'Height', 'acf' ),
  234. 'append' => 'px',
  235. '_append' => 'min_width',
  236. )
  237. );
  238. acf_render_field_setting(
  239. $field,
  240. array(
  241. 'label' => '',
  242. 'type' => 'text',
  243. 'name' => 'min_size',
  244. 'prepend' => __( 'File size', 'acf' ),
  245. 'append' => 'MB',
  246. '_append' => 'min_width',
  247. )
  248. );
  249. // max
  250. acf_render_field_setting(
  251. $field,
  252. array(
  253. 'label' => __( 'Maximum', 'acf' ),
  254. 'instructions' => __( 'Restrict which images can be uploaded', 'acf' ),
  255. 'type' => 'text',
  256. 'name' => 'max_width',
  257. 'prepend' => __( 'Width', 'acf' ),
  258. 'append' => 'px',
  259. )
  260. );
  261. acf_render_field_setting(
  262. $field,
  263. array(
  264. 'label' => '',
  265. 'type' => 'text',
  266. 'name' => 'max_height',
  267. 'prepend' => __( 'Height', 'acf' ),
  268. 'append' => 'px',
  269. '_append' => 'max_width',
  270. )
  271. );
  272. acf_render_field_setting(
  273. $field,
  274. array(
  275. 'label' => '',
  276. 'type' => 'text',
  277. 'name' => 'max_size',
  278. 'prepend' => __( 'File size', 'acf' ),
  279. 'append' => 'MB',
  280. '_append' => 'max_width',
  281. )
  282. );
  283. // allowed type
  284. acf_render_field_setting(
  285. $field,
  286. array(
  287. 'label' => __( 'Allowed file types', 'acf' ),
  288. 'instructions' => __( 'Comma separated list. Leave blank for all types', 'acf' ),
  289. 'type' => 'text',
  290. 'name' => 'mime_types',
  291. )
  292. );
  293. }
  294. /*
  295. * format_value()
  296. *
  297. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  298. *
  299. * @type filter
  300. * @since 3.6
  301. * @date 23/01/13
  302. *
  303. * @param $value (mixed) the value which was loaded from the database
  304. * @param $post_id (mixed) the $post_id from which the value was loaded
  305. * @param $field (array) the field array holding all the field options
  306. *
  307. * @return $value (mixed) the modified value
  308. */
  309. function format_value( $value, $post_id, $field ) {
  310. // bail early if no value
  311. if ( empty( $value ) ) {
  312. return false;
  313. }
  314. // bail early if not numeric (error message)
  315. if ( ! is_numeric( $value ) ) {
  316. return false;
  317. }
  318. // convert to int
  319. $value = intval( $value );
  320. // format
  321. if ( $field['return_format'] == 'url' ) {
  322. return wp_get_attachment_url( $value );
  323. } elseif ( $field['return_format'] == 'array' ) {
  324. return acf_get_attachment( $value );
  325. }
  326. // return
  327. return $value;
  328. }
  329. /*
  330. * get_media_item_args
  331. *
  332. * description
  333. *
  334. * @type function
  335. * @date 27/01/13
  336. * @since 3.6.0
  337. *
  338. * @param $vars (array)
  339. * @return $vars
  340. */
  341. function get_media_item_args( $vars ) {
  342. $vars['send'] = true;
  343. return( $vars );
  344. }
  345. /*
  346. * update_value()
  347. *
  348. * This filter is appied to the $value before it is updated in the db
  349. *
  350. * @type filter
  351. * @since 3.6
  352. * @date 23/01/13
  353. *
  354. * @param $value - the value which will be saved in the database
  355. * @param $post_id - the $post_id of which the value will be saved
  356. * @param $field - the field array holding all the field options
  357. *
  358. * @return $value - the modified value
  359. */
  360. function update_value( $value, $post_id, $field ) {
  361. return acf_get_field_type( 'file' )->update_value( $value, $post_id, $field );
  362. }
  363. /**
  364. * validate_value
  365. *
  366. * This function will validate a basic file input
  367. *
  368. * @type function
  369. * @date 11/02/2014
  370. * @since 5.0.0
  371. *
  372. * @param $post_id (int)
  373. * @return $post_id (int)
  374. */
  375. function validate_value( $valid, $value, $field, $input ) {
  376. return acf_get_field_type( 'file' )->validate_value( $valid, $value, $field, $input );
  377. }
  378. /**
  379. * Additional validation for the image field when submitted via REST.
  380. *
  381. * @param bool $valid
  382. * @param int $value
  383. * @param array $field
  384. *
  385. * @return bool|WP_Error
  386. */
  387. public function validate_rest_value( $valid, $value, $field ) {
  388. return acf_get_field_type( 'file' )->validate_rest_value( $valid, $value, $field );
  389. }
  390. /**
  391. * Return the schema array for the REST API.
  392. *
  393. * @param array $field
  394. * @return array
  395. */
  396. public function get_rest_schema( array $field ) {
  397. return acf_get_field_type( 'file' )->get_rest_schema( $field );
  398. }
  399. /**
  400. * Apply basic formatting to prepare the value for default REST output.
  401. *
  402. * @param mixed $value
  403. * @param string|int $post_id
  404. * @param array $field
  405. * @return mixed
  406. */
  407. public function format_value_for_rest( $value, $post_id, array $field ) {
  408. return acf_format_numerics( $value );
  409. }
  410. }
  411. // initialize
  412. acf_register_field_type( 'acf_field_image' );
  413. endif; // class_exists check
  414. ?>