PHP code example of groupone / wap-client

1. Go to this page and download the library: Download groupone/wap-client library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

groupone / wap-client example snippets


use WapClient; // global facade defined by the package..

add_action('admin_menu', function () {
    WapClient::register_chat_page([
        'menu_slug'   => 'my-plugin-wap-chat',
        'parent_slug' => 'my-plugin-settings',
        'page_title'  => 'AI Assistant',
        'product'     => 'my-product-slug',
        'server_url'  => 'https://your-wap-backend.example.com',
        'grnd'        => [
            'issuer_url'  => 'https://api.my-brand.com/grnd/token',
            'license_key' => get_option('my_product_license_key'),
        ],
        'terms_url'   => 'https://example.com/terms',   // optional: footer "Terms of Use" link + consent gate
        'privacy_url' => 'https://example.com/privacy', // optional: footer "Privacy Policy" link
    ]);
});

WapClient::register_chat_page([
    'menu_slug'         => 'my-plugin-onboarding',
    'page_title'        => 'Set up your site',   // used as the document <title>
    'hidden_admin_menu' => true,
    'render_mode'       => 'standalone',
    'product'           => 'my-product',
    'server_url'        => 'https://your-wap-backend.example.com',
    'grnd'              => ['issuer_url' => '…', 'license_key' => '…'],
]);

WapClient::register_chat_page([
    'menu_slug'            => 'my-plugin-onboarding',
    'render_mode'          => 'standalone',
    'standalone_shell_css' => false,
    // …
]);

add_filter('wap_client_standalone_shell_css', function (bool $enabled, string $menu_slug): bool {
    return 'my-plugin-onboarding' === $menu_slug ? false : $enabled;
}, 10, 2);

add_filter('wap_client_standalone_body_class', function (array $classes, string $menu_slug): array {
    if ('my-plugin-onboarding' === $menu_slug) {
        $classes[] = 'my-wizard';
    }
    return $classes;
}, 10, 2);

'render' => function (array $page): void {
    

\WapClient::register_chat_column([
    'id'         => 'my-plugin-assistant',   // namespaces state + DOM ids
    'product'    => 'my-product',
    'server_url' => 'https://wap.group.one',
    'grnd'       => ['issuer_url' => '…', 'license_key' => get_option('my_product_license')],

    // Opt screens in — at least one of these, or the column renders nowhere.
    'screens'       => ['toplevel_page_my-plugin'],
    'should_render' => fn ($screen, string $hook): bool
        => $screen && 0 === strpos($screen->id, 'my-plugin'),

    'column' => ['side' => 'right', 'width' => '400px'],
]);

// Add (or remove) screens for one column.
add_filter('wap_client_column_screens', fn (array $s, string $id): array
    => 'my-plugin-assistant' === $id ? [...$s, 'dashboard'] : $s, 10, 2);

// Retheme the framing (mirrors wap_client_layout).
add_filter('wap_client_column', fn (array $c): array => [...$c, 'width' => '360px']);

$grnd = \WapClient::get_grnd_token([
    'product'        => 'my-product',
    'server_url'     => 'https://wap.group.one',
    'issuer_url'     => 'https://api.my-brand.com/grnd/token', // or pass a grnd_provider callable
    'license_key'    => get_option('my_product_license'),
    'force_new'      => $_POST['force_new'] ?? false,           // widget's 401-retry flag
    // Forward any extra headers verbatim to the brand issuer on every
    // exchange. Useful for relaying auth/identity headers (TOTP, client
    // domain, custom tokens) from the surrounding request.
    'extra_headers'  => [
        'X-TOTP'                 => $_POST['totp']   ?? '',
        'X-Onecom-Client-Domain' => $_POST['domain'] ?? home_url(),
    ],
]);

if (is_wp_error($grnd)) {
    error_log('WAP get_grnd_token failed: ' . $grnd->get_error_code());
    wp_send_json_error(['message' => 'Assistant unavailable right now.'], 502);
}
wp_send_json_success(['token' => $grnd]);  // the browser hands it to WAP directly

add_action('admin_init', function () {
    \WapClient::register_chat_embed([
        'id'         => 'my-plugin-content-ai',   // namespaces the DOM id + consent/auth lookups
        'product'    => 'my-product',
        'server_url' => 'https://wap.group.one',
        'grnd'       => ['issuer_url' => '…', 'license_key' => get_option('my_product_license')],

        // Where the assets load — at least one of these, or the embed activates nowhere.
        'screens'       => ['my-plugin_page_my-plugin-content-ai', 'post'],
        'should_render' => fn ($screen, string $hook): bool => $screen && 'post' === $screen->base,

        'layout' => ['height' => '640px'],
    ]);
});

// Inside your tab body, metabox callback, template partial…
\WapClient::render_chatbox('my-plugin-content-ai');          // echoes

$html = \WapClient::get_chatbox('my-plugin-content-ai');     // …or returns the markup

if (\WapClient::has_chatbox('my-plugin-content-ai')) {       // …or ask first
    echo '<h2>Chat</h2>';
    \WapClient::render_chatbox('my-plugin-content-ai');
}

add_filter('wap_client_embed_screens', fn (array $s, string $id): array
    => 'my-plugin-content-ai' === $id ? [...$s, 'dashboard'] : $s, 10, 2);

  'grnd_provider' => function () {
      return MyBrand\Api::exchange_license_for_grnd(get_option('my_license'));
  },
  
bash
php tests/test-token-manager.php
php tests/test-grnd-acquisition.php
php tests/test-chat-widget-page-modes.php
php tests/test-chat-column.php