<?php

//--------------------------------------------------
// Setup

    
header("Content-Security-Policy: default-src 'none'; base-uri 'none'; form-action 'self'; style-src 'unsafe-inline'; img-src 'self'; frame-ancestors 'none'; require-trusted-types-for 'script'; trusted-types 'none'; block-all-mixed-content");

    require_once(
__DIR__ '/../wordpress/wp-load.php');

    
$db_path __DIR__ '/../wordpress/wp-content/db.php';

//--------------------------------------------------
// Test

    
$test_count 8// Version 2 uses tests 9 - 10

    
$test_id intval($_POST['test'] ?? 0);

    if (
$test_id >= && $test_id <= $test_count) {
        
$test_path __DIR__ '/tests/' str_pad(intval($test_id), 3'0'STR_PAD_LEFT) . '.php';
    } else {
        
$test_path NULL;
    }

//--------------------------------------------------
// Download source

    
$download = ($_GET['download'] ?? NULL);

    if (
$download == 'db') {

        
header('Content-Type: application/octet-stream; charset=UTF-8');
        
header('Content-Disposition: attachment; filename="db.php"');
        
header('Content-Length: ' filesize($db_path));
        
readfile($db_path);
        exit();

    }

//--------------------------------------------------
// View source

    
$view = ($_GET['view'] ?? NULL);

    if (
$view == 'index') {

        
highlight_file(__DIR__ '/index.php');
        exit();

    } else if (
$view == 'db') {

        
highlight_file($db_path);
        exit();

    } else if (
$view == 'db2') {

        
highlight_file(__DIR__ '/wpdb/db2.php');
        exit();

    }

//--------------------------------------------------
// Run test

    
if ($test_path !== NULL) {

        require_once(
$test_path);

        
$current_time round((($c2 $c1)/1000000000), 4);
        
$patched_time round((($p2 $p1)/1000000000), 4);

        
header('X-Response-Ref: ' $test_id ',' $current_time ',' $patched_time);

    }

//--------------------------------------------------
// HTML

?>
<!DOCTYPE html>
<html lang="en-GB" xml:lang="en-GB" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8" />
    <title>WPDB 2</title>
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="Testing changes with WPDB." />
    <style>
        .output q {
            color: #DD0000;
            white-space: pre;
        }
        .output q::before,
        .output q::after {
            content: "'";
        }
        .fast_new,
        .output strong.matched {
            color: #007700;
        }
        .fast_old,
        .output strong.different {
            color: #DD0000;
        }
        blockquote {
            margin: 1em 0 1em 1em;
            overflow-x: auto;
            white-space: nowrap;
        }
    </style>
</head>
<body>

    <h1>WP-DB</h1>
    <p>Testing a patch to support Escaping Table/Field Names (<a href="https://core.trac.wordpress.org/ticket/52506">Ticket #52506</a>, via <a href="https://github.com/WordPress/wordpress-develop/pull/2192">PR #2192</a>).</p>

    <h2>Run Test</h2>
    <form action="./" method="post">
        <ul>
            <?php for ($k 1$k <= $test_count$k++) { ?>

                <li>

                    <p><input type="submit" name="test" value="<?= htmlentities(str_pad($k3'0'STR_PAD_LEFT)) ?>" /></p>

                    <?php if ($k === $test_id && $test_path !== NULL) { ?>
                        <p>
                            <?= htmlentities(str_pad($current_time6'0'STR_PAD_RIGHT)) ?> - Current WPDB Time<?= ($current_time $patched_time ' ← <strong class="fast_old">Fastest</strong>' ' (+' round((($current_time $patched_time) - 1) * 100) . '%)'?><br />
                            <?= htmlentities(str_pad($patched_time6'0'STR_PAD_RIGHT)) ?> - Patched WPDB Time<?= ($patched_time $current_time ' ← <strong class="fast_new">Fastest</strong>' ' (+' round((($patched_time $current_time) - 1) * 100) . '%)'?>
                        </p>
                        <p class="output">
                            <q><?= htmlentities($result1?></q> - Current WPDB Ouput<br />
                            <q><?= htmlentities($result2?></q> - Patched WPDB Ouput - <?= ($result1 == $result2 '<strong class="matched">Matched</strong>' '<strong class="different">Different</strong>'?>
                        </p>

                        <blockquote>
                            <?php highlight_file($test_path); ?>
                        </blockquote>

                    <?php ?>

                </li>

            <?php ?>
        </ul>
    </form>

    <h2>View Source</h2>

    <p><a href="./?view=db">View /wp-content/db.php</a> (<a href="./?download=db">download</a>)</p>

    <p><a href="./?view=index">View /index.php</a> to see how this page works.</p>

    <p>This is running on a Virtual Machine (noisy neighbours), does not account for TurboBoost, or CPU temperature changes (throttling).</p>

    <p>There is also a version with <a href="./?view=db2">support for IN()</a> via '%...s'.</p>

</body>
</html>