HEX
Server: nginx
System: Linux pool195-106-36.bur.atomicsites.net 6.12.57+deb12-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.12.57-1~bpo12+1 (2025-11-17) x86_64
User: (0)
PHP: 8.3.32
Disabled: pcntl_fork
Upload Files
File: /wordpress/plugins/wp-cloud-client/beta/src/BusinessProfile/ProfileRepository.php
<?php

declare(strict_types=1);

namespace VPlugins\WPCloudClient\BusinessProfile;

use VPlugins\WPCloudClient\Settings\SettingsRepository;

/**
 * Single source of truth for reading and writing business profile data.
 */
final class ProfileRepository {

	private const OPTION_KEY = 'bpr_business_profile';

	/**
	 * Constructor.
	 *
	 * @param SettingsRepository $settings Settings repository, used for multisite detection.
	 */
	public function __construct(
		private readonly SettingsRepository $settings,
	) {}

	/**
	 * Retrieve the business profile data.
	 *
	 * On multisite, this is a single network-wide value stored on the primary
	 * site — wp-cloud-manager pushes it once, to the network's main domain,
	 * so every subsite must read that same copy rather than its own.
	 *
	 * @return array<string, mixed>|null The profile data or null if not set.
	 */
	public function get(): ?array {
		$data = $this->settings->isRealMultisite()
			? get_blog_option( get_main_site_id(), self::OPTION_KEY, null )
			: get_option( self::OPTION_KEY, null );

		if ( ! is_array( $data ) || empty( $data ) ) {
			return null;
		}

		return $data;
	}

	/**
	 * Update the business profile data.
	 *
	 * On multisite, always writes to the primary site's own option,
	 * regardless of which site's context this is called from; see
	 * {@see self::get()}.
	 *
	 * @param array<string, mixed> $data The profile data to store.
	 * @return void
	 */
	public function update( array $data ): void {
		if ( $this->settings->isRealMultisite() ) {
			update_blog_option( get_main_site_id(), self::OPTION_KEY, $data );
			return;
		}

		update_option( self::OPTION_KEY, $data );
	}
}