136 lines
4.8 KiB
PHP
136 lines
4.8 KiB
PHP
<?php
|
|
/*
|
|
Plugin Name: Amazon KDP Marketplace Redirect
|
|
Description: Redirect registered KDP editions to the visitor's Amazon marketplace.
|
|
Version: 1.0.0
|
|
Author: Seb Molenaar
|
|
Author URI: https://sebs.space
|
|
*/
|
|
|
|
namespace Sebstr\AmazonKdp;
|
|
|
|
if (!defined('YOURLS_ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
require_once __DIR__ . '/routing.php';
|
|
|
|
const SETTINGS = 'sebstr_amazon_kdp_asins';
|
|
|
|
\yourls_add_action('plugins_loaded', __NAMESPACE__ . '\\register_settings');
|
|
\yourls_add_action('redirect_shorturl', __NAMESPACE__ . '\\prepare_redirect');
|
|
|
|
function prepare_redirect(array $args): void
|
|
{
|
|
$original = $args[0];
|
|
$product = product_url($original);
|
|
if ($product === null || !array_key_exists($product['asin'], registry())) {
|
|
return;
|
|
}
|
|
|
|
// This hook runs before core click tracking and before pre_redirect.
|
|
\yourls_remove_action('pre_redirect', 'sebstr_amazon');
|
|
|
|
$country = visitor_country($_SERVER['REMOTE_ADDR'] ?? '', $_SERVER['HTTP_CF_IPCOUNTRY'] ?? '');
|
|
$destination = marketplace_url($original, $product, $country);
|
|
|
|
\yourls_add_filter('redirect_location', static function ($location) use ($original, $destination) {
|
|
if ($location !== $original) {
|
|
return $location;
|
|
}
|
|
|
|
header('Cache-Control: private, no-store, max-age=0');
|
|
header('Cloudflare-CDN-Cache-Control: no-store');
|
|
return $destination;
|
|
});
|
|
\yourls_add_filter('redirect_code', static function ($code, $location) use ($destination) {
|
|
return $location === $destination ? 302 : $code;
|
|
}, 10, 2);
|
|
}
|
|
|
|
function registry(): array
|
|
{
|
|
$entries = \yourls_get_option(SETTINGS, []);
|
|
return is_array($entries) ? $entries : [];
|
|
}
|
|
|
|
function register_settings(): void
|
|
{
|
|
if (defined('YOURLS_ADMIN') && YOURLS_ADMIN) {
|
|
\yourls_register_plugin_page('sebstr_amazon_kdp', 'Amazon KDP', __NAMESPACE__ . '\\settings_page');
|
|
}
|
|
}
|
|
|
|
function settings_page(): void
|
|
{
|
|
$entries = registry();
|
|
$notice = '';
|
|
if (($_SERVER['REQUEST_METHOD'] ?? '') === 'POST') {
|
|
$nonce = $_POST['nonce'] ?? '';
|
|
\yourls_verify_nonce(SETTINGS, is_string($nonce) ? $nonce : '');
|
|
$input = $_POST['asin'] ?? null;
|
|
$asin = is_string($input) ? normalize_asin(trim($input)) : null;
|
|
$operation = $_POST['operation'] ?? '';
|
|
$title = $_POST['title'] ?? '';
|
|
|
|
if ($asin === null) {
|
|
$notice = 'Enter a ten-character ASIN.';
|
|
} elseif ($operation === 'remove' || ($operation === 'save' && is_string($title) && strlen($title) <= 1024)) {
|
|
$updated = $entries;
|
|
if ($operation === 'remove') {
|
|
unset($updated[$asin]);
|
|
} else {
|
|
$updated[$asin] = trim($title);
|
|
}
|
|
if ($updated === $entries || \yourls_update_option(SETTINGS, $updated)) {
|
|
$entries = $updated;
|
|
$notice = $operation === 'remove' ? 'ASIN removed.' : 'ASIN saved.';
|
|
} else {
|
|
$notice = 'The registry could not be saved.';
|
|
}
|
|
} else {
|
|
$notice = 'Enter a title of up to 1,024 bytes.';
|
|
}
|
|
}
|
|
|
|
ksort($entries, SORT_STRING);
|
|
$nonce = \yourls_create_nonce(SETTINGS);
|
|
?>
|
|
<h2>Amazon KDP ASINs</h2>
|
|
<p>Register each edition once. Registration applies to all short links for that ASIN.</p>
|
|
<?php if ($notice !== ''): ?>
|
|
<p role="status"><?= escape($notice) ?></p>
|
|
<?php endif; ?>
|
|
<form method="post">
|
|
<input type="hidden" name="nonce" value="<?= escape($nonce) ?>">
|
|
<label>ASIN <input name="asin" required maxlength="10" pattern="[A-Za-z0-9]{10}"></label>
|
|
<label>Title (optional) <input name="title" maxlength="256"></label>
|
|
<button type="submit" name="operation" value="save">Save ASIN</button>
|
|
</form>
|
|
<?php if ($entries !== []): ?>
|
|
<table>
|
|
<thead><tr><th scope="col">ASIN</th><th scope="col">Title</th><th scope="col">Action</th></tr></thead>
|
|
<tbody>
|
|
<?php foreach ($entries as $asin => $title): ?>
|
|
<tr>
|
|
<td><?= escape((string) $asin) ?></td>
|
|
<td><?= escape($title) ?></td>
|
|
<td>
|
|
<form method="post">
|
|
<input type="hidden" name="nonce" value="<?= escape($nonce) ?>">
|
|
<input type="hidden" name="asin" value="<?= escape((string) $asin) ?>">
|
|
<button type="submit" name="operation" value="remove" aria-label="Remove <?= escape((string) $asin) ?>">Remove</button>
|
|
</form>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
</tbody>
|
|
</table>
|
|
<?php endif;
|
|
}
|
|
|
|
function escape(string $value): string
|
|
{
|
|
return htmlspecialchars($value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
|
}
|