Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added additional random parameters #877

Merged
merged 2 commits into from
Aug 8, 2014
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions modules/theme-tools/random-redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down