PluginProbe
Redirection / 5.0
Redirection v5.0
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / actions / random.php

random.php in Redirection 5.0, at actions/random.php

43 lines 769 B
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 require_once dirname( __FILE__ ) . '/url.php';
4
5 /**
6 * URL action - redirect to a URL
7 */
8 class Random_Action extends Url_Action {
9 /**
10 * Get a random URL
11 *
12 * @return string|null
13 */
14 private function get_random_url() {
15 // Pick a random WordPress page
16 global $wpdb;
17
18 $id = $wpdb->get_var( "SELECT ID FROM {$wpdb->prefix}posts WHERE post_status='publish' AND post_password='' AND post_type='post' ORDER BY RAND() LIMIT 0,1" );
19 if ( $id ) {
20 $url = get_permalink( $id );
21
22 if ( $url ) {
23 return $url;
24 }
25 }
26
27 return null;
28 }
29
30 /**
31 * Run this action. May not return from this function.
32 *
33 * @return void
34 */
35 public function run() {
36 $target = $this->get_random_url();
37
38 if ( $target ) {
39 $this->redirect_to( $target );
40 }
41 }
42 }
43