PageRenderTime 25ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/elementor/includes/controls/groups/image-size.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 400 lines | 166 code | 61 blank | 173 comment | 29 complexity | cd5f5bd847cadf62964efbf3f5bcd46a MD5 | raw file
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor image size control.
  8. *
  9. * A base control for creating image size control. Displays input fields to define
  10. * one of the default image sizes (thumbnail, medium, medium_large, large) or custom
  11. * image dimensions.
  12. *
  13. * @since 1.0.0
  14. */
  15. class Group_Control_Image_Size extends Group_Control_Base {
  16. /**
  17. * Fields.
  18. *
  19. * Holds all the image size control fields.
  20. *
  21. * @since 1.2.2
  22. * @access protected
  23. * @static
  24. *
  25. * @var array Image size control fields.
  26. */
  27. protected static $fields;
  28. /**
  29. * Get image size control type.
  30. *
  31. * Retrieve the control type, in this case `image-size`.
  32. *
  33. * @since 1.0.0
  34. * @access public
  35. * @static
  36. *
  37. * @return string Control type.
  38. */
  39. public static function get_type() {
  40. return 'image-size';
  41. }
  42. /**
  43. * Get attachment image HTML.
  44. *
  45. * Retrieve the attachment image HTML code.
  46. *
  47. * Note that some widgets use the same key for the media control that allows
  48. * the image selection and for the image size control that allows the user
  49. * to select the image size, in this case the third parameter should be null
  50. * or the same as the second parameter. But when the widget uses different
  51. * keys for the media control and the image size control, when calling this
  52. * method you should pass the keys.
  53. *
  54. * @since 1.0.0
  55. * @access public
  56. * @static
  57. *
  58. * @param array $settings Control settings.
  59. * @param string $image_size_key Optional. Settings key for image size.
  60. * Default is `image`.
  61. * @param string $image_key Optional. Settings key for image. Default
  62. * is null. If not defined uses image size key
  63. * as the image key.
  64. *
  65. * @return string Image HTML.
  66. */
  67. public static function get_attachment_image_html( $settings, $image_size_key = 'image', $image_key = null ) {
  68. if ( ! $image_key ) {
  69. $image_key = $image_size_key;
  70. }
  71. $image = $settings[ $image_key ];
  72. // Old version of image settings.
  73. if ( ! isset( $settings[ $image_size_key . '_size' ] ) ) {
  74. $settings[ $image_size_key . '_size' ] = '';
  75. }
  76. $size = $settings[ $image_size_key . '_size' ];
  77. $image_class = ! empty( $settings['hover_animation'] ) ? 'elementor-animation-' . $settings['hover_animation'] : '';
  78. $html = '';
  79. // If is the new version - with image size.
  80. $image_sizes = get_intermediate_image_sizes();
  81. $image_sizes[] = 'full';
  82. if ( ! empty( $image['id'] ) && ! wp_attachment_is_image( $image['id'] ) ) {
  83. $image['id'] = '';
  84. }
  85. $is_static_render_mode = Plugin::$instance->frontend->is_static_render_mode();
  86. // On static mode don't use WP responsive images.
  87. if ( ! empty( $image['id'] ) && in_array( $size, $image_sizes ) && ! $is_static_render_mode ) {
  88. $image_class .= " attachment-$size size-$size";
  89. $image_attr = [
  90. 'class' => trim( $image_class ),
  91. ];
  92. $html .= wp_get_attachment_image( $image['id'], $size, false, $image_attr );
  93. } else {
  94. $image_src = self::get_attachment_image_src( $image['id'], $image_size_key, $settings );
  95. if ( ! $image_src && isset( $image['url'] ) ) {
  96. $image_src = $image['url'];
  97. }
  98. if ( ! empty( $image_src ) ) {
  99. $image_class_html = ! empty( $image_class ) ? ' class="' . $image_class . '"' : '';
  100. $html .= sprintf( '<img src="%s" title="%s" alt="%s"%s />', esc_attr( $image_src ), Control_Media::get_image_title( $image ), Control_Media::get_image_alt( $image ), $image_class_html );
  101. }
  102. }
  103. /**
  104. * Get Attachment Image HTML
  105. *
  106. * Filters the Attachment Image HTML
  107. *
  108. * @since 2.4.0
  109. * @param string $html the attachment image HTML string
  110. * @param array $settings Control settings.
  111. * @param string $image_size_key Optional. Settings key for image size.
  112. * Default is `image`.
  113. * @param string $image_key Optional. Settings key for image. Default
  114. * is null. If not defined uses image size key
  115. * as the image key.
  116. */
  117. return apply_filters( 'elementor/image_size/get_attachment_image_html', $html, $settings, $image_size_key, $image_key );
  118. }
  119. /**
  120. * Safe print attachment image HTML.
  121. *
  122. * @uses get_attachment_image_html.
  123. *
  124. * @access public
  125. * @static
  126. *
  127. * @param array $settings Control settings.
  128. * @param string $image_size_key Optional. Settings key for image size.
  129. * Default is `image`.
  130. * @param string $image_key Optional. Settings key for image. Default
  131. * is null. If not defined uses image size key
  132. * as the image key.
  133. */
  134. public static function print_attachment_image_html( array $settings, $image_size_key = 'image', $image_key = null ) {
  135. Utils::print_wp_kses_extended( self::get_attachment_image_html( $settings, $image_size_key, $image_key ), [ 'image' ] );
  136. }
  137. /**
  138. * Get all image sizes.
  139. *
  140. * Retrieve available image sizes with data like `width`, `height` and `crop`.
  141. *
  142. * @since 1.0.0
  143. * @access public
  144. * @static
  145. *
  146. * @return array An array of available image sizes.
  147. */
  148. public static function get_all_image_sizes() {
  149. global $_wp_additional_image_sizes;
  150. $default_image_sizes = [ 'thumbnail', 'medium', 'medium_large', 'large' ];
  151. $image_sizes = [];
  152. foreach ( $default_image_sizes as $size ) {
  153. $image_sizes[ $size ] = [
  154. 'width' => (int) get_option( $size . '_size_w' ),
  155. 'height' => (int) get_option( $size . '_size_h' ),
  156. 'crop' => (bool) get_option( $size . '_crop' ),
  157. ];
  158. }
  159. if ( $_wp_additional_image_sizes ) {
  160. $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
  161. }
  162. /** This filter is documented in wp-admin/includes/media.php */
  163. return apply_filters( 'image_size_names_choose', $image_sizes );
  164. }
  165. /**
  166. * Get attachment image src.
  167. *
  168. * Retrieve the attachment image source URL.
  169. *
  170. * @since 1.0.0
  171. * @access public
  172. * @static
  173. *
  174. * @param string $attachment_id The attachment ID.
  175. * @param string $image_size_key Settings key for image size.
  176. * @param array $settings Control settings.
  177. *
  178. * @return string Attachment image source URL.
  179. */
  180. public static function get_attachment_image_src( $attachment_id, $image_size_key, array $settings ) {
  181. if ( empty( $attachment_id ) ) {
  182. return false;
  183. }
  184. $size = $settings[ $image_size_key . '_size' ];
  185. if ( 'custom' !== $size ) {
  186. $attachment_size = $size;
  187. } else {
  188. // Use BFI_Thumb script
  189. // TODO: Please rewrite this code.
  190. require_once ELEMENTOR_PATH . 'includes/libraries/bfi-thumb/bfi-thumb.php';
  191. $custom_dimension = $settings[ $image_size_key . '_custom_dimension' ];
  192. $attachment_size = [
  193. // Defaults sizes
  194. 0 => null, // Width.
  195. 1 => null, // Height.
  196. 'bfi_thumb' => true,
  197. 'crop' => true,
  198. ];
  199. $has_custom_size = false;
  200. if ( ! empty( $custom_dimension['width'] ) ) {
  201. $has_custom_size = true;
  202. $attachment_size[0] = $custom_dimension['width'];
  203. }
  204. if ( ! empty( $custom_dimension['height'] ) ) {
  205. $has_custom_size = true;
  206. $attachment_size[1] = $custom_dimension['height'];
  207. }
  208. if ( ! $has_custom_size ) {
  209. $attachment_size = 'full';
  210. }
  211. }
  212. $image_src = wp_get_attachment_image_src( $attachment_id, $attachment_size );
  213. if ( empty( $image_src[0] ) && 'thumbnail' !== $attachment_size ) {
  214. $image_src = wp_get_attachment_image_src( $attachment_id );
  215. }
  216. return ! empty( $image_src[0] ) ? $image_src[0] : '';
  217. }
  218. /**
  219. * Get child default arguments.
  220. *
  221. * Retrieve the default arguments for all the child controls for a specific group
  222. * control.
  223. *
  224. * @since 1.2.2
  225. * @access protected
  226. *
  227. * @return array Default arguments for all the child controls.
  228. */
  229. protected function get_child_default_args() {
  230. return [
  231. 'include' => [],
  232. 'exclude' => [],
  233. ];
  234. }
  235. /**
  236. * Init fields.
  237. *
  238. * Initialize image size control fields.
  239. *
  240. * @since 1.2.2
  241. * @access protected
  242. *
  243. * @return array Control fields.
  244. */
  245. protected function init_fields() {
  246. $fields = [];
  247. $fields['size'] = [
  248. 'label' => _x( 'Image Size', 'Image Size Control', 'elementor' ),
  249. 'type' => Controls_Manager::SELECT,
  250. ];
  251. $fields['custom_dimension'] = [
  252. 'label' => _x( 'Image Dimension', 'Image Size Control', 'elementor' ),
  253. 'type' => Controls_Manager::IMAGE_DIMENSIONS,
  254. 'description' => esc_html__( 'You can crop the original image size to any custom size. You can also set a single value for height or width in order to keep the original size ratio.', 'elementor' ),
  255. 'condition' => [
  256. 'size' => 'custom',
  257. ],
  258. 'separator' => 'none',
  259. ];
  260. return $fields;
  261. }
  262. /**
  263. * Prepare fields.
  264. *
  265. * Process image size control fields before adding them to `add_control()`.
  266. *
  267. * @since 1.2.2
  268. * @access protected
  269. *
  270. * @param array $fields Image size control fields.
  271. *
  272. * @return array Processed fields.
  273. */
  274. protected function prepare_fields( $fields ) {
  275. $image_sizes = $this->get_image_sizes();
  276. $args = $this->get_args();
  277. if ( ! empty( $args['default'] ) && isset( $image_sizes[ $args['default'] ] ) ) {
  278. $default_value = $args['default'];
  279. } else {
  280. // Get the first item for default value.
  281. $default_value = array_keys( $image_sizes );
  282. $default_value = array_shift( $default_value );
  283. }
  284. $fields['size']['options'] = $image_sizes;
  285. $fields['size']['default'] = $default_value;
  286. if ( ! isset( $image_sizes['custom'] ) ) {
  287. unset( $fields['custom_dimension'] );
  288. }
  289. return parent::prepare_fields( $fields );
  290. }
  291. /**
  292. * Get image sizes.
  293. *
  294. * Retrieve available image sizes after filtering `include` and `exclude` arguments.
  295. *
  296. * @since 2.0.0
  297. * @access private
  298. *
  299. * @return array Filtered image sizes.
  300. */
  301. private function get_image_sizes() {
  302. $wp_image_sizes = self::get_all_image_sizes();
  303. $args = $this->get_args();
  304. if ( $args['include'] ) {
  305. $wp_image_sizes = array_intersect_key( $wp_image_sizes, array_flip( $args['include'] ) );
  306. } elseif ( $args['exclude'] ) {
  307. $wp_image_sizes = array_diff_key( $wp_image_sizes, array_flip( $args['exclude'] ) );
  308. }
  309. $image_sizes = [];
  310. foreach ( $wp_image_sizes as $size_key => $size_attributes ) {
  311. $control_title = ucwords( str_replace( '_', ' ', $size_key ) );
  312. if ( is_array( $size_attributes ) ) {
  313. $control_title .= sprintf( ' - %d x %d', $size_attributes['width'], $size_attributes['height'] );
  314. }
  315. $image_sizes[ $size_key ] = $control_title;
  316. }
  317. $image_sizes['full'] = _x( 'Full', 'Image Size Control', 'elementor' );
  318. if ( ! empty( $args['include']['custom'] ) || ! in_array( 'custom', $args['exclude'] ) ) {
  319. $image_sizes['custom'] = _x( 'Custom', 'Image Size Control', 'elementor' );
  320. }
  321. return $image_sizes;
  322. }
  323. /**
  324. * Get default options.
  325. *
  326. * Retrieve the default options of the image size control. Used to return the
  327. * default options while initializing the image size control.
  328. *
  329. * @since 1.9.0
  330. * @access protected
  331. *
  332. * @return array Default image size control options.
  333. */
  334. protected function get_default_options() {
  335. return [
  336. 'popover' => false,
  337. ];
  338. }
  339. }