mh_custom_posts_widget

The MH Magazine lite mh custom posts widget class.

Defined (1)

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

/includes/widgets/mh-custom-posts.php  
  1. class mh_custom_posts_widget extends WP_Widget { 
  2. function __construct() { 
  3. parent::__construct( 
  4. 'mh_custom_posts', esc_html_x('MH Custom Posts [lite]', 'widget name', 'mh-magazine-lite'),  
  5. array( 
  6. 'classname' => 'mh_custom_posts',  
  7. 'description' => esc_html__('Custom Posts Widget to display posts based on categories or tags.', '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. if (0 !== $instance['category']) { 
  15. $query_args['cat'] = $instance['category']; 
  16. if (!empty($instance['tags'])) { 
  17. $tag_slugs = explode(', ', $instance['tags']); 
  18. $tag_slugs = array_map('trim', $tag_slugs); 
  19. $query_args['tag_slug__in'] = $tag_slugs; 
  20. if (!empty($instance['postcount'])) { 
  21. $query_args['posts_per_page'] = $instance['postcount']; 
  22. if (0 !== $instance['offset']) { 
  23. $query_args['offset'] = $instance['offset']; 
  24. if (1 === $instance['sticky']) { 
  25. $query_args['ignore_sticky_posts'] = true; 
  26. $widget_loop = new WP_Query($query_args); 
  27. echo $args['before_widget']; 
  28. if (!empty($instance['title'])) { 
  29. echo $args['before_title']; 
  30. if ($instance['category'] != 0) { 
  31. echo '<a href="' . esc_url(get_category_link($instance['category'])) . '" class="mh-widget-title-link">'; 
  32. echo esc_html(apply_filters('widget_title', $instance['title'])); 
  33. if ($instance['category'] != 0) { 
  34. echo '</a>'; 
  35. echo $args['after_title']; 
  36. } ?> 
  37. <ul class="mh-custom-posts-widget clearfix"><?php 
  38. while ($widget_loop->have_posts()) : $widget_loop->the_post(); ?> 
  39. <li class="post-<?php the_ID(); ?> mh-custom-posts-item mh-custom-posts-small clearfix"> 
  40. <figure class="mh-custom-posts-thumb"> 
  41. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php 
  42. if (has_post_thumbnail()) { 
  43. the_post_thumbnail('mh-magazine-lite-small'); 
  44. } else { 
  45. echo '<img class="mh-image-placeholder" src="' . get_template_directory_uri() . '/images/placeholder-small.png' . '" alt="' . esc_html__('No Image', 'mh-magazine-lite') . '" />'; 
  46. } ?> 
  47. </a> 
  48. </figure> 
  49. <div class="mh-custom-posts-header"> 
  50. <p class="mh-custom-posts-small-title"> 
  51. <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"> 
  52. <?php the_title(); ?> 
  53. </a> 
  54. </p> 
  55. <div class="mh-meta mh-custom-posts-meta"> 
  56. <?php mh_magazine_lite_loop_meta(); ?> 
  57. </div> 
  58. </div> 
  59. </li><?php 
  60. endwhile; 
  61. wp_reset_postdata(); ?> 
  62. </ul><?php 
  63. echo $args['after_widget']; 
  64. function update($new_instance, $old_instance) { 
  65. $instance = array(); 
  66. if (!empty($new_instance['title'])) { 
  67. $instance['title'] = sanitize_text_field($new_instance['title']); 
  68. if (0 !== absint($new_instance['category'])) { 
  69. $instance['category'] = absint($new_instance['category']); 
  70. if (!empty($new_instance['tags'])) { 
  71. $tag_slugs = explode(', ', $new_instance['tags']); 
  72. $tag_slugs = array_map('sanitize_title', $tag_slugs); 
  73. $instance['tags'] = implode(', ', $tag_slugs); 
  74. if (0 !== absint($new_instance['postcount'])) { 
  75. if (absint($new_instance['postcount']) > 50) { 
  76. $instance['postcount'] = 50; 
  77. } else { 
  78. $instance['postcount'] = absint($new_instance['postcount']); 
  79. if (0 !== absint($new_instance['offset'])) { 
  80. if (absint($new_instance['offset']) > 50) { 
  81. $instance['offset'] = 50; 
  82. } else { 
  83. $instance['offset'] = absint($new_instance['offset']); 
  84. $instance['sticky'] = (!empty($new_instance['sticky'])) ? 1 : 0; 
  85. return $instance; 
  86. function form($instance) { 
  87. $defaults = array('title' => '', 'category' => 0, 'tags' => '', 'postcount' => 5, 'offset' => 0, 'sticky' => 1); 
  88. $instance = wp_parse_args($instance, $defaults); ?> 
  89. <p> 
  90. <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:', 'mh-magazine-lite'); ?></label> 
  91. <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')); ?>" /> 
  92. </p> 
  93. <p> 
  94. <label for="<?php echo esc_attr($this->get_field_id('category')); ?>"><?php esc_html_e('Select a Category:', 'mh-magazine-lite'); ?></label> 
  95. <select id="<?php echo esc_attr($this->get_field_id('category')); ?>" class="widefat" name="<?php echo esc_attr($this->get_field_name('category')); ?>"> 
  96. <option value="0" <?php selected(0, $instance['category']); ?>><?php esc_html_e('All', 'mh-magazine-lite'); ?></option><?php 
  97. $categories = get_categories(); 
  98. foreach ($categories as $cat) { ?> 
  99. <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 
  100. } ?> 
  101. </select> 
  102. <small><?php _e('Select a category to display posts from.', 'mh-magazine-lite'); ?></small> 
  103. </p> 
  104. <p> 
  105. <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> 
  106. <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')); ?>" /> 
  107. </p> 
  108. <p> 
  109. <label for="<?php echo esc_attr($this->get_field_id('postcount')); ?>"><?php esc_html_e('Post Count (max. 50):', 'mh-magazine-lite'); ?></label> 
  110. <input class="widefat" type="text" value="<?php echo absint($instance['postcount']); ?>" name="<?php echo esc_attr($this->get_field_name('postcount')); ?>" id="<?php echo esc_attr($this->get_field_id('postcount')); ?>" /> 
  111. </p> 
  112. <p> 
  113. <label for="<?php echo esc_attr($this->get_field_id('offset')); ?>"><?php esc_html_e('Skip Posts (max. 50):', 'mh-magazine-lite'); ?></label> 
  114. <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')); ?>" /> 
  115. </p> 
  116. <p> 
  117. <input id="<?php echo esc_attr($this->get_field_id('sticky')); ?>" name="<?php echo esc_attr($this->get_field_name('sticky')); ?>" type="checkbox" value="1" <?php checked(1, $instance['sticky']); ?> /> 
  118. <label for="<?php echo esc_attr($this->get_field_id('sticky')); ?>"><?php esc_html_e('Ignore Sticky Posts', 'mh-magazine-lite'); ?></label> 
  119. </p> 
  120. <p> 
  121. <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'); ?> 
  122. </p><?php