-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandomPostOnRefresh.php
More file actions
377 lines (324 loc) · 11.8 KB
/
RandomPostOnRefresh.php
File metadata and controls
377 lines (324 loc) · 11.8 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
/**
* Plugin Name: Random Post on Refresh
* Description: Show a random post on every page load.
* Plugin URI: http://wpscholar.com/wordpress-plugins/random-post-on-refresh/
* Version: 1.2.3
* Author: Micah Wood
* Author URI: https://wpscholar.com
* Requires at least: 6.4
* Requires PHP: 7.4
* Text Domain: random-post-on-refresh
* Domain Path: /languages
* License: GPL3
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*
* Copyright 2018-2025 by Micah Wood - All rights reserved.
*
* @package RandomPostOnRefresh
*/
if ( ! class_exists( 'RandomPostOnRefresh' ) ) {
/**
* Class RandomPostOnRefresh
*/
class RandomPostOnRefresh {
const SHORTCODE = 'random_post_on_refresh';
const DEFAULT_ATTRIBUTES = array(
'author' => '',
'class' => '',
'ids' => '',
'image_required' => 'true',
'not' => '',
'orderby' => 'date',
'order' => 'DESC',
'post_type' => 'post',
'posts_per_page' => 100,
'search' => '',
'show' => 'title, image, excerpt',
'size' => 'large',
'taxonomy' => '',
'terms' => '',
);
/**
* Initialize the plugin.
*/
public static function initialize() {
add_action( 'init', array( __CLASS__, 'load_textdomain' ) );
add_filter( 'widget_text', 'do_shortcode' );
add_action( 'wp_enqueue_scripts', array( __CLASS__, 'wp_enqueue_scripts' ) );
add_shortcode( self::SHORTCODE, array( __CLASS__, 'shortcode' ) );
}
/**
* Load the textdomain.
*/
public static function load_textdomain() {
load_plugin_textdomain( 'random-post-on-refresh', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Enqueue style.
*/
public static function wp_enqueue_scripts() {
$plugin_version = get_file_data( __FILE__, array( 'Version' ), 'plugin' );
wp_register_style( self::SHORTCODE, plugins_url( '/assets/random-post-on-refresh.css', __FILE__ ), array(), $plugin_version );
}
/**
* Shortcode handler
*
* @param array $atts Shortcode attributes
*
* @return bool|string
*/
public static function shortcode( $atts ) {
wp_enqueue_style( self::SHORTCODE );
$atts = shortcode_atts(
self::DEFAULT_ATTRIBUTES,
array_change_key_case( array_filter( (array) $atts ), CASE_LOWER ),
self::SHORTCODE
);
$image_size = $atts['size'];
$groups = array_filter(
array_map(
function ( $group ) {
return self::list_to_array( $group );
},
self::list_to_array( $atts['show'], '|' )
)
);
$can_show = array( 'title', 'image', 'excerpt', 'content' );
$show = array_merge( ...$groups );
$show_title = in_array( 'title', $show, true );
$show_image = in_array( 'image', $show, true );
$show_excerpt = in_array( 'excerpt', $show, true );
$show_content = in_array( 'content', $show, true );
$image_required = wp_validate_boolean( $atts['image_required'] );
// Check for featured image support
if ( $show_image && ! current_theme_supports( 'post-thumbnails' ) ) {
return self::error(
__( 'Sorry, your theme does not support featured images. Update the "show" attribute to exclude the "image" option.', 'random-post-on-refresh' ),
'[' . self::SHORTCODE . ' show="title, excerpt"]'
);
}
// Taxonomy validation
if ( ! empty( $atts['taxonomy'] ) && ! taxonomy_exists( $atts['taxonomy'] ) ) {
return self::error(
sprintf(
// Translators: %1$s is replaced with taxonomy shortcode argument and %2$s is replaced with a comma-separated list of available taxonomies.
__( 'Sorry, taxonomy "%1$s" is invalid. Valid options are: %2$s. Please check your shortcode implementation.', 'random-post-on-refresh' ),
$atts['taxonomy'],
implode( ', ', get_taxonomies() )
),
'[' . self::SHORTCODE . ' taxonomy="' . $atts['taxonomy'] . '"]'
);
}
// Taxonomy/term attribute validation
if ( ! empty( $atts['terms'] ) && empty( $atts['taxonomy'] ) ) {
return self::error(
__( 'Sorry, you cannot use the terms attribute without the taxonomy attribute. Please check your shortcode implementation.', 'random-post-on-refresh' ),
'[' . self::SHORTCODE . ' terms="' . $atts['terms'] . '"]'
);
}
if ( empty( $atts['terms'] ) && ! empty( $atts['taxonomy'] ) ) {
return self::error(
__( 'Sorry, you cannot use the taxonomy attribute without the terms attribute. Please check your shortcode implementation.', 'random-post-on-refresh' ),
'[' . self::SHORTCODE . ' taxonomy="' . $atts['taxonomy'] . '"]'
);
}
// Post type validation
$post_types = array_filter( array_map( 'trim', explode( ',', $atts['post_type'] ) ) );
foreach ( $post_types as $post_type ) {
if ( ! post_type_exists( $post_type ) ) {
return self::error(
sprintf(
// Translators: %1$s is replaced with post_type shortcode argument and %2$s is replaced with a comma-separated list of available post types.
__( 'Sorry, post type "%1$s" is invalid. Valid options are: %2$s. Please check your shortcode implementation.', 'random-post-on-refresh' ),
$post_type,
implode( ', ', get_post_types( array( 'public' => true ) ) )
),
'[' . self::SHORTCODE . ' post_type="' . $atts['post_type'] . '"]'
);
}
}
// Build query args using the new method
$query_args = self::build_query_args( $atts );
$query = new WP_Query( $query_args );
if ( ! $query->have_posts() ) {
return self::error(
__( 'Sorry, no matching posts were found. Your query may be too restrictive. Please check your shortcode implementation.', 'random-post-on-refresh' ) .
( $image_required ? ' ' . __( 'Currently, only posts with featured images will be shown. Perhaps try setting the "image_required" property to "false"?', 'random-post-on-refresh' ) : '' )
);
}
$posts = $query->posts;
/**
* The randomly selected post.
*
* @var WP_Post $post
*/
$post = $posts[ array_rand( $posts ) ];
if ( $show_image && $image_required && ! has_post_thumbnail( $post ) ) {
return self::error(
__( 'Sorry, the selected post does not have a featured image.', 'random-post-on-refresh' )
);
}
$display = array();
foreach ( $groups as $items ) {
if ( count( $groups ) > 1 ) {
$display[] = '<span class="random-post-on-refresh__group">';
}
foreach ( $items as $item ) {
if ( in_array( $item, $can_show, true ) ) {
switch ( $item ) {
case 'title':
$display['title'] = $show_title ? sprintf( '<span class="random-post-on-refresh__title">%s</span>', esc_html( get_the_title( $post ) ) ) : '';
break;
case 'image':
$display['image'] = $show_image && has_post_thumbnail( $post ) ? sprintf( '<span class="random-post-on-refresh__image">%s</span>', get_the_post_thumbnail( $post, $image_size ) ) : '';
break;
case 'excerpt':
$display['excerpt'] = $show_excerpt ? sprintf( '<span class="random-post-on-refresh__excerpt">%s</span>', self::get_the_excerpt( $post ) ) : '';
break;
case 'content':
$display['content'] = $show_content ? sprintf( '<span class="random-post-on-refresh__content">%s</span>', apply_filters( 'the_content', wp_kses_post( $post->post_content ) ) ) : '';
break;
}
}
}
if ( count( $groups ) > 1 ) {
$display[] = '</span>';
}
}
return sprintf(
'<div class="random-post-on-refresh %s"><a href="%s">%s</a></div>',
esc_attr(
implode(
' ',
array_filter(
array(
count( $groups ) > 1 ? '--has-groups' : '',
$atts['class'],
)
)
)
),
esc_url( get_the_permalink( $post ) ),
implode( '', array_filter( $display ) )
);
}
/**
* Build WP_Query arguments from shortcode attributes.
*
* @param array $atts Shortcode attributes
* @return array Query arguments for WP_Query
*/
public static function build_query_args( $atts ) {
$post_types = array_filter( array_map( 'trim', explode( ',', $atts['post_type'] ) ) );
$orderby = $atts['orderby'];
// Just in case someone uses "random" instead of "rand".
if ( 'random' === $orderby ) {
$orderby = 'rand';
}
$query_args = array(
'post_type' => $post_types,
'posts_per_page' => absint( $atts['posts_per_page'] ),
'orderby' => $orderby,
'order' => strtoupper( $atts['order'] ) === 'ASC' ? 'ASC' : 'DESC',
);
if ( ! empty( $atts['author'] ) ) {
$query_args['author__in'] = self::parse_id_list( $atts['author'] );
}
if ( ! empty( $atts['ids'] ) ) {
$query_args['post__in'] = self::parse_id_list( $atts['ids'] );
}
if ( ! empty( $atts['not'] ) ) {
$query_args['post__not_in'] = self::parse_id_list( $atts['not'] );
}
if ( ! empty( $atts['search'] ) ) {
$query_args['s'] = $atts['search'];
}
if ( ! empty( $atts['taxonomy'] ) && ! empty( $atts['terms'] ) ) {
$terms = self::parse_id_list( $atts['terms'] );
if ( 'category' === $atts['taxonomy'] ) {
$query_args['category__in'] = $terms;
} elseif ( 'post_tag' === $atts['taxonomy'] ) {
$query_args['tag__in'] = $terms;
} else {
$query_args['tax_query'] = array(
array(
'taxonomy' => $atts['taxonomy'],
'field' => 'term_id',
'terms' => $terms,
),
);
}
}
// Only fetch posts with images?
if ( ! empty( $atts['show'] ) && strpos( $atts['show'], 'image' ) !== false && wp_validate_boolean( $atts['image_required'] ) ) {
$query_args['meta_query'] = array( array( 'key' => '_thumbnail_id' ) );
}
// Never load the current post.
$query_args['post__not_in'][] = get_the_ID();
$query_args = apply_filters( 'random_post_on_refresh_query_args', $query_args, $atts );
return $query_args;
}
/**
* Parse an ID list into an array.
*
* @param string $id_list A comma separated list of IDs
*
* @return int[]
*/
public static function parse_id_list( $id_list ) {
$ids = array();
if ( ! empty( $id_list ) ) {
$ids = array_values( array_filter( array_map( 'absint', explode( ',', preg_replace( '#[^0-9,]#', '', $id_list ) ) ) ) );
}
return $ids;
}
/**
* Convert a list (string) to an array
*
* @param string $separated_list A delimiter separated list of items
* @param string $delimiter The delimiter used to separate items.
*
* @return array
*/
public static function list_to_array( $separated_list, $delimiter = ',' ) {
return array_filter( array_map( 'trim', explode( $delimiter, $separated_list ) ) );
}
/**
* Get the excerpt for a specific post (outside of the loop).
*
* @param WP_Post $post The WordPress post.
*
* @return string
*/
public static function get_the_excerpt( WP_Post $post ) {
return (string) apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ) );
}
/**
* Setup error message.
*
* @param string $message The error message to display.
*
* @param string $example (optional) An example shortcode usage.
*
* @return string
*/
public static function error( $message, $example = '' ) {
if ( current_user_can( 'edit_posts', get_the_ID() ) ) {
return sprintf(
'<div class="random-post-on-refresh-error"><p>%1$s</p>%2$s<p>%3$s</p><p>%4$s</p></div>',
esc_html( $message ),
empty( $example ) ? '' : '<p>' . esc_html( $example ) . '</p>',
sprintf(
'<a href="%1$s" target="_blank" rel="noreferrer noopener">%2$s</a>',
'https://wordpress.org/plugins/random-post-on-refresh/#faq-header',
__( 'Consult the documentation', 'random-post-on-refresh' )
),
esc_html__( 'Note: This helpful notification is only visible to logged in users who can edit this shortcode.', 'random-post-on-refresh' )
);
}
return '';
}
}
add_action( 'plugins_loaded', array( 'RandomPostOnRefresh', 'initialize' ) );
}