-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.php
75 lines (62 loc) · 2.77 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
<?php
/*
Plugin Name: Regenerate Shortened URL
Plugin URI: https://github.com/TheLeonKing/yourls-api-regenerate-url
Description: Adds a custom API action 'regenerate_url' to generate a new keyword for a URL that has already been shortened.
Version: 0.1
Author: The Leon King
Author URI: https://github.com/TheLeonKing/
*/
yourls_add_filter( 'api_action_regenerate_url', 'api_regenerate_url' );
function api_regenerate_url() {
// Check if an old keyword was passed.
if ( ! isset( $_REQUEST['old'] ) ) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need an 'old' parameter",
'message' => 'error: missing param',
);
}
// Sanitize the old keyword, so as to accept both 'http://ozh.in/abc' and 'abc'.
$oldkeyword = $_REQUEST['old'];
$oldkeyword = str_replace( YOURLS_SITE . '/' , '', $oldkeyword);
$oldkeyword = yourls_sanitize_string( $oldkeyword );
// Find the long URL and title associated with the keyword.
$url = yourls_get_keyword_longurl( $oldkeyword );
$url = urldecode($url);
$title = yourls_get_keyword_title( $oldkeyword );
// Use the provided keyword, or generate a new one.
if ( isset( $_REQUEST['new'] ) ) {
$newkeyword = $_REQUEST['new'];
if ( ! yourls_keyword_is_free($newkeyword) ) {
return array(
'statusCode' => 400,
'simple' => "Keyword $newkeyword already exists",
'message' => 'error: new keyword already exists',
);
}
} else {
$newkeyword = yourls_apply_filter( 'random_keyword', $newkeyword, $url, $title );
// Keep generating new keywords until we find one that is unused.
while ( ! yourls_keyword_is_free($newkeyword) ) {
$newkeyword = yourls_apply_filter( 'random_keyword', $newkeyword, $url, $title );
}
}
// Update the database and send a response.
if ( yourls_edit_link( $url, $oldkeyword, $newkeyword, $title ) ) {
return array(
'statusCode' => 200,
'simple' => "Keyword $oldkeyword changed to $newkeyword for $url",
'message' => 'success: changed',
);
} else {
return array(
'statusCode' => 500,
'status' => 'fail',
'simple' => 'Error: could not update keyword',
'message' => 'error: unknown error',
);
}
}
?>