PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 12.5
Jetpack – WP Security, Backup, Speed, & Growth v12.5
16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 14.3.1 All 501 releases
jetpack / modules / theme-tools / random-redirect.php

random-redirect.php in Jetpack – WP Security, Backup, Speed, & Growth 12.5, at modules/theme-tools/random-redirect.php

102 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Verify that the Random Redirect plugin this code is from is not active
21 // See https://plugins.trac.wordpress.org/ticket/1898
22 if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
23 require_once ABSPATH . 'wp-admin/includes/plugin.php';
24 if ( is_plugin_active( 'random-redirect/random-redirect.php' ) ) {
25 return;
26 }
27 }
28
29 // Acceptable URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1
30 if ( ! isset( $_GET['random'] ) && ! ( isset( $_SERVER['REQUEST_URI'] ) && in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ), true ) ) ) {
31 return;
32 }
33
34 // Ignore POST requests.
35 if ( ! empty( $_POST ) ) {
36 return;
37 }
38
39 // Persistent AppEngine abuse. ORDER BY RAND is expensive.
40 if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && strstr( filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 'AppEngine-Google' ) ) {
41 wp_die( 'Please <a href="https://en.support.wordpress.com/contact/" rel="noopener noreferrer" target="_blank">contact support</a>' );
42 }
43
44 $where = array(
45 "post_password = ''",
46 "post_status = 'publish'",
47 );
48 $where_args = array();
49
50 // Set default post type.
51 $post_type = get_post_type();
52
53 // Change the post type if the parameter is set.
54 if ( isset( $_GET['random_post_type'] ) && post_type_exists( sanitize_key( $_GET['random_post_type'] ) ) ) {
55 $post_type = sanitize_key( $_GET['random_post_type'] );
56 }
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 $where[] = 'p.post_type = %s';
64 $where_args[] = $post_type;
65
66 // Set author name if we're on an author archive.
67 if ( is_author() ) {
68 $where[] = 'post_author = %s';
69 $where_args[] = get_the_author_meta( 'ID' );
70 }
71
72 // Set default category type
73 if ( is_category() ) {
74 $category = get_the_category();
75 if ( isset( $category ) && ! empty( $category ) ) {
76 $random_cat_id = $category[0]->term_id;
77 }
78 }
79
80 // Set the category ID if the parameter is set.
81 if ( isset( $_GET['random_cat_id'] ) ) {
82 $random_cat_id = (int) $_GET['random_cat_id'];
83 }
84
85 global $wpdb;
86
87 $where = implode( ' AND ', $where );
88 if ( isset( $random_cat_id ) ) {
89 // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
90 $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 ) );
91 } else {
92 // phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
93 $random_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts AS p WHERE $where ORDER BY RAND() LIMIT 1", ...$where_args ) );
94 }
95
96 $permalink = get_permalink( $random_id );
97 wp_safe_redirect( $permalink );
98 exit;
99 }
100
101 add_action( 'template_redirect', 'jetpack_matt_random_redirect' );
102