1
0
Fork 0
mirror of https://github.com/sbrl/Pepperminty-Wiki.git synced 2024-11-22 04:23:01 +00:00
Pepperminty-Wiki/modules/action-random.php

45 lines
1.6 KiB
PHP
Raw Normal View History

2016-12-07 20:40:16 +00:00
<?php
register_module([
"name" => "Random Page",
2017-10-25 22:00:04 +00:00
"version" => "0.2",
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;
/**
* @api {get} ?action=random[&mode={mode}] Redirects to a random page.
2016-12-07 20:40:16 +00:00
* @apiName RawSource
* @apiGroup Page
* @apiPermission Anonymous
*
* @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;
$mode = preg_replace("/[^a-z-_]/i", "", $_GET["mode"] ?? "");
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) {
global $settings;
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);
$redirect_url = "?page=" . rawurlencode($randomPageName);
if(!empty($mode)) $redirect_url .= "&mode=$mode";
header("location: $redirect_url");
2016-12-07 20:40:16 +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
}
]);
?>