forked from WordPress/browsehappy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
226 lines (186 loc) · 8.32 KB
/
functions.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php
// Locale detection.
require dirname( __FILE__ ) . '/inc/locale.php';
function browsehappy_get_browser_data( $browser = false ) {
$data = array(
'chrome' => (object) array(
'name' => 'Google Chrome',
'long_name' => 'Google Chrome',
'wikipedia' => 'Google_Chrome',
'normalized' => 1, // just first number
'facebook' => 'googlechrome',
'url' => 'http://www.google.com/chrome',
'info' => __( '“A fast new browser from Google. Try it now!”', 'browsehappy' ),
),
'firefox' => (object) array(
'name' => 'Mozilla Firefox',
'long_name' => 'Mozilla Firefox',
'wikipedia' => 'Firefox',
'normalized' => 1.5, // include second number if non-zero
'facebook' => 'Firefox',
'url' => 'http://www.firefox.com/',
'info' => __( "“Your online security is Firefox's top priority. Firefox is free, and made to help you get the most out of the web.”", 'browsehappy' ),
),
'safari' => (object) array(
'name' => 'Safari',
'long_name' => 'Apple Safari',
'wikipedia' => 'Safari',
'normalized' => 1.5, // include second number if non-zero
'facebook' => false,
'url' => 'http://www.apple.com/safari/',
'info' => __( '“Safari for Mac and Windows from Apple, the world’s most innovative browser.”', 'browsehappy' ),
),
'opera' => (object) array(
'name' => 'Opera',
'long_name' => 'Opera',
'wikipedia' => 'Opera',
'normalized' => 2, // include second number
'facebook' => 'Opera',
'url' => 'http://www.opera.com/',
'info' => __( '“The fastest browser on Earth—secure, powerful and easy to use, with excellent privacy protection. And it is free.”', 'browsehappy' ),
),
'ie' => (object) array(
'name' => 'Internet Explorer',
'long_name' => 'Microsoft Internet Explorer',
'wikipedia' => 'Internet_Explorer',
'normalized' => 1, // just first number
'facebook' => 'internetexplorer',
'url' => 'http://www.microsoft.com/windows/internet-explorer/',
'info' => __( '“Designed to help you take control of your privacy and browse with confidence. Free from Microsoft.”', 'browsehappy' ),
),
);
if ( false === $browser )
return $data;
if ( ! isset( $data[ $browser ] ) )
return false;
return $data[ $browser ];
}
add_action( 'browsehappy_version', 'browsehappy_echo_version' );
add_filter( 'get_browsehappy_version', 'browsehappy_fetch_version' );
function browsehappy_echo_version( $browser ) {
echo browsehappy_fetch_version( $browser );
}
function browsehappy_fetch_version( $browser, $normalize = true ) {
$fragment = browsehappy_get_browser_data( $browser )->wikipedia;
if ( ! $fragment )
return false;
// Unexpiring transients are autoloaded. We expire these manually on cron instead.
$stored_version = get_transient( 'browsehappy_version_' . $browser );
if ( false !== $stored_version ) {
if ( $normalize )
return browsehappy_normalize_version( $browser, $stored_version );
return $stored_version;
}
$url = 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&rvprop=content&format=php&titles=Template:Latest_stable_software_release/';
$url .= $fragment;
$response = wp_remote_get( $url );
if ( is_wp_error( $response ) )
return false;
if ( ! $content = wp_remote_retrieve_body( $response ) )
return false;
if ( ! is_serialized( $content ) )
return false;
$content = maybe_unserialize( $content );
$page = array_pop( $content['query']['pages'] );
$raw_data = explode( "\n", $page['revisions'][0]['*'] );
$version = false;
foreach( $raw_data as $data ) {
$data = trim( $data, '| ' );
if ( false !== strpos( $data, 'Android' ) || false !== strpos( $data, 'iOS' ) )
continue;
if ( ( false !== $pos = strpos( $data, 'latest_release_version' ) ) || ( false !== $pos = strpos( $data, 'latest release version' ) ) ) {
if ( $pos )
$data = substr( $data, $pos );
$version = trim( str_replace( array( 'latest_release_version', 'latest release version', '=' ), '', $data ), '| ' ) . " ";
$version = substr( $version, 0, strpos( $version, ' ' ) );
break;
}
}
if ( false === $version )
return false;
$version = preg_replace( '/[^0-9\.]/', '', $version );
set_transient( 'browsehappy_version_' . $browser, $version );
if ( $normalize )
return browsehappy_normalize_version( $browser, $version );
return $version;
}
function browsehappy_normalize_version( $browser, $version ) {
$normalize = browsehappy_get_browser_data( $browser )->normalized;
$version = explode( '.', $version );
if ( 1.5 == $normalize ) {
$return = $version[0];
if ( '0' !== $version[1] )
$return .= '.' . $version[1];
return $return;
}
$return = array();
for ( $i = 0; $i < $normalize; $i++ ) {
$return[] = $version[ $i ];
}
return implode( '.', $return );
}
add_action( 'init', 'browsehappy_schedule_version_check' );
function browsehappy_schedule_version_check() {
if ( ! wp_next_scheduled( 'browsehappy_clear_version_cache' ) )
wp_schedule_event( time(), 'twicedaily', 'browsehappy_clear_version_cache' );
}
add_action( 'browsehappy_clear_version_cache', 'browsehappy_clear_version_cache' );
function browsehappy_clear_version_cache() {
$browsers = array_keys( browsehappy_get_browser_data() );
foreach ( $browsers as $browser )
delete_transient( 'browsehappy_version_' . $browser );
}
add_action( 'browsehappy_like_button', 'browsehappy_like_button' );
function browsehappy_like_button( $browser ) {
$facebook_page = browsehappy_get_browser_data( $browser )->facebook;
if ( false === $facebook_page )
return;
?>
<p class="likebutton"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fwww.facebook.com%2F<?php echo $facebook_page; ?>&layout=button_count&show_faces=false&width=172&action=like&font=lucida+grande&colorscheme=light&height=20" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:172px; height:20px;"></iframe></p>
<?php
}
add_action( 'init', 'browsehappy_init' );
remove_action( 'template_redirect', 'wp_old_slug_redirect' );
// Runs at end of init. Supplants global WP object.
function browsehappy_init() {
if ( false === get_option( 'rewrite_rules' ) )
add_option( 'rewrite_rules', '' );
global $wp;
$wp = new BrowseHappy_WP;
}
class BrowseHappy_WP extends WP {
function query_posts() { }
function handle_404() {
status_header( 200 );
}
}
if ( function_exists( 'browsehappy_parse_user_agent' ) )
add_action( 'browsehappy_browser_notice', 'browsehappy_browser_notice' );
function browsehappy_browser_notice() {
$ua = $_SERVER['HTTP_USER_AGENT'];
$results = browsehappy_parse_user_agent( $ua );
if ( ! $results['upgrade'] )
return;
?>
<div id="browser-status" class="wrap">
<?php if ( $results['name'] == 'Internet Explorer' && strpos( $ua, 'Windows NT 5.' ) !== false ) : ?>
<?php if ( $results['insecure'] ) : ?>
<p><?php printf( __( 'It looks like you’re using an insecure version of %s.', 'browsehappy' ), $results['name'] ); ?>
<?php _e( 'Using an outdated browser makes your computer unsafe.', 'browsehappy' ); ?>
<?php else : ?>
<p><?php _e( 'It looks like you’re using an old version of Internet Explorer.', 'browsehappy' ); ?>
<?php endif; ?>
<?php _e( 'On Windows XP, you are unable to update to the latest version. For the best experience on the web, we suggest you try a new browser.', 'browsehappy' ); ?></p>
<?php elseif ( $results['insecure'] ) : ?>
<p class="browser-status-text"><?php printf( __( 'It looks like you’re using an insecure version of %s.', 'browsehappy' ), $results['name'] ); ?>
<?php _e( 'Using an outdated browser makes your computer unsafe.', 'browsehappy' ); ?>
<?php _e( 'For the best experience on the web, please update your browser.', 'browsehappy' ); ?></p>
<p class="browser-status-action"><a href="<?php echo esc_url( $results['update_url'] ); ?>"><?php _e( 'Upgrade now!', 'browsehappy' ); ?></a></p>
<?php else : ?>
<p class="browser-status-text"><?php printf( __( 'Your browser is out of date! It looks like you’re using an old version of %s.', 'browsehappy' ), $results['name'] ); ?>
<?php _e( 'For the best experience on the web, please update your browser.', 'browsehappy' ); ?></p>
<p class="browser-status-action"><a href="<?php echo esc_url( $results['update_url'] ); ?>"><?php _e( 'Upgrade now!', 'browsehappy' ); ?></a></p>
<?php endif; ?>
</div>
<?php
}