Skip to content

Commit

Permalink
Website: Preview the PR specified by the referer header
Browse files Browse the repository at this point in the history
Enables easy linking to Playground PRs from WordPress and Gutenberg
GitHub PRs without interpolating the PR number in the link.

This PR adds a https://playground.wordpress.net/preview-referrer-pr.php script
that looks at the Referer header, extracts the repo and the PR number
from it, and redirects to the relevant PR previewer.

 ## Testing instructions

1. Run the script on a HTTPS domain (or use my link: https://adamadam.blog/referer.php)
2. Link to that script from a WordPress develop and a Gutenberg PR
3. Click those links
4. Confirm you were redirected to /wordpress.html or /gutenberg.html
   with a proper `?pr=` query parameter

Props to @gziolo for the idea
  • Loading branch information
adamziel committed Sep 13, 2024
1 parent db78ea4 commit 6e4e0f9
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions packages/playground/website/public/preview-referrer-pr.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

$referrer = $_SERVER['HTTP_REFERER'];
$prNumber = null;

if (preg_match('/github\.com\/WordPress\/gutenberg\/pull\/(?<prNumber>\d+)/i', $referrer, $matches)) {
$prNumber = $matches['prNumber'];
header('Location: ./gutenberg.html?pr=' . $prNumber);
exit;
} else if (preg_match('/github\.com\/WordPress\/wordpress-develop\/pull\/(?<prNumber>\d+)/i', $referrer, $matches)) {
$prNumber = $matches['prNumber'];
header('Location: ./wordpress.html?pr=' . $prNumber);
exit;
} else {
header('HTTP/1.1 403 Forbidden');
echo 'Invalid referrer';
exit;
}

0 comments on commit 6e4e0f9

Please sign in to comment.