Viewing: face-registration copy.php
<?php
/**
* Plugin Name: FACES Registration Endpoint
* Description: Adds a simple REST endpoint /wp-json/faces/v1/register to email FACES registration submissions.
* Version: 1.0.0
* Author: ChatGPT
* License: GPL-2.0-or-later
*
* Usage:
* 1) Upload this single file to wp-content/plugins/face-registration.php
* 2) Activate in WordPress โ Plugins.
* 3) Your front-end can POST JSON to /wp-json/faces/v1/register
* (see the fetch() in your form file). The email is sent to info@yucai.org by default.
* 4) To change recipient or subject, add filters in your theme/plugin:
* add_filter('faces_registration_to', fn() => 'faces@yucai.org');
* add_filter('faces_registration_subject', fn($s) => 'New FACES Registration โ Site A');
*/
if (!defined('ABSPATH')) { exit; }
add_action('rest_api_init', function () {
register_rest_route('faces/v1', '/register', [
'methods' => ['POST', 'OPTIONS'],
'callback' => 'faces_handle_register',
'permission_callback' => '__return_true', // Public endpoint; add verification if needed (nonce, recaptcha, etc.)
]);
});
/**
* Handle registration payload and email it.
* @param WP_REST_Request $req
* @return WP_REST_Response|WP_Error
*/
function faces_handle_register($req) {
// Simple CORS/preflight support (tweak as needed)
if ($req->get_method() === 'OPTIONS') {
return new WP_REST_Response(null, 204);
}
$p = $req->get_json_params();
if (!is_array($p)) {
return new WP_Error('bad_json', 'Invalid JSON payload', ['status' => 400]);
}
// Basic validation
if (empty($p['parents']) || empty($p['students'])) {
return new WP_Error('missing_fields', 'Parents and students are required', ['status' => 400]);
}
$to = apply_filters('faces_registration_to', 'info@yucai.org');
$subject = apply_filters('faces_registration_subject', 'ๆฐ็่ฒๆไนๅฎถๆฅๅ');
$headers = ['Content-Type: text/html; charset=UTF-8'];
$headers = apply_filters('faces_registration_headers', $headers);
ob_start();
?>
<style>
body { font-family: -apple-system, Segoe UI, Roboto, Arial, sans-serif; color:#111; }
h1,h2 { margin: .2rem 0; }
table { border-collapse: collapse; width: 100%; margin: .5rem 0 1rem; }
th, td { border: 1px solid #e5e7eb; padding: .4rem .5rem; text-align: left; vertical-align: top; }
.muted { color:#6b7280; }
</style>
<h1>่ฒๆไนๅฎถๆฅๅไฟกๆฏ</h1>
<p class="muted">ๆไบคๆถ้ด๏ผ<?php echo esc_html( current_time('mysql') ); ?></p>
<h2>ๅฎถ้ฟ / ็ๆคไบบ</h2>
<table>
<thead><tr><th>#</th><th>ๅงๅ</th><th>ไธๅญฆ็ๅ
ณ็ณป</th><th>้ฎ็ฎฑ</th><th>็ต่ฏ</th></tr></thead>
<tbody>
<?php foreach (($p['parents'] ?? []) as $i => $parent): ?>
<tr>
<td><?php echo (int)($i+1); ?></td>
<td><?php echo esc_html($parent['name'] ?? ''); ?></td>
<td><?php echo esc_html($parent['relationship'] ?? ''); ?></td>
<td><?php echo esc_html($parent['email'] ?? ''); ?></td>
<td><?php echo esc_html($parent['phone'] ?? ''); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<p><strong>ๅฎถๅบญไฝๅ๏ผ</strong> <?php echo esc_html($p['address'] ?? ''); ?></p>
<h2>ๅญฆ็ไฟกๆฏ</h2>
<table>
<thead><tr><th>#</th><th>ๅงๅ</th><th>ๅบ็ๆฅๆ</th><th>ๅนด็บง</th><th>ๆงๅซ</th><th>ๅญฆๆ ก</th></tr></thead>
<tbody>
<?php foreach (($p['students'] ?? []) as $i => $s): ?>
<tr>
<td><?php echo (int)($i+1); ?></td>
<td><?php echo esc_html($s['name'] ?? ''); ?></td>
<td><?php echo esc_html($s['dob'] ?? ''); ?></td>
<td><?php echo esc_html($s['grade'] ?? ''); ?></td>
<td><?php echo esc_html($s['gender'] ?? ''); ?></td>
<td><?php echo esc_html($s['school'] ?? ''); ?></td>
</tr>
<?php if (!empty($s['interests']) || !empty($s['notes'])): ?>
<tr>
<td class="muted"> </td>
<td colspan="5">
<?php if (!empty($s['interests'])): ?>
<div><strong>ๅ
ด่ถฃไธ็น้ฟ๏ผ</strong> <?php echo esc_html($s['interests']); ?></div>
<?php endif; ?>
<?php if (!empty($s['notes'])): ?>
<div><strong>็ป่ๅธ็ๅคๆณจ๏ผ</strong> <?php echo nl2br( esc_html($s['notes']) ); ?></div>
<?php endif; ?>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</tbody>
</table>
<h2>ๆฅๅ่ฏพ็จ</h2>
<p><?php echo esc_html( implode(', ', $p['classSelections'] ?? []) ); ?></p>
<h2>ไธ่ฏพๆถ้ดๅๅฅฝ</h2>
<p>ๆฅๆ๏ผ<?php echo esc_html($p['preferredDays'] ?? ''); ?>
| ๆถ้ด๏ผ<?php echo esc_html($p['preferredTimes'] ?? ''); ?></p>
<h2>่กฅๅ
่ฏดๆ</h2>
<p><?php echo nl2br( esc_html($p['programs'] ?? '') ); ?></p>
<h2>ๅๆๆกๆฌพ</h2>
<p>
็
ง็/่ง้ขๆๆ๏ผ<?php echo !empty($p['photoConsent']) ? 'ๆฏ' : 'ๅฆ'; ?><br/>
่ดฃไปป่ฑๅ
ๅๆ๏ผ<?php echo !empty($p['liability']) ? 'ๆฏ' : 'ๅฆ'; ?><br/>
็ฅๆๆฅๅ่ดน๏ผ<?php echo !empty($p['regFee']) ? 'ๆฏ' : 'ๅฆ'; ?>
</p>
<h2>็ญพๅ</h2>
<p><?php echo esc_html($p['signature'] ?? ''); ?> โ <?php echo esc_html($p['sigDate'] ?? ''); ?></p>
<?php
$body = ob_get_clean();
$sent = wp_mail($to, $subject, $body, $headers);
if (!$sent) {
return new WP_Error('mail_failed', 'Email failed to send', ['status' => 500]);
}
return new WP_REST_Response(['ok' => true], 200);
}
Close