-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmu-class-wp-webhook.php
101 lines (83 loc) · 2.67 KB
/
mu-class-wp-webhook.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: WordPress Custom Webhook
* Plugin URI: https://github.com/Preciousomonze/wordpress-custom-webhook
* Description: Helps create the webhook url and also handling whatever is sent to the url :)
* Author: Precious Omonzejele (Code Explorer)
* Author URI: https://codeexplorer.ninja
* Version: 1.0.0
* Requires at least: 4.9.0
* Tested up to: 5.2
* License: GPLv3 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Pekky_WP_Webhook' ) ) {
class Pekky_WP_Webhook {
private static $_instance = null;
// These paramaters are for custom webhook
//your url will look like https://site.com/pekky-api/pekky_webhook
// network_site_url( self::$webhook . DIRECTORY_SEPARATOR . self::$webhook_tag ); //this helps return the full url
/**
* Parent wekbhook
* replace with a unique value you want
*
* @var string
*/
private static $webhook = 'pekky-api';
/**
* webhook tag
* replace with a unique value you want
*
* @var string
*/
private static $webhook_tag = 'pekky_webhook';
/**
* ini prefix, leave as it is :)
*
* @var string
*/
private static $ini_hook_prefix = 'pekky_';
/**
* Action to be triggered when the url is loaded
* replace with a unique value you want
*
* @var string
*/
private static $webhook_action = 'hook_action';
/**
* Construdor :)
*/
public function __construct() {
add_action( 'init', array( $this, 'setup' ) );
add_action( 'parse_request', array( $this, 'parse_request' ) );
add_action( self::$ini_hook_prefix.self::$webhook_action, array( $this, 'webhook_handler' ) );
}
public function setup() {
$this->add_rewrite_rules_tags();
$this->add_rewrite_rules();
}
/**
* Handles the HTTP Request sent to your site's webhook
*/
public function webhook_handler() {
$input = $_POST;
//start your payload processing here
}
public function parse_request( &$wp ) {
$ini = self::$ini_hook_prefix;
if( array_key_exists( self::$webhook_tag, $wp->query_vars ) ) {
do_action( $ini.self::$webhook_action );
die(0);
}
}
protected function add_rewrite_rules_tags() {
add_rewrite_tag( '%' . self::$webhook_tag . '%', '([^&]+)' );
}
protected function add_rewrite_rules() {
add_rewrite_rule( '^' . self::$webhook . '/([^/]*)/?', 'index.php?' . self::$webhook_tag . '=$matches[1]', 'top' );
}
}
}
new Pekky_WP_Webhook();