mh_magazine_lite_posts_focus

The MH Magazine lite mh magazine lite posts focus class.

Defined (1)

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

/includes/widgets/mh-posts-focus.php  
  1. class mh_magazine_lite_posts_focus extends WP_Widget { 
  2. function __construct() { 
  3. parent::__construct( 
  4. 'mh_magazine_lite_posts_focus', esc_html_x('MH Posts Focus [lite]', 'widget name', 'mh-magazine-lite'),  
  5. array( 
  6. 'classname' => 'mh_magazine_lite_posts_focus',  
  7. 'description' => esc_html__('MH Posts Focus widget to display 5 posts with focus on large post in the middle (if placed in full-width widget area).', '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. if ($max_posts > 3) { 
  30. $alignment = ' mh-posts-focus-inner'; 
  31. } else { 
  32. $alignment = ' mh-posts-focus-full'; 
  33. if (!empty($instance['title'])) { 
  34. echo $args['before_title']; 
  35. if ($instance['category'] != 0) { 
  36. echo '<a href="' . esc_url(get_category_link($instance['category'])) . '" class="mh-widget-title-link">'; 
  37. echo esc_html(apply_filters('widget_title', $instance['title'])); 
  38. if ($instance['category'] != 0) { 
  39. echo '</a>'; 
  40. echo $args['after_title']; 
  41. echo '<div class="mh-row mh-posts-focus-widget clearfix">' . "\n"; 
  42. while ($widget_posts->have_posts()) : $widget_posts->the_post(); 
  43. if ($counter === 1) { ?> 
  44. <div class="mh-col-3-4 mh-posts-focus-wrap<?php echo esc_attr($alignment); ?> clearfix"> 
  45. <div class="mh-col-3-4 mh-posts-focus-wrap mh-posts-focus-large clearfix"> 
  46. <article class="post-<?php the_ID(); ?> mh-posts-focus-item mh-posts-focus-item-large clearfix"> 
  47. <figure class="mh-posts-focus-thumb mh-posts-focus-thumb-large"> 
  48. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php 
  49. if (has_post_thumbnail()) { 
  50. the_post_thumbnail('mh-magazine-lite-large'); 
  51. } else { 
  52. echo '<img class="mh-image-placeholder" src="' . get_template_directory_uri() . '/images/placeholder-large.png' . '" alt="' . esc_html__('No Picture', 'mh-magazine-lite') . '" />'; 
  53. } ?> 
  54. </a> 
  55. </figure> 
  56. <h3 class="mh-posts-focus-title mh-posts-focus-title-large"> 
  57. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"> 
  58. <?php the_title(); ?> 
  59. </a> 
  60. </h3> 
  61. <div class="mh-meta mh-posts-focus-meta mh-posts-focus-meta-large"> 
  62. <?php mh_magazine_lite_loop_meta(); ?> 
  63. </div> 
  64. <div class="mh-posts-focus-excerpt mh-posts-focus-excerpt-large clearfix"> 
  65. <?php the_excerpt(); ?> 
  66. </div> 
  67. </article> 
  68. </div><?php 
  69. if ($counter === 2) { 
  70. echo '<div class="mh-col-1-4 mh-posts-focus-wrap mh-posts-focus-small mh-posts-focus-small-inner clearfix">' . "\n"; 
  71. if ($counter >= 2) { ?> 
  72. <article class="post-<?php the_ID(); ?> mh-posts-focus-item mh-posts-focus-item-small clearfix"> 
  73. <figure class="mh-posts-focus-thumb mh-posts-focus-thumb-small"> 
  74. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php 
  75. if (has_post_thumbnail()) { 
  76. the_post_thumbnail('mh-magazine-lite-medium'); 
  77. } else { 
  78. echo '<img class="mh-image-placeholder" src="' . get_template_directory_uri() . '/images/placeholder-medium.png' . '" alt="' . esc_html__('No Picture', 'mh-magazine-lite') . '" />'; 
  79. } ?> 
  80. </a> 
  81. </figure> 
  82. <h3 class="mh-posts-focus-title mh-posts-focus-title-small"> 
  83. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" rel="bookmark"> 
  84. <?php the_title(); ?> 
  85. </a> 
  86. </h3> 
  87. <div class="mh-meta mh-posts-focus-meta mh-posts-focus-meta-small"> 
  88. <?php mh_magazine_lite_loop_meta(); ?> 
  89. </div> 
  90. <div class="mh-posts-focus-excerpt mh-posts-focus-excerpt-small clearfix"> 
  91. <?php the_excerpt(); ?> 
  92. </div> 
  93. </article><?php 
  94. if ($counter === 2 && $counter === $max_posts || $counter === 3) { 
  95. echo '</div>' . "\n"; 
  96. if ($counter === 3) { 
  97. echo '</div>' . "\n"; 
  98. echo '<div class="mh-col-1-4 mh-posts-focus-wrap mh-posts-focus-small mh-posts-focus-outer clearfix">' . "\n"; 
  99. if ($counter === $max_posts) { 
  100. echo '</div>' . "\n"; 
  101. $counter++; 
  102. endwhile; 
  103. wp_reset_postdata(); 
  104. echo '</div>' . "\n"; 
  105. endif; 
  106. echo $args['after_widget']; 
  107. function update($new_instance, $old_instance) { 
  108. $instance = array(); 
  109. if (!empty($new_instance['title'])) { 
  110. $instance['title'] = sanitize_text_field($new_instance['title']); 
  111. if (0 !== absint($new_instance['category'])) { 
  112. $instance['category'] = absint($new_instance['category']); 
  113. if (!empty($new_instance['tags'])) { 
  114. $tag_slugs = explode(', ', $new_instance['tags']); 
  115. $tag_slugs = array_map('sanitize_title', $tag_slugs); 
  116. $instance['tags'] = implode(', ', $tag_slugs); 
  117. if (0 !== absint($new_instance['offset'])) { 
  118. if (absint($new_instance['offset']) > 50) { 
  119. $instance['offset'] = 50; 
  120. } else { 
  121. $instance['offset'] = absint($new_instance['offset']); 
  122. return $instance; 
  123. function form($instance) { 
  124. $defaults = array('title' => '', 'category' => 0, 'tags' => '', 'offset' => 0); 
  125. $instance = wp_parse_args($instance, $defaults); ?> 
  126. <p> 
  127. <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'mh-magazine-lite'); ?></label> 
  128. <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')); ?>" /> 
  129. </p> 
  130. <p> 
  131. <label for="<?php echo esc_attr($this->get_field_id('category')); ?>"><?php esc_html_e('Select a Category:', 'mh-magazine-lite'); ?></label> 
  132. <select id="<?php echo esc_attr($this->get_field_id('category')); ?>" class="widefat" name="<?php echo esc_attr($this->get_field_name('category')); ?>"> 
  133. <option value="0" <?php selected(0, $instance['category']); ?>><?php esc_html_e('All', 'mh-magazine-lite'); ?></option><?php 
  134. $categories = get_categories(); 
  135. foreach ($categories as $cat) { ?> 
  136. <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 
  137. } ?> 
  138. </select> 
  139. <small><?php esc_html_e('Select a category to display posts from.', 'mh-magazine-lite'); ?></small> 
  140. </p> 
  141. <p> 
  142. <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> 
  143. <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')); ?>" /> 
  144. </p> 
  145. <p> 
  146. <label for="<?php echo esc_attr($this->get_field_id('offset')); ?>"><?php esc_html_e('Skip Posts (max. 50):', 'mh-magazine-lite'); ?></label> 
  147. <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')); ?>" /> 
  148. </p> 
  149. <p> 
  150. <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'); ?> 
  151. </p><?php