-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathfunctions.php
More file actions
305 lines (261 loc) · 11 KB
/
functions.php
File metadata and controls
305 lines (261 loc) · 11 KB
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<?php
// Locale detection.
require dirname( __FILE__ ) . '/inc/locale.php';
function browsehappy_get_browser_data( $browser = false ) {
// In order to avoid non-English language translations of browser
// descriptions from being invalidated due to string changes, until such time
// as traslators submit translations, only English will immediately show the
// latest strings. Other languages will temporarily show the outdated (but
// translated at least) strings.
$latest_strings = ( ! class_exists( 'Browse_Happy_Locale' ) || 0 === strpos( Browse_Happy_Locale::locale(), 'en' ) );
$data = array(
'chrome' => (object) array(
'name' => 'Google Chrome',
'long_name' => 'Google Chrome',
'wikipedia' => 'Google_Chrome',
'wikidata' => 'Q777',
'normalized' => 1, // just first number
'facebook' => 'googlechrome',
'url' => class_exists( 'Browse_Happy_Locale' ) && 'zh_CN' === Browse_Happy_Locale::locale() ? 'https://www.google.cn/chrome' : 'https://www.google.com/chrome',
'info' => ( $latest_strings ?
__( '“Get more done with the new Google Chrome. A more simple, secure, and faster web browser than ever, with Google’s smarts built-in.”', 'browsehappy' )
: __( '“A fast new browser from Google. Try it now!”', 'browsehappy' )
),
),
'firefox' => (object) array(
'name' => 'Mozilla Firefox',
'long_name' => 'Mozilla Firefox',
'wikipedia' => 'Firefox',
'wikidata' => 'Q698',
'normalized' => 1.5, // include second number if non-zero
'facebook' => 'Firefox',
'url' => 'https://www.mozilla.org/firefox/',
'info' => ( $latest_strings ?
__( '“Faster page loading, less memory usage and packed with features, the new Firefox is here.”', 'browsehappy' )
: __( "“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',
'wikidata' => 'Q35773',
'normalized' => 1.5, // include second number if non-zero
'facebook' => false,
'url' => 'https://www.apple.com/safari/',
'info' => ( $latest_strings ?
__( '“Safari is faster and more energy efficient than other browsers. You can shop safely and simply in Safari on your Mac.”', 'browsehappy' )
: str_replace( 'and Windows ', '', __( '“Safari for Mac and Windows from Apple, the world’s most innovative browser.”', 'browsehappy' ) )
),
),
'vivaldi' => (object) array(
'name' => 'Vivaldi',
'long_name' => 'Vivaldi',
'wikipedia' => 'Vivaldi_(web_browser)',
'wikidata' => 'Q18913176',
'normalized' => 1.5, // include second number if non-zero
'facebook' => 'vivaldi.browser',
'url' => 'https://vivaldi.com/',
'info' => __( '“Powerful. Personal. Private. It’s a web browser. But fun, with clever features.”', 'browsehappy' ),
),
'edge' => (object) array(
'name' => 'Microsoft Edge',
'long_name' => 'Microsoft Edge',
'wikipedia' => 'Microsoft_Edge',
'wikidata' => 'Q18698690',
'normalized' => 1, // just first number
'facebook' => 'MicrosoftEdge',
'url' => 'https://www.microsoft.com/edge',
'info' => ( $latest_strings ?
__( '“Microsoft Edge offers world-class performance with more privacy, more productivity, and more value while you browse.”', 'browsehappy' )
: __( '“Microsoft Edge ranks first when put to real world page load tests. Whether you use the web to search, watch or play, this browser won’t slow you down.”', 'browsehappy' )
),
),
'brave' => (object) array(
'name' => 'Brave',
'long_name' => 'Brave',
'wikipedia' => 'Brave_(web_browser)',
'wikidata' => 'Q22906900',
'normalized' => 1.5, // include second number if non-zero
'facebook' => 'bravetheinternet',
'url' => 'https://brave.com/',
'info' => __( '“The Brave browser is a fast, private and secure web browser for PC, Mac and mobile.”', '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, $rank = true ) {
$fragment = browsehappy_get_browser_data( $browser )->wikidata;
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;
}
$rank_type = $rank ? 'PreferredRank' : 'NormalRank';
$limit = $rank ? 'LIMIT 1' : '';
// See https://github.com/WordPress/browsehappy/issues/37
$query = "
SELECT ?version WHERE {
wd:{$fragment} p:P348 [
ps:P348 ?version;
pq:P548 wd:Q2804309;
wikibase:rank wikibase:{$rank_type}
].
}
ORDER BY DESC (?version) {$limit}
";
$request = wp_remote_get( add_query_arg(
array(
'format' => 'json',
'query' => rawurlencode( $query ),
),
'https://query.wikidata.org/bigdata/namespace/wdq/sparql'
) );
if ( is_wp_error( $request ) ) {
return false;
}
$data = json_decode( wp_remote_retrieve_body( $request ) );
if (
empty( $data ) ||
empty( $data->results ) ||
! is_array( $data->results->bindings )
) {
return false;
}
if (
empty( $data->results->bindings[0] ) ||
empty( $data->results->bindings[0]->version ) ||
empty( $data->results->bindings[0]->version->value )
) {
if ( $rank ) {
return browsehappy_fetch_version( $browser, $normalize, false );
} else {
return false;
}
}
if ( ! $rank ) {
usort( $data->results->bindings, function( $a, $b ) {
return strcmp( $b->version->value, $a->version->value );
} );
}
$version = $data->results->bindings[0]->version->value;
// Remove minor version inside parentheses, like Vivaldi's "6.4 (3160.34)"
$version = preg_replace( '#\(\d+\.\d+\)#', '', $version );
$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( '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;
}
// Short-circuit query and 404 handling on the front-end.
if ( is_admin() ) :
class BrowseHappy_WP extends WP {}
else :
class BrowseHappy_WP extends WP {
function query_posts() { }
function handle_404() {
status_header( 200 );
}
}
endif;
function browsehappy_load_textdomain() {
load_theme_textdomain( 'browsehappy', get_template_directory() . '/languages' );
/* translators: Enter either 'ltr' if target language is written left-to-right or 'rtl' if target language is written right-to-left. */
$GLOBALS['wp_locale']->text_direction = _x( 'ltr', 'text direction', 'browsehappy' );
}
add_action( 'after_setup_theme', 'browsehappy_load_textdomain' );
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
}
if ( class_exists( 'Browse_Happy_Locale' ) )
add_action( 'browsehappy_locale_notice', 'browsehappy_locale_notice' );
function browsehappy_locale_notice() {
if ( 0 === strpos( Browse_Happy_Locale::locale(), 'en' ) ) // && Browse_Happy_Locale::$guessed )
return;
?>
<div id="i18n-alert">
<p><?php
/* translators: "English" should be translated directly and not to the name of your language. */
printf( __( 'Browse Happy is also available in English. <a href="%s">Click here to change the language to English</a>.', 'browsehappy' ), '/?locale=en' );
?></p>
</div>
<?php
}