Skip to content

Commit 246abe9

Browse files
committed
Ensure code meets coding standards.
Don't allow the current post to be selected as the random post. Always return the last 100 matching items and select the random post from one of those. Updated styles to work with TwentyNineteen. Tested in WordPress 5.0
1 parent 40852d4 commit 246abe9

File tree

3 files changed

+66
-30
lines changed

3 files changed

+66
-30
lines changed

RandomPostOnRefresh.php

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Plugin URI: http://wpscholar.com/wordpress-plugins/random-post-on-refresh/
77
* Author: Micah Wood
88
* Author URI: http://wpscholar.com
9-
* Version: 1.0
9+
* Version: 1.1
1010
* Text Domain: random-post-on-refresh
1111
* License: GPL3
1212
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
@@ -32,14 +32,18 @@ public static function initialize() {
3232
add_shortcode( self::SHORTCODE, array( __CLASS__, 'shortcode' ) );
3333
}
3434

35+
/**
36+
* Enqueue style.
37+
*/
3538
public static function wp_enqueue_scripts() {
36-
wp_register_style( self::SHORTCODE, plugins_url( '/assets/random-post-on-refresh.css', __FILE__ ) );
39+
$plugin_version = get_file_data( __FILE__, array( 'Version' ), 'plugin' );
40+
wp_register_style( self::SHORTCODE, plugins_url( '/assets/random-post-on-refresh.css', __FILE__ ), array(), $plugin_version );
3741
}
3842

3943
/**
4044
* Shortcode handler
4145
*
42-
* @param array $atts
46+
* @param array $atts Shortcode attributes
4347
*
4448
* @return bool|string
4549
*/
@@ -66,15 +70,20 @@ public static function shortcode( $atts ) {
6670

6771
$image_size = $atts['size'];
6872

69-
$groups = array_filter( array_map( function ( $group ) {
70-
return self::list_to_array( $group );
71-
}, self::list_to_array( $atts['show'], '|' ) ) );
73+
$groups = array_filter(
74+
array_map(
75+
function ( $group ) {
76+
return self::list_to_array( $group );
77+
},
78+
self::list_to_array( $atts['show'], '|' )
79+
)
80+
);
7281

7382
$can_show = [ 'title', 'image', 'excerpt', 'content' ];
74-
$show = array_merge( ...$groups );
83+
$show = array_merge( ...$groups );
7584

76-
$show_title = in_array( 'title', $show, true );
77-
$show_image = in_array( 'image', $show, true );
85+
$show_title = in_array( 'title', $show, true );
86+
$show_image = in_array( 'image', $show, true );
7887
$show_excerpt = in_array( 'excerpt', $show, true );
7988
$show_content = in_array( 'content', $show, true );
8089

@@ -89,7 +98,9 @@ public static function shortcode( $atts ) {
8998
// Taxonomy validation
9099
if ( ! empty( $atts['taxonomy'] ) && ! taxonomy_exists( $atts['taxonomy'] ) ) {
91100
return self::error(
92-
sprintf( __( 'Sorry, taxonomy "%s" is invalid. Valid options are: %s. Please check your shortcode implementation.', 'random-post-on-refresh' ),
101+
sprintf(
102+
// Translators: %1$s is replaced with taxonomy shortcode argument and %2$s is replaced with a comma-separated list of available taxonomies.
103+
__( 'Sorry, taxonomy "%1$s" is invalid. Valid options are: %2$s. Please check your shortcode implementation.', 'random-post-on-refresh' ),
93104
$atts['taxonomy'],
94105
implode( ', ', get_taxonomies() )
95106
),
@@ -100,14 +111,14 @@ public static function shortcode( $atts ) {
100111
// Taxonomy/term attribute validation
101112
if ( ! empty( $atts['terms'] ) && empty( $atts['taxonomy'] ) ) {
102113
return self::error(
103-
sprintf( __( 'Sorry, you cannot use the terms attribute without the taxonomy attribute. Please check your shortcode implementation.', 'random-post-on-refresh' ), $post_type ),
114+
__( 'Sorry, you cannot use the terms attribute without the taxonomy attribute. Please check your shortcode implementation.', 'random-post-on-refresh' ),
104115
'[' . self::SHORTCODE . ' terms="' . $atts['terms'] . '"]'
105116
);
106117
}
107118

108119
if ( empty( $atts['terms'] ) && ! empty( $atts['taxonomy'] ) ) {
109120
return self::error(
110-
sprintf( __( 'Sorry, you cannot use the taxonomy attribute without the terms attribute. Please check your shortcode implementation.', 'random-post-on-refresh' ), $post_type ),
121+
__( 'Sorry, you cannot use the taxonomy attribute without the terms attribute. Please check your shortcode implementation.', 'random-post-on-refresh' ),
111122
'[' . self::SHORTCODE . ' taxonomy="' . $atts['taxonomy'] . '"]'
112123
);
113124
}
@@ -119,7 +130,8 @@ public static function shortcode( $atts ) {
119130
if ( ! post_type_exists( $post_type ) ) {
120131
return self::error(
121132
sprintf(
122-
__( 'Sorry, post type "%s" is invalid. Valid options are: %s. Please check your shortcode implementation.', 'random-post-on-refresh' ),
133+
// Translators: %1$s is replaced with post_type shortcode argument and %2$s is replaced with a comma-separated list of available post types.
134+
__( 'Sorry, post type "%1$s" is invalid. Valid options are: %2$s. Please check your shortcode implementation.', 'random-post-on-refresh' ),
123135
$post_type,
124136
implode( ', ', get_post_types( [ 'public' => true ] ) )
125137
),
@@ -153,7 +165,7 @@ public static function shortcode( $atts ) {
153165
$terms = self::parse_id_list( $atts['terms'] );
154166
if ( 'category' === $atts['taxonomy'] ) {
155167
$query_args['category__in'] = $terms;
156-
} else if ( 'post_tag' === $atts['taxonomy'] ) {
168+
} elseif ( 'post_tag' === $atts['taxonomy'] ) {
157169
$query_args['tag__in'] = $terms;
158170
} else {
159171
$query_args['tax_query'] = [
@@ -167,6 +179,9 @@ public static function shortcode( $atts ) {
167179
$query_args['meta_query'] = [ [ 'key' => '_thumbnail_id' ] ];
168180
}
169181

182+
// Never load the current post.
183+
$query_args['post__not_in'][] = get_the_ID();
184+
170185
$query = new WP_Query( $query_args );
171186

172187
if ( ! $query->have_posts() ) {
@@ -178,6 +193,8 @@ public static function shortcode( $atts ) {
178193
$posts = $query->posts;
179194

180195
/**
196+
* The randomly selected post.
197+
*
181198
* @var WP_Post $post
182199
*/
183200
$post = $posts[ array_rand( $posts ) ];
@@ -218,10 +235,17 @@ public static function shortcode( $atts ) {
218235

219236
return sprintf(
220237
'<div class="random-post-on-refresh %s"><a href="%s">%s</a></div>',
221-
esc_attr( implode( ' ', array_filter( [
222-
count( $groups ) > 1 ? '--has-groups' : '',
223-
$atts['class']
224-
] ) ) ),
238+
esc_attr(
239+
implode(
240+
' ',
241+
array_filter(
242+
[
243+
count( $groups ) > 1 ? '--has-groups' : '',
244+
$atts['class'],
245+
]
246+
)
247+
)
248+
),
225249
esc_url( get_the_permalink( $post ) ),
226250
implode( '', array_filter( $display ) )
227251
);
@@ -230,7 +254,7 @@ public static function shortcode( $atts ) {
230254
/**
231255
* Parse an ID list into an array.
232256
*
233-
* @param string $list
257+
* @param string $list A comma separated list of IDs
234258
*
235259
* @return int[]
236260
*/
@@ -246,8 +270,8 @@ public static function parse_id_list( $list ) {
246270
/**
247271
* Convert a list (string) to an array
248272
*
249-
* @param string $list
250-
* @param string $delimiter
273+
* @param string $list A delimiter separated list of items
274+
* @param string $delimiter The delimiter used to separate items.
251275
*
252276
* @return array
253277
*/
@@ -258,7 +282,7 @@ public static function list_to_array( $list, $delimiter = ',' ) {
258282
/**
259283
* Get the excerpt for a specific post (outside of the loop).
260284
*
261-
* @param WP_Post $post
285+
* @param WP_Post $post The WordPress post.
262286
*
263287
* @return string
264288
*/
@@ -271,9 +295,9 @@ public static function get_the_excerpt( WP_Post $post ) {
271295
/**
272296
* Setup error message.
273297
*
274-
* @param string $message
298+
* @param string $message The error message to display.
275299
*
276-
* @param string $example
300+
* @param string $example (optional) An example shortcode usage.
277301
*
278302
* @return string
279303
*/
@@ -298,4 +322,4 @@ public static function error( $message, $example = '' ) {
298322

299323
RandomPostOnRefresh::initialize();
300324

301-
}
325+
}

assets/random-post-on-refresh.css

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.random-post-on-refresh {
2-
margin: 10px 0;
2+
margin-top: 10px;
3+
margin-bottom: 10px;
34
}
45

56
.random-post-on-refresh a,
@@ -51,4 +52,4 @@
5152
.random-post-on-refresh-error p:last-of-type {
5253
margin-bottom: 0;
5354
padding-bottom: 0;
54-
}
55+
}

readme.txt

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
=== Random Post on Refresh ===
2-
Contributors: woodent, Imzodigital
2+
Contributors: woodent, wpscholar, Imzodigital
33
Donate link: https://www.paypal.me/wpscholar
44
Tags: random post, post rotation, different post
55
Requires at least: 4.5
6-
Tested up to: 4.9.4
7-
Stable tag: 1.0
6+
Tested up to: 5.0
7+
Stable tag: 1.1
88
License: GPLv3
99
License URI: http://www.gnu.org/licenses/gpl-3.0.html
1010

@@ -77,9 +77,20 @@ Keep in mind that any of these attributes can be combined as needed. Example: `
7777

7878
== Changelog ==
7979

80+
= 1.1 =
81+
82+
* Ensure code meets coding standards.
83+
* Don't allow the current post to be selected as the random post.
84+
* Always return the last 100 matching items and select the random post from one of those.
85+
* Tested in WordPress 5.0
86+
8087
= 1.0 =
8188

8289
* Tested in WordPress version 4.9.2
8390

8491
== Upgrade Notice ==
8592

93+
= 1.1 =
94+
95+
* The latest update pulls from a larger selection of matching posts before selecting a random post. Works with WordPress 5.0.
96+

0 commit comments

Comments
 (0)