random-redirect.php
| 1 | <?php |
| 2 | /* |
| 3 | Plugin Name: Random Redirect |
| 4 | Plugin URI: https://wordpress.org/extend/plugins/random-redirect/ |
| 5 | Description: Allows you to create a link to yourblog.example.com/?random which will redirect someone to a random post on your blog, in a StumbleUpon-like fashion. |
| 6 | Version: 1.2-wpcom |
| 7 | Author: Matt Mullenweg |
| 8 | Author URI: http://photomatt.net/ |
| 9 | */ |
| 10 | |
| 11 | function jetpack_matt_random_redirect() { |
| 12 | // Verify that the Random Redirect plugin this code is from is not active |
| 13 | // See http://plugins.trac.wordpress.org/ticket/1898 |
| 14 | if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
| 15 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 16 | if ( is_plugin_active( 'random-redirect/random-redirect.php' ) ) return; |
| 17 | } |
| 18 | |
| 19 | // Set default post type. |
| 20 | $post_type = get_post_type(); |
| 21 | |
| 22 | // Set default category type |
| 23 | if ( is_category() ) { |
| 24 | $category = get_the_category(); |
| 25 | if ( isset( $category ) && ! empty( $category ) ) { |
| 26 | $random_cat_id = $category[0]->term_id; |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | // Set author name if we're on an author archive. |
| 31 | if ( is_author() ) { |
| 32 | $random_author_name = get_the_author_meta( 'user_login' ); |
| 33 | $random_author_query = 'AND user_login = "' . $random_author_name . '"'; |
| 34 | } else { |
| 35 | $random_author_query = ''; |
| 36 | } |
| 37 | |
| 38 | // Acceptable URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1 |
| 39 | if ( ! isset( $_GET['random'] ) && ! in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ) ) ) |
| 40 | return; |
| 41 | |
| 42 | // Ignore POST requests. |
| 43 | if ( ! empty( $_POST ) ) |
| 44 | return; |
| 45 | |
| 46 | // Persistent AppEngine abuse. ORDER BY RAND is expensive. |
| 47 | if ( strstr( $_SERVER['HTTP_USER_AGENT'], 'AppEngine-Google' ) ) |
| 48 | wp_die( 'Please <a href="http://en.support.wordpress.com/contact/" rel="noopener noreferrer" target="_blank">contact support</a>' ); |
| 49 | |
| 50 | // Set the category ID if the parameter is set. |
| 51 | if ( isset( $_GET['random_cat_id'] ) ) |
| 52 | $random_cat_id = (int) $_GET['random_cat_id']; |
| 53 | |
| 54 | // Change the post type if the parameter is set. |
| 55 | if ( isset( $_GET['random_post_type'] ) && post_type_exists( $_GET['random_post_type'] ) ) |
| 56 | $post_type = $_GET['random_post_type']; |
| 57 | |
| 58 | // Don't show a random page if 'page' isn't specified as the post type specifically. |
| 59 | if ( 'page' === $post_type && is_front_page() && ! isset( $_GET['random_post_type'] ) ) { |
| 60 | $post_type = 'post'; |
| 61 | } |
| 62 | |
| 63 | global $wpdb; |
| 64 | |
| 65 | if ( isset( $random_cat_id ) ) { |
| 66 | $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 ) ); |
| 67 | } else { |
| 68 | $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 ) ); |
| 69 | } |
| 70 | |
| 71 | $permalink = get_permalink( $random_id ); |
| 72 | wp_safe_redirect( $permalink ); |
| 73 | exit; |
| 74 | } |
| 75 | |
| 76 | add_action( 'template_redirect', 'jetpack_matt_random_redirect' ); |
| 77 |