| 1 |
<?php |
| 2 |
/** |
| 3 |
* Recent post widget |
| 4 |
* |
| 5 |
* @package wpstream-theme |
| 6 |
*/ |
| 7 |
|
| 8 |
// Exit if accessed directly. |
| 9 |
if ( ! defined( 'ABSPATH' ) ) { |
| 10 |
exit; |
| 11 |
} |
| 12 |
|
| 13 |
if ( ! class_exists( 'Wpstream_Recent_Post_Widget' ) ) { |
| 14 |
/** |
| 15 |
* Widget for displaying the most recent posts. |
| 16 |
* |
| 17 |
* @since 2.8.0 |
| 18 |
*/ |
| 19 |
class Wpstream_Recent_Post_Widget extends Wpstream_Widget_Base { |
| 20 |
|
| 21 |
/** |
| 22 |
* Sets up a new widget instance. |
| 23 |
* |
| 24 |
* @since 2.8.0 |
| 25 |
*/ |
| 26 |
public function __construct() { |
| 27 |
$widget_ops = array( |
| 28 |
'description' => __( 'Your site’s most recent Posts.', 'hello-wpstream' ), |
| 29 |
); |
| 30 |
parent::__construct( 'wpstream-recent-posts', __( 'Wpstream Recent Posts', 'hello-wpstream' ), $widget_ops ); |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Outputs the content for the current widget instance. |
| 35 |
* |
| 36 |
* @since 2.8.0 |
| 37 |
* |
| 38 |
* @param array $args Display arguments including 'before_title', 'after_title', |
| 39 |
* 'before_widget', and 'after_widget'. |
| 40 |
* @param array $instance Settings for the current widget instance. |
| 41 |
*/ |
| 42 |
public function widget( $args, $instance ) { |
| 43 |
$default_title = __( 'Recent Posts', 'hello-wpstream' ); |
| 44 |
$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : $default_title; |
| 45 |
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 3; |
| 46 |
|
| 47 |
$r = new WP_Query( |
| 48 |
array( |
| 49 |
'posts_per_page' => $number, |
| 50 |
'no_found_rows' => true, |
| 51 |
'post_status' => 'publish', |
| 52 |
'ignore_sticky_posts' => true, |
| 53 |
) |
| 54 |
); |
| 55 |
|
| 56 |
if ( ! $r->have_posts() ) { |
| 57 |
return; |
| 58 |
} |
| 59 |
?> |
| 60 |
|
| 61 |
<?php echo wp_kses_post( $args['before_widget']); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> |
| 62 |
|
| 63 |
<?php |
| 64 |
if ( $title ) { |
| 65 |
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 66 |
echo wp_kses_post($args['before_title']) . esc_html( $title ) .wp_kses_post( $args['after_title']); |
| 67 |
} |
| 68 |
?> |
| 69 |
|
| 70 |
<ul class="wpstream-recent-post"> |
| 71 |
<?php |
| 72 |
while ( $r->have_posts() ) { |
| 73 |
echo '<li class="">'; |
| 74 |
$r->the_post(); |
| 75 |
get_template_part( |
| 76 |
'template-parts/single/cards/blog-card-v1', |
| 77 |
'', |
| 78 |
array( |
| 79 |
'type' => 'widget', |
| 80 |
'class' => '', |
| 81 |
'show_category' => false, |
| 82 |
) |
| 83 |
); |
| 84 |
echo '</li>'; |
| 85 |
} |
| 86 |
wp_reset_postdata(); |
| 87 |
?> |
| 88 |
</ul> |
| 89 |
|
| 90 |
<?php |
| 91 |
|
| 92 |
echo wp_kses_post($args['after_widget']); //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Handles updating the settings for the current widget instance. |
| 97 |
* |
| 98 |
* @since 2.8.0 |
| 99 |
* |
| 100 |
* @param array $new_instance New settings for this instance as input by the user via |
| 101 |
* WP_Widget::form(). |
| 102 |
* @param array $old_instance Old settings for this instance. |
| 103 |
* @return array Updated settings to save. |
| 104 |
*/ |
| 105 |
public function update( $new_instance, $old_instance ) { |
| 106 |
$instance = $old_instance; |
| 107 |
$instance['title'] = sanitize_text_field( $new_instance['title'] ); |
| 108 |
$instance['number'] = (int) $new_instance['number']; |
| 109 |
|
| 110 |
return $instance; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Outputs the settings form for the widget. |
| 115 |
* |
| 116 |
* @since 2.8.0 |
| 117 |
* |
| 118 |
* @param array $instance Current settings. |
| 119 |
*/ |
| 120 |
public function form( $instance ) { |
| 121 |
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : ''; |
| 122 |
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 3; |
| 123 |
?> |
| 124 |
<p> |
| 125 |
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'hello-wpstream' ); ?></label> |
| 126 |
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /> |
| 127 |
</p> |
| 128 |
|
| 129 |
<p> |
| 130 |
<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of posts to show:', 'hello-wpstream' ); ?></label> |
| 131 |
<input class="tiny-text" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="number" step="1" min="1" value="<?php echo esc_attr( $number ); ?>" size="3" /> |
| 132 |
</p> |
| 133 |
<?php |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|