<?php
/**
 * Administration réseau (multisite).
 *
 * @package PCC
 * @license GPL-2.0-or-later
 */

defined( 'ABSPATH' ) || exit;

class PCC_Network_Admin {

	const PAGE_SLUG = 'php-compatibility-checker-by-volade-network';

	/** @var PCC_Plugin */
	private $plugin;

	public function __construct( PCC_Plugin $plugin ) {
		$this->plugin = $plugin;
		add_action( 'network_admin_menu', array( $this, 'register_menu' ) );
		add_action( 'network_admin_edit_pcc_save_network_defaults', array( $this, 'handle_save_defaults' ) );
	}

	public function register_menu() {
		add_submenu_page(
			'settings.php',
			__( 'PHP Compatibility Checker by Volade', 'php-compatibility-checker-by-volade' ),
			__( 'PHP Compatibility', 'php-compatibility-checker-by-volade' ),
			'manage_network_options',
			self::PAGE_SLUG,
			array( $this, 'render_page' )
		);
	}

	public static function settings_url( $extra = array() ) {
		$args = array_merge( array( 'page' => self::PAGE_SLUG ), $extra );
		return network_admin_url( 'settings.php?' . http_build_query( $args ) );
	}

	public function handle_save_defaults() {
		if ( ! current_user_can( 'manage_network_options' ) ) {
			wp_die( esc_html__( 'Access denied.', 'php-compatibility-checker-by-volade' ) );
		}
		check_admin_referer( 'pcc_save_network_defaults' );

		$defaults = array(
			'target_php'   => sanitize_text_field( wp_unslash( $_POST['target_php'] ?? '' ) ),
			'skip_vendor'  => ! empty( $_POST['skip_vendor'] ),
			'scan_themes'  => ! empty( $_POST['scan_themes'] ),
		);
		update_site_option( 'pcc_network_defaults', $defaults );

		wp_safe_redirect( add_query_arg( 'updated', '1', self::settings_url() ) );
		exit;
	}

	public function render_page() {
		if ( ! current_user_can( 'manage_network_options' ) ) {
			return;
		}

		$defaults = get_site_option( 'pcc_network_defaults', PCC_Settings::defaults() );
		$sites    = get_sites( array( 'number' => 50 ) );

		echo '<div class="wrap pcc-wrap pcc-wrap-network">';
		echo '<h1>' . esc_html__( 'PHP Compatibility Checker by Volade', 'php-compatibility-checker-by-volade' ) . '</h1>';

		if ( isset( $_GET['updated'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
			echo '<div class="notice notice-success"><p>' . esc_html__( 'Network defaults saved.', 'php-compatibility-checker-by-volade' ) . '</p></div>';
		}

		echo '<p>' . esc_html__( 'Configure recommended defaults for new subsites. Each site runs scans from its own admin screen.', 'php-compatibility-checker-by-volade' ) . '</p>';

		echo '<form method="post" action="' . esc_url( network_admin_url( 'edit.php?action=pcc_save_network_defaults' ) ) . '">';
		wp_nonce_field( 'pcc_save_network_defaults' );
		echo '<table class="form-table"><tbody>';
		echo '<tr><th><label for="pcc-net-target">' . esc_html__( 'Default target PHP', 'php-compatibility-checker-by-volade' ) . '</label></th>';
		echo '<td><input name="target_php" id="pcc-net-target" type="text" value="' . esc_attr( $defaults['target_php'] ?? '' ) . '" class="regular-text" /></td></tr>';
		echo '<tr><th>' . esc_html__( 'Options', 'php-compatibility-checker-by-volade' ) . '</th><td>';
		echo '<label><input type="checkbox" name="scan_themes" value="1" ' . checked( ! empty( $defaults['scan_themes'] ), true, false ) . ' /> ' . esc_html__( 'Scan themes by default', 'php-compatibility-checker-by-volade' ) . '</label><br />';
		echo '<label><input type="checkbox" name="skip_vendor" value="1" ' . checked( ! isset( $defaults['skip_vendor'] ) || ! empty( $defaults['skip_vendor'] ), true, false ) . ' /> ' . esc_html__( 'Skip vendor folders', 'php-compatibility-checker-by-volade' ) . '</label>';
		echo '</td></tr></tbody></table>';
		submit_button( __( 'Save network defaults', 'php-compatibility-checker-by-volade' ) );
		echo '</form>';

		if ( ! empty( $sites ) ) {
			echo '<h2>' . esc_html__( 'Subsites', 'php-compatibility-checker-by-volade' ) . '</h2>';
			echo '<table class="widefat striped"><thead><tr>';
			echo '<th>' . esc_html__( 'Site', 'php-compatibility-checker-by-volade' ) . '</th>';
			echo '<th>' . esc_html__( 'Scan page', 'php-compatibility-checker-by-volade' ) . '</th>';
			echo '</tr></thead><tbody>';
			foreach ( $sites as $site ) {
				$url = get_admin_url( (int) $site->blog_id, 'tools.php?page=php-compatibility-checker-by-volade' );
				echo '<tr><td>' . esc_html( $site->domain . $site->path ) . '</td>';
				echo '<td><a href="' . esc_url( $url ) . '">' . esc_html__( 'Open scan', 'php-compatibility-checker-by-volade' ) . '</a></td></tr>';
			}
			echo '</tbody></table>';
		}

		echo '</div>';
	}
}
