136 lines
6.2 KiB
PHP
136 lines
6.2 KiB
PHP
<?php
|
|
/**
|
|
* Plugin Name: JumPHP Embed
|
|
* Description: Shows applications of a JumPHP playground (https://jumphp.net) as pages of this site, without iframe: [jumphp_block app="..." block="..."] and the application under its base path (/chat/).
|
|
* Version: 1.0.0
|
|
* Requires PHP: 7.4
|
|
* License: MIT
|
|
*
|
|
* Settings > JumPHP Embed: the playground's address, then one line per application (its slug, the site identifier and secret the
|
|
* application's owner gave, the base path declared in the playground's studio). docs/embed.md of JumPHP.
|
|
* The secret can also come from wp-config.php: define('JUMPHP_EMBED_SECRET_<SLUG>', 'jes_...') (slug in upper case, - as _), it then never
|
|
* goes into the database.
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
// the kit: copied in by build.sh (the zip), else the one of the JumPHP repository next to it
|
|
require_once is_file(__DIR__ . '/jumphp-embed-kit.php') ? __DIR__ . '/jumphp-embed-kit.php' : __DIR__ . '/../../jumphp-embed.php';
|
|
|
|
final class JumphpEmbedPlugin
|
|
{
|
|
const OPTION = 'jumphp_embed';
|
|
|
|
/** @return array{server: string, apps: list<array{app: string, site: string, secret: string, base: string}>} */
|
|
public static function settings(): array
|
|
{
|
|
$o = get_option(self::OPTION, []);
|
|
$apps = [];
|
|
foreach ((array)($o['apps'] ?? []) as $app) {
|
|
if (!empty($app['app'])) {
|
|
$const = 'JUMPHP_EMBED_SECRET_' . strtoupper(str_replace('-', '_', $app['app']));
|
|
$app['secret'] = defined($const) ? (string)constant($const) : (string)($app['secret'] ?? '');
|
|
$apps[] = $app;
|
|
}
|
|
}
|
|
return ['server' => (string)($o['server'] ?? 'https://jumphp.net'), 'apps' => $apps];
|
|
}
|
|
|
|
public static function kit(array $app): ?JumphpEmbed
|
|
{
|
|
try {
|
|
return new JumphpEmbed(['server' => self::settings()['server'], 'app' => $app['app'], 'site' => $app['site'], 'secret' => $app['secret'],
|
|
'base' => $app['base'], 'cache' => WP_CONTENT_DIR . '/cache/jumphp-embed']);
|
|
} catch (InvalidArgumentException $e) {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/** The application under its base path, before WordPress renders anything (no theme, no query: the files are streamed as they come). */
|
|
public static function route(): void
|
|
{
|
|
$path = (string)parse_url((string)($_SERVER['REQUEST_URI'] ?? '/'), PHP_URL_PATH);
|
|
foreach (self::settings()['apps'] as $app) {
|
|
$base = '/' . trim((string)$app['base'], '/');
|
|
if ($base !== '/' && ($path === $base || strpos($path, $base . '/') === 0)) {
|
|
$kit = self::kit($app);
|
|
if ($kit !== null) {
|
|
while (ob_get_level() > 0) {
|
|
ob_end_clean();
|
|
}
|
|
if ($kit->serve()) {
|
|
exit;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/** [jumphp_block app="caramail-chat" block="login"] */
|
|
public static function shortcode($atts): string
|
|
{
|
|
$atts = shortcode_atts(['app' => '', 'block' => ''], (array)$atts, 'jumphp_block');
|
|
foreach (self::settings()['apps'] as $app) {
|
|
if ($app['app'] === $atts['app']) {
|
|
$kit = self::kit($app);
|
|
return $kit !== null ? $kit->block((string)$atts['block']) : '';
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
public static function admin(): void
|
|
{
|
|
add_options_page('JumPHP Embed', 'JumPHP Embed', 'manage_options', 'jumphp-embed', [self::class, 'page']);
|
|
}
|
|
|
|
public static function register(): void
|
|
{
|
|
register_setting('jumphp_embed', self::OPTION, ['type' => 'array', 'sanitize_callback' => [self::class, 'sanitize']]);
|
|
}
|
|
|
|
public static function sanitize($input): array
|
|
{
|
|
$apps = [];
|
|
foreach ((array)($input['apps'] ?? []) as $app) {
|
|
$slug = sanitize_title((string)($app['app'] ?? ''));
|
|
if ($slug === '') {
|
|
continue;
|
|
}
|
|
$apps[] = ['app' => $slug, 'site' => preg_replace('/[^a-z0-9_]/', '', (string)($app['site'] ?? '')),
|
|
'secret' => trim((string)($app['secret'] ?? '')), 'base' => '/' . trim(preg_replace('#[^a-z0-9/_-]#', '', strtolower((string)($app['base'] ?? ''))), '/')];
|
|
}
|
|
return ['server' => esc_url_raw(rtrim((string)($input['server'] ?? 'https://jumphp.net'), '/')), 'apps' => $apps];
|
|
}
|
|
|
|
public static function page(): void
|
|
{
|
|
$o = get_option(self::OPTION, []);
|
|
$apps = (array)($o['apps'] ?? []);
|
|
$apps[] = ['app' => '', 'site' => '', 'secret' => '', 'base' => ''];
|
|
echo '<div class="wrap"><h1>JumPHP Embed</h1><form method="post" action="options.php">';
|
|
settings_fields('jumphp_embed');
|
|
echo '<table class="form-table"><tr><th><label for="jumphp-server">Playground</label></th><td><input id="jumphp-server" class="regular-text" name="'
|
|
. self::OPTION . '[server]" value="' . esc_attr((string)($o['server'] ?? 'https://jumphp.net')) . '"></td></tr></table>';
|
|
echo '<table class="widefat"><thead><tr><th>Application (slug)</th><th>Site (es_...)</th><th>Secret (jes_...)</th><th>Base path</th></tr></thead><tbody>';
|
|
foreach ($apps as $i => $app) {
|
|
echo '<tr>';
|
|
foreach (['app' => 'caramail-chat', 'site' => 'es_...', 'secret' => 'jes_...', 'base' => '/chat'] as $key => $placeholder) {
|
|
echo '<td><input name="' . self::OPTION . "[apps][$i][$key]\" value=\"" . esc_attr((string)($app[$key] ?? '')) . '" placeholder="' . esc_attr($placeholder)
|
|
. '"' . ($key === 'secret' ? ' type="password" autocomplete="off"' : '') . '></td>';
|
|
}
|
|
echo '</tr>';
|
|
}
|
|
echo '</tbody></table><p class="description">Shortcode: <code>[jumphp_block app="caramail-chat" block="login"]</code>. The application answers at its base path (https://this-site/chat/). '
|
|
. 'An empty slug removes a line.</p>';
|
|
submit_button();
|
|
echo '</form></div>';
|
|
}
|
|
}
|
|
|
|
add_action('plugins_loaded', [JumphpEmbedPlugin::class, 'route'], 0);
|
|
add_shortcode('jumphp_block', [JumphpEmbedPlugin::class, 'shortcode']);
|
|
add_action('admin_menu', [JumphpEmbedPlugin::class, 'admin']);
|
|
add_action('admin_init', [JumphpEmbedPlugin::class, 'register']);
|