Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add camptix webhook plugin #1351

Open
wants to merge 6 commits into
base: production
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions public_html/wp-content/plugins/camptix-webhook/addons/webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
<?php
/**
* Allows integration with 3rd party services via webhook.
*/
class CampTix_Webhook extends CampTix_Addon {

/**
ivankristianto marked this conversation as resolved.
Show resolved Hide resolved
* Runs during CampTix init.
*/
public function camptix_init() {
global $camptix;

// Admin Settings UI.
if ( current_user_can( $camptix->caps['manage_options'] ) ) {
add_filter( 'camptix_setup_sections', array( $this, 'setup_sections' ) );
add_action( 'camptix_menu_setup_controls', array( $this, 'setup_controls' ), 10, 1 );
add_filter( 'camptix_validate_options', array( $this, 'validate_options' ), 10, 2 );
}

add_action( 'save_post_tix_attendee', array( $this, 'trigger_webhook_async' ), 10, 2 );
add_action( 'camptix_webhook_trigger', array( $this, 'trigger_webhook' ) );
}

/**
* Add a new section to the Setup screen.
*/
public function setup_sections( $sections ) {
$sections['webhook-ui'] = esc_html__( 'Webhook', 'wordcamporg' );

return $sections;
}

/**
* Add some controls to our Setup section.
*/
public function setup_controls( $section ) {
global $camptix;

if ( 'webhook-ui' != $section )
return;

add_settings_section( 'general', esc_html__( 'Attendees Webhook', 'wordcamporg' ), array( $this, 'setup_controls_section' ), 'camptix_options' );

// Fields
$camptix->add_settings_field_helper( 'webhook-enabled', esc_html__( 'Enabled', 'wordcamporg' ), 'field_yesno', 'general' );
$camptix->add_settings_field_helper( 'webhook-url', esc_html__( 'Webhook URL', 'wordcamporg' ), 'field_text', 'general', esc_html__( 'Add the webhook URL, ensure to start with https://', 'wordcamporg' ) );
ivankristianto marked this conversation as resolved.
Show resolved Hide resolved

add_action( 'camptix_setup_buttons', array( $this, 'setup_buttons_test_webhook' ) );
}

/**
* Runs whenever the CampTix option is updated.
*/
public function validate_options( $output, $input ) {
if ( isset( $input['webhook-enabled'] ) )
$output['webhook-enabled'] = (bool) $input['webhook-enabled'];

if ( ! empty( $input['webhook-url'] ) ) {
$output['webhook-url'] = sanitize_url( $input['webhook-url'] );
}

return $output;
}

/**
* Setup section description.
*/
public function setup_controls_section() {
?>
<p><?php esc_html_e( 'Enable webhook when attendees data get created/updated.', 'wordcamporg' ); ?></p>

<p><strong><?php esc_html_e( 'Note: Please do test the webhook before your enable it.', 'wordcamporg' ); ?></strong></p>
ivankristianto marked this conversation as resolved.
Show resolved Hide resolved
<?php
}

/**
* Setup buttons.
*/
public function setup_buttons_test_webhook() {
$button = '<button type="button" class="button button-secondary" name="camptix_action">' . esc_html__( 'Test Webhook', 'wordcamporg' ) . '</button>';

echo wp_kses_post( $button );
}


/**
* Trigger webhook asynchronously.
* Use cron to trigger webhook 5 seconds after attendee is updated.
* So this process won't block the main process. And prevents multiple triggers.
*
* @param int $post_id Attendee ID.
* @param WP_Post $post Attendee Post Object.
* @return void
*/
public function trigger_webhook_async( $post_id, $post ) {
// Trigger webhook asynchronously.
if ( ! wp_next_scheduled( 'camptix_webhook_trigger', [ $post_id ] ) ) {
wp_schedule_single_event( time() + 5, 'camptix_webhook_trigger', [ $post_id ] );
}
}

/**
* Trigger webhook when attendee is updated.
*
* @param int $post_id Attendee ID.
* @return void
*/
public function trigger_webhook( $post_id ) {
/** @var CampTix_Plugin $camptix */
global $camptix;

$camptix_options = $camptix->get_options();

$is_enabled = isset( $camptix_options['webhook-enabled'] ) ? $camptix_options['webhook-enabled'] : false;
$webhook_url = isset( $camptix_options['webhook-url'] ) ? $camptix_options['webhook-url'] : '';

if ( ! $is_enabled ) {
return;
}

if ( empty( $webhook_url ) ) {
return;
}

$post = get_post( $post_id );

if ( 'tix_attendee' !== $post->post_type ) {
return;
}

// Get attendee data.
$attendee = [
'timestamp' => time(),
'status' => $post->post_status,
'tix_email' => get_post_meta( $post_id, 'tix_email', true ),
'tix_first_name' => get_post_meta( $post_id, 'tix_first_name', true ),
'tix_last_name' => get_post_meta( $post_id, 'tix_last_name', true ),
'tix_ticket_id' => get_post_meta( $post_id, 'tix_ticket_id', true ),
'tix_coupon' => get_post_meta( $post_id, 'tix_coupon', true ),
];

// Prepare webhook data.
$response = wp_remote_post(
$webhook_url,
[
'body' => wp_json_encode( $attendee ),
'headers' => [
'Content-Type' => 'application/json',
],
]
);

// Log the response.
if ( is_wp_error( $response ) ) {
$this->log( sprintf( 'Webhook failed: %s', $response->get_error_message() ), $post_id, $response );
}

$this->log( 'Webhook triggered', $post_id, $response );
}

/**
* Write a log entry to CampTix.
*/
public function log( $message, $post_id = 0, $data = null ) {
global $camptix;
$camptix->log( $message, $post_id, $data, 'webhook' );
}

/**
* Register self as a CampTix addon.
*/
public static function register_addon() {
camptix_register_addon( __CLASS__ );
}
}

CampTix_Webhook::register_addon();
17 changes: 17 additions & 0 deletions public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* Plugin Name: CampTix - Webhook
* Description: An addon for CampTix that allows 3rd party integration via webhook.
* Version: 0.1
* Author: Ivan Kristianto
* Author URI: https://profiles.wordpress.org/ivankristianto/
*/

if ( ! defined( 'ABSPATH' ) ) {
exit;
}

function camptix_webhook_register() {
require_once( plugin_dir_path( __FILE__ ) . 'addons/webhook.php' );
}
add_action( 'camptix_load_addons', 'camptix_webhook_register' );
Loading