Advertisement
TangibleDesign

Untitled

Sep 6th, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.46 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. die( 'Access denied.' );
  5. }
  6.  
  7. if ( ! class_exists( 'My_Home_Carousel_Post_Shortcode' ) ) :
  8.  
  9. class My_Home_Carousel_Post_Shortcode {
  10.  
  11. /**
  12. * settings
  13. *
  14. * Prepare settings for Visual Composer element
  15. */
  16. public static function settings() {
  17. $categories_vc = array(
  18. esc_html__( 'Any', 'myhome-core' ) => 0
  19. );
  20. $categories = get_categories(
  21. array(
  22. 'orderby' => 'name',
  23. 'order' => 'ASC'
  24. )
  25. );
  26.  
  27. foreach ( $categories as $category ) {
  28. $categories_vc[$category->name] = $category->term_id;
  29. }
  30.  
  31. return array(
  32. 'name' => esc_html__( 'Post Carousel', 'myhome-core' ),
  33. 'base' => 'mh_carousel_post',
  34. 'icon' => plugins_url( 'myhome-core/public/img/vc-icon.png' ),
  35. 'category' => esc_html__( 'MyHome', 'myhome-core' ),
  36. 'params' => array(
  37. // Category
  38. array(
  39. 'group' => esc_html__( 'General', 'myhome-core' ),
  40. 'type' => 'dropdown',
  41. 'heading' => esc_html__( 'Category', 'myhome-core' ),
  42. 'param_name' => 'posts_category',
  43. 'value' => $categories_vc
  44. ),
  45. // Visible
  46. array(
  47. 'group' => esc_html__( 'General', 'myhome-core' ),
  48. 'type' => 'dropdown',
  49. 'heading' => esc_html__( 'Max Visible', 'myhome-core' ),
  50. 'param_name' => 'owl_visible',
  51. 'value' => array(
  52. esc_html__( 'Default - 3', 'myhome-core' ) => 'owl-carousel--visible-3',
  53. esc_html__( '1 ', 'myhome-core' ) => 'owl-carousel--visible-1',
  54. esc_html__( '2 ', 'myhome-core' ) => 'owl-carousel--visible-2',
  55. esc_html__( '3 ', 'myhome-core' ) => 'owl-carousel--visible-3',
  56. esc_html__( '4 ', 'myhome-core' ) => 'owl-carousel--visible-4',
  57. esc_html__( '5 ', 'myhome-core' ) => 'owl-carousel--visible-5',
  58. ),
  59. ),
  60. // Posts limit
  61. array(
  62. 'group' => esc_html__( 'General', 'myhome-core' ),
  63. 'type' => 'textfield',
  64. 'heading' => esc_html__( 'Posts limit', 'myhome-core' ),
  65. 'param_name' => 'posts_limit',
  66. 'value' => 5
  67. ),
  68. // Read more
  69. array(
  70. 'group' => esc_html__( 'General', 'myhome-core' ),
  71. 'type' => 'textfield',
  72. 'heading' => esc_html__( 'Read more text', 'myhome-core' ),
  73. 'param_name' => 'read_more_text',
  74. 'value' => esc_html__( 'Read more', 'myhome-core' )
  75. ),
  76. // Style
  77. array(
  78. 'group' => esc_html__( 'General', 'myhome-core' ),
  79. 'type' => 'dropdown',
  80. 'heading' => esc_html__( 'Style', 'myhome-core' ),
  81. 'param_name' => 'posts_style',
  82. 'value' => array(
  83. esc_html__( 'Default', 'myhome-core' ) => '',
  84. esc_html__( 'White Background', 'myhome-core' ) => 'mh-post-grid--white',
  85. esc_html__( 'Dark Background', 'myhome-core' ) => 'mh-post-grid--dark',
  86. ),
  87. ),
  88. // Dots
  89. array(
  90. 'group' => esc_html__( 'General', 'myhome-core' ),
  91. 'type' => 'dropdown',
  92. 'heading' => esc_html__( 'Dots', 'myhome-core' ),
  93. 'param_name' => 'owl_dots',
  94. 'value' => array(
  95. esc_html__( 'Yes', 'myhome-core' ) => '',
  96. esc_html__( 'No', 'myhome-core' ) => 'owl-carousel--no-dots',
  97. ),
  98. ),
  99. // Auto play
  100. array(
  101. 'group' => esc_html__( 'General', 'myhome-core' ),
  102. 'type' => 'checkbox',
  103. 'heading' => esc_html__( 'Auto play', 'myhome-core' ),
  104. 'param_name' => 'owl_auto_play',
  105. 'value' => 'true',
  106. 'std' => 'true',
  107. 'save_always' => true
  108. )
  109. )
  110. );
  111. }
  112.  
  113. /**
  114. * init
  115. *
  116. * Generate shortcode output base on provided options
  117. */
  118. public function init( $opts ) {
  119. $atts = array(
  120. 'posts_style' => '',
  121. 'posts_limit' => 5,
  122. 'posts_category' => '',
  123. 'read_more_text' => esc_html__( 'Read more', 'myhome-core' ),
  124. 'owl_visible' => 'owl-carousel--visible-3',
  125. 'owl_dots' => ''
  126. );
  127. if ( function_exists( 'vc_map_get_attributes' ) ) {
  128. $atts = array_merge( $atts, vc_map_get_attributes( 'mh_carousel_post', $opts ) );
  129. }
  130.  
  131. $args = array(
  132. 'ignore_sticky_posts' => true,
  133. 'posts_per_page' => intval( $atts['posts_limit'] ),
  134. );
  135. $posts_category = intval( $atts['posts_category'] );
  136. if ( $posts_category ) {
  137. $args['category'] = $posts_category;
  138. }
  139.  
  140. if ( ! empty( My_Home_Core()->lang ) ) {
  141. $args['suppress_filters'] = 0;
  142. }
  143.  
  144. $carousel_posts = get_posts( $args );
  145. $class = $atts['owl_visible'] . ' ' . $atts['owl_dots'];
  146. if ( $atts['owl_auto_play'] != 'true' ) {
  147. $class .= ' owl-carousel--no-auto-play';
  148. }
  149. ob_start();
  150.  
  151. global $post;
  152. ?>
  153. <div class="owl-carousel <?php echo esc_attr( $class ); ?>">
  154. <?php foreach ( $carousel_posts as $post ) : setup_postdata( $post ); ?>
  155. <article
  156. <?php post_class(
  157. 'mh-post-grid mh-post-grid--img-absolute '
  158. . $atts['posts_style']
  159. ); ?>>
  160. <a href="<?php the_permalink(); ?>"
  161. title="<?php the_title_attribute(); ?>"
  162. class="mh-post-grid__thumbnail">
  163. <?php if ( has_post_thumbnail() ) : ?>
  164. <span class="mh-thumbnail__inner ">
  165. <?php My_Home_Image::the_image( get_post_thumbnail_id(), 'standard', get_the_title() ); ?>
  166. </span>
  167. <span class="mh-caption">
  168. <span class="mh-caption__inner"><?php echo esc_html( get_the_date( 'j F Y' ) ); ?></span>
  169. </span>
  170. <?php endif; ?>
  171. </a>
  172. <div class="mh-post-grid__inner">
  173. <h3 class="mh-post-grid__heading">
  174. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
  175. <?php echo esc_html( get_the_title() ); ?>
  176. </a>
  177. </h3>
  178. <div class="mh-post-grid__excerpt">
  179. <?php echo esc_html( wp_trim_words( get_the_excerpt(), 35, '...' ) ); ?>
  180. </div>
  181. <div class="mh-post-grid__btn-wrapper">
  182. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"
  183. class="mdl-button mdl-js-button mdl-button--raised mdl-js-ripple-effect mdl-button--primary-ghost">
  184. <?php echo esc_html( $atts['read_more_text'] ); ?>
  185. </a>
  186. </div>
  187. </div>
  188. </article>
  189. <?php endforeach; ?>
  190. </div>
  191. <?php
  192. wp_reset_postdata();
  193.  
  194. return ob_get_clean();
  195. }
  196.  
  197. }
  198.  
  199. endif;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement