*/ private array $pending = array(); public function __construct( ReleaseScheduler $scheduler, UrlResolver $urls, PairingService $pairing, RunCapabilityCache $capabilities ) { $this->scheduler = $scheduler; $this->urls = $urls; $this->pairing = $pairing; $this->capabilities = $capabilities; } public function register_hooks(): void { add_action( 'transition_post_status', array( $this, 'post_status_changed' ), 10, 3 ); add_action( 'switch_theme', array( $this, 'theme_switched' ), 10, 2 ); add_action( 'upgrader_process_complete', array( $this, 'extension_updated' ), 10, 2 ); add_action( 'shutdown', array( $this, 'flush' ) ); } public function post_status_changed( string $new_status, string $old_status, \WP_Post $post ): void { if ( ! $this->is_public_content_change( $new_status, $post ) ) { return; } $url = $this->urls->published_post( $post ); if ( $url instanceof RunUrl ) { $this->collect( ReleaseEvent::CONTENT_PUBLISHED, 'post-' . $post->ID, array( $url ) ); } } public function theme_switched( string $name, \WP_Theme $theme ): void { $this->collect( ReleaseEvent::THEME_SWITCHED, 'theme-' . $theme->get_stylesheet(), array() ); } /** * WordPress supplies the upgrader dynamically at this hook boundary. * * @param mixed $upgrader WordPress upgrader instance. * @param array $hook_extra Completed update metadata. */ public function extension_updated( $upgrader, array $hook_extra ): void { $type = $hook_extra['type'] ?? null; $action = $hook_extra['action'] ?? null; if ( 'update' !== $action || ! in_array( $type, array( 'plugin', 'theme' ), true ) ) { return; } $encoded = wp_json_encode( $hook_extra ); $source = is_string( $encoded ) ? substr( hash( 'sha256', $encoded ), 0, 16 ) : 'update'; $this->collect( ReleaseEvent::EXTENSION_UPDATED, 'extension-' . $source, array() ); } public function manual(): void { $this->collect( ReleaseEvent::MANUAL, 'manual', array() ); } public function flush(): void { foreach ( $this->pending as $event ) { $this->scheduler->schedule( $event ); } $this->pending = array(); } private function is_public_content_change( string $status, \WP_Post $post ): bool { return 'publish' === $status && in_array( $post->post_type, array( 'post', 'page', 'product' ), true ) && false === wp_is_post_revision( $post->ID ) && false === wp_is_post_autosave( $post->ID ); } /** @param list $affected_urls Public affected URLs. */ private function collect( string $type, string $source, array $affected_urls ): void { if ( ! $this->pairing->is_connected() || ! $this->can_collect( $type ) ) { return; } $urls = $this->unique_urls( array_merge( array( $this->urls->homepage() ), $affected_urls ) ); $occurred_at = gmdate( DATE_ATOM ); $external_id = 'wp-' . substr( hash( 'sha256', $type . '|' . $source . '|' . wp_generate_uuid4() ), 0, 40 ); $this->pending[] = new ReleaseEvent( $type, $external_id, $occurred_at, $urls ); } private function can_collect( string $type ): bool { return ReleaseEvent::MANUAL === $type || $this->capabilities->allows_automatic_collection(); } /** @param list $urls @return list */ private function unique_urls( array $urls ): array { $unique = array(); foreach ( $urls as $url ) { $unique[ $url->url . '|' . $url->device_profile ] = $url; } return array_values( $unique ); } }