walletpress/wp-plugin/includes/class-walletpress.php
RMI Admin ba8d1f1261 Initial commit: WalletPress v1.0.0-beta
Self-hosted wallet management platform for WordPress:
- WP plugin: vault, generate, token gates, payments, wallet login, 15 admin pages
- CLI: 17 commands covering all backend feature groups
- Landing pages + PDF documentation
- Connects to rmi-backend API (86+ endpoints, 29+ chains)
2026-06-27 17:12:49 +07:00

140 lines
7.5 KiB
PHP

<?php
defined('ABSPATH') || exit;
class WalletPress {
private array $modules = [];
private ?WalletPressAPI $api = null;
public function init(): void {
add_action('init', [$this, 'load_textdomain']);
add_action('init', [$this, 'register_shortcodes']);
add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend']);
add_action('admin_enqueue_scripts', [$this, 'enqueue_admin']);
add_action('rest_api_init', [$this, 'register_routes']);
add_filter('script_loader_tag', [$this, 'add_module_scripts'], 10, 3);
$this->api = new WalletPressAPI(
get_option('walletpress_api_url', ''),
get_option('walletpress_api_key', '')
);
$this->modules = [
'admin' => new WalletPressAdmin($this),
'auth' => new WalletPressAuth($this),
'gate' => new WalletPressGate($this),
'payments' => new WalletPressPayments($this),
];
do_action('walletpress_init', $this);
}
public function api(): WalletPressAPI { return $this->api; }
public function module(string $name): ?object { return $this->modules[$name] ?? null; }
public function load_textdomain(): void {
load_plugin_textdomain('walletpress', false, dirname(plugin_basename(WALLETPRESS_FILE)) . '/languages');
}
public function register_shortcodes(): void {
add_shortcode('wallet_connect', [WalletPressAuth::class, 'render_connect_button']);
add_shortcode('wallet_gate', [WalletPressGate::class, 'render_gate']);
add_shortcode('wallet_pay', [WalletPressPayments::class, 'render_pay_button']);
add_shortcode('wallet_profile', [$this, 'render_profile']);
add_shortcode('wallet_generate', [$this, 'render_generate_shortcode']);
}
public function render_generate_shortcode(): string {
if (!current_user_can('manage_options')) return '';
ob_start(); ?>
<div class="walletpress-generate-shortcode">
<form class="wp-generate-form">
<select name="chain"><?php foreach ($this->supported_chains() as $k => $c): ?>
<option value="<?php echo esc_attr($k); ?>"><?php echo esc_html($c['label']); ?></option>
<?php endforeach; ?></select>
<input type="text" name="label" placeholder="Label (optional)">
<button type="submit" class="walletpress-btn walletpress-btn-pay">Generate</button>
</form>
<pre class="wp-gen-result" style="display:none;"></pre>
</div>
<?php
return ob_get_clean();
}
public function register_routes(): void {
$c = 'WalletPressAuth';
register_rest_route('walletpress/v1', '/auth/nonce', ['methods' => 'GET', 'callback' => [$c, 'rest_nonce'], 'permission_callback' => '__return_true']);
register_rest_route('walletpress/v1', '/auth/verify', ['methods' => 'POST', 'callback' => [$c, 'rest_verify'], 'permission_callback' => '__return_true']);
register_rest_route('walletpress/v1', '/auth/me', ['methods' => 'GET', 'callback' => [$c, 'rest_me'], 'permission_callback' => '__return_true']);
register_rest_route('walletpress/v1', '/generate', ['methods' => 'POST', 'callback' => [$this, 'rest_generate'], 'permission_callback' => function() { return current_user_can('manage_options'); }]);
register_rest_route('walletpress/v1', '/vault', ['methods' => 'GET', 'callback' => [$this, 'rest_vault'], 'permission_callback' => function() { return current_user_can('manage_options'); }]);
}
public function rest_generate(WP_REST_Request $req): WP_REST_Response {
$result = $this->api->generate_wallet(sanitize_text_field($req->get_param('chain')), sanitize_text_field($req->get_param('label')));
return new WP_REST_Response($result ?: ['error' => 'Generation failed'], $result ? 200 : 502);
}
public function rest_vault(): WP_REST_Response {
$vault = $this->api->list_vault();
return new WP_REST_Response($vault ?: ['error' => 'Failed to fetch vault'], $vault ? 200 : 502);
}
public function enqueue_frontend(): void {
wp_enqueue_script('walletpress', WALLETPRESS_URL . 'assets/js/walletpress.js', [], WALLETPRESS_VERSION, true);
wp_enqueue_style('walletpress', WALLETPRESS_URL . 'assets/css/walletpress.css', [], WALLETPRESS_VERSION);
$user = wp_get_current_user();
wp_localize_script('walletpress', 'walletpress', [
'rest' => esc_url_raw(rest_url('walletpress/v1')),
'nonce' => wp_create_nonce('wp_rest'),
'api_url' => get_option('walletpress_api_url', ''),
'user' => $user->ID ? ['id' => $user->ID, 'display_name' => $user->display_name, 'wallet' => get_user_meta($user->ID, 'walletpress_address', true) ?: null] : null,
'chains' => $this->supported_chains(),
]);
}
public function enqueue_admin(string $hook): void {
if (str_contains($hook, 'walletpress')) {
wp_enqueue_style('walletpress-admin', WALLETPRESS_URL . 'assets/css/walletpress.css', [], WALLETPRESS_VERSION);
wp_enqueue_script('walletpress-admin', WALLETPRESS_URL . 'assets/js/walletpress-admin.js', [], WALLETPRESS_VERSION, true);
wp_localize_script('walletpress-admin', 'walletpress_admin', [
'nonce' => wp_create_nonce('walletpress_admin'),
'ajax' => admin_url('admin-ajax.php'),
]);
}
}
public function add_module_scripts(string $tag, string $handle, string $src): string {
return ($handle === 'walletpress') ? str_replace('<script', '<script type="module"', $tag) : $tag;
}
public function supported_chains(): array {
return [
'solana' => ['label' => 'Solana', 'icon' => 'sol', 'wallet' => 'phantom'],
'ethereum' => ['label' => 'Ethereum', 'icon' => 'eth', 'wallet' => 'metamask'],
'base' => ['label' => 'Base', 'icon' => 'base', 'wallet' => 'metamask'],
'polygon' => ['label' => 'Polygon', 'icon' => 'matic', 'wallet' => 'metamask'],
'bsc' => ['label' => 'BNB Chain', 'icon' => 'bnb', 'wallet' => 'metamask'],
'arbitrum' => ['label' => 'Arbitrum', 'icon' => 'arb', 'wallet' => 'metamask'],
'optimism' => ['label' => 'Optimism', 'icon' => 'op', 'wallet' => 'metamask'],
'avalanche' => ['label' => 'Avalanche', 'icon' => 'avax', 'wallet' => 'metamask'],
'bitcoin' => ['label' => 'Bitcoin', 'icon' => 'btc', 'wallet' => ''],
'tron' => ['label' => 'Tron', 'icon' => 'trx', 'wallet' => ''],
];
}
public function render_profile(): string {
if (!is_user_logged_in()) return do_shortcode('[wallet_connect]');
$user = wp_get_current_user();
$wallet = get_user_meta($user->ID, 'walletpress_address', true);
ob_start(); ?>
<div class="walletpress-profile">
<h3><?php esc_html_e('Wallet Profile', 'walletpress'); ?></h3>
<p><strong><?php esc_html_e('User:', 'walletpress'); ?></strong> <?php echo esc_html($user->display_name); ?></p>
<?php if ($wallet): ?>
<p><strong><?php esc_html_e('Wallet:', 'walletpress'); ?></strong> <code><?php echo esc_html(substr($wallet, 0, 6) . '...' . substr($wallet, -4)); ?></code></p>
<button class="walletpress-disconnect button"><?php esc_html_e('Disconnect Wallet', 'walletpress'); ?></button>
<?php else: echo do_shortcode('[wallet_connect]'); endif; ?>
</div>
<?php return ob_get_clean();
}
}