PageRenderTime 44ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/advanced-custom-fields-pro/fields/image.php

https://gitlab.com/sihabudinahmad/asppi
PHP | 514 lines | 236 code | 125 blank | 153 comment | 27 complexity | d00bcfb171d0ff87ee0e0f53f8f57b68 MD5 | raw file
  1. <?php
  2. /*
  3. * ACF Image Field Class
  4. *
  5. * All the logic for this field type
  6. *
  7. * @class acf_field_image
  8. * @extends acf_field
  9. * @package ACF
  10. * @subpackage Fields
  11. */
  12. if( ! class_exists('acf_field_image') ) :
  13. class acf_field_image extends acf_field {
  14. /*
  15. * __construct
  16. *
  17. * This function will setup the field type data
  18. *
  19. * @type function
  20. * @date 5/03/2014
  21. * @since 5.0.0
  22. *
  23. * @param n/a
  24. * @return n/a
  25. */
  26. function __construct() {
  27. // vars
  28. $this->name = 'image';
  29. $this->label = __("Image",'acf');
  30. $this->category = 'content';
  31. $this->defaults = array(
  32. 'return_format' => 'array',
  33. 'preview_size' => 'thumbnail',
  34. 'library' => 'all',
  35. 'min_width' => 0,
  36. 'min_height' => 0,
  37. 'min_size' => 0,
  38. 'max_width' => 0,
  39. 'max_height' => 0,
  40. 'max_size' => 0,
  41. 'mime_types' => ''
  42. );
  43. $this->l10n = array(
  44. 'select' => __("Select Image",'acf'),
  45. 'edit' => __("Edit Image",'acf'),
  46. 'update' => __("Update Image",'acf'),
  47. 'uploadedTo' => __("Uploaded to this post",'acf'),
  48. 'all' => __("All images",'acf'),
  49. );
  50. // filters
  51. add_filter('get_media_item_args', array($this, 'get_media_item_args'));
  52. add_filter('wp_prepare_attachment_for_js', array($this, 'wp_prepare_attachment_for_js'), 10, 3);
  53. // do not delete!
  54. parent::__construct();
  55. }
  56. /*
  57. * render_field()
  58. *
  59. * Create the HTML interface for your field
  60. *
  61. * @param $field - an array holding all the field's data
  62. *
  63. * @type action
  64. * @since 3.6
  65. * @date 23/01/13
  66. */
  67. function render_field( $field ) {
  68. // enqueue
  69. acf_enqueue_uploader();
  70. // vars
  71. $div = array(
  72. 'class' => 'acf-image-uploader acf-cf',
  73. 'data-preview_size' => $field['preview_size'],
  74. 'data-library' => $field['library'],
  75. 'data-mime_types' => $field['mime_types']
  76. );
  77. $url = '';
  78. // has value?
  79. if( $field['value'] && is_numeric($field['value']) ) {
  80. $url = wp_get_attachment_image_src($field['value'], $field['preview_size']);
  81. if( $url ) {
  82. $url = $url[0];
  83. $div['class'] .= ' has-value';
  84. }
  85. }
  86. // uploader
  87. $uploader = acf_get_setting('uploader');
  88. if( $uploader == 'basic' ) {
  89. $div['class'] .= ' basic';
  90. }
  91. ?>
  92. <div <?php acf_esc_attr_e( $div ); ?>>
  93. <div class="acf-hidden">
  94. <?php acf_hidden_input(array( 'name' => $field['name'], 'value' => $field['value'], 'data-name' => 'id' )); ?>
  95. </div>
  96. <div class="view show-if-value acf-soh">
  97. <img data-name="image" src="<?php echo $url; ?>" alt=""/>
  98. <ul class="acf-hl acf-soh-target">
  99. <?php if( $uploader != 'basic' ): ?>
  100. <li><a class="acf-icon dark" data-name="edit" href="#"><i class="acf-sprite-edit"></i></a></li>
  101. <?php endif; ?>
  102. <li><a class="acf-icon dark" data-name="remove" href="#"><i class="acf-sprite-delete"></i></a></li>
  103. </ul>
  104. </div>
  105. <div class="view hide-if-value">
  106. <?php if( $uploader == 'basic' ): ?>
  107. <?php if( $field['value'] && !is_numeric($field['value']) ): ?>
  108. <div class="acf-error-message"><p><?php echo $field['value']; ?></p></div>
  109. <?php endif; ?>
  110. <input type="file" name="<?php echo $field['name']; ?>" id="<?php echo $field['id']; ?>" />
  111. <?php else: ?>
  112. <p style="margin:0;"><?php _e('No image selected','acf'); ?> <a data-name="add" class="acf-button" href="#"><?php _e('Add Image','acf'); ?></a></p>
  113. <?php endif; ?>
  114. </div>
  115. </div>
  116. <?php
  117. }
  118. /*
  119. * render_field_settings()
  120. *
  121. * Create extra options for your field. This is rendered when editing a field.
  122. * The value of $field['name'] can be used (like bellow) to save extra data to the $field
  123. *
  124. * @type action
  125. * @since 3.6
  126. * @date 23/01/13
  127. *
  128. * @param $field - an array holding all the field's data
  129. */
  130. function render_field_settings( $field ) {
  131. // clear numeric settings
  132. $clear = array(
  133. 'min_width',
  134. 'min_height',
  135. 'min_size',
  136. 'max_width',
  137. 'max_height',
  138. 'max_size'
  139. );
  140. foreach( $clear as $k ) {
  141. if( empty($field[$k]) ) {
  142. $field[$k] = '';
  143. }
  144. }
  145. // return_format
  146. acf_render_field_setting( $field, array(
  147. 'label' => __('Return Value','acf'),
  148. 'instructions' => __('Specify the returned value on front end','acf'),
  149. 'type' => 'radio',
  150. 'name' => 'return_format',
  151. 'layout' => 'horizontal',
  152. 'choices' => array(
  153. 'array' => __("Image Array",'acf'),
  154. 'url' => __("Image URL",'acf'),
  155. 'id' => __("Image ID",'acf')
  156. )
  157. ));
  158. // preview_size
  159. acf_render_field_setting( $field, array(
  160. 'label' => __('Preview Size','acf'),
  161. 'instructions' => __('Shown when entering data','acf'),
  162. 'type' => 'select',
  163. 'name' => 'preview_size',
  164. 'choices' => acf_get_image_sizes()
  165. ));
  166. // library
  167. acf_render_field_setting( $field, array(
  168. 'label' => __('Library','acf'),
  169. 'instructions' => __('Limit the media library choice','acf'),
  170. 'type' => 'radio',
  171. 'name' => 'library',
  172. 'layout' => 'horizontal',
  173. 'choices' => array(
  174. 'all' => __('All', 'acf'),
  175. 'uploadedTo' => __('Uploaded to post', 'acf')
  176. )
  177. ));
  178. // min
  179. acf_render_field_setting( $field, array(
  180. 'label' => __('Minimum','acf'),
  181. 'instructions' => __('Restrict which images can be uploaded','acf'),
  182. 'type' => 'text',
  183. 'name' => 'min_width',
  184. 'prepend' => __('Width', 'acf'),
  185. 'append' => 'px',
  186. ));
  187. acf_render_field_setting( $field, array(
  188. 'label' => '',
  189. 'type' => 'text',
  190. 'name' => 'min_height',
  191. 'prepend' => __('Height', 'acf'),
  192. 'append' => 'px',
  193. 'wrapper' => array(
  194. 'data-append' => 'min_width'
  195. )
  196. ));
  197. acf_render_field_setting( $field, array(
  198. 'label' => '',
  199. 'type' => 'text',
  200. 'name' => 'min_size',
  201. 'prepend' => __('File size', 'acf'),
  202. 'append' => 'MB',
  203. 'wrapper' => array(
  204. 'data-append' => 'min_width'
  205. )
  206. ));
  207. // max
  208. acf_render_field_setting( $field, array(
  209. 'label' => __('Maximum','acf'),
  210. 'instructions' => __('Restrict which images can be uploaded','acf'),
  211. 'type' => 'text',
  212. 'name' => 'max_width',
  213. 'prepend' => __('Width', 'acf'),
  214. 'append' => 'px',
  215. ));
  216. acf_render_field_setting( $field, array(
  217. 'label' => '',
  218. 'type' => 'text',
  219. 'name' => 'max_height',
  220. 'prepend' => __('Height', 'acf'),
  221. 'append' => 'px',
  222. 'wrapper' => array(
  223. 'data-append' => 'max_width'
  224. )
  225. ));
  226. acf_render_field_setting( $field, array(
  227. 'label' => '',
  228. 'type' => 'text',
  229. 'name' => 'max_size',
  230. 'prepend' => __('File size', 'acf'),
  231. 'append' => 'MB',
  232. 'wrapper' => array(
  233. 'data-append' => 'max_width'
  234. )
  235. ));
  236. // allowed type
  237. acf_render_field_setting( $field, array(
  238. 'label' => __('Allowed file types','acf'),
  239. 'instructions' => __('Comma separated list. Leave blank for all types','acf'),
  240. 'type' => 'text',
  241. 'name' => 'mime_types',
  242. ));
  243. }
  244. /*
  245. * format_value()
  246. *
  247. * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
  248. *
  249. * @type filter
  250. * @since 3.6
  251. * @date 23/01/13
  252. *
  253. * @param $value (mixed) the value which was loaded from the database
  254. * @param $post_id (mixed) the $post_id from which the value was loaded
  255. * @param $field (array) the field array holding all the field options
  256. *
  257. * @return $value (mixed) the modified value
  258. */
  259. function format_value( $value, $post_id, $field ) {
  260. // bail early if no value
  261. if( empty($value) ) {
  262. return $value;
  263. }
  264. // convert to int
  265. $value = intval($value);
  266. // format
  267. if( $field['return_format'] == 'url' ) {
  268. return wp_get_attachment_url( $value );
  269. } elseif( $field['return_format'] == 'array' ) {
  270. return acf_get_attachment( $value );
  271. }
  272. return $value;
  273. }
  274. /*
  275. * get_media_item_args
  276. *
  277. * description
  278. *
  279. * @type function
  280. * @date 27/01/13
  281. * @since 3.6.0
  282. *
  283. * @param $vars (array)
  284. * @return $vars
  285. */
  286. function get_media_item_args( $vars ) {
  287. $vars['send'] = true;
  288. return($vars);
  289. }
  290. /*
  291. * image_size_names_choose
  292. *
  293. * @description:
  294. * @since: 3.5.7
  295. * @created: 13/01/13
  296. */
  297. /*
  298. function image_size_names_choose( $sizes )
  299. {
  300. global $_wp_additional_image_sizes;
  301. if( $_wp_additional_image_sizes )
  302. {
  303. foreach( $_wp_additional_image_sizes as $k => $v )
  304. {
  305. $title = $k;
  306. $title = str_replace('-', ' ', $title);
  307. $title = str_replace('_', ' ', $title);
  308. $title = ucwords( $title );
  309. $sizes[ $k ] = $title;
  310. }
  311. // foreach( $image_sizes as $image_size )
  312. }
  313. return $sizes;
  314. }
  315. */
  316. /*
  317. * wp_prepare_attachment_for_js
  318. *
  319. * this filter allows ACF to add in extra data to an attachment JS object
  320. * This sneaky hook adds the missing sizes to each attachment in the 3.5 uploader.
  321. * It would be a lot easier to add all the sizes to the 'image_size_names_choose' filter but
  322. * then it will show up on the normal the_content editor
  323. *
  324. * @type function
  325. * @since: 3.5.7
  326. * @date 13/01/13
  327. *
  328. * @param {int} $post_id
  329. * @return {int} $post_id
  330. */
  331. function wp_prepare_attachment_for_js( $response, $attachment, $meta ) {
  332. // only for image
  333. if( $response['type'] != 'image' ) {
  334. return $response;
  335. }
  336. // make sure sizes exist. Perhaps they dont?
  337. if( !isset($meta['sizes']) ) {
  338. return $response;
  339. }
  340. $attachment_url = $response['url'];
  341. $base_url = str_replace( wp_basename( $attachment_url ), '', $attachment_url );
  342. if( isset($meta['sizes']) && is_array($meta['sizes']) ) {
  343. foreach( $meta['sizes'] as $k => $v ) {
  344. if( !isset($response['sizes'][ $k ]) ) {
  345. $response['sizes'][ $k ] = array(
  346. 'height' => $v['height'],
  347. 'width' => $v['width'],
  348. 'url' => $base_url . $v['file'],
  349. 'orientation' => $v['height'] > $v['width'] ? 'portrait' : 'landscape',
  350. );
  351. }
  352. }
  353. }
  354. return $response;
  355. }
  356. /*
  357. * update_value()
  358. *
  359. * This filter is appied to the $value before it is updated in the db
  360. *
  361. * @type filter
  362. * @since 3.6
  363. * @date 23/01/13
  364. *
  365. * @param $value - the value which will be saved in the database
  366. * @param $post_id - the $post_id of which the value will be saved
  367. * @param $field - the field array holding all the field options
  368. *
  369. * @return $value - the modified value
  370. */
  371. function update_value( $value, $post_id, $field ) {
  372. // array?
  373. if( is_array($value) && isset($value['ID']) ) {
  374. return $value['ID'];
  375. }
  376. // object?
  377. if( is_object($value) && isset($value->ID) ) {
  378. return $value->ID;
  379. }
  380. // return
  381. return $value;
  382. }
  383. }
  384. new acf_field_image();
  385. endif;
  386. ?>