feat(agentforce): use site key for filtering#51
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the Agentforce plugin’s filtering contract to use a stable opaque site_key token (instead of raw site/blog IDs) in hidden prechat fields, and propagates that same token into ingestion records for downstream filtering. It also adjusts the CMP runtime timing so hidden prechat fields are resolved when Embedded Messaging is actually ready.
Changes:
- Replace prechat hidden field payload with
site_keysourced from plugin config, and add aget_site_key()helper. - Extend ingestion post record schema + default transformer to include
site_key. - Move CMP prechat field lookup into the
onEmbeddedMessagingReadyhandler to avoid early bailout due to late-populated bootstrap/consent data.
Reviewed changes
Copilot reviewed 9 out of 9 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
| vip-agentforce.php | Minor whitespace-only adjustment. |
| utils/class-configs.php | Adds get_site_key() and updates hidden prechat fields to use site_key. |
| modules/ingestion/class-ingestion-post-record.php | Adds site_key to ingestion record schema and serialization. |
| modules/ingestion/class-default-transformer.php | Populates site_key on generated ingestion records. |
| assets/src/js/cmp-manager.js | Resolves prechatFields at Embedded Messaging ready time. |
| tests/phpunit/test-ingestion.php | Updates expected ingestion record payload shape to include site_key. |
| tests/phpunit/test-ingestion-post-record.php | Updates record fixtures/expectations for site_key. |
| tests/phpunit/test-default-transformer.php | Updates record fixture to include site_key. |
| tests/phpunit/test-cmp.php | Updates localization/prechat field tests to assert site_key behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| @@ -57,6 +57,7 @@ public static function get_embedding_script(): string { | |||
| * Get the config | |||
| * @return array{ | |||
| * salesforce_instance_url?: string, | |||
There was a problem hiding this comment.
The get_config() return-shape phpdoc was updated, but it still doesn’t document the new site_key config value that is now read by get_site_key(). This makes the public config contract incomplete/misleading for maintainers—please add site_key?: string to the documented array shape (or otherwise document it consistently).
| * salesforce_instance_url?: string, | |
| * salesforce_instance_url?: string, | |
| * site_key?: string, |
| 'site_id_blog_id' => $site_id . '_' . $blog_id, | ||
| ); | ||
| if ( '' !== $site_key ) { | ||
| $fields['site_key'] = $site_key; |
There was a problem hiding this comment.
get_prechat_fields() now returns an empty array when site_key is missing/empty, which means CMP localization will omit prechatFields entirely and the widget will never set any hidden prechat fields. If site_key is required for filtering, consider surfacing a warning (e.g., via Logger) and/or providing an explicit fallback/exception so misconfiguration doesn’t fail silently.
| $fields['site_key'] = $site_key; | |
| $fields['site_key'] = $site_key; | |
| } else { | |
| // Warn when site_key is missing so misconfiguration doesn't fail silently. | |
| error_log( 'VIP Agentforce: get_prechat_fields() called with an empty site_key; prechat fields will be empty and the widget may not set hidden prechat fields as expected.' ); |
| * @return array{ | ||
| * site_id: string, | ||
| * blog_id: string, | ||
| * site_key?: string, |
There was a problem hiding this comment.
The to_array() return-shape docblock marks site_key as optional (site_key?: string), but the method always includes a site_key key (defaulting to an empty string). Update the phpdoc to site_key: string to match the actual serialized schema.
| * site_key?: string, | |
| * site_key: string, |
| const setupPrechatFields = () => { | ||
| const prechatFields = getPrechatFields(); | ||
| if (!prechatFields || Object.keys(prechatFields).length === 0) { | ||
| return; | ||
| } | ||
|
|
||
| window.addEventListener('onEmbeddedMessagingReady', () => { | ||
| const prechatFields = getPrechatFields(); | ||
| if (!prechatFields || Object.keys(prechatFields).length === 0) { | ||
| return; | ||
| } |
There was a problem hiding this comment.
setupPrechatFields() now always registers an onEmbeddedMessagingReady listener, even when prechatFields are absent. If the SDK is unloaded and reloaded, this can accumulate multiple listeners and cause repeated setHiddenPrechatFields calls. Consider registering the listener with { once: true }, or tracking/removing the handler on unload to prevent duplicates.
| @@ -48,6 +49,7 @@ public static function transform( ?Ingestion_Post_Record $record, \WP_Post $post | |||
| [ | |||
| 'site_id' => $site_id, | |||
| 'blog_id' => $blog_id, | |||
| 'site_key' => $site_key, | |||
| 'post_id' => $post_id, | |||
There was a problem hiding this comment.
Default_Transformer now populates site_key from Configs::get_site_key(), but the existing transformer tests don’t assert that this value is propagated into the record. Please add/extend a unit test to prime configs with a non-empty site_key and verify the resulting record includes it.
Description
This PR updates the plugin-side Agentforce filtering contract to use a stable opaque
site_keyinstead of sending raw site/blog identifiers in prechat.The plugin now sends
site_keyas the hidden prechat field and includes the same token in ingestion records so downstream search can filter directly on that value.It also adjusts the CMP runtime so hidden prechat fields are resolved when the widget is actually ready. In
assets/src/js/cmp-manager.js, theprechatFieldslookup now happens inside theonEmbeddedMessagingReadyhandler instead of before the handler is registered. That avoids bailing out too early when consent/bootstrap data is populated slightly later in the page lifecycle.Pre-review checklist
Please make sure the items below have been covered before requesting a review:
Pre-deploy checklist
Steps to Test
repos/vip-agentforce.env.php/VIP_AGENTFORCE_CONFIGSso the plugin has a valid Agentforce embed config and asite_keyvalue.Custom.site_keyinstead ofsite_id_blog_id.site_keyis present in the plugin-side ingestion record shape.