This commit is contained in:
2026-01-11 19:39:55 +01:00
commit c1042c9cde
98 changed files with 9086 additions and 0 deletions

93
src/API/File_Server.php Normal file
View File

@@ -0,0 +1,93 @@
<?php
namespace RomhackPlaza\API;
defined( 'ABSPATH' ) || exit;
class File_Server {
const int FAV_SERVER_NEED_CHANGE = 3600; // In seconds.
const array ENDPOINTS = [
'random-server' => "servers/take-a-random-server.php?return_type=%s",
'server-by-id' => "servers/get-server-by-id.php?server_id=%s",
];
private(set) public string $server_url;
private(set) public int $server_id;
public function __construct( int $post_id = 0, bool $get_a_server = true ) {
if( $get_a_server ){
$resp = self::get_favorite_server( $post_id );
if( $resp !== null ) {
$this->server_url = $resp[0];
$this->server_id = intval($resp[1]);
} else {
$this->server_url = "";
$this->server_id = -1;
}
}
}
public function print_js(){
echo sprintf( '<script>const chosenDownloadServer="%s";const chosenDownloadServerId="%d";</script>', $this->server_url, $this->server_id );
}
public function print(){
echo $this->server_url . "|" . $this->server_id;
}
public static function build_url( string $endpoint, ...$args ){
return sprintf( $_ENV['ROMHACKPLAZA_API_URL'] . $endpoint, ...$args );
}
public static function get_favorite_server( int $post_id ){
if( $post_id == 0 )
return self::get_a_random_server();
$favorite_server = get_post_meta( $post_id, 'favorite_server', true ) ?? false;
$favorite_server_timestamp = get_post_meta( $post_id, 'favorite_server_timestamp', true ) ?? false;
if( $favorite_server === false || $favorite_server_timestamp === false )
return self::get_a_random_server();
$time = time() - intval( $favorite_server_timestamp );
if( $time > self::FAV_SERVER_NEED_CHANGE )
return self::get_a_random_server();
$server_url = wp_remote_get( self::build_url( self::ENDPOINTS['server-by-id'], $favorite_server ) )['body'];
return [ $server_url, $favorite_server ];
}
public static function get_a_random_server( $return_type = "PHP" ){
switch( $return_type ) {
case "PHP":
case "JS":
break;
default:
$return_type = "RAW";
break;
}
$resp = wp_remote_get( self::build_url( self::ENDPOINTS['random-server'], $return_type ) );
if( !is_wp_error( $resp ) ) {
if ($return_type === "PHP")
return explode("|", $resp['body']);
else
echo $resp['body'];
}
return null;
}
}