Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Added additional random parameters #877

Merged
merged 2 commits into from

5 participants

iannuttall Ben Lobaugh Enej Bajgoric George Stephanis Jeremy Herve
iannuttall
Owner

Added random_cat_id and random_post_type parameters to match the functionality of the original Random Redirects plugin.

iannuttall iannuttall Added additional random parameters
Added random_cat_id and random_post_type parameters to match the functionality of the original Random Redirects plugin.
a9f1f4f
George Stephanis

cc: @enejb @blobaugh -- could one / both of you read this through and vet it?

@iannuttall -- should be fine to pull request, and we can test it more easily there.

Should line 23 be an or?

This looks cleaner than it was before to me.

FYI the LIMIT in the sql is specific to MySQL and will break on other sql servers if they do not have good parsing and translation of the sql calls.

Owner

On line 50 we should prepend the table name for clarity. post_type => p.post_type.

Other suggestion would be to use is_post_type_archive() instead of ! is_front_page()
Use function post_type_exists() to check if the post type being passed via the url $_GET['random'] or $_GET['random_post_type']
is actually a valid post type instead of line 40.

Another suggestion would be to be able to use the existing url structure to get the query parameters. This way we don't reinvent the wheel. The idea is that you take any archive url and add ?random and are redirected to one a single post based that would have been part of that archive list.
Since the function is being added to template redirect. We have already fetched the posts once. If we are don't have any pagination we can just use php to pick a random number and redirect the user there.

This would also allow us to have have more flexibility not just be restricted to categories and post types but any type of custom taxonomy, authors, dates.

Ben Lobaugh blobaugh added this to the 3.2 milestone
Ben Lobaugh

@iannuttall could you please check over the feedback and update the PR?

iannuttall
Owner

@blobaugh yeah, will take a look this weekend and see what I can do :)

iannuttall
Owner

@blobaugh:

Should line 23 be an or?

It was && in the original version. I tried with an or, but it doesn't fire without &&.

FYI the LIMIT in the sql is specific to MySQL and will break on other sql servers if they do not have
good parsing and translation of the sql calls.

I'm not sure what to change if anything for this. The query was already in the original file. :/

@enejb:

On line 50 we should prepend the table name for clarity. post_type => p.post_type.

Done!

Other suggestion would be to use is_post_type_archive() instead of ! is_front_page()
Use function post_type_exists() to check if the post type being passed via the url $_GET['random'] or $_GET['random_post_type']
is actually a valid post type instead of line 40.

Instead of setting the default to be 'post' I changed it to be get_post_type(); instead. In the event of the $_GET['random_post_type'] I changed it to use post_type_exists().

Another suggestion would be to be able to use the existing url structure to get the query parameters. This way we don't reinvent the wheel. The idea is that you take any archive url and add ?random and are redirected to one a single post based that would have been part of that archive list.

You should be able to use &random on any category or author archive pages now without having to specify the category ID.

Since the function is being added to template redirect. We have already fetched the posts once. If we are don't have any pagination we can just use php to pick a random number and redirect the user there.

I wasn't really sure what you meant here, so I didn't action anything.

This would also allow us to have have more flexibility not just be restricted to categories and post types but any type of custom taxonomy, authors, dates.

This would be awesome to add, I'm just not 100% sure how to go about covering all the different types of archives programmatically.

Ben Lobaugh blobaugh merged commit 212a24c into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 19, 2014
  1. iannuttall

    Added additional random parameters

    iannuttall authored
    Added random_cat_id and random_post_type parameters to match the functionality of the original Random Redirects plugin.
Commits on Aug 7, 2014
  1. iannuttall

    Added additional random parameters Fixes #867

    iannuttall authored
    Made changes as per @enejb's comments.
This page is out of date. Refresh to see the latest.
Showing with 31 additions and 14 deletions.
  1. +31 −14 modules/theme-tools/random-redirect.php
45 modules/theme-tools/random-redirect.php
View
@@ -16,32 +16,49 @@ function jetpack_matt_random_redirect() {
if ( is_plugin_active( 'random-redirect/random-redirect.php' ) ) return;
}
- // Acceptables URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1
+ // Set default post type.
+ $post_type = get_post_type();
+
+ // Set default category type
+ if ( is_category() ) {
+ $category = get_the_category();
+ $random_cat_id = $category[0]->term_id;
+ }
+
+ // Set author name if we're on an author archive.
+ if ( is_author() ) {
+ $random_author_name = get_the_author_meta( 'user_login' );
+ $random_author_query = 'AND user_login = "' . $random_author_name . '"';
+ }
+
+ // Acceptable URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1
if ( ! isset( $_GET['random'] ) && ! in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ) ) )
return;
- // Ignore requests that include more than just the random parameter.
- if ( ! empty( $_POST ) || ( isset( $_GET['random'] ) && count( $_GET ) > 1 ) )
+ // Ignore POST requests.
+ if ( ! empty( $_POST ) )
return;
// Persistent AppEngine abuse. ORDER BY RAND is expensive.
if ( strstr( $_SERVER['HTTP_USER_AGENT'], 'AppEngine-Google' ) )
wp_die( 'Please <a href="http://en.support.wordpress.com/contact/">contact support</a>' );
- // /?random should always show a random post, even if the home page is a static page.
- if ( is_front_page() ) {
- $post_type = 'post';
- }
- else {
- // Use the post type of the current page as the context for the random lookup.
- $post_type = get_post_type();
+ // Set the category ID if the parameter is set.
+ if ( isset( $_GET['random_cat_id'] ) )
+ $random_cat_id = (int) $_GET['random_cat_id'];
- if ( ! $post_type )
- $post_type = 'post';
- }
+ // Change the post type if the parameter is set.
+ if ( isset( $_GET['random_post_type'] ) && post_type_exists( $_GET['random_post_type'] ) )
+ $post_type = $_GET['random_post_type'];
global $wpdb;
- $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_password = '' AND post_status = 'publish' ORDER BY RAND() LIMIT 1", $post_type ) );
+
+ if ( isset( $random_cat_id ) ) {
+ $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT ID FROM $wpdb->posts AS p INNER JOIN $wpdb->term_relationships AS tr ON (p.ID = tr.object_id AND tr.term_taxonomy_id = %s) INNER JOIN $wpdb->term_taxonomy AS tt ON(tr.term_taxonomy_id = tt.term_taxonomy_id AND taxonomy = 'category') WHERE p.post_type = %s AND post_password = '' AND post_status = 'publish' %s ORDER BY RAND() LIMIT 1", $random_cat_id, $post_type, $random_author_query ) );
+ } else {
+ $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_type = %s AND post_password = '' AND post_status = 'publish' %s ORDER BY RAND() LIMIT 1", $post_type, $random_author_query ) );
+ }
+
$permalink = get_permalink( $random_id );
wp_safe_redirect( $permalink );
exit;
Something went wrong with that request. Please try again.