-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
95 lines (82 loc) · 3.07 KB
/
plugin.php
File metadata and controls
95 lines (82 loc) · 3.07 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
<?php
/*
Plugin Name: Quickchart QR Code Short URLS
Plugin URI: https://github.com/tyler71/yourls-quickchart-qr-code
Description: Add suffix to shorturls to display QR Code
Version: 1.0
Author: tyler71
Author URI: https://github.com/tyler71/yourls-quickchart-qr-code
*/
// Based on https://yourls.org/docs/development/examples/qrcode
// Plugin settings page etc.
yourls_add_action( 'plugins_loaded', 'qr_code_add_settings' );
function qr_code_add_settings() {
yourls_register_plugin_page( 'qr_code_settings', 'Qr Code Settings', 'qr_code_add_settings_page' );
}
function qr_code_add_settings_page() {
$quickchart_url = yourls_get_option('quickchart_url', 'https://quickchart.io');
// Check if form was submitted
if( isset( $_POST['qr_suffix'] ) ) {
// If so, verify nonce
yourls_verify_nonce( 'qr_code_settings' );
// and process submission if nonce is valid
qr_code_setting_update();
}
$qr_suffix = yourls_get_option('qr_suffix', '/qr');
$nonce = yourls_create_nonce( 'qr_code_settings' );
echo <<<HTML
<main>
<h2>QR Code Settings</h2>
<form method="post">
<input type="hidden" name="nonce" value="$nonce" />
<p>
<label>Suffix</label>
<input type="string" name="qr_suffix" value="$qr_suffix" />
</p>
<p>
<label>Quickchart Url</label>
<input type="string" name="quickchart_url" value="$quickchart_url" />
</p>
<p><input type="submit" value="Save" class="button" /></p>
</form>
</main>
HTML;
}
function qr_code_setting_update() {
$qr_suffix = $_POST['qr_suffix'];
if( $qr_suffix ) {
if( is_string( $qr_suffix ) ) {
yourls_update_option( 'qr_suffix', strval( $qr_suffix ) );
} else {
echo "Error: Length given was not a string.";
}
} else {
echo "Error: No length value given.";
}
$quickchart_url = $_POST['quickchart_url'];
if( $quickchart_url ) {
if( is_string( $quickchart_url ) ) {
yourls_update_option( 'quickchart_url', strval( $quickchart_url ) );
} else {
echo "Error: Length given was not a string.";
}
} else {
echo "Error: No length value given.";
}
}
// Kick in if the loader does not recognize a valid pattern
yourls_add_action('redirect_keyword_not_found', 'yourls_qrcode', 1);
function yourls_qrcode( $request ) {
// Get authorized charset in keywords and make a regexp pattern
$pattern = yourls_make_regexp_pattern( yourls_get_shorturl_charset() );
// Shorturl is like bleh.qr?
if( preg_match( "@^([$pattern]+)" . yourls_get_option("qr_suffix") . "?/?$@", $request[0], $matches ) ) {
// this shorturl exists?
$keyword = yourls_sanitize_keyword( $matches[1] );
if( yourls_is_shorturl( $keyword ) ) {
// Show the QR code then!
header('Location: ' . yourls_get_option('quickchart_url') . '/qr?text='.YOURLS_SITE.'/'.$keyword);
exit;
}
}
}