vc-column.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * WPBakery WPBakery Page Builder shortcodes
  7. *
  8. * @package WPBakeryPageBuilder
  9. *
  10. */
  11. class WPBakeryShortCode_VC_Column extends WPBakeryShortCode {
  12. /**
  13. * @var string
  14. */
  15. public $nonDraggableClass = 'vc-non-draggable-column';
  16. /**
  17. * @param $settings
  18. */
  19. public function __construct( $settings ) {
  20. parent::__construct( $settings );
  21. $this->shortcodeScripts();
  22. }
  23. /**
  24. *
  25. */
  26. protected function shortcodeScripts() {
  27. wp_register_script( 'vc_jquery_skrollr_js', vc_asset_url( 'lib/bower/skrollr/dist/skrollr.min.js' ), array( 'jquery' ), WPB_VC_VERSION, true );
  28. wp_register_script( 'vc_youtube_iframe_api_js', 'https://www.youtube.com/iframe_api', array(), WPB_VC_VERSION, true );
  29. }
  30. /**
  31. * @param $controls
  32. * @param string $extended_css
  33. *
  34. * @return string
  35. * @throws \Exception
  36. */
  37. public function getColumnControls( $controls, $extended_css = '' ) {
  38. $output = '<div class="vc_controls vc_control-column vc_controls-visible' . ( ! empty( $extended_css ) ? " {$extended_css}" : '' ) . '">';
  39. $controls_end = '</div>';
  40. if ( ' bottom-controls' === $extended_css ) {
  41. $control_title = __( 'Append to this column', 'js_composer' );
  42. } else {
  43. $control_title = __( 'Prepend to this column', 'js_composer' );
  44. }
  45. if ( vc_user_access()->part( 'shortcodes' )->checkStateAny( true, 'custom', null )->get() ) {
  46. $controls_add = '<a class="vc_control column_add vc_column-add" data-vc-control="add" href="#" title="' . $control_title . '"><i class="vc-composer-icon vc-c-icon-add"></i></a>';
  47. } else {
  48. $controls_add = '';
  49. }
  50. $controls_edit = '<a class="vc_control column_edit vc_column-edit" data-vc-control="edit" href="#" title="' . __( 'Edit this column', 'js_composer' ) . '"><i class="vc-composer-icon vc-c-icon-mode_edit"></i></a>';
  51. $controls_delete = '<a class="vc_control column_delete vc_column-delete" data-vc-control="delete" href="#" title="' . __( 'Delete this column', 'js_composer' ) . '"><i class="vc-composer-icon vc-c-icon-delete_empty"></i></a>';
  52. $editAccess = vc_user_access_check_shortcode_edit( $this->shortcode );
  53. $allAccess = vc_user_access_check_shortcode_all( $this->shortcode );
  54. if ( is_array( $controls ) && ! empty( $controls ) ) {
  55. foreach ( $controls as $control ) {
  56. if ( 'add' === $control || ( $editAccess && 'edit' === $control ) || $allAccess ) {
  57. $method_name = vc_camel_case( 'output-editor-control-' . $control );
  58. if ( method_exists( $this, $method_name ) ) {
  59. $output .= $this->$method_name();
  60. } else {
  61. $control_var = 'controls_' . $control;
  62. if ( isset( ${$control_var} ) ) {
  63. $output .= ${$control_var};
  64. }
  65. }
  66. }
  67. }
  68. return $output . $controls_end;
  69. } elseif ( is_string( $controls ) && 'full' === $controls ) {
  70. if ( $allAccess ) {
  71. return $output . $controls_add . $controls_edit . $controls_delete . $controls_end;
  72. } elseif ( $editAccess ) {
  73. return $output . $controls_add . $controls_edit . $controls_end;
  74. }
  75. return $output . $controls_add . $controls_end;
  76. } elseif ( is_string( $controls ) ) {
  77. $control_var = 'controls_' . $controls;
  78. if ( 'add' === $controls || ( $editAccess && 'edit' === $controls || $allAccess ) && isset( ${$control_var} ) ) {
  79. return $output . ${$control_var} . $controls_end;
  80. }
  81. return $output . $controls_end;
  82. }
  83. if ( $allAccess ) {
  84. return $output . $controls_add . $controls_edit . $controls_delete . $controls_end;
  85. } elseif ( $editAccess ) {
  86. return $output . $controls_add . $controls_edit . $controls_end;
  87. }
  88. return $output . $controls_add . $controls_end;
  89. }
  90. /**
  91. * @param $param
  92. * @param $value
  93. *
  94. * @return string
  95. */
  96. public function singleParamHtmlHolder( $param, $value ) {
  97. $output = '';
  98. // Compatibility fixes.
  99. $old_names = array(
  100. 'yellow_message',
  101. 'blue_message',
  102. 'green_message',
  103. 'button_green',
  104. 'button_grey',
  105. 'button_yellow',
  106. 'button_blue',
  107. 'button_red',
  108. 'button_orange',
  109. );
  110. $new_names = array(
  111. 'alert-block',
  112. 'alert-info',
  113. 'alert-success',
  114. 'btn-success',
  115. 'btn',
  116. 'btn-info',
  117. 'btn-primary',
  118. 'btn-danger',
  119. 'btn-warning',
  120. );
  121. $value = str_ireplace( $old_names, $new_names, $value );
  122. $param_name = isset( $param['param_name'] ) ? $param['param_name'] : '';
  123. $type = isset( $param['type'] ) ? $param['type'] : '';
  124. $class = isset( $param['class'] ) ? $param['class'] : '';
  125. if ( isset( $param['holder'] ) && 'hidden' !== $param['holder'] ) {
  126. $output .= '<' . $param['holder'] . ' class="wpb_vc_param_value ' . $param_name . ' ' . $type . ' ' . $class . '" name="' . $param_name . '">' . $value . '</' . $param['holder'] . '>';
  127. }
  128. return $output;
  129. }
  130. /**
  131. * @param $atts
  132. * @param null $content
  133. *
  134. * @return string
  135. */
  136. public function contentAdmin( $atts, $content = null ) {
  137. $width = '';
  138. // @codingStandardsIgnoreLine
  139. extract( vc_map_get_attributes( $this->getShortcode(), $atts ) );
  140. $output = '';
  141. $column_controls = $this->getColumnControls( $this->settings( 'controls' ) );
  142. $column_controls_bottom = $this->getColumnControls( 'add', 'bottom-controls' );
  143. if ( 'column_14' === $width || '1/4' === $width ) {
  144. $width = array( 'vc_col-sm-3' );
  145. } elseif ( 'column_14-14-14-14' === $width ) {
  146. $width = array(
  147. 'vc_col-sm-3',
  148. 'vc_col-sm-3',
  149. 'vc_col-sm-3',
  150. 'vc_col-sm-3',
  151. );
  152. } elseif ( 'column_13' === $width || '1/3' === $width ) {
  153. $width = array( 'vc_col-sm-4' );
  154. } elseif ( 'column_13-23' === $width ) {
  155. $width = array(
  156. 'vc_col-sm-4',
  157. 'vc_col-sm-8',
  158. );
  159. } elseif ( 'column_13-13-13' === $width ) {
  160. $width = array(
  161. 'vc_col-sm-4',
  162. 'vc_col-sm-4',
  163. 'vc_col-sm-4',
  164. );
  165. } elseif ( 'column_12' === $width || '1/2' === $width ) {
  166. $width = array( 'vc_col-sm-6' );
  167. } elseif ( 'column_12-12' === $width ) {
  168. $width = array(
  169. 'vc_col-sm-6',
  170. 'vc_col-sm-6',
  171. );
  172. } elseif ( 'column_23' === $width || '2/3' === $width ) {
  173. $width = array( 'vc_col-sm-8' );
  174. } elseif ( 'column_34' === $width || '3/4' === $width ) {
  175. $width = array( 'vc_col-sm-9' );
  176. } elseif ( 'column_16' === $width || '1/6' === $width ) {
  177. $width = array( 'vc_col-sm-2' );
  178. } elseif ( ' column_56' === $width || ' 5/6' === $width ) {
  179. $width = array( 'vc_col-sm-10' );
  180. } else {
  181. $width = array( '' );
  182. }
  183. $count = count( $width );
  184. for ( $i = 0; $i < $count; $i ++ ) {
  185. $output .= '<div ' . $this->mainHtmlBlockParams( $width, $i ) . '>';
  186. $output .= str_replace( '%column_size%', wpb_translateColumnWidthToFractional( $width[ $i ] ), $column_controls );
  187. $output .= '<div class="wpb_element_wrapper">';
  188. $output .= '<div ' . $this->containerHtmlBlockParams( $width, $i ) . '>';
  189. $output .= do_shortcode( shortcode_unautop( $content ) );
  190. $output .= '</div>';
  191. if ( isset( $this->settings['params'] ) ) {
  192. $inner = '';
  193. foreach ( $this->settings['params'] as $param ) {
  194. $param_value = isset( ${$param['param_name']} ) ? ${$param['param_name']} : '';
  195. if ( is_array( $param_value ) ) {
  196. // Get first element from the array
  197. reset( $param_value );
  198. $first_key = key( $param_value );
  199. $param_value = $param_value[ $first_key ];
  200. }
  201. $inner .= $this->singleParamHtmlHolder( $param, $param_value );
  202. }
  203. $output .= $inner;
  204. }
  205. $output .= '</div>';
  206. $output .= str_replace( '%column_size%', wpb_translateColumnWidthToFractional( $width[ $i ] ), $column_controls_bottom );
  207. $output .= '</div>';
  208. }
  209. return $output;
  210. }
  211. /**
  212. * @return string
  213. */
  214. public function customAdminBlockParams() {
  215. return '';
  216. }
  217. /**
  218. * @param $width
  219. * @param $i
  220. *
  221. * @return string
  222. * @throws \Exception
  223. */
  224. public function mainHtmlBlockParams( $width, $i ) {
  225. $sortable = ( vc_user_access_check_shortcode_all( $this->shortcode ) ? 'wpb_sortable' : $this->nonDraggableClass );
  226. return 'data-element_type="' . $this->settings['base'] . '" data-vc-column-width="' . wpb_vc_get_column_width_indent( $width[ $i ] ) . '" class="wpb_' . $this->settings['base'] . ' ' . $sortable . '' . ( ! empty( $this->settings['class'] ) ? ' ' . $this->settings['class'] : '' ) . ' ' . $this->templateWidth() . ' wpb_content_holder"' . $this->customAdminBlockParams();
  227. }
  228. /**
  229. * @param $width
  230. * @param $i
  231. *
  232. * @return string
  233. */
  234. public function containerHtmlBlockParams( $width, $i ) {
  235. return 'class="wpb_column_container vc_container_for_children"';
  236. }
  237. /**
  238. * @param string $content
  239. *
  240. * @return string
  241. */
  242. public function template( $content = '' ) {
  243. return $this->contentAdmin( $this->atts );
  244. }
  245. /**
  246. * @return string
  247. */
  248. protected function templateWidth() {
  249. return '<%= window.vc_convert_column_size(params.width) %>';
  250. }
  251. /**
  252. * @param string $font_color
  253. *
  254. * @return string
  255. */
  256. public function buildStyle( $font_color = '' ) {
  257. $style = '';
  258. if ( ! empty( $font_color ) ) {
  259. $style .= vc_get_css_color( 'color', $font_color );
  260. }
  261. return empty( $style ) ? $style : ' style="' . esc_attr( $style ) . '"';
  262. }
  263. }