This repository has been archived by the owner on Aug 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
in-app-browser-escape.php
134 lines (114 loc) · 2.91 KB
/
in-app-browser-escape.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
/**
* Plugin Name: Escape Facebook/Instagram In-App Browsers
* Description: Site visitors using Facebook/Instagram in-app browsers on Android devices will be prompted open a link in external browser.
* Author: WPForms
* Author URI: https://wpforms.com
* Version: 1.0.2
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
/**
* Break out of Instagram/Facebook in-app browser on Android.
*
* @since 1.0.0
*/
add_action(
'template_redirect',
static function () {
if ( ! in_breakout_mode() ) {
return;
}
if ( in_breakout_mode() && is_inapp_browser() ) {
header( 'Content-type: image/gif' );
header( 'Content-Disposition: attachment; filename=out.gif' );
header( 'Content-Transfer-Encoding: binary' );
header( 'Accept-Ranges: bytes' );
echo base64_decode('R0lGODlhAQABAJAAAP8AAAAAACH5BAUQAAAALAAAAAABAAEAAAICBAEAOw=='); // phpcs:ignore
exit;
}
if ( in_breakout_mode() && ! empty( $_GET['redirect_to'] ) ) { // phpcs:ignore
nocache_headers();
wp_safe_redirect( esc_url_raw( $_GET['redirect_to'] ) ); // phpcs:ignore
exit;
}
}
);
/**
* Register custom query var for identifying Instagram/Facebook.
*
* @since 1.0.0
*
* @param array $query_vars Allowlist of query variables.
*
* @return array Updated list.
*/
add_filter(
'query_vars',
static function ( $query_vars ) {
$query_vars[] = 'in-app';
return $query_vars;
}
);
/**
* Register custom rewrite rule to redirect Instagram/Facebook in-app browser to.
*
* @since 1.0.0
*/
add_action(
'init',
static function () {
add_rewrite_rule( '^in-app?','index.php?in-app=1','top' );
}
);
/**
* Flush rewrite rules on plugin activation.
*/
register_activation_hook(
__FILE__,
static function () {
delete_option( 'rewrite_rules' );
}
);
/**
* Print a simple redirect script for Instagram/Facebook in-app browser on Android.
*
* @since 1.0.0
*/
add_action(
'wp_head',
static function () {
?>
<script>
if( navigator.userAgent.indexOf('Android') !== -1 && ( navigator.userAgent.indexOf('Instagram') !== -1 || navigator.userAgent.match(/\bFB[\w_]+\//) !== null ) ) {
var originalLocation = window.location.href;
window.location.href = '<?php echo esc_url_raw( home_url( 'in-app/?redirect_to=' ) ); ?>' + originalLocation;
}
</script>
<?php
}
);
/**
* Is it Instagram/Facebook in-app browser on Android?
*
* @since 1.0.0
*
* @return bool
*/
function is_inapp_browser() {
// phpcs:disable
return isset( $_SERVER['HTTP_USER_AGENT'] )
&& strpos( $_SERVER['HTTP_USER_AGENT'], 'Android' )
&& ( strpos( $_SERVER['HTTP_USER_AGENT'], 'Instagram' ) || preg_match( '/\bFB[\w_]+\//', $_SERVER['HTTP_USER_AGENT'] ) );
// phpcs:enable
}
/**
* Are we in the break-out of in-app browser redirect?
*
* @since 1.0.0
*
* @return bool
*/
function in_breakout_mode() {
return get_query_var( 'in-app' ) !== '';
}