Automattic\WooCommerce\Admin\Features\OnboardingTasks\Tasks

LaunchYourStore::possibly_hide_wp_admin_bar()publicWC 1.0

Hide the WP admin bar when the user is previewing the site.

Method of the class: LaunchYourStore{}

No Hooks.

Return

null. Nothing (null).

Usage

$LaunchYourStore = new LaunchYourStore();
$LaunchYourStore->possibly_hide_wp_admin_bar( $show );
$show(true|false) (required)
Whether to show the admin bar.

LaunchYourStore::possibly_hide_wp_admin_bar() code WC 9.0.1

public function possibly_hide_wp_admin_bar( $show ) {
	if ( isset( $_GET['site-preview'] ) ) { // @phpcs:ignore
		return false;
	}

	global $wp;
	$http_referer = wp_get_referer() ?? '';
	$parsed_url   = wp_parse_url( $http_referer, PHP_URL_QUERY );
	$query_string = is_string( $parsed_url ) ? $parsed_url : '';

	// Check if the user is coming from the site preview link.
	if ( strpos( $query_string, 'site-preview' ) !== false ) {
		if ( ! isset( $_SERVER['REQUEST_URI'] ) ) {
			return $show;
		}

		// Redirect to the current URL with the site-preview query string.
		$current_url =
			add_query_arg(
				array(
					'site-preview' => 1,
				),
				esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) )
			);
		wp_safe_redirect( $current_url );
		exit;
	}

	return $show;
}