-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.php
56 lines (48 loc) · 1.62 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
<?php
/**
* Plugin Name: WP Slack Slash Command Example
* Description: An example of using the WordPress REST API as a backend for a Slack Slash Command
* Author: Andy Brudtkuhl
* Author URI: http://youmetandy.com
* Version: 0.1
* Plugin URI: https://github.com/abrudtkuhl/wp-slack-slash-command
* License: GPL2+
*/
add_action( 'rest_api_init', function () {
register_rest_route( 'api', '/slash', array(
'methods' => 'GET',
'callback' => 'get_content',
) );
}
function get_content() {
if( isset( $_GET['token'] ) ) {
// /heykramer gif
if( isset( $_GET['command'] ) && $_GET['command'] == 'gif' ) {
$post = get_posts( array(
'category_name' => 'gifs',
'orderby' => 'rand',
'posts_per_page' => 1,
) );
}
// /heykramer quote
if( isset( $_GET['command'] ) && $_GET['command'] == 'quote' ) {
$post = get_posts( array(
'category_name' => 'quotes',
'orderby' => 'rand',
'posts_per_page' => 1,
) );
}
// no command given? just give something random
if( !isset($post) ) {
$post = get_posts( array(
'orderby' => 'rand',
'posts_per_page' => 1,
) );
}
// slack formatted response
$response = array('response_type' => 'in_channel', 'text' => $post[0]->post_content);
return $response;
}
// huh?
return "these arent the droids you are looking for :wave:";
}