vc-tabs.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Class WPBakeryShortCode_Vc_Tabs
  7. */
  8. class WPBakeryShortCode_Vc_Tabs extends WPBakeryShortCode {
  9. public static $filter_added = false;
  10. protected $controls_css_settings = 'out-tc vc_controls-content-widget';
  11. protected $controls_list = array(
  12. 'edit',
  13. 'clone',
  14. 'delete',
  15. );
  16. /**
  17. * WPBakeryShortCode_Vc_Tabs constructor.
  18. * @param $settings
  19. */
  20. public function __construct( $settings ) {
  21. parent::__construct( $settings );
  22. if ( ! self::$filter_added ) {
  23. add_filter( 'vc_inline_template_content', array(
  24. $this,
  25. 'setCustomTabId',
  26. ) );
  27. self::$filter_added = true;
  28. }
  29. }
  30. /**
  31. * @param $atts
  32. * @param null $content
  33. * @return mixed|string
  34. * @throws \Exception
  35. */
  36. public function contentAdmin( $atts, $content = null ) {
  37. $width = $custom_markup = '';
  38. $shortcode_attributes = array( 'width' => '1/1' );
  39. foreach ( $this->settings['params'] as $param ) {
  40. if ( 'content' !== $param['param_name'] ) {
  41. $shortcode_attributes[ $param['param_name'] ] = isset( $param['value'] ) ? $param['value'] : null;
  42. } elseif ( 'content' === $param['param_name'] && null === $content ) {
  43. $content = $param['value'];
  44. }
  45. }
  46. extract( shortcode_atts( $shortcode_attributes, $atts ) );
  47. // Extract tab titles
  48. preg_match_all( '/vc_tab title="([^\"]+)"(\stab_id\=\"([^\"]+)\"){0,1}/i', $content, $matches, PREG_OFFSET_CAPTURE );
  49. $tab_titles = array();
  50. if ( isset( $matches[0] ) ) {
  51. $tab_titles = $matches[0];
  52. }
  53. $tmp = '';
  54. if ( count( $tab_titles ) ) {
  55. $tmp .= '<ul class="clearfix tabs_controls">';
  56. foreach ( $tab_titles as $tab ) {
  57. preg_match( '/title="([^\"]+)"(\stab_id\=\"([^\"]+)\"){0,1}/i', $tab[0], $tab_matches, PREG_OFFSET_CAPTURE );
  58. if ( isset( $tab_matches[1][0] ) ) {
  59. $tmp .= '<li><a href="#tab-' . ( isset( $tab_matches[3][0] ) ? $tab_matches[3][0] : sanitize_title( $tab_matches[1][0] ) ) . '">' . $tab_matches[1][0] . '</a></li>';
  60. }
  61. }
  62. $tmp .= '</ul>' . "\n";
  63. }
  64. $elem = $this->getElementHolder( $width );
  65. $iner = '';
  66. foreach ( $this->settings['params'] as $param ) {
  67. $param_value = isset( ${$param['param_name']} ) ? ${$param['param_name']} : '';
  68. if ( is_array( $param_value ) ) {
  69. // Get first element from the array
  70. reset( $param_value );
  71. $first_key = key( $param_value );
  72. $param_value = $param_value[ $first_key ];
  73. }
  74. $iner .= $this->singleParamHtmlHolder( $param, $param_value );
  75. }
  76. if ( isset( $this->settings['custom_markup'] ) && '' !== $this->settings['custom_markup'] ) {
  77. if ( '' !== $content ) {
  78. $custom_markup = str_ireplace( '%content%', $tmp . $content, $this->settings['custom_markup'] );
  79. } elseif ( '' === $content && isset( $this->settings['default_content_in_template'] ) && '' !== $this->settings['default_content_in_template'] ) {
  80. $custom_markup = str_ireplace( '%content%', $this->settings['default_content_in_template'], $this->settings['custom_markup'] );
  81. } else {
  82. $custom_markup = str_ireplace( '%content%', '', $this->settings['custom_markup'] );
  83. }
  84. $iner .= do_shortcode( $custom_markup );
  85. }
  86. $elem = str_ireplace( '%wpb_element_content%', $iner, $elem );
  87. $output = $elem;
  88. return $output;
  89. }
  90. /**
  91. * @return string
  92. */
  93. public function getTabTemplate() {
  94. return '<div class="wpb_template">' . do_shortcode( '[vc_tab title="Tab" tab_id=""][/vc_tab]' ) . '</div>';
  95. }
  96. /**
  97. * @param $content
  98. * @return string|string[]|null
  99. */
  100. public function setCustomTabId( $content ) {
  101. return preg_replace( '/tab\_id\=\"([^\"]+)\"/', 'tab_id="$1-' . time() . '"', $content );
  102. }
  103. }