mh_slider_hp_widget

The MH Magazine lite mh slider hp widget class.

Defined (1)

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

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