forked from timcrockford/yourls-api-edit-url
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
101 lines (89 loc) · 3.2 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
Plugin Name: Update Shortened URL
Plugin URI: https://github.com/timcrockford/yourls-api-edit-url
Description: Define a custom API action 'update' and 'geturl'
Version: 0.2.2
Author: Tim Crockford
Author URI: http://codearoundcorners.com/
*/
yourls_add_filter( 'api_action_update', 'api_edit_url_update' );
yourls_add_filter( 'api_action_geturl', 'api_edit_url_get' );
function api_edit_url_update() {
if ( ! isset( $_REQUEST['shorturl'] ) ) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need a 'shorturl' parameter",
'message' => 'error: missing param',
);
}
if ( ! isset( $_REQUEST['url'] ) ) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need a 'url' parameter",
'message' => 'error: missing param',
);
}
$shorturl = $_REQUEST['shorturl'];
$url = $_REQUEST['url'];
if ( yourls_get_protocol( $shorturl ) ) {
$keyword = yourls_get_relative_url( $shorturl );
} else {
$keyword = $shorturl;
}
if ( ! yourls_is_shorturl( $keyword ) ) {
return array(
'statusCode' => 404,
'status' => 'fail',
'simple ' => "Error: keyword $keyword not found",
'message' => 'error: not found',
);
}
$title = '';
if ( isset($_REQUEST['title']) ) $title = $_REQUEST['title'];
if( yourls_edit_link( $url, $keyword, $keyword, $title ) ) {
return array(
'statusCode' => 200,
'simple' => "Keyword $keyword updated to $url",
'message' => 'success: updated',
);
} else {
return array(
'statusCode' => 500,
'status' => 'fail',
'simple' => 'Error: could not edit keyword, not sure why :-/',
'message' => 'error: unknown error',
);
}
}
function api_edit_url_get() {
if ( ! isset( $_REQUEST['url'] ) ) {
return array(
'statusCode' => 400,
'status' => 'fail',
'simple' => "Need a 'url' parameter",
'message' => 'error: missing param',
);
}
$url = $_REQUEST['url'];
$url_exists = yourls_url_exists($url);
if ( $url_exists ) {
return array(
'statusCode' => 200,
'simple' => "Keyword for $url is " . $url_exists->keyword,
'message' => 'success: found',
'keyword' => $url_exists->keyword,
);
} else {
return array(
'statusCode' => 500,
'status' => 'fail',
'simple' => "Error: could not find keyword for url $url",
'message' => 'error: not found',
'keyword' => '',
);
}
}
?>