/core/fields/image.php

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