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/1.2.6/src/Settings/NetworkSettingsPage.php
<?php

declare(strict_types=1);

namespace VPlugins\WPCloudClient\Settings;

use VPlugins\WPCloudClient\Analytics\GoogleAnalytics4;
use VPlugins\WPCloudClient\BusinessProfile\ProfileHelperPage;

/**
 * Network Admin-only settings page for multisite installs.
 *
 * These settings (GA4 tracking ID, mail interceptor toggle, business profile)
 * are single network-wide values shared by every site (see
 * {@see SettingsRepository::getGa4TrackingId()} for why), so on multisite
 * only a network super-admin may view or change them, and only from Network
 * Admin — {@see \VPlugins\WPCloudClient\Plugin} registers this instead of
 * SettingsPage when SettingsRepository::isMultisite() is true, so no
 * per-site admin_menu entry exists at all on multisite.
 */
final class NetworkSettingsPage {

	public const SLUG   = 'wpcc-network-settings';
	public const ACTION = 'wpcc_network_settings_save';

	/**
	 * Class constructor.
	 *
	 * @param SettingsRepository $settings      Settings repository instance.
	 * @param GoogleAnalytics4   $ga4           Google Analytics 4 instance.
	 * @param ProfileHelperPage  $profileHelper Business profile helper page.
	 */
	public function __construct(
		private readonly SettingsRepository $settings,
		private readonly GoogleAnalytics4 $ga4,
		private readonly ?ProfileHelperPage $profileHelper,
	) {}

	/**
	 * Register the settings page under Network Admin > Settings.
	 *
	 * @return void
	 */
	public function addMenuPage(): void {
		add_submenu_page(
			'settings.php',
			__( 'WP Cloud Client', 'wp-cloud-client' ),
			__( 'WP Cloud Client', 'wp-cloud-client' ),
			'manage_network_options',
			self::SLUG,
			[ $this, 'render' ],
		);
	}

	/**
	 * Render the network settings page HTML.
	 *
	 * @return void
	 */
	public function render(): void {
		if ( ! current_user_can( 'manage_network_options' ) ) {
			return;
		}

		$configured = $this->settings->isConfigured();
		?>
		<div class="wrap">
			<h1><?php echo esc_html( get_admin_page_title() ); ?></h1>

			<?php
			// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only display flag set by our own redirect after save(), not a state-changing action.
			if ( isset( $_GET['updated'] ) ) :
				?>
				<div class="notice notice-success is-dismissible"><p><?php esc_html_e( 'Settings saved.', 'wp-cloud-client' ); ?></p></div>
			<?php endif; ?>

			<div class="wpcc-status-indicator" style="margin: 15px 0; padding: 10px 15px; border-left: 4px solid <?php echo $configured ? '#00a32a' : '#d63638'; ?>; background: #fff;">
				<strong><?php esc_html_e( 'Connection Status:', 'wp-cloud-client' ); ?></strong>
				<?php if ( $configured ) : ?>
					<span style="color: #00a32a;"><?php esc_html_e( 'Configured', 'wp-cloud-client' ); ?></span>
				<?php else : ?>
					<span style="color: #d63638;"><?php esc_html_e( 'Not Configured', 'wp-cloud-client' ); ?></span>
				<?php endif; ?>
				<p class="description"><?php esc_html_e( 'These settings apply to every site in this network. Authentication credentials are provisioned automatically by the server.', 'wp-cloud-client' ); ?></p>
			</div>

			<form action="<?php echo esc_url( network_admin_url( 'edit.php?action=' . self::ACTION ) ); ?>" method="post">
				<?php wp_nonce_field( self::ACTION ); ?>

				<?php if ( ! $this->settings->isStaging() ) : ?>
					<h2><?php esc_html_e( 'Google Analytics 4', 'wp-cloud-client' ); ?></h2>
					<p><?php esc_html_e( 'Optionally connect a custom Google Analytics 4 property to track every site in this network.', 'wp-cloud-client' ); ?></p>
					<table class="form-table" role="presentation">
						<tr>
							<th scope="row"><label for="ga4_tracking_id"><?php esc_html_e( 'Custom Google Analytics 4 Tracking ID', 'wp-cloud-client' ); ?></label></th>
							<td>
								<input type="text" id="ga4_tracking_id" name="ga4_tracking_id" value="<?php echo esc_attr( $this->settings->getGa4TrackingId() ?? '' ); ?>" class="regular-text" placeholder="G-XXXXXXXXXX" />
								<p class="description"><?php esc_html_e( 'Enter your own Google Analytics 4 Tracking ID (format: G-XXXXXXXXXX). Tracking will run concurrently with the built-in tracking.', 'wp-cloud-client' ); ?></p>
							</td>
						</tr>
					</table>
				<?php endif; ?>

				<h2><?php esc_html_e( 'Email History', 'wp-cloud-client' ); ?></h2>
				<p><?php esc_html_e( 'WP Cloud Client intercepts outgoing emails to provide an Email History log in the dashboard. Disable this if you are using a third-party SMTP plugin that should handle all mail delivery.', 'wp-cloud-client' ); ?></p>
				<table class="form-table" role="presentation">
					<tr>
						<th scope="row"><?php esc_html_e( 'Disable Website Pro Mail System', 'wp-cloud-client' ); ?></th>
						<td>
							<label>
								<input type="checkbox" id="disable_mail_interceptor" name="disable_mail_interceptor" value="1" <?php checked( $this->settings->isMailInterceptorDisabled() ); ?> />
								<?php esc_html_e( 'Disable email interception (Email History will not be available)', 'wp-cloud-client' ); ?>
							</label>
						</td>
					</tr>
				</table>

				<?php submit_button( __( 'Save Network Settings', 'wp-cloud-client' ) ); ?>
			</form>

			<h2><?php esc_html_e( 'Business Profile', 'wp-cloud-client' ); ?></h2>
			<?php
			if ( null !== $this->profileHelper ) {
				$this->profileHelper->render();
			} else {
				printf( '<p>%s</p>', esc_html__( 'Business Profile module is not available.', 'wp-cloud-client' ) );
			}
			?>
		</div>
		<?php
	}

	/**
	 * Handle the network settings form submission.
	 *
	 * Hooked to network_admin_edit_{self::ACTION}. SettingsPage's Settings
	 * API/options.php form processing doesn't work in Network Admin context,
	 * so this saves directly through SettingsRepository and redirects back.
	 *
	 * @return void
	 */
	public function save(): void {
		check_admin_referer( self::ACTION );

		if ( ! current_user_can( 'manage_network_options' ) ) {
			wp_die( esc_html__( 'You do not have permission to do that.', 'wp-cloud-client' ) );
		}

		if ( ! $this->settings->isStaging() && isset( $_POST['ga4_tracking_id'] ) ) {
			$this->settings->setGa4TrackingId( $this->ga4->sanitizeTrackingId( wp_unslash( (string) $_POST['ga4_tracking_id'] ) ) );
		}

		$this->settings->setMailInterceptorDisabled( ! empty( $_POST['disable_mail_interceptor'] ) );

		wp_safe_redirect(
			add_query_arg(
				[
					'page'    => self::SLUG,
					'updated' => 'true',
				],
				network_admin_url( 'settings.php' ),
			)
		);
		exit;
	}
}