| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * WP AI Client: WP_AI_Client_Event_Dispatcher class 4 * 5 * @package WordPress 6 * @subpackage AI 7 * @since 7.0.0 8 */ 9 10 use WordPress\AiClientDependencies\Psr\EventDispatcher\EventDispatcherInterface; 11 12 /** 13 * WordPress-specific PSR-14 event dispatcher for the AI Client. 14 * 15 * Bridges PSR-14 events to WordPress action hooks, enabling plugins to hook 16 * into AI client lifecycle events. 17 * 18 * @since 7.0.0 19 * @internal Intended only to wire up the PHP AI Client SDK to WordPress's hook system. 20 * @access private 21 */ 22 class WP_AI_Client_Event_Dispatcher implements EventDispatcherInterface { 23 24 /** 25 * Dispatches an event to WordPress action hooks. 26 * 27 * Converts the event class name to a WordPress action hook name and fires it. 28 * For example, BeforeGenerateResultEvent becomes wp_ai_client_before_generate_result. 29 * 30 * @since 7.0.0 31 * 32 * @param object $event The event object to dispatch. 33 * @return object The same event object, potentially modified by listeners. 34 */ 35 public function dispatch( object $event ): object { 36 $event_name = $this->get_hook_name_portion_for_event( $event ); 37 38 /** 39 * Fires when an AI client event is dispatched. 40 * 41 * The dynamic portion of the hook name, `$event_name`, refers to the 42 * snake_case version of the event class name, without the `_event` suffix. 43 * 44 * For example, an event class named `BeforeGenerateResultEvent` will fire the 45 * `wp_ai_client_before_generate_result` action hook. 46 * 47 * In practice, the available action hook names are: 48 * 49 * - wp_ai_client_before_generate_result 50 * - wp_ai_client_after_generate_result 51 * 52 * @since 7.0.0 53 * 54 * @param object $event The event object. 55 */ 56 do_action( "wp_ai_client_{$event_name}", $event ); 57 58 return $event; 59 } 60 61 /** 62 * Converts an event object class name to a WordPress action hook name portion. 63 * 64 * @since 7.0.0 65 * 66 * @param object $event The event object. 67 * @return string The hook name portion derived from the event class name. 68 */ 69 private function get_hook_name_portion_for_event( object $event ): string { 70 $class_name = get_class( $event ); 71 $pos = strrpos( $class_name, '\\' ); 72 $short_name = false !== $pos ? substr( $class_name, $pos + 1 ) : $class_name; 73 74 // Convert PascalCase to snake_case. 75 $snake_case = strtolower( (string) preg_replace( '/([a-z])([A-Z])/', '$1_$2', $short_name ) ); 76 77 // Strip '_event' suffix if present. 78 if ( str_ends_with( $snake_case, '_event' ) ) { 79 $snake_case = (string) substr( $snake_case, 0, -6 ); 80 } 81 82 return $snake_case; 83 } 84 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Jun 13 09:38:55 2026 | Cross-referenced by PHPXref |