PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 7.2.1
Jetpack – WP Security, Backup, Speed, & Growth v7.2.1
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 14.4.2 All 500 releases
jetpack / modules / theme-tools / random-redirect.php
random-redirect.php
84 lines 3.1 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: 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' ) ) {
17 return;
18 }
19 }
20
21 // Set default post type.
22 $post_type = get_post_type();
23
24 // Set default category type
25 if ( is_category() ) {
26 $category = get_the_category();
27 if ( isset( $category ) && ! empty( $category ) ) {
28 $random_cat_id = $category[0]->term_id;
29 }
30 }
31
32 // Set author name if we're on an author archive.
33 if ( is_author() ) {
34 $random_author_name = get_the_author_meta( 'user_login' );
35 $random_author_query = 'AND user_login = "' . $random_author_name . '"';
36 } else {
37 $random_author_query = '';
38 }
39
40 // Acceptable URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1
41 if ( ! isset( $_GET['random'] ) && ! in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ) ) ) {
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 ( strstr( $_SERVER['HTTP_USER_AGENT'], 'AppEngine-Google' ) ) {
52 wp_die( 'Please <a href="http://en.support.wordpress.com/contact/" rel="noopener noreferrer" target="_blank">contact support</a>' );
53 }
54
55 // Set the category ID if the parameter is set.
56 if ( isset( $_GET['random_cat_id'] ) ) {
57 $random_cat_id = (int) $_GET['random_cat_id'];
58 }
59
60 // Change the post type if the parameter is set.
61 if ( isset( $_GET['random_post_type'] ) && post_type_exists( $_GET['random_post_type'] ) ) {
62 $post_type = $_GET['random_post_type'];
63 }
64
65 // Don't show a random page if 'page' isn't specified as the post type specifically.
66 if ( 'page' === $post_type && is_front_page() && ! isset( $_GET['random_post_type'] ) ) {
67 $post_type = 'post';
68 }
69
70 global $wpdb;
71
72 if ( isset( $random_cat_id ) ) {
73 $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 ) );
74 } else {
75 $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 ) );
76 }
77
78 $permalink = get_permalink( $random_id );
79 wp_safe_redirect( $permalink );
80 exit;
81 }
82
83 add_action( 'template_redirect', 'jetpack_matt_random_redirect' );
84