PHP 8.2.31
Preview: UsePubSub.php Size: 1.51 KB
//home/nshryvcy/radiantskinclinics.org/wp-content/plugins/woocommerce/packages/blueprint/src/UsePubSub.php

<?php

namespace Automattic\WooCommerce\Blueprint;

trait UsePubSub {

	/**
	 * Subscribers.
	 *
	 * @var array
	 */
	private array $subscribers = array();

	/**
	 * Subscribe to an event with a callback.
	 *
	 * @param string   $event The event name.
	 * @param callable $callback The callback to execute when the event is published.
	 * @return void
	 */
	public function subscribe( string $event, callable $callback ): void {
		if ( ! isset( $this->subscribers[ $event ] ) ) {
			$this->subscribers[ $event ] = array();
		}

		$this->subscribers[ $event ][] = $callback;
	}

	/**
	 * Publish an event to all subscribers.
	 *
	 * @param string $event The event name.
	 * @param mixed  ...$args Arguments to pass to the callbacks.
	 * @return void
	 */
	public function publish( string $event, ...$args ): void {
		if ( ! isset( $this->subscribers[ $event ] ) ) {
			return;
		}

		foreach ( $this->subscribers[ $event ] as $callback ) {
			call_user_func( $callback, ...$args );
		}
	}

	/**
	 * Unsubscribe a specific callback from an event.
	 *
	 * @param string   $event The event name.
	 * @param callable $callback The callback to remove.
	 * @return void
	 */
	public function unsubscribe( string $event, callable $callback ): void {
		if ( ! isset( $this->subscribers[ $event ] ) ) {
			return;
		}

		$this->subscribers[ $event ] = array_filter(
			$this->subscribers[ $event ],
			fn( $subscriber ) => $subscriber !== $callback
		);

		if ( empty( $this->subscribers[ $event ] ) ) {
			unset( $this->subscribers[ $event ] );
		}
	}
}

Directory Contents

Dirs: 8 × Files: 15

Name Size Perms Modified Actions
Cli DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
docs DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
Exporters DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
Importers DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
Schemas DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
Steps DIR
- drwxr-xr-x 2026-05-29 02:43:21
Edit Download
474 B lrw-r--r-- 2025-05-12 21:07:28
Edit Download
1.81 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
6.05 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
1.78 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
4.30 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
2.34 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
4.46 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
4.07 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
1.45 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
680 B lrw-r--r-- 2025-05-12 21:07:28
Edit Download
3.56 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
3.13 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
1.51 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
9.72 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download
4.39 KB lrw-r--r-- 2025-05-12 21:07:28
Edit Download

If ZipArchive is unavailable, a .tar will be created (no compression).