-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-pts-featured-posts-widget.php
185 lines (160 loc) · 5.25 KB
/
class-pts-featured-posts-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php
if ( ! defined( 'ABSPATH') ) {
exit;
}
/**
* PTS_Featured_Posts_Widget class.
*
* @extends WP_Widget
*/
class PTS_Featured_Posts_Widget extends WP_Widget {
private $featured_post_types = array();
/**
* __construct function.
*
* @access public
* @return void
*/
public function __construct() {
$this->featured_post_types = (array) get_option( 'pts_featured_post_types_settings' );
parent::__construct( 'pts_featured_posts_widget', esc_html__( 'Featured Posts Widget', 'post-type-spotlight' ), array( 'description' => esc_html__( 'Featured Posts Widget', 'post-type-spotlight' ) ) );
}
/**
* widget function.
*
* @access public
* @param mixed $args
* @param mixed $instance
* @return void
*/
public function widget( $args = array(), $instance ) {
$widget_settings = wp_parse_args(
$instance,
array(
'number' => 5,
'title' => '',
'post_type' => '',
)
);
/**
* Define our widget defaults
*/
$defaults = array(
'before_widget' => '',
'after_widget' => '',
);
// Parse incoming $args into an array and merge it with $defaults.
$args = wp_parse_args( $args, $defaults );
if ( empty( $widget_settings['post_type'] ) ) {
return;
}
$title = apply_filters( 'widget_title', $widget_settings['title'] );
echo wp_kses_post( $args['before_widget'] );
if ( ! empty( $title ) ) {
echo wp_kses_post( $args['before_title'] . $title . $args['after_title'] );
}
$args = array(
'posts_per_page' => (int) $widget_settings['number'],
'post_type' => $widget_settings['post_type'],
'tax_query' => array(
array(
'taxonomy' => 'pts_feature_tax',
'field' => 'slug',
'terms' => array( 'featured' ),
),
),
);
$featured_posts = new WP_Query( $args );
if ( $featured_posts->have_posts() ) : ?>
<div class="pts-widget-post-container">
<?php while ( $featured_posts->have_posts() ) :
$featured_posts->the_post();
?>
<div <?php post_class( 'pts-featured-post' ); ?>>
<h3 title="<?php the_title_attribute(); ?>"><a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
</div>
<?php endwhile; ?>
</div>
<?php
endif;
wp_reset_postdata();
echo wp_kses_post( $args['after_widget'] );
}
/**
* update function.
*
* @access public
* @param mixed $new_instance
* @param mixed $old_instance
* @return mixed|array
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['number'] = (int) $new_instance['number'];
if ( in_array( $new_instance['post_type'], $this->featured_post_types, true ) ) {
$instance['post_type'] = $new_instance['post_type'];
}
return $instance;
}
/**
* Widget form.
*
* @access public
* @param mixed $instance
* @return void
*/
public function form( $instance ) {
$defaults = array(
'number' => 5,
'title' => '',
'post_type' => '',
);
$instance = wp_parse_args( $instance, $defaults );
if ( empty( $this->featured_post_types ) ) :
?>
<p>
<?php
echo wp_kses(
sprintf(
// translators: Link to options screen
__( 'You need to select a featured post type on the <a href="%1$s">Settings->Writing screen</a> before you can use this widget.', 'post-type-spotlight' ),
esc_url( admin_url( 'options-writing.php' ) )
),
array(
'a' => array(
'href' => array(),
),
'p' => array(),
)
);
?>
</p>
<?php else : ?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Title', 'post-type-spotlight' ); ?>:</label>
<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( $instance['title'] ); ?>" />
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>"><?php esc_html_e( 'Post type to feature', 'post-type-spotlight' ); ?>:</label>
<select id="<?php echo esc_attr( $this->get_field_id( 'post_type' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'post_type' ) ); ?>" class="widefat">
<option value=""><?php esc_html_e( 'Select post type', 'post-type-spotlight' ); ?>...</option>
<?php
foreach ( $this->featured_post_types as $pt ) {
if ( $current_post_type = get_post_type_object( $pt ) ) : // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.Found, Squiz.PHP.DisallowMultipleAssignments.Found
?>
<option value="<?php echo esc_attr( $pt ); ?>" <?php selected( $instance['post_type'], $pt ); ?>><?php echo esc_html( $current_post_type->labels->name ); ?></option>
<?php
endif;
}
?>
</select>
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>"><?php esc_html_e( 'Number of Posts', 'post-type-spotlight' ); ?>:</label>
<input size="2" id="<?php echo esc_attr( $this->get_field_id( 'number' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number' ) ); ?>" type="text" value="<?php echo esc_attr( $instance['number'] ); ?>" />
</p>
<?php
endif;
}
}