compat
2 months ago
content-options
1 month ago
js
6 years ago
responsive-videos
1 year ago
site-logo
8 months ago
social-menu
2 months ago
content-options.php
8 months ago
featured-content.php
8 months ago
random-redirect.php
6 days ago
responsive-videos.php
8 months ago
site-breadcrumbs.php
1 year ago
site-logo.php
8 months ago
social-links.php
8 months ago
social-menu.php
8 months ago
random-redirect.php
131 lines
| 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: https://ma.tt/ |
| 9 | * Text Domain: jetpack |
| 10 | * |
| 11 | * @package automattic/jetpack |
| 12 | */ |
| 13 | |
| 14 | // phpcs:disable WordPress.Security.NonceVerification -- No changes to the site here, it just redirects. |
| 15 | |
| 16 | /** |
| 17 | * Redirects to a random post on the site. |
| 18 | */ |
| 19 | function jetpack_matt_random_redirect() { |
| 20 | /** |
| 21 | * Allows disabling the random redirect feature. |
| 22 | * |
| 23 | * @since 16.1 |
| 24 | * |
| 25 | * @param bool $enabled Whether the random redirect feature is enabled. Default true. |
| 26 | */ |
| 27 | if ( ! apply_filters( 'jetpack_random_redirect_enabled', true ) ) { |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | // Verify that the Random Redirect plugin this code is from is not active |
| 32 | // See https://plugins.trac.wordpress.org/ticket/1898 |
| 33 | if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) { |
| 34 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
| 35 | if ( is_plugin_active( 'random-redirect/random-redirect.php' ) ) { |
| 36 | return; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // Acceptable URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1 |
| 41 | if ( ! isset( $_GET['random'] ) && ! ( isset( $_SERVER['REQUEST_URI'] ) && in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ), true ) ) ) { |
| 42 | return; |
| 43 | } |
| 44 | |
| 45 | // Ignore POST requests. |
| 46 | if ( ! empty( $_POST ) ) { |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | // Persistent AppEngine abuse. ORDER BY RAND is expensive. |
| 51 | if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && strstr( filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 'AppEngine-Google' ) ) { |
| 52 | wp_die( 'Please <a href="https://en.support.wordpress.com/contact/" rel="noopener noreferrer" target="_blank">contact support</a>' ); |
| 53 | } |
| 54 | |
| 55 | $where = array( |
| 56 | "post_password = ''", |
| 57 | "post_status = 'publish'", |
| 58 | ); |
| 59 | $where_args = array(); |
| 60 | |
| 61 | // Set default post type. |
| 62 | $post_type = get_post_type(); |
| 63 | |
| 64 | // Change the post type if the parameter is set. |
| 65 | if ( isset( $_GET['random_post_type'] ) && post_type_exists( sanitize_key( $_GET['random_post_type'] ) ) ) { |
| 66 | $post_type = sanitize_key( $_GET['random_post_type'] ); |
| 67 | } |
| 68 | |
| 69 | // Don't show a random page if 'page' isn't specified as the post type specifically. |
| 70 | if ( 'page' === $post_type && is_front_page() && ! isset( $_GET['random_post_type'] ) ) { |
| 71 | $post_type = 'post'; |
| 72 | } |
| 73 | |
| 74 | $where[] = 'p.post_type = %s'; |
| 75 | $where_args[] = $post_type; |
| 76 | |
| 77 | // Set author name if we're on an author archive. |
| 78 | if ( is_author() ) { |
| 79 | $where[] = 'post_author = %s'; |
| 80 | $where_args[] = get_the_author_meta( 'ID' ); |
| 81 | } |
| 82 | |
| 83 | // Set default category type |
| 84 | if ( is_category() ) { |
| 85 | $category = get_the_category(); |
| 86 | if ( isset( $category ) && ! empty( $category ) ) { |
| 87 | $random_cat_id = $category[0]->term_id; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // Set the category ID if the parameter is set. |
| 92 | if ( isset( $_GET['random_cat_id'] ) ) { |
| 93 | $random_cat_id = (int) $_GET['random_cat_id']; |
| 94 | } |
| 95 | |
| 96 | global $wpdb; |
| 97 | |
| 98 | $where = implode( ' AND ', $where ); |
| 99 | |
| 100 | // Pick a post via COUNT plus a random OFFSET rather than ORDER BY RAND(), which randomizes and sorts every candidate row on each request. |
| 101 | if ( isset( $random_cat_id ) ) { |
| 102 | $from_where = "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 $where"; |
| 103 | $query_args = array_merge( array( $random_cat_id ), $where_args ); |
| 104 | } else { |
| 105 | $from_where = "FROM $wpdb->posts AS p WHERE $where"; |
| 106 | $query_args = $where_args; |
| 107 | } |
| 108 | |
| 109 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 110 | $post_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT( DISTINCT p.ID ) $from_where", ...$query_args ) ); |
| 111 | |
| 112 | if ( $post_count < 1 ) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | $query_args[] = wp_rand( 0, $post_count - 1 ); |
| 117 | // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare |
| 118 | $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT DISTINCT p.ID $from_where ORDER BY p.ID LIMIT 1 OFFSET %d", ...$query_args ) ); |
| 119 | |
| 120 | if ( ! $random_id ) { |
| 121 | return; |
| 122 | } |
| 123 | |
| 124 | // @phan-suppress-next-line PhanTypeMismatchArgument |
| 125 | $permalink = get_permalink( $random_id ); |
| 126 | wp_safe_redirect( $permalink ); |
| 127 | exit( 0 ); |
| 128 | } |
| 129 | |
| 130 | add_action( 'template_redirect', 'jetpack_matt_random_redirect' ); |
| 131 |