Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 1
100.00% covered (success)
100.00%
1 / 1
CRAP
n/a
0 / 0
jetpack_matt_random_redirect
n/a
0 / 0
n/a
0 / 0
21
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 * @deprecated $$next-version$$ WordPress 7.2 and later handle random content redirects in core, and this loads only on older versions.
20 */
21function jetpack_matt_random_redirect() {
22    // Verify that the Random Redirect plugin this code is from is not active
23    // See https://plugins.trac.wordpress.org/ticket/1898
24    if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
25        require_once ABSPATH . 'wp-admin/includes/plugin.php';
26        if ( is_plugin_active( 'random-redirect/random-redirect.php' ) ) {
27            return;
28        }
29    }
30
31    // Acceptable URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1
32    if ( ! isset( $_GET['random'] ) && ! ( isset( $_SERVER['REQUEST_URI'] ) && in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ), true ) ) ) {
33        return;
34    }
35
36    // Ignore POST requests.
37    if ( ! empty( $_POST ) ) {
38        return;
39    }
40
41    // Persistent AppEngine abuse.  ORDER BY RAND is expensive.
42    if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && strstr( filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 'AppEngine-Google' ) ) {
43        wp_die( 'Please <a href="https://en.support.wordpress.com/contact/" rel="noopener noreferrer" target="_blank">contact support</a>' );
44    }
45
46    $where      = array(
47        "post_password = ''",
48        "post_status = 'publish'",
49    );
50    $where_args = array();
51
52    // Set default post type.
53    $post_type = get_post_type();
54
55    // Change the post type if the parameter is set.
56    if ( isset( $_GET['random_post_type'] ) && post_type_exists( sanitize_key( $_GET['random_post_type'] ) ) ) {
57        $post_type = sanitize_key( $_GET['random_post_type'] );
58    }
59
60    // Don't show a random page if 'page' isn't specified as the post type specifically.
61    if ( 'page' === $post_type && is_front_page() && ! isset( $_GET['random_post_type'] ) ) {
62        $post_type = 'post';
63    }
64
65    $where[]      = 'p.post_type = %s';
66    $where_args[] = $post_type;
67
68    // Set author name if we're on an author archive.
69    if ( is_author() ) {
70        $where[]      = 'post_author = %s';
71        $where_args[] = get_the_author_meta( 'ID' );
72    }
73
74    // Set default category type
75    if ( is_category() ) {
76        $category = get_the_category();
77        if ( isset( $category ) && ! empty( $category ) ) {
78            $random_cat_id = $category[0]->term_id;
79        }
80    }
81
82    // Set the category ID if the parameter is set.
83    if ( isset( $_GET['random_cat_id'] ) ) {
84        $random_cat_id = (int) $_GET['random_cat_id'];
85    }
86
87    global $wpdb;
88
89    $where = implode( ' AND ', $where );
90    if ( isset( $random_cat_id ) ) {
91        // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
92        $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 $where ORDER BY RAND() LIMIT 1", $random_cat_id, ...$where_args ) );
93    } else {
94        // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
95        $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts AS p WHERE $where ORDER BY RAND() LIMIT 1", ...$where_args ) );
96    }
97
98    // @phan-suppress-next-line PhanTypeMismatchArgument
99    $permalink = get_permalink( $random_id );
100    wp_safe_redirect( $permalink );
101    exit( 0 );
102}
103
104add_action( 'template_redirect', 'jetpack_matt_random_redirect' );