acf_field_image

The Advanced Custom Fields acf field image class.

Defined (1)

The class is defined in the following location(s).

/core/fields/image.php  
  1. class acf_field_image extends acf_field 
  2.  
  3. /** 
  4. * __construct 
  5. * Set name / label needed for actions / filters 
  6. * @since 3.6 
  7. * @date 23/01/13 
  8. */ 
  9.  
  10. function __construct() 
  11. // vars 
  12. $this->name = 'image'; 
  13. $this->label = __("Image", 'acf'); 
  14. $this->category = __("Content", 'acf'); 
  15. $this->defaults = array( 
  16. 'save_format' => 'object',  
  17. 'preview_size' => 'thumbnail',  
  18. 'library' => 'all' 
  19. ); 
  20. $this->l10n = array( 
  21. 'select' => __("Select Image", 'acf'),  
  22. 'edit' => __("Edit Image", 'acf'),  
  23. 'update' => __("Update Image", 'acf'),  
  24. 'uploadedTo' => __("Uploaded to this post", 'acf'),  
  25. ); 
  26.  
  27.  
  28. // do not delete! 
  29. parent::__construct(); 
  30.  
  31.  
  32. // filters 
  33. add_filter('get_media_item_args', array($this, 'get_media_item_args')); 
  34. add_filter('wp_prepare_attachment_for_js', array($this, 'wp_prepare_attachment_for_js'), 10, 3); 
  35.  
  36.  
  37. // JSON 
  38. add_action('wp_ajax_acf/fields/image/get_images', array($this, 'ajax_get_images'), 10, 1); 
  39. add_action('wp_ajax_nopriv_acf/fields/image/get_images', array($this, 'ajax_get_images'), 10, 1); 
  40.  
  41.  
  42. /** 
  43. * create_field() 
  44. * Create the HTML interface for your field 
  45. * @param $field - an array holding all the field's data 
  46. * @type action 
  47. * @since 3.6 
  48. * @date 23/01/13 
  49. */ 
  50.  
  51. function create_field( $field ) 
  52. // vars 
  53. $o = array( 
  54. 'class' => '',  
  55. 'url' => '',  
  56. ); 
  57.  
  58.  
  59. // has value? 
  60. if( $field['value'] && is_numeric($field['value']) ) { 
  61.  
  62. $url = wp_get_attachment_image_src($field['value'], $field['preview_size']); 
  63.  
  64. if( $url ) { 
  65.  
  66. $o['url'] = $url[0]; 
  67. $o['class'] = 'active'; 
  68.  
  69.  
  70.  
  71. ?> 
  72. <div class="acf-image-uploader clearfix <?php echo $o['class']; ?>" data-preview_size="<?php echo $field['preview_size']; ?>" data-library="<?php echo $field['library']; ?>" > 
  73. <input class="acf-image-value" type="hidden" name="<?php echo $field['name']; ?>" value="<?php echo $field['value']; ?>" /> 
  74. <div class="has-image"> 
  75. <div class="hover"> 
  76. <ul class="bl"> 
  77. <li><a class="acf-button-delete ir" href="#"><?php _e("Remove", 'acf'); ?></a></li> 
  78. <li><a class="acf-button-edit ir" href="#"><?php _e("Edit", 'acf'); ?></a></li> 
  79. </ul> 
  80. </div> 
  81. <img class="acf-image-image" src="<?php echo $o['url']; ?>" alt=""/> 
  82. </div> 
  83. <div class="no-image"> 
  84. <p><?php _e('No image selected', 'acf'); ?> <input type="button" class="button add-image" value="<?php _e('Add Image', 'acf'); ?>" /> 
  85. </div> 
  86. </div> 
  87. <?php 
  88.  
  89.  
  90. /** 
  91. * create_options() 
  92. * Create extra options for your field. This is rendered when editing a field. 
  93. * The value of $field['name'] can be used (like bellow) to save extra data to the $field 
  94. * @type action 
  95. * @since 3.6 
  96. * @date 23/01/13 
  97. * @param $field - an array holding all the field's data 
  98. */ 
  99.  
  100. function create_options( $field ) 
  101. // vars 
  102. $key = $field['name']; 
  103.  
  104. ?> 
  105. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  106. <td class="label"> 
  107. <label><?php _e("Return Value", 'acf'); ?></label> 
  108. <p><?php _e("Specify the returned value on front end", 'acf') ?></p> 
  109. </td> 
  110. <td> 
  111. <?php 
  112. do_action('acf/create_field', array( 
  113. 'type' => 'radio',  
  114. 'name' => 'fields['.$key.'][save_format]',  
  115. 'value' => $field['save_format'],  
  116. 'layout' => 'horizontal',  
  117. 'choices' => array( 
  118. 'object' => __("Image Object", 'acf'),  
  119. 'url' => __("Image URL", 'acf'),  
  120. 'id' => __("Image ID", 'acf') 
  121. )); 
  122. ?> 
  123. </td> 
  124. </tr> 
  125. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  126. <td class="label"> 
  127. <label><?php _e("Preview Size", 'acf'); ?></label> 
  128. <p><?php _e("Shown when entering data", 'acf') ?></p> 
  129. </td> 
  130. <td> 
  131. <?php 
  132.  
  133. do_action('acf/create_field', array( 
  134. 'type' => 'radio',  
  135. 'name' => 'fields['.$key.'][preview_size]',  
  136. 'value' => $field['preview_size'],  
  137. 'layout' => 'horizontal',  
  138. 'choices' => apply_filters('acf/get_image_sizes', array()) 
  139. )); 
  140.  
  141. ?> 
  142. </td> 
  143. </tr> 
  144. <tr class="field_option field_option_<?php echo $this->name; ?>"> 
  145. <td class="label"> 
  146. <label><?php _e("Library", 'acf'); ?></label> 
  147. <p><?php _e("Limit the media library choice", 'acf') ?></p> 
  148. </td> 
  149. <td> 
  150. <?php 
  151.  
  152. do_action('acf/create_field', array( 
  153. 'type' => 'radio',  
  154. 'name' => 'fields['.$key.'][library]',  
  155. 'value' => $field['library'],  
  156. 'layout' => 'horizontal',  
  157. 'choices' => array( 
  158. 'all' => __('All', 'acf'),  
  159. 'uploadedTo' => __('Uploaded to post', 'acf') 
  160. )); 
  161.  
  162. ?> 
  163. </td> 
  164. </tr> 
  165. <?php 
  166.  
  167.  
  168.  
  169. /** 
  170. * format_value_for_api() 
  171. * 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 
  172. * @type filter 
  173. * @since 3.6 
  174. * @date 23/01/13 
  175. * @param $value - the value which was loaded from the database 
  176. * @param $post_id - the $post_id from which the value was loaded 
  177. * @param $field - the field array holding all the field options 
  178. * @return $value - the modified value 
  179. */ 
  180.  
  181. function format_value_for_api( $value, $post_id, $field ) 
  182.  
  183. // validate 
  184. if( !$value ) 
  185. return false; 
  186.  
  187.  
  188. // format 
  189. if( $field['save_format'] == 'url' ) 
  190. $value = wp_get_attachment_url( $value ); 
  191. elseif( $field['save_format'] == 'object' ) 
  192. $attachment = get_post( $value ); 
  193.  
  194.  
  195. // validate 
  196. if( !$attachment ) 
  197. return false;  
  198.  
  199.  
  200. // create array to hold value data 
  201. $src = wp_get_attachment_image_src( $attachment->ID, 'full' ); 
  202.  
  203. $value = array( 
  204. 'id' => $attachment->ID,  
  205. 'alt' => get_post_meta($attachment->ID, '_wp_attachment_image_alt', true),  
  206. 'title' => $attachment->post_title,  
  207. 'caption' => $attachment->post_excerpt,  
  208. 'description' => $attachment->post_content,  
  209. 'mime_type' => $attachment->post_mime_type,  
  210. 'url' => $src[0],  
  211. 'width' => $src[1],  
  212. 'height' => $src[2],  
  213. 'sizes' => array(),  
  214. ); 
  215.  
  216.  
  217. // find all image sizes 
  218. $image_sizes = get_intermediate_image_sizes(); 
  219.  
  220. if( $image_sizes ) 
  221. foreach( $image_sizes as $image_size ) 
  222. // find src 
  223. $src = wp_get_attachment_image_src( $attachment->ID, $image_size ); 
  224.  
  225. // add src 
  226. $value[ 'sizes' ][ $image_size ] = $src[0]; 
  227. $value[ 'sizes' ][ $image_size . '-width' ] = $src[1]; 
  228. $value[ 'sizes' ][ $image_size . '-height' ] = $src[2]; 
  229. // foreach( $image_sizes as $image_size ) 
  230. // if( $image_sizes ) 
  231.  
  232.  
  233. return $value; 
  234.  
  235.  
  236.  
  237. /** 
  238. * get_media_item_args 
  239. * @description:  
  240. * @since: 3.6 
  241. * @created: 27/01/13 
  242. */ 
  243.  
  244. function get_media_item_args( $vars ) 
  245. $vars['send'] = true; 
  246. return($vars); 
  247.  
  248.  
  249. /** 
  250. * ajax_get_images 
  251. * @description:  
  252. * @since: 3.5.7 
  253. * @created: 13/01/13 
  254. */ 
  255.  
  256. function ajax_get_images() 
  257. // vars 
  258. $options = array( 
  259. 'nonce' => '',  
  260. 'images' => array(),  
  261. 'preview_size' => 'thumbnail' 
  262. ); 
  263. $return = array(); 
  264.  
  265.  
  266. // load post options 
  267. $options = array_merge($options, $_POST); 
  268.  
  269.  
  270. // verify nonce 
  271. if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') ) 
  272. die(0); 
  273.  
  274.  
  275. if( $options['images'] ) 
  276. foreach( $options['images'] as $id ) 
  277. $url = wp_get_attachment_image_src( $id, $options['preview_size'] ); 
  278.  
  279.  
  280. $return[] = array( 
  281. 'id' => $id,  
  282. 'url' => $url[0],  
  283. ); 
  284.  
  285.  
  286. // return json 
  287. echo json_encode( $return ); 
  288. die; 
  289.  
  290.  
  291.  
  292. /** 
  293. * image_size_names_choose 
  294. * @description:  
  295. * @since: 3.5.7 
  296. * @created: 13/01/13 
  297. */ 
  298.  
  299. function image_size_names_choose( $sizes ) 
  300. global $_wp_additional_image_sizes; 
  301.  
  302. if( $_wp_additional_image_sizes ) 
  303. foreach( $_wp_additional_image_sizes as $k => $v ) 
  304. $title = $k; 
  305. $title = str_replace('-', ' ', $title); 
  306. $title = str_replace('_', ' ', $title); 
  307. $title = ucwords( $title ); 
  308.  
  309. $sizes[ $k ] = $title; 
  310. // foreach( $image_sizes as $image_size ) 
  311.  
  312. return $sizes; 
  313.  
  314.  
  315. /** 
  316. * wp_prepare_attachment_for_js 
  317. * @description: This sneaky hook adds the missing sizes to each attachment in the 3.5 uploader. It would be a lot easier to add all the sizes to the 'image_size_names_choose' filter but then it will show up on the normal the_content editor 
  318. * @since: 3.5.7 
  319. * @created: 13/01/13 
  320. */ 
  321.  
  322. function wp_prepare_attachment_for_js( $response, $attachment, $meta ) 
  323. // only for image 
  324. if( $response['type'] != 'image' ) 
  325. return $response; 
  326.  
  327.  
  328. // make sure sizes exist. Perhaps they dont? 
  329. if( !isset($meta['sizes']) ) 
  330. return $response; 
  331.  
  332.  
  333. $attachment_url = $response['url']; 
  334. $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url ); 
  335.  
  336. if( isset($meta['sizes']) && is_array($meta['sizes']) ) 
  337. foreach( $meta['sizes'] as $k => $v ) 
  338. if( !isset($response['sizes'][ $k ]) ) 
  339. $response['sizes'][ $k ] = array( 
  340. 'height' => $v['height'],  
  341. 'width' => $v['width'],  
  342. 'url' => $base_url . $v['file'],  
  343. 'orientation' => $v['height'] > $v['width'] ? 'portrait' : 'landscape',  
  344. ); 
  345.  
  346. return $response; 
  347.  
  348.  
  349. /** 
  350. * update_value() 
  351. * This filter is appied to the $value before it is updated in the db 
  352. * @type filter 
  353. * @since 3.6 
  354. * @date 23/01/13 
  355. * @param $value - the value which will be saved in the database 
  356. * @param $post_id - the $post_id of which the value will be saved 
  357. * @param $field - the field array holding all the field options 
  358. * @return $value - the modified value 
  359. */ 
  360.  
  361. function update_value( $value, $post_id, $field ) 
  362. // array? 
  363. if( is_array($value) && isset($value['id']) ) 
  364. $value = $value['id'];  
  365.  
  366. // object? 
  367. if( is_object($value) && isset($value->ID) ) 
  368. $value = $value->ID; 
  369.  
  370. return $value; 
  371.  
  372.