-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathplugin.php
64 lines (57 loc) · 1.78 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
<?php
/*
Plugin Name: API Action - Delete
Plugin URI: http://www.claytondaley.com/
Description: Adds a "delete" action to the API. This action requires authentication even if the site is public. This action accepts either a shorturl or keyword passed using the "shorturl" parameter.
Version: 1.0
Author: Clayton Daley (derived from API-Action by Ozh)
Author URI: http://www.claytondaley.com/
*/
// Define custom action "delete"
yourls_add_filter( 'api_action_delete', 'clayton_api_action_delete' );
// Actually delete
function clayton_api_action_delete() {
// We don't want unauthenticated users deleting links
// If YOURLS is in public mode, force authentication anyway
if (!yourls_is_private()) {
yourls_do_action( 'require_auth' );
require_once( YOURLS_INC.'/auth.php' );
}
// Need 'shorturl' parameter
if( !isset( $_REQUEST['shorturl'] ) ) {
return array(
'statusCode' => 400,
'simple' => "Need a 'shorturl' parameter",
'message' => 'error: missing param',
);
}
$shorturl = $_REQUEST['shorturl'];
// Check if valid shorturl
if( !yourls_is_shorturl( $shorturl ) ) {
return array(
'statusCode' => 404,
'simple' => 'Error: short URL not found',
'message' => 'error: not found',
);
}
// Is $shorturl a URL (http://sho.rt/abc) or a keyword (abc) ?
if( yourls_get_protocol( $shorturl ) ) {
$keyword = yourls_get_relative_url( $shorturl );
} else {
$keyword = $shorturl;
}
// Delete shorturl
if( yourls_delete_link_by_keyword( $keyword ) ) {
return array(
'statusCode' => 200,
'simple' => "Shorturl $shorturl deleted",
'message' => 'success: deleted',
);
} else {
return array(
'statusCode' => 500,
'simple' => 'Error: could not delete shorturl, not sure why :-/',
'message' => 'error: unknown error',
);
}
}