<?php
/**
 * Plugin Name: 1&1 Product Subdomain
 * Plugin URI: http://www.1and1.com/
 * Description: Handles product subdomain installs in accordance with search engines best practices.
 * Version: 1.1.0
 * Author: 1&1
 * Author URI: http://www.1and1.com/
 */

if ( ! defined( 'ABSPATH' ) ) {
	die();
}

class Product_Subdomains {

	public function __construct() {
		$domain = $this->get_main_domain( $_SERVER['SERVER_NAME'] );

		if ( in_array( $domain, $this->product_subdomains() ) ) {
			update_option( 'blog_public', 0 );
			if ( $this->is_options_reading_page() ) {
				add_action( 'admin_footer', array( $this, 'disable_input' ), 1000, 0 );
			}
		}
	}

	public function disable_input() {
		echo '<script>document.querySelector("#blog_public").disabled = true</script>';
	}

	private function get_main_domain( $domain ) {
		$domain_parts = explode( '.', $domain );
		$domain_parts = array_slice( $domain_parts, - 2 );
		$main_domain  = implode( '.', $domain_parts );

		return $main_domain;
	}

	private function product_subdomains() {
		return array(
			'apps-1and1.net',
			'apps-1and1.com',
			'online.de',
			'live-website.com'
		);
	}

	private function is_options_reading_page() {
		return stripos( admin_url( 'options-reading.php' ), $_SERVER['SCRIPT_NAME'] );
	}

}

new Product_Subdomains;