Skip to content

Commit 45779ae

Browse files
authored
Home screen: Use correct url when adding to phone's home screen (#2889)
* Implement dynamic start_url in PWA manifest based on HTTP referrer - Added a method to determine the start_url for the PWA manifest based on the HTTP referrer. - If the referrer is a dt-home page, the corresponding URL is used as the start_url; otherwise, it defaults to '/'. - This change enhances the user experience by ensuring the app launches from the appropriate context when accessed from different entry points. * Refactor PWA URL handling for magic key extraction - Cleaned up whitespace in the URL validation logic for better readability. - Ensured consistent formatting in the conditional checks for the magic key. - Maintained functionality to validate and construct the dt-home URL based on the magic key from the referrer path.
1 parent af152a2 commit 45779ae

1 file changed

Lines changed: 57 additions & 1 deletion

File tree

dt-core/configuration/class-pwa.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ private function print_manifest_json() {
7777
$instance_name = get_bloginfo( 'name' );
7878
$instance_desc = get_bloginfo( 'description' );
7979

80+
// Determine start_url based on referrer
81+
// If manifest is requested from dt-home page, use that URL as start_url
82+
$start_url = $this->get_start_url_from_referrer();
83+
8084
$data = array(
8185
'id' => home_url(),
8286
'name' => $instance_name,
@@ -87,7 +91,7 @@ private function print_manifest_json() {
8791
'orientation' => 'portrait',
8892
'display' => 'minimal-ui',
8993
'scope' => '/',
90-
'start_url' => '/',
94+
'start_url' => $start_url,
9195
'icons' => [
9296
[
9397
'sizes' => '192x192',
@@ -141,6 +145,58 @@ private function print_manifest_json() {
141145
exit;
142146
}
143147

148+
/**
149+
* Get start_url based on HTTP referrer.
150+
* If referrer is a dt-home page (apps/launcher/{key}), return that URL.
151+
* Otherwise, return default '/'.
152+
*
153+
* @since 1.0.0
154+
* @return string The start_url to use in manifest
155+
*/
156+
private function get_start_url_from_referrer() {
157+
// Check if HTTP_REFERER is set
158+
if ( ! isset( $_SERVER['HTTP_REFERER'] ) || empty( $_SERVER['HTTP_REFERER'] ) ) {
159+
return '/';
160+
}
161+
162+
$referer = esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
163+
164+
// Parse the referrer URL
165+
$referer_parts = parse_url( $referer );
166+
if ( ! isset( $referer_parts['path'] ) ) {
167+
return '/';
168+
}
169+
170+
$referer_path = trim( $referer_parts['path'], '/' );
171+
$path_segments = explode( '/', $referer_path );
172+
173+
// Check if referrer is a dt-home page: apps/launcher/{magic_key}
174+
// Path segments should be: ['apps', 'launcher', '{magic_key}']
175+
if ( count( $path_segments ) >= 3
176+
&& $path_segments[0] === 'apps'
177+
&& $path_segments[1] === 'launcher'
178+
&& ! empty( $path_segments[2] ) ) {
179+
180+
// Extract the magic key (third segment)
181+
$magic_key = sanitize_text_field( $path_segments[2] );
182+
183+
// Validate magic key format (should be alphanumeric, typically 32+ chars)
184+
if ( ! empty( $magic_key ) && strlen( $magic_key ) >= 16 ) {
185+
// Build the dt-home URL path (base URL only, no additional path segments)
186+
// This ensures the app always starts at the home screen, not a sub-page
187+
$dt_home_path = '/apps/launcher/' . $magic_key;
188+
189+
// Note: We intentionally don't preserve query strings or additional path segments
190+
// to ensure consistent behavior when launching from home screen
191+
192+
return $dt_home_path;
193+
}
194+
}
195+
196+
// Default: return home page
197+
return '/';
198+
}
199+
144200
public function scripts() {
145201
// to add a custom install prompt, include this js.
146202
// dt_theme_enqueue_script( 'pwa', 'dt-assets/js/pwa.js' );

0 commit comments

Comments
 (0)