Skip to content

Instantly share code, notes, and snippets.

@Preciousomonze
Created July 10, 2019 11:34
Show Gist options
  • Save Preciousomonze/488607adb5cd9315a82622141678b3b6 to your computer and use it in GitHub Desktop.
Save Preciousomonze/488607adb5cd9315a82622141678b3b6 to your computer and use it in GitHub Desktop.
Sendbox Shipping woocommerce plugin - Webhook dev
<?php
/**
* Webhook class to handle everything webhook
*
* Helps create the webhook url and also handling whatever is sent to the url :)
*
* @author Precious Omonze (Code Explorer) <https://github.com/Preciousomonze>
* License: GPLv2 or later
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'Woos_Webhook' ) ) {
class Woos_Webhook {
private static $_instance = null;
// Some custom parameters specific to gateway
// These paramaters are for custom webhook
private static $webhook = 'sendbox-api';
private static $webhook_tag = 'pekky_sendbox_webhook';//:)
private static $webhook_action = 'pekky_wh_action';//action to be triggered when the url is loaded
public function __construct() {
add_action( 'init', array( $this, 'setup' ) );
add_action( 'parse_request', array( $this, 'parse_request' ) );
add_action( self::$webhook_action, array( $this, 'webhook_handler' ) );
}
public function setup() {
$this->add_rewrite_rules_tags();
$this->add_rewrite_rules();
}
/**
* Handles the HTTP Request sent from SendBox to site's webhook
*
*
* @since 1.0.0
*
* @return bool
*/
public function webhook_handler() {
echo "in, your face, my 7k";
$input = $_POST;
//start your payload processing here
}
// network_site_url( self::$webhook . DIRECTORY_SEPARATOR . self::$webhook_tag ) //this helps return the full url
public function parse_request( &$wp ) {
if( array_key_exists( self::$webhook_tag, $wp->query_vars ) ) {
do_action( self::$webhook_action );
die(0);
}
}
protected function add_rewrite_rules_tags() {
add_rewrite_tag( '%' . self::$webhook_tag . '%', '([^&]+)' );
}
protected function add_rewrite_rules() {
//To use like http://site.com/sendbox-api/pekky_sendbox_webhook/
add_rewrite_rule( '^' . self::$webhook . '/([^/]*)/?', 'index.php?' . self::$webhook_tag . '=$matches[1]', 'top' );
}
}
}
new Woos_Webhook();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment