vc-single-image.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Class WPBakeryShortCode_Vc_Single_image
  7. */
  8. class WPBakeryShortCode_Vc_Single_Image extends WPBakeryShortCode {
  9. /**
  10. * WPBakeryShortCode_Vc_Single_image constructor.
  11. * @param $settings
  12. */
  13. public function __construct( $settings ) {
  14. parent::__construct( $settings );
  15. $this->jsScripts();
  16. }
  17. public function jsScripts() {
  18. wp_register_script( 'zoom', vc_asset_url( 'lib/bower/zoom/jquery.zoom.min.js' ), array( 'jquery' ), WPB_VC_VERSION, true );
  19. wp_register_script( 'vc_image_zoom', vc_asset_url( 'lib/vc_image_zoom/vc_image_zoom.min.js' ), array(
  20. 'jquery',
  21. 'zoom',
  22. ), WPB_VC_VERSION, true );
  23. }
  24. /**
  25. * @param $param
  26. * @param $value
  27. * @return string
  28. */
  29. public function singleParamHtmlHolder( $param, $value ) {
  30. $output = '';
  31. // Compatibility fixes
  32. $old_names = array(
  33. 'yellow_message',
  34. 'blue_message',
  35. 'green_message',
  36. 'button_green',
  37. 'button_grey',
  38. 'button_yellow',
  39. 'button_blue',
  40. 'button_red',
  41. 'button_orange',
  42. );
  43. $new_names = array(
  44. 'alert-block',
  45. 'alert-info',
  46. 'alert-success',
  47. 'btn-success',
  48. 'btn',
  49. 'btn-info',
  50. 'btn-primary',
  51. 'btn-danger',
  52. 'btn-warning',
  53. );
  54. $value = str_ireplace( $old_names, $new_names, $value );
  55. $param_name = isset( $param['param_name'] ) ? $param['param_name'] : '';
  56. $type = isset( $param['type'] ) ? $param['type'] : '';
  57. $class = isset( $param['class'] ) ? $param['class'] : '';
  58. if ( 'attach_image' === $param['type'] && 'image' === $param_name ) {
  59. $output .= '<input type="hidden" class="wpb_vc_param_value ' . $param_name . ' ' . $type . ' ' . $class . '" name="' . $param_name . '" value="' . $value . '" />';
  60. $element_icon = $this->settings( 'icon' );
  61. $img = wpb_getImageBySize( array(
  62. 'attach_id' => (int) preg_replace( '/[^\d]/', '', $value ),
  63. 'thumb_size' => 'thumbnail',
  64. ) );
  65. $this->setSettings( 'logo', ( $img ? $img['thumbnail'] : '<img width="150" height="150" src="' . esc_url( vc_asset_url( 'vc/blank.gif' ) ) . '" class="attachment-thumbnail vc_general vc_element-icon" data-name="' . $param_name . '" alt="" title="" style="display: none;" />' ) . '<span class="no_image_image vc_element-icon' . ( ! empty( $element_icon ) ? ' ' . $element_icon : '' ) . ( $img && ! empty( $img['p_img_large'][0] ) ? ' image-exists' : '' ) . '"></span><a href="#" class="column_edit_trigger' . ( $img && ! empty( $img['p_img_large'][0] ) ? ' image-exists' : '' ) . '">' . esc_html__( 'Add image', 'js_composer' ) . '</a>' );
  66. $output .= $this->outputTitleTrue( $this->settings['name'] );
  67. } elseif ( ! empty( $param['holder'] ) ) {
  68. if ( 'input' === $param['holder'] ) {
  69. $output .= '<' . $param['holder'] . ' readonly="true" class="wpb_vc_param_value ' . $param_name . ' ' . $type . ' ' . $class . '" name="' . $param_name . '" value="' . $value . '">';
  70. } elseif ( in_array( $param['holder'], array(
  71. 'img',
  72. 'iframe',
  73. ), true ) ) {
  74. $output .= '<' . $param['holder'] . ' class="wpb_vc_param_value ' . $param_name . ' ' . $type . ' ' . $class . '" name="' . $param_name . '" src="' . esc_url( $value ) . '">';
  75. } elseif ( 'hidden' !== $param['holder'] ) {
  76. $output .= '<' . $param['holder'] . ' class="wpb_vc_param_value ' . $param_name . ' ' . $type . ' ' . $class . '" name="' . $param_name . '">' . $value . '</' . $param['holder'] . '>';
  77. }
  78. }
  79. if ( ! empty( $param['admin_label'] ) && true === $param['admin_label'] ) {
  80. $output .= '<span class="vc_admin_label admin_label_' . $param['param_name'] . ( empty( $value ) ? ' hidden-label' : '' ) . '"><label>' . $param['heading'] . '</label>: ' . $value . '</span>';
  81. }
  82. return $output;
  83. }
  84. /**
  85. * @param $img_id
  86. * @param $img_size
  87. * @return string
  88. */
  89. public function getImageSquareSize( $img_id, $img_size ) {
  90. if ( preg_match_all( '/(\d+)x(\d+)/', $img_size, $sizes ) ) {
  91. $exact_size = array(
  92. 'width' => isset( $sizes[1][0] ) ? $sizes[1][0] : '0',
  93. 'height' => isset( $sizes[2][0] ) ? $sizes[2][0] : '0',
  94. );
  95. } else {
  96. $image_downsize = image_downsize( $img_id, $img_size );
  97. $exact_size = array(
  98. 'width' => $image_downsize[1],
  99. 'height' => $image_downsize[2],
  100. );
  101. }
  102. $exact_size_int_w = (int) $exact_size['width'];
  103. $exact_size_int_h = (int) $exact_size['height'];
  104. if ( isset( $exact_size['width'] ) && $exact_size_int_w !== $exact_size_int_h ) {
  105. $img_size = $exact_size_int_w > $exact_size_int_h ? $exact_size['height'] . 'x' . $exact_size['height'] : $exact_size['width'] . 'x' . $exact_size['width'];
  106. }
  107. return $img_size;
  108. }
  109. /**
  110. * @param $title
  111. * @return string
  112. */
  113. protected function outputTitle( $title ) {
  114. return '';
  115. }
  116. /**
  117. * @param $title
  118. * @return string
  119. */
  120. protected function outputTitleTrue( $title ) {
  121. return '<h4 class="wpb_element_title">' . $title . ' ' . $this->settings( 'logo' ) . '</h4>';
  122. }
  123. }