vc-media-grid.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. require_once vc_path_dir( 'SHORTCODES_DIR', 'vc-basic-grid.php' );
  6. /**
  7. * Class WPBakeryShortCode_Vc_Media_Grid
  8. */
  9. class WPBakeryShortCode_Vc_Media_Grid extends WPBakeryShortCode_Vc_Basic_Grid {
  10. /**
  11. * WPBakeryShortCode_Vc_Media_Grid constructor.
  12. * @param $settings
  13. */
  14. public function __construct( $settings ) {
  15. parent::__construct( $settings );
  16. add_filter( $this->shortcode . '_items_list', array(
  17. $this,
  18. 'setItemsIfEmpty',
  19. ) );
  20. }
  21. /**
  22. * @return mixed|string
  23. */
  24. protected function getFileName() {
  25. return 'vc_basic_grid';
  26. }
  27. /**
  28. * @param $max_items
  29. */
  30. protected function setPagingAll( $max_items ) {
  31. $this->atts['items_per_page'] = $this->atts['query_items_per_page'] = apply_filters( 'vc_basic_grid_items_per_page_all_max_items', self::$default_max_items );
  32. }
  33. /**
  34. * @param $atts
  35. * @return array
  36. */
  37. public function buildQuery( $atts ) {
  38. if ( empty( $atts['include'] ) ) {
  39. $atts['include'] = - 1;
  40. }
  41. $settings = array(
  42. 'include' => $atts['include'],
  43. 'posts_per_page' => apply_filters( 'vc_basic_grid_max_items', self::$default_max_items ),
  44. 'offset' => 0,
  45. 'post_type' => 'attachment',
  46. 'orderby' => 'post__in',
  47. );
  48. return $settings;
  49. }
  50. /**
  51. * @param $items
  52. * @return string
  53. */
  54. public function setItemsIfEmpty( $items ) {
  55. if ( empty( $items ) ) {
  56. require_once vc_path_dir( 'PARAMS_DIR', 'vc_grid_item/class-vc-grid-item.php' );
  57. $grid_item = new Vc_Grid_Item();
  58. $grid_item->setGridAttributes( $this->atts );
  59. $grid_item->shortcodes();
  60. $item = '[vc_gitem]<img src="' . esc_url( vc_asset_url( 'vc/vc_gitem_image.png' ) ) . '">[/vc_gitem]';
  61. $grid_item->parseTemplate( $item );
  62. $items = str_repeat( $grid_item->renderItem( get_post( (int) vc_request_param( 'vc_post_id' ) ) ), 3 );
  63. }
  64. return $items;
  65. }
  66. /**
  67. * @param $param
  68. * @param $value
  69. * @return string
  70. */
  71. public function singleParamHtmlHolder( $param, $value ) {
  72. $output = '';
  73. // Compatibility fixes
  74. // TODO: check $old_names & &new_names. Leftover from copypasting?
  75. $old_names = array(
  76. 'yellow_message',
  77. 'blue_message',
  78. 'green_message',
  79. 'button_green',
  80. 'button_grey',
  81. 'button_yellow',
  82. 'button_blue',
  83. 'button_red',
  84. 'button_orange',
  85. );
  86. $new_names = array(
  87. 'alert-block',
  88. 'alert-info',
  89. 'alert-success',
  90. 'btn-success',
  91. 'btn',
  92. 'btn-info',
  93. 'btn-primary',
  94. 'btn-danger',
  95. 'btn-warning',
  96. );
  97. $value = str_ireplace( $old_names, $new_names, $value );
  98. $param_name = isset( $param['param_name'] ) ? $param['param_name'] : '';
  99. $type = isset( $param['type'] ) ? $param['type'] : '';
  100. $class = isset( $param['class'] ) ? $param['class'] : '';
  101. if ( isset( $param['holder'] ) && 'hidden' !== $param['holder'] ) {
  102. $output .= '<' . $param['holder'] . ' class="wpb_vc_param_value ' . $param_name . ' ' . $type . ' ' . $class . '" name="' . $param_name . '">' . $value . '</' . $param['holder'] . '>';
  103. }
  104. if ( 'include' === $param_name ) {
  105. $images_ids = empty( $value ) ? array() : explode( ',', trim( $value ) );
  106. $output .= '<ul class="attachment-thumbnails' . ( empty( $images_ids ) ? ' image-exists' : '' ) . '" data-name="' . $param_name . '">';
  107. foreach ( $images_ids as $image ) {
  108. $img = wpb_getImageBySize( array(
  109. 'attach_id' => (int) $image,
  110. 'thumb_size' => 'thumbnail',
  111. ) );
  112. $output .= ( $img ? '<li>' . $img['thumbnail'] . '</li>' : '<li><img width="150" height="150" src="' . esc_url( vc_asset_url( 'vc/blank.gif' ) ) . '" class="attachment-thumbnail" alt="" title="" /></li>' );
  113. }
  114. $output .= '</ul>';
  115. $output .= '<a href="#" class="column_edit_trigger' . ( ! empty( $images_ids ) ? ' image-exists' : '' ) . '">' . esc_html__( 'Add images', 'js_composer' ) . '</a>';
  116. }
  117. return $output;
  118. }
  119. }