'array', 'sanitize_callback' => [self::class, 'sanitize'], 'default' => [], ]); } /** * @param array $input * @return array{api_url: string, api_key: string, mode: string} */ public static function sanitize($input): array { $input = is_array($input) ? $input : []; $mode = isset($input['mode']) && $input['mode'] === 'async' ? 'async' : 'sync'; return [ 'api_url' => isset($input['api_url']) ? esc_url_raw(wp_unslash((string) $input['api_url'])) : 'https://docuhook.com', 'api_key' => isset($input['api_key']) ? sanitize_text_field(wp_unslash((string) $input['api_key'])) : '', 'mode' => $mode, ]; } public static function test_render(): void { if (!current_user_can('manage_options')) { wp_die(esc_html__('Forbidden', 'docuhook')); } check_admin_referer('docuhook_test_render'); $result = DocuHook_Client::render( [ 'site' => get_bloginfo('name'), 'url' => home_url('/'), 'time' => gmdate('c'), ], '' . '

DocuHook test

' . '

{{site}}

{{url}}

{{time}}

' . '', ['mode' => 'sync', 'filename' => 'docuhook-test.pdf'] ); set_transient( 'docuhook_admin_notice_' . get_current_user_id(), [ 'ok' => !empty($result['ok']), 'code' => (int) ($result['code'] ?? 0), 'message' => isset($result['error']) ? (string) $result['error'] : 'ok', ], 60 ); wp_safe_redirect(admin_url('options-general.php?page=docuhook')); exit; } public static function admin_notices(): void { if (!current_user_can('manage_options')) { return; } // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only page routing for admin notice scope. $page = isset($_GET['page']) ? sanitize_text_field(wp_unslash($_GET['page'])) : ''; if ($page !== 'docuhook') { return; } $notice = get_transient('docuhook_admin_notice_' . get_current_user_id()); if (!is_array($notice)) { return; } delete_transient('docuhook_admin_notice_' . get_current_user_id()); $ok = !empty($notice['ok']); $code = isset($notice['code']) ? (int) $notice['code'] : 0; $message = isset($notice['message']) ? (string) $notice['message'] : ''; $class = $ok ? 'notice-success' : 'notice-error'; echo '

'; if ($ok) { echo esc_html( sprintf( /* translators: %d: HTTP status code */ __('Test render succeeded (HTTP %d).', 'docuhook'), $code ) ); } else { echo esc_html( sprintf( /* translators: 1: HTTP status code, 2: error message */ __('Test render failed (HTTP %1$d): %2$s', 'docuhook'), $code, $message ) ); } echo '

'; } public static function render_page(): void { if (!current_user_can('manage_options')) { return; } $settings = DocuHook_Client::settings(); ?>

docuhook.com. Create an account, copy an API key, then paste it below.', 'docuhook'), esc_url('https://docuhook.com') ), [ 'a' => [ 'href' => true, 'target' => true, 'rel' => true, ], ] ); ?>


$result = docuhook_render(
    [ 'order_id' => 123, 'customer' => 'Ada' ],
    '<h1>Order {{order_id}} for {{customer}}</h1>',
    [ 'mode' => 'sync' ]
);

if ( ! empty( $result['ok'] ) ) {
    echo $result['html']; // merged HTML from DocuHook
}