forked from YOURLS/plugin-sample
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
117 lines (105 loc) · 3.6 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
<?php
/*
Plugin Name: Slack Notifier
Plugin URI: https://github.com/jfix/yourls-plugin-slack-notifier
Description: Get a Slack notification each time someone registers a URL
Version: 1.0.1
Author: Jakob Fix
Author URI: https://github.com/jfix
*/
// No direct call
if( !defined( 'YOURLS_ABSPATH' ) ) die();
// associate the "post_add_new_link" with the "jfix_notify_slack" function
yourls_add_action( 'post_add_new_link', 'jfix_notify_slack' );
function jfix_notify_slack( $args ) {
// get the info we want to send to Slack
$techInfo = $args[3];
$url = $args[0];
$keyword = $args[1];
$shortURL = YOURLS_SITE . "/" . $keyword;
$shortSite = substr(YOURLS_SITE, strpos(YOURLS_SITE, "://") + 3);
// no guarantee the page title is provided
$title = empty($args[2]) ? $shortURL : $args[2];
$ip = empty($techInfo['url']['ip']) ? "unknown" : $techInfo['url']['ip'];
// timestamp as required by Slack
$date = empty($techInfo['url']['date']) ? time() : strtotime($techInfo['url']['date']);
// fallback date time string
$formattedDate = date("j F Y, G:i:s", $date);
// prepare the Slack-specific parameters
$slackWebhook = "__SLACK_WEBHOOK__";
$opts = [
"Content-type: application/json"
];
// documentation here: https://app.slack.com/block-kit-builder/
$contents = <<<EOS
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "📢 Somebody just registered a short URL on $shortSite. The keyword is *$keyword* and the original URL $url. Below are some more details:"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "📔 *Page title*:\n$title"
},
{
"type": "mrkdwn",
"text": "🌎 *URL*:\n$url"
},
{
"type": "mrkdwn",
"text": "🪄 *Keyword:* `$keyword`"
},
{
"type": "mrkdwn",
"text": "🖥 *IP address of creator*: $ip"
},
{
"type": "mrkdwn",
"text": "📅 *Date time*: <!date^$date^{date_short} {time}|$formattedDate UTC>"
},
{
"type": "mrkdwn",
"text": "📈 *Statistics*: <$shortURL+|$shortSite/$keyword+>"
},
]
}
]
}
EOS;
// for debugging
// error_log(print_r($args, true));
// send the POST request to Slack
yourls_http_post_body($slackWebhook, $opts, $contents);
}
/*
$args contains this array:
Array
(
[0] => https://www.google.com/
[1] => theKeyWord
[2] => Google Search
[3] => Array
(
[url] => Array
(
[keyword] => theKeyWord
[url] => https://www.google.com/
[title] => Google Search
[date] => 2021-07-12 12:48:10
[ip] => ::1
)
[status] => success
[message] => https://www.google.com/ added to database
[title] => Google Search
[html] => a DOM element, table row to be inserted in the admin table maybe?
[shorturl] => http://localhost:8080/yourls/theKeyWord
)
)
*/