Skip to content

Commit

Permalink
Random Redirect: add module to jetpack-mu-wpcom package (#38374)
Browse files Browse the repository at this point in the history
* Random Redirect: add to jetpack-mu-wpcom package for simple sites

* Prevent Phan warning

* Fix wrong hook
  • Loading branch information
monsieur-z authored Jul 18, 2024
1 parent 15f7929 commit c5dfe76
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add Random Redirect module for simple sites
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public static function init() {
add_action( 'plugins_loaded', array( __CLASS__, 'load_verbum_comments' ) );
add_action( 'wp_loaded', array( __CLASS__, 'load_verbum_comments_admin' ) );
add_action( 'admin_menu', array( __CLASS__, 'load_wpcom_simple_odyssey_stats' ) );
add_action( 'plugins_loaded', array( __CLASS__, 'load_wpcom_random_redirect' ) );
}

// These features run only on atomic sites.
Expand Down Expand Up @@ -357,4 +358,11 @@ public static function load_custom_css() {
require_once __DIR__ . '/features/custom-css/custom-css/preprocessors.php';
require_once __DIR__ . '/features/custom-css/custom-css.php';
}

/**
* Load the Random Redirect feature.
*/
public static function load_wpcom_random_redirect() {
require_once __DIR__ . '/features/random-redirect/random-redirect.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php
/**
* Plugin Name: Random Redirect
* Plugin URI: https://wordpress.org/extend/plugins/random-redirect/
* 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.
* Version: 1.2-wpcom
* Author: Matt Mullenweg
* Author URI: https://ma.tt/
* Text Domain: jetpack
*
* @package automattic/jetpack
*/

// phpcs:disable WordPress.Security.NonceVerification -- No changes to the site here, it just redirects.

/**
* Redirects to a random post on the site.
*/
function jetpack_matt_random_redirect() {
// Verify that the Random Redirect plugin this code is from is not active
// See https://plugins.trac.wordpress.org/ticket/1898
if ( ! ( defined( 'IS_WPCOM' ) && IS_WPCOM ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
if ( is_plugin_active( 'random-redirect/random-redirect.php' ) ) {
return;
}
}

// Acceptable URL formats: /[...]/?random=[post type], /?random, /&random, /&random=1
if ( ! isset( $_GET['random'] ) && ! ( isset( $_SERVER['REQUEST_URI'] ) && in_array( strtolower( $_SERVER['REQUEST_URI'] ), array( '/&random', '/&random=1' ), true ) ) ) {
return;
}

// Ignore POST requests.
if ( ! empty( $_POST ) ) {
return;
}

// Persistent AppEngine abuse. ORDER BY RAND is expensive.
if ( isset( $_SERVER['HTTP_USER_AGENT'] ) && strstr( filter_var( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ), 'AppEngine-Google' ) ) {
wp_die( 'Please <a href="https://en.support.wordpress.com/contact/" rel="noopener noreferrer" target="_blank">contact support</a>' );
}

$where = array(
"post_password = ''",
"post_status = 'publish'",
);
$where_args = array();

// Set default post type.
$post_type = get_post_type();

// Change the post type if the parameter is set.
if ( isset( $_GET['random_post_type'] ) && post_type_exists( sanitize_key( $_GET['random_post_type'] ) ) ) {
$post_type = sanitize_key( $_GET['random_post_type'] );
}

// Don't show a random page if 'page' isn't specified as the post type specifically.
if ( 'page' === $post_type && is_front_page() && ! isset( $_GET['random_post_type'] ) ) {
$post_type = 'post';
}

$where[] = 'p.post_type = %s';
$where_args[] = $post_type;

// Set author name if we're on an author archive.
if ( is_author() ) {
$where[] = 'post_author = %s';
$where_args[] = get_the_author_meta( 'ID' );
}

// Set default category type
if ( is_category() ) {
$category = get_the_category();
if ( isset( $category ) && ! empty( $category ) ) {
$random_cat_id = $category[0]->term_id;
}
}

// Set the category ID if the parameter is set.
if ( isset( $_GET['random_cat_id'] ) ) {
$random_cat_id = (int) $_GET['random_cat_id'];
}

global $wpdb;

$where = implode( ' AND ', $where );
if ( isset( $random_cat_id ) ) {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber
$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 ) );
} else {
// phpcs:ignore WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
$random_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts AS p WHERE $where ORDER BY RAND() LIMIT 1", ...$where_args ) );
}

// @phan-suppress-next-line PhanTypeMismatchArgument
$permalink = get_permalink( $random_id );
wp_safe_redirect( $permalink );
exit;
}

add_action( 'template_redirect', 'jetpack_matt_random_redirect' );

0 comments on commit c5dfe76

Please sign in to comment.