mh_magazine_lite_posts_stacked

The MH Magazine lite mh magazine lite posts stacked class.

Defined (1)

The class is defined in the following location(s).

/includes/widgets/mh-posts-stacked.php  
  1. class mh_magazine_lite_posts_stacked extends WP_Widget { 
  2. function __construct() { 
  3. parent::__construct( 
  4. 'mh_magazine_lite_posts_stacked', esc_html_x('MH Posts Stacked [lite]', 'widget name', 'mh-magazine-lite'),  
  5. array( 
  6. 'classname' => 'mh_magazine_lite_posts_stacked',  
  7. 'description' => esc_html__('MH Posts Stacked widget to display 5 stacked posts nicely including thumbnail, title and meta data.', 'mh-magazine-lite'),  
  8. 'customize_selective_refresh' => true 
  9. ); 
  10. function widget($args, $instance) { 
  11. $defaults = array('title' => '', 'category' => 0, 'tags' => '', 'postcount' => 5, 'offset' => 0, 'sticky' => 1); 
  12. $instance = wp_parse_args($instance, $defaults); 
  13. $query_args = array(); 
  14. $query_args['posts_per_page'] = $instance['postcount']; 
  15. $query_args['ignore_sticky_posts'] = $instance['sticky']; 
  16. if (0 !== $instance['category']) { 
  17. $query_args['cat'] = $instance['category']; 
  18. if (!empty($instance['tags'])) { 
  19. $tag_slugs = explode(', ', $instance['tags']); 
  20. $tag_slugs = array_map('trim', $tag_slugs); 
  21. $query_args['tag_slug__in'] = $tag_slugs; 
  22. if (0 !== $instance['offset']) { 
  23. $query_args['offset'] = $instance['offset']; 
  24. $widget_posts = new WP_Query($query_args); 
  25. $max_posts = $widget_posts->post_count; 
  26. echo $args['before_widget']; 
  27. if ($widget_posts->have_posts()) : 
  28. $counter = 1; 
  29. $stacked_border = ''; 
  30. if (!empty($instance['title'])) { 
  31. echo $args['before_title']; 
  32. if ($instance['category'] != 0) { 
  33. echo '<a href="' . esc_url(get_category_link($instance['category'])) . '" class="mh-widget-title-link">'; 
  34. echo esc_html(apply_filters('widget_title', $instance['title'])); 
  35. if ($instance['category'] != 0) { 
  36. echo '</a>'; 
  37. echo $args['after_title']; 
  38. echo '<div class="mh-posts-stacked-widget clearfix">' . "\n"; 
  39. while ($widget_posts->have_posts()) : $widget_posts->the_post(); 
  40. if ($counter === 1) { ?> 
  41. <div class="mh-posts-stacked-wrap mh-posts-stacked-large"> 
  42. <div class="post-<?php the_ID(); ?> mh-posts-stacked-content"> 
  43. <div class="mh-posts-stacked-thumb mh-posts-stacked-thumb-large"> 
  44. <a class="mh-posts-stacked-overlay mh-posts-stacked-overlay-large" href="<?php the_permalink(); ?>"></a><?php 
  45. if (has_post_thumbnail()) { 
  46. the_post_thumbnail('mh-magazine-lite-large'); 
  47. } else { 
  48. echo '<img class="mh-image-placeholder" src="' . get_template_directory_uri() . '/images/placeholder-large.png' . '" alt="' . esc_html__('No Picture', 'mh-magazine-lite') . '" />'; 
  49. } ?> 
  50. <article class="mh-posts-stacked-item"> 
  51. <h3 class="mh-posts-stacked-title mh-posts-stacked-title-large"> 
  52. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"> 
  53. <?php the_title(); ?> 
  54. </a> 
  55. </h3> 
  56. <div class="mh-posts-stacked-meta mh-posts-stacked-meta-large"> 
  57. <?php mh_magazine_lite_loop_meta(); ?> 
  58. </div> 
  59. </article> 
  60. </div> 
  61. </div> 
  62. </div><?php 
  63. if ($counter === 2) { 
  64. echo '<div class="mh-posts-stacked-wrap mh-posts-stacked-columns clearfix">' . "\n"; 
  65. if ($counter === 4 || $counter === 5) { 
  66. $stacked_border = ' mh-posts-stacked-overlay-last'; 
  67. if ($counter >= 2) { ?> 
  68. <div class="mh-posts-stacked-wrap mh-posts-stacked-small"> 
  69. <div class="post-<?php the_ID(); ?> mh-posts-stacked-content"> 
  70. <div class="mh-posts-stacked-thumb mh-posts-stacked-thumb-small"> 
  71. <a class="mh-posts-stacked-overlay mh-posts-stacked-overlay-small<?php echo esc_attr($stacked_border); ?>" href="<?php the_permalink(); ?>"></a><?php 
  72. if (has_post_thumbnail()) { 
  73. the_post_thumbnail('mh-magazine-lite-medium'); 
  74. } else { 
  75. echo '<img class="mh-image-placeholder" src="' . get_template_directory_uri() . '/images/placeholder-medium.png' . '" alt="' . esc_html__('No Picture', 'mh-magazine-lite') . '" />'; 
  76. } ?> 
  77. <article class="mh-posts-stacked-item"> 
  78. <h3 class="mh-posts-stacked-title mh-posts-stacked-title-small"> 
  79. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"> 
  80. <?php the_title(); ?> 
  81. </a> 
  82. </h3> 
  83. <div class="mh-posts-stacked-meta mh-posts-stacked-meta-small"> 
  84. <?php mh_magazine_lite_loop_meta(); ?> 
  85. </div> 
  86. </article> 
  87. </div> 
  88. </div> 
  89. </div><?php 
  90. if ($counter > 1 && $counter === $max_posts) { 
  91. echo '</div>' . "\n"; 
  92. $counter++; 
  93. endwhile; 
  94. wp_reset_postdata(); 
  95. echo '</div>' . "\n"; 
  96. endif; 
  97. echo $args['after_widget']; 
  98. function update($new_instance, $old_instance) { 
  99. $instance = array(); 
  100. if (!empty($new_instance['title'])) { 
  101. $instance['title'] = sanitize_text_field($new_instance['title']); 
  102. if (0 !== absint($new_instance['category'])) { 
  103. $instance['category'] = absint($new_instance['category']); 
  104. if (!empty($new_instance['tags'])) { 
  105. $tag_slugs = explode(', ', $new_instance['tags']); 
  106. $tag_slugs = array_map('sanitize_title', $tag_slugs); 
  107. $instance['tags'] = implode(', ', $tag_slugs); 
  108. if (0 !== absint($new_instance['offset'])) { 
  109. if (absint($new_instance['offset']) > 50) { 
  110. $instance['offset'] = 50; 
  111. } else { 
  112. $instance['offset'] = absint($new_instance['offset']); 
  113. return $instance; 
  114. function form($instance) { 
  115. $defaults = array('title' => '', 'category' => 0, 'tags' => '', 'offset' => 0, 'sticky' => 1); 
  116. $instance = wp_parse_args($instance, $defaults); ?> 
  117. <p> 
  118. <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'mh-magazine-lite'); ?></label> 
  119. <input class="widefat" type="text" value="<?php echo esc_attr($instance['title']); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" id="<?php echo esc_attr($this->get_field_id('title')); ?>" /> 
  120. </p> 
  121. <p> 
  122. <label for="<?php echo esc_attr($this->get_field_id('category')); ?>"><?php esc_html_e('Select a Category:', 'mh-magazine-lite'); ?></label> 
  123. <select id="<?php echo esc_attr($this->get_field_id('category')); ?>" class="widefat" name="<?php echo esc_attr($this->get_field_name('category')); ?>"> 
  124. <option value="0" <?php selected(0, $instance['category']); ?>><?php esc_html_e('All', 'mh-magazine-lite'); ?></option><?php 
  125. $categories = get_categories(); 
  126. foreach ($categories as $cat) { ?> 
  127. <option value="<?php echo absint($cat->cat_ID); ?>" <?php selected($cat->cat_ID, $instance['category']); ?>><?php echo esc_html($cat->cat_name) . ' (' . absint($cat->category_count) . ')'; ?></option><?php 
  128. } ?> 
  129. </select> 
  130. <small><?php esc_html_e('Select a category to display posts from.', 'mh-magazine-lite'); ?></small> 
  131. </p> 
  132. <p> 
  133. <label for="<?php echo esc_attr($this->get_field_id('tags')); ?>"><?php esc_html_e('Filter Posts by Tags (e.g. lifestyle):', 'mh-magazine-lite'); ?></label> 
  134. <input class="widefat" type="text" value="<?php echo esc_attr($instance['tags']); ?>" name="<?php echo esc_attr($this->get_field_name('tags')); ?>" id="<?php echo esc_attr($this->get_field_id('tags')); ?>" /> 
  135. </p> 
  136. <p> 
  137. <label for="<?php echo esc_attr($this->get_field_id('offset')); ?>"><?php esc_html_e('Skip Posts (max. 50):', 'mh-magazine-lite'); ?></label> 
  138. <input class="widefat" type="text" value="<?php echo absint($instance['offset']); ?>" name="<?php echo esc_attr($this->get_field_name('offset')); ?>" id="<?php echo esc_attr($this->get_field_id('offset')); ?>" /> 
  139. </p> 
  140. <p> 
  141. <strong><?php esc_html_e('Info:', 'mh-magazine-lite'); ?></strong> <?php esc_html_e('This is the lite version of this widget with basic features. More features and options are available in the premium version of MH Magazine.', 'mh-magazine-lite'); ?> 
  142. </p><?php