<?php
/**
 * White label / Client mode (Ultimate Bundle).
 *
 * @package EasySite_Pro
 */

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

/**
 * Apply white-label customizations.
 */
function easysite_white_label_init() {
	if ( ! easysite_get_mod( 'easysite_client_mode', false ) ) {
		return;
	}

	if ( easysite_get_mod( 'easysite_hide_wp_branding', false ) ) {
		add_filter( 'admin_footer_text', '__return_empty_string' );
		add_action( 'admin_bar_menu', 'easysite_remove_wp_logo', 999 );
		add_action( 'admin_bar_menu', 'easysite_admin_bar_brand', 1 );
	}

	add_action( 'wp_dashboard_setup', 'easysite_client_dashboard_widget' );
	add_action( 'admin_menu', 'easysite_simplify_admin_menu', 999 );
}
add_action( 'init', 'easysite_white_label_init' );

/**
 * Remove WP logo from admin bar.
 *
 * @param WP_Admin_Bar $wp_admin_bar Admin bar.
 */
function easysite_remove_wp_logo( $wp_admin_bar ) {
	$wp_admin_bar->remove_node( 'wp-logo' );
}

/**
 * Agency brand in admin bar.
 *
 * @param WP_Admin_Bar $wp_admin_bar Admin bar.
 */
function easysite_admin_bar_brand( $wp_admin_bar ) {
	$wp_admin_bar->add_node( array(
		'id'    => 'easysite-brand',
		'title' => esc_html( easysite_get_brand_name() ),
		'href'  => admin_url( 'admin.php?page=easysite-setup' ),
	) );
}

/**
 * Custom welcome dashboard widget.
 */
function easysite_client_dashboard_widget() {
	$agency  = easysite_get_brand_name();
	$message = easysite_get_mod( 'easysite_help_message', __( 'Welcome! Use EasySite Setup to manage your website content easily.', 'easysite-pro' ) );

	wp_add_dashboard_widget(
		'easysite_client_welcome',
		sprintf( __( 'Welcome from %s', 'easysite-pro' ), $agency ),
		function () use ( $message ) {
			echo '<p>' . esc_html( $message ) . '</p>';
		$front_id = (int) get_option( 'page_on_front' );
		$ve_url   = $front_id ? add_query_arg( 'easysite_edit', '1', get_permalink( $front_id ) ) : add_query_arg( 'easysite_edit', '1', home_url( '/' ) );
		echo '<p><a class="button button-primary" href="' . esc_url( $ve_url ) . '">' . esc_html__( 'Edit your site visually', 'easysite-pro' ) . '</a></p>';
		echo '<p><a class="button" href="' . esc_url( admin_url( 'admin.php?page=easysite-site-settings' ) ) . '">' . esc_html__( 'Site colors & logo', 'easysite-pro' ) . '</a></p>';
		}
	);
}

/**
 * Hide advanced menus in client mode.
 */
function easysite_simplify_admin_menu() {
	$hide = array( 'tools.php', 'options-general.php' );
	if ( easysite_get_mod( 'easysite_client_mode', false ) ) {
		foreach ( $hide as $page ) {
			remove_menu_page( $page );
		}
	}
}

/**
 * Custom login logo.
 */
function easysite_login_logo() {
	if ( ! has_custom_logo() ) {
		return;
	}
	$logo_id = get_theme_mod( 'custom_logo' );
	$logo    = wp_get_attachment_image_src( $logo_id, 'full' );
	if ( ! $logo ) {
		return;
	}
	?>
	<style>
		#login h1 a {
			background-image: url(<?php echo esc_url( $logo[0] ); ?>);
			background-size: contain;
			width: 100%;
			max-width: 320px;
		}
	</style>
	<?php
}
add_action( 'login_enqueue_scripts', 'easysite_login_logo' );

/**
 * Login logo URL.
 *
 * @return string
 */
function easysite_login_logo_url() {
	return home_url( '/' );
}
add_filter( 'login_headerurl', 'easysite_login_logo_url' );
