Skip to content

Commit 584bfda

Browse files
committed
Latest updates. Enabled shortcode attribute to allow posts without images to be shown. Translation updates.
1 parent 6a130e9 commit 584bfda

File tree

3 files changed

+143
-19
lines changed

3 files changed

+143
-19
lines changed

RandomPostOnRefresh.php

Lines changed: 27 additions & 17 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: https://wpscholar.com
9-
* Version: 1.1.1
9+
* Version: 1.2
1010
* Text Domain: random-post-on-refresh
1111
* License: GPL3
1212
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
@@ -53,16 +53,17 @@ public static function shortcode( $atts ) {
5353

5454
$atts = shortcode_atts(
5555
array(
56-
'author' => '',
57-
'ids' => '',
58-
'not' => '',
59-
'post_type' => 'post',
60-
'search' => '',
61-
'taxonomy' => '',
62-
'terms' => '',
63-
'class' => '',
64-
'size' => 'large',
65-
'show' => 'title, image, excerpt',
56+
'author' => '',
57+
'class' => '',
58+
'ids' => '',
59+
'image_required' => 'true',
60+
'not' => '',
61+
'post_type' => 'post',
62+
'search' => '',
63+
'show' => 'title, image, excerpt',
64+
'size' => 'large',
65+
'taxonomy' => '',
66+
'terms' => '',
6667
),
6768
array_change_key_case( array_filter( (array) $atts ), CASE_LOWER ),
6869
self::SHORTCODE
@@ -87,6 +88,8 @@ function ( $group ) {
8788
$show_excerpt = in_array( 'excerpt', $show, true );
8889
$show_content = in_array( 'content', $show, true );
8990

91+
$image_required = wp_validate_boolean( $atts['image_required'] );
92+
9093
// Check for featured image support
9194
if ( $show_image && ! current_theme_supports( 'post-thumbnails' ) ) {
9295
return self::error(
@@ -175,7 +178,8 @@ function ( $group ) {
175178
}
176179
}
177180

178-
if ( $show_image ) {
181+
// Only fetch posts with images?
182+
if ( $show_image && $image_required ) {
179183
$query_args['meta_query'] = [ [ 'key' => '_thumbnail_id' ] ];
180184
}
181185

@@ -186,7 +190,8 @@ function ( $group ) {
186190

187191
if ( ! $query->have_posts() ) {
188192
return self::error(
189-
__( 'Sorry, no matching posts were found. Your query may be too restrictive. Please check your shortcode implementation.', 'random-post-on-refresh' )
193+
__( 'Sorry, no matching posts were found. Your query may be too restrictive. Please check your shortcode implementation.', 'random-post-on-refresh' ) .
194+
( $image_required ? ' ' . __( 'Currently, only posts with featured images will be shown. Perhaps try setting the "image_required" property to "false"?', 'random-post-on-refresh' ) : '' )
190195
);
191196
}
192197

@@ -199,7 +204,7 @@ function ( $group ) {
199204
*/
200205
$post = $posts[ array_rand( $posts ) ];
201206

202-
if ( $show_image && ! has_post_thumbnail( $post ) ) {
207+
if ( $show_image && $image_required && ! has_post_thumbnail( $post ) ) {
203208
return self::error(
204209
__( 'Sorry, the selected post does not have a featured image.', 'random-post-on-refresh' )
205210
);
@@ -217,7 +222,7 @@ function ( $group ) {
217222
$display['title'] = $show_title ? sprintf( '<span class="random-post-on-refresh__title">%s</span>', esc_html( get_the_title( $post ) ) ) : '';
218223
break;
219224
case 'image':
220-
$display['image'] = $show_image ? sprintf( '<span class="random-post-on-refresh__image">%s</span>', get_the_post_thumbnail( $post, $image_size ) ) : '';
225+
$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 ) ) : '';
221226
break;
222227
case 'excerpt':
223228
$display['excerpt'] = $show_excerpt ? sprintf( '<span class="random-post-on-refresh__excerpt">%s</span>', self::get_the_excerpt( $post ) ) : '';
@@ -304,10 +309,15 @@ public static function error( $message, $example = '' ) {
304309
if ( current_user_can( 'edit_posts', get_the_ID() ) ) {
305310

306311
return sprintf(
307-
'<div class="random-post-on-refresh-error"><p>%s</p>%s<p>%s</p></div>',
312+
'<div class="random-post-on-refresh-error"><p>%1$s</p>%2$s<p>%3$s</p><p>%4$s</p></div>',
308313
esc_html( $message ),
309314
empty( $example ) ? '' : '<p>' . esc_html( $example ) . '</p>',
310-
esc_html( 'Note: This helpful notification is only visible to logged in users who can edit this shortcode.' )
315+
sprintf(
316+
'<a href="%1$s" target="_blank" rel="noreferrer noopener">%2$s</a>',
317+
'https://wordpress.org/plugins/random-post-on-refresh/#faq-header',
318+
__( 'Consult the documentation', 'random-post-on-refresh' )
319+
),
320+
esc_html__( 'Note: This helpful notification is only visible to logged in users who can edit this shortcode.', 'random-post-on-refresh' )
311321
);
312322

313323
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#, fuzzy
2+
msgid ""
3+
msgstr ""
4+
"Project-Id-Version: Random Post on Refresh\n"
5+
"Report-Msgid-Bugs-To: \n"
6+
"POT-Creation-Date: 2019-09-23 16:12+0000\n"
7+
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
8+
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
9+
"Language-Team: \n"
10+
"Language: \n"
11+
"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n"
12+
"MIME-Version: 1.0\n"
13+
"Content-Type: text/plain; charset=UTF-8\n"
14+
"Content-Transfer-Encoding: 8bit\n"
15+
"X-Generator: Loco https://localise.biz/\n"
16+
"X-Loco-Version: 2.3.0; wp-5.2.3"
17+
18+
#: RandomPostOnRefresh.php:318
19+
msgid "Consult the documentation"
20+
msgstr ""
21+
22+
#: RandomPostOnRefresh.php:194
23+
msgid ""
24+
"Currently, only posts with featured images will be shown. Perhaps try "
25+
"setting the \"image_required\" property to \"false\"?"
26+
msgstr ""
27+
28+
#. URI of the plugin
29+
msgid "http://wpscholar.com/wordpress-plugins/random-post-on-refresh/"
30+
msgstr ""
31+
32+
#. Author URI of the plugin
33+
msgid "https://wpscholar.com"
34+
msgstr ""
35+
36+
#. Author of the plugin
37+
msgid "Micah Wood"
38+
msgstr ""
39+
40+
#: RandomPostOnRefresh.php:320
41+
msgid ""
42+
"Note: This helpful notification is only visible to logged in users who can "
43+
"edit this shortcode."
44+
msgstr ""
45+
46+
#. Name of the plugin
47+
msgid "Random Post on Refresh"
48+
msgstr ""
49+
50+
#. Description of the plugin
51+
msgid "Show a random post on every page load."
52+
msgstr ""
53+
54+
#: RandomPostOnRefresh.php:193 svn/trunk/RandomPostOnRefresh.php:189
55+
msgid ""
56+
"Sorry, no matching posts were found. Your query may be too restrictive. "
57+
"Please check your shortcode implementation."
58+
msgstr ""
59+
60+
#. %1$s is replaced with post_type shortcode argument and %2$s is replaced with a comma-separated list of available post types.
61+
#: RandomPostOnRefresh.php:137 svn/trunk/RandomPostOnRefresh.php:134
62+
#, php-format
63+
msgid ""
64+
"Sorry, post type \"%1$s\" is invalid. Valid options are: %2$s. Please check "
65+
"your shortcode implementation."
66+
msgstr ""
67+
68+
#. %1$s is replaced with taxonomy shortcode argument and %2$s is replaced with a comma-separated list of available taxonomies.
69+
#: RandomPostOnRefresh.php:106 svn/trunk/RandomPostOnRefresh.php:103
70+
#, php-format
71+
msgid ""
72+
"Sorry, taxonomy \"%1$s\" is invalid. Valid options are: %2$s. Please check "
73+
"your shortcode implementation."
74+
msgstr ""
75+
76+
#: svn/tags/1.0/RandomPostOnRefresh.php:92
77+
#, php-format
78+
msgid ""
79+
"Sorry, taxonomy \"%s\" is invalid. Valid options are: %s. Please check your "
80+
"shortcode implementation."
81+
msgstr ""
82+
83+
#: RandomPostOnRefresh.php:209 svn/trunk/RandomPostOnRefresh.php:204
84+
msgid "Sorry, the selected post does not have a featured image."
85+
msgstr ""
86+
87+
#: RandomPostOnRefresh.php:124 svn/trunk/RandomPostOnRefresh.php:121
88+
msgid ""
89+
"Sorry, you cannot use the taxonomy attribute without the terms attribute. "
90+
"Please check your shortcode implementation."
91+
msgstr ""
92+
93+
#: RandomPostOnRefresh.php:117 svn/trunk/RandomPostOnRefresh.php:114
94+
msgid ""
95+
"Sorry, you cannot use the terms attribute without the taxonomy attribute. "
96+
"Please check your shortcode implementation."
97+
msgstr ""
98+
99+
#: RandomPostOnRefresh.php:96 svn/trunk/RandomPostOnRefresh.php:93
100+
msgid ""
101+
"Sorry, your theme does not support featured images. Update the \"show\" "
102+
"attribute to exclude the \"image\" option."
103+
msgstr ""

readme.txt

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

@@ -57,6 +57,8 @@ The `[random_post_on_refresh]` shortcode supports a few attributes to give you m
5757

5858
* **ids** - Provide a comma-separated list of post IDs to pull random posts from. Example: `[random_post_on_refresh ids="19, 87, 113, 997"]`
5959

60+
* **image_required** - Only applies when the `show` attribute contains `image`. This attribute defaults to `true` and will cause WordPress to filter out any posts without images. If set to `false`, then posts with or without images will be shown. Example: `[random_post_on_refresh show="title, image" image_required="false"]`
61+
6062
* **not** - Provide a comma-separated list of post IDs to exclude. Example: `[random_post_on_refresh not="3, 456, 876"]`
6163

6264
* **post_type** - Provide a post type or a comma-separated list of post types to pull from. You must use the internal post type name. Default is `post`. Example: `[random_post_on_refresh post_type="page"]`
@@ -77,6 +79,11 @@ Keep in mind that any of these attributes can be combined as needed. Example: `
7779

7880
== Changelog ==
7981

82+
= 1.2 =
83+
84+
* Added a shortcode attribute to allow posts without images to be shown when the `show` attribute contains `image`.
85+
* Bug fix for certain strings not being translatable.
86+
8087
= 1.1.1 =
8188

8289
* Fixed bug where post excerpts were being generated incorrectly.
@@ -102,3 +109,7 @@ Keep in mind that any of these attributes can be combined as needed. Example: `
102109

103110
* Bug fix for how post excerpts are generated. Tested with WordPress 5.2.3.
104111

112+
= 1.2 =
113+
114+
* Minor bug fixes and improvements.
115+

0 commit comments

Comments
 (0)