-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathplugin.php
82 lines (67 loc) · 1.74 KB
/
plugin.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
<?php
/*
Plugin Name: detect-mobile-device
Plugin URI: https://github.com/guessi/yourls-mobile-detect
Description: A simple plugin for converting query string by device type
Version: 3.0.0
Author: guessi
Author URI: https://github.com/guessi
*/
/**
*
* Upstream Library Included:
* - https://github.com/serbanghita/Mobile-Detect/blob/3.74.3/src/MobileDetect.php
*
* License:
* - https://github.com/serbanghita/Mobile-Detect/blob/3.74.3/LICENSE
*
*/
require_once 'MobileDetect.php';
/**
*
* no direct call
*
*/
if( !defined( 'YOURLS_ABSPATH' ) ) die();
yourls_add_filter( 'get_request', 'detect_mobile_device' );
/**
*
* Check for request query string existence
*
* @param string $keyword The query string
* @return boolean If query string have shorten url for strtolower($ostype) . $keyword
*
*/
function is_link_defined( $keyword ) {
return ! yourls_keyword_is_free( yourls_sanitize_keyword( $keyword ) );
}
/**
*
* Return query result by device type
*
* @param string $keyword The query string
* @return string strtolower($ostype) . $keyword
*
*/
function detect_mobile_device( $keyword ) {
/**
* do not apply mobile detection for valid user
* or it will break "stats" feature: http://short.en/keyword+ (endwith +)
*/
if( yourls_is_valid_user() === true ) {
return $keyword;
}
$detect = new \Detection\MobileDetect;
$result = $keyword;
if ($detect->is('iOS') || $detect->is('iPadOS')) {
if (is_link_defined( 'ios' . $keyword )) {
$result = 'ios' . $keyword;
}
}
if ($detect->is('AndroidOS')) {
if (is_link_defined( 'androidos' . $keyword )) {
$result = 'androidos' . $keyword;
}
}
return $result;
}