image-dimensions.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor image dimensions control.
  8. *
  9. * A base control for creating image dimension control. Displays image width
  10. * input, image height input and an apply button.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Control_Image_Dimensions extends Control_Base_Multiple {
  15. /**
  16. * Get image dimensions control type.
  17. *
  18. * Retrieve the control type, in this case `image_dimensions`.
  19. *
  20. * @since 1.0.0
  21. * @access public
  22. *
  23. * @return string Control type.
  24. */
  25. public function get_type() {
  26. return 'image_dimensions';
  27. }
  28. /**
  29. * Get image dimensions control default values.
  30. *
  31. * Retrieve the default value of the image dimensions control. Used to return the
  32. * default values while initializing the image dimensions control.
  33. *
  34. * @since 1.0.0
  35. * @access public
  36. *
  37. * @return array Control default value.
  38. */
  39. public function get_default_value() {
  40. return [
  41. 'width' => '',
  42. 'height' => '',
  43. ];
  44. }
  45. /**
  46. * Get image dimensions control default settings.
  47. *
  48. * Retrieve the default settings of the image dimensions control. Used to return
  49. * the default settings while initializing the image dimensions control.
  50. *
  51. * @since 1.0.0
  52. * @access protected
  53. *
  54. * @return array Control default settings.
  55. */
  56. protected function get_default_settings() {
  57. return [
  58. 'show_label' => false,
  59. 'label_block' => true,
  60. ];
  61. }
  62. /**
  63. * Render image dimensions control output in the editor.
  64. *
  65. * Used to generate the control HTML in the editor using Underscore JS
  66. * template. The variables for the class are available using `data` JS
  67. * object.
  68. *
  69. * @since 1.0.0
  70. * @access public
  71. */
  72. public function content_template() {
  73. if ( ! $this->is_image_editor_supports() ) : ?>
  74. <div class="elementor-panel-alert elementor-panel-alert-danger">
  75. <?php echo __( 'The server does not have ImageMagick or GD installed and/or enabled! Any of these libraries are required for WordPress to be able to resize images. Please contact your server administrator to enable this before continuing.', 'elementor' ); ?>
  76. </div>
  77. <?php
  78. return;
  79. endif;
  80. ?>
  81. <# if ( data.description ) { #>
  82. <div class="elementor-control-field-description">{{{ data.description }}}</div>
  83. <# } #>
  84. <div class="elementor-control-field">
  85. <label class="elementor-control-title">{{{ data.label }}}</label>
  86. <div class="elementor-control-input-wrapper">
  87. <div class="elementor-image-dimensions-field">
  88. <?php $control_uid = $this->get_control_uid( 'width' ); ?>
  89. <input id="<?php echo $control_uid; ?>" type="text" data-setting="width" />
  90. <label for="<?php echo $control_uid; ?>" class="elementor-image-dimensions-field-description"><?php echo __( 'Width', 'elementor' ); ?></label>
  91. </div>
  92. <div class="elementor-image-dimensions-separator">x</div>
  93. <div class="elementor-image-dimensions-field">
  94. <?php $control_uid = $this->get_control_uid( 'height' ); ?>
  95. <input id="<?php echo $control_uid; ?>" type="text" data-setting="height" />
  96. <label for="<?php echo $control_uid; ?>" class="elementor-image-dimensions-field-description"><?php echo __( 'Height', 'elementor' ); ?></label>
  97. </div>
  98. <button class="elementor-button elementor-button-success elementor-image-dimensions-apply-button"><?php echo __( 'Apply', 'elementor' ); ?></button>
  99. </div>
  100. </div>
  101. <?php
  102. }
  103. /**
  104. * Image editor support.
  105. *
  106. * Used to determine whether the editor supports a given image mime-type.
  107. *
  108. * @since 2.0.0
  109. * @access private
  110. *
  111. * @return bool Whether the editor supports the given mime-type.
  112. */
  113. private function is_image_editor_supports() {
  114. $arg = [
  115. 'mime_type' => 'image/jpeg',
  116. ];
  117. return ( wp_image_editor_supports( $arg ) );
  118. }
  119. }