2016-12-07 20:40:16 +00:00
< ? php
2020-09-23 22:22:39 +00:00
/* This Source Code Form is subject to the terms of the Mozilla Public
* License , v . 2.0 . If a copy of the MPL was not distributed with this
* file , You can obtain one at https :// mozilla . org / MPL / 2.0 /. */
2016-12-07 20:40:16 +00:00
register_module ([
" name " => " Random Page " ,
2021-09-02 21:39:10 +00:00
" version " => " 0.3.1 " ,
2016-12-07 20:40:16 +00:00
" author " => " Starbeamrainbowlabs " ,
" description " => " Adds an action called 'random' that redirects you to a random page. " ,
" id " => " action-random " ,
" code " => function () {
global $settings ;
/**
2018-02-14 22:46:31 +00:00
* @ api { get } ? action = random [ & mode = { modeName }] Redirects to a random page
* @ apiName Random
2016-12-07 20:40:16 +00:00
* @ apiGroup Page
* @ apiPermission Anonymous
2018-02-14 22:36:29 +00:00
*
2018-02-14 22:46:31 +00:00
* @ apiParam { string } mode The view mode to redirect to . This parameter is basically just passed through to the direct . It works in the same way as the mode parameter on the view action does .
2016-12-07 20:40:16 +00:00
*/
add_action ( " random " , function () {
global $pageindex ;
2021-09-02 21:39:10 +00:00
$mode = slugify ( $_GET [ " mode " ] ? ? " " );
2018-02-14 22:36:29 +00:00
2016-12-07 20:40:16 +00:00
$pageNames = array_keys ( get_object_vars ( $pageindex ));
2017-10-25 22:00:04 +00:00
// Filter out pages we shouldn't send the user to
$pageNames = array_values ( array_filter ( $pageNames , function ( $pagename ) {
2018-07-01 12:33:27 +00:00
global $settings , $pageindex ;
if ( $settings -> random_page_exclude_redirects &&
isset ( $pageindex -> $pagename -> redirect ) &&
$pageindex -> $pagename -> redirect === true )
return false ;
2017-10-25 22:00:04 +00:00
return preg_match ( $settings -> random_page_exclude , $pagename ) === 0 ? true : false ;
}));
2016-12-07 20:40:16 +00:00
$randomPageName = $pageNames [ array_rand ( $pageNames )];
http_response_code ( 307 );
2018-02-14 22:36:29 +00:00
$redirect_url = " ?page= " . rawurlencode ( $randomPageName );
if ( ! empty ( $mode )) $redirect_url .= " &mode= $mode " ;
header ( " location: $redirect_url " );
2016-12-07 20:40:16 +00:00
});
2017-01-02 21:25:18 +00:00
add_help_section ( " 26-random-redirect " , " Jumping to a random page " , " <p> $settings->sitename has a function that can send you to a random page. To use it, click <a href='?action=random'>here</a>. $settings->admindetails_name ( $settings->sitename 's adminstrator) may have added it to one of the menus.</p> " );
2016-12-07 20:40:16 +00:00
}
]);
?>