From 7f46629e800dc5dbed14af85426b1d5c7d6d752e Mon Sep 17 00:00:00 2001 From: Ivan Kristianto Date: Sun, 28 Jul 2024 21:53:09 +0700 Subject: [PATCH 1/6] Add camptix webhook plugin --- .../camptix-webhook/addons/webhook.php | 177 ++++++++++++++++++ .../camptix-webhook/camptix-webhook.php | 17 ++ 2 files changed, 194 insertions(+) create mode 100644 public_html/wp-content/plugins/camptix-webhook/addons/webhook.php create mode 100644 public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php diff --git a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php new file mode 100644 index 000000000..e5e8723e1 --- /dev/null +++ b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php @@ -0,0 +1,177 @@ +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' ) ); + + 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() { + ?> +

+ +

+ ' . esc_html__( 'Test Webhook', 'wordcamporg' ) . ''; + + 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(); \ No newline at end of file diff --git a/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php b/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php new file mode 100644 index 000000000..46847f131 --- /dev/null +++ b/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php @@ -0,0 +1,17 @@ + Date: Sun, 28 Jul 2024 22:00:48 +0700 Subject: [PATCH 2/6] Add extra lines --- .../wp-content/plugins/camptix-webhook/addons/webhook.php | 2 +- .../wp-content/plugins/camptix-webhook/camptix-webhook.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php index e5e8723e1..40ac55ed4 100644 --- a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php +++ b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php @@ -174,4 +174,4 @@ public static function register_addon() { } } -CampTix_Webhook::register_addon(); \ No newline at end of file +CampTix_Webhook::register_addon(); diff --git a/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php b/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php index 46847f131..499bbc33e 100644 --- a/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php +++ b/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php @@ -14,4 +14,4 @@ function camptix_webhook_register() { require_once( plugin_dir_path( __FILE__ ) . 'addons/webhook.php' ); } -add_action( 'camptix_load_addons', 'camptix_webhook_register' ); \ No newline at end of file +add_action( 'camptix_load_addons', 'camptix_webhook_register' ); From 5182a386ecca773630a594ea302869fa9fa7c989 Mon Sep 17 00:00:00 2001 From: Ivan Kristianto Date: Sun, 18 Aug 2024 15:04:29 +0700 Subject: [PATCH 3/6] Add integration with various metas --- .../camptix-webhook/addons/webhook.php | 69 +++++-- .../camptix-webhook/camptix-webhook.php | 31 +++- .../camptix-webhook/inc/integration.php | 170 ++++++++++++++++++ 3 files changed, 254 insertions(+), 16 deletions(-) create mode 100644 public_html/wp-content/plugins/camptix-webhook/inc/integration.php diff --git a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php index 40ac55ed4..695a77a54 100644 --- a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php +++ b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php @@ -1,25 +1,41 @@ 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 js for testing webhook. + add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); } 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. @@ -43,7 +59,7 @@ public function setup_controls( $section ) { // 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' ) ); + $camptix->add_settings_field_helper( 'webhook-url', esc_html__( 'Webhook URL', 'wordcamporg' ), 'field_text', 'general', esc_html__( 'Webhook URL including protocol such as https://', 'wordcamporg' ) ); add_action( 'camptix_setup_buttons', array( $this, 'setup_buttons_test_webhook' ) ); } @@ -69,7 +85,7 @@ public function setup_controls_section() { ?>

-

+

' . 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. @@ -128,10 +143,13 @@ public function trigger_webhook( $post_id ) { return; } + $triggered_number = absint( get_post_meta( $post_id, 'tix_webhook_triggered_number', true ) ); + // Get attendee data. - $attendee = [ + $attendee_data = [ 'timestamp' => time(), 'status' => $post->post_status, + 'is_new_entry' => $triggered_number === 0, '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 ), @@ -139,17 +157,21 @@ public function trigger_webhook( $post_id ) { 'tix_coupon' => get_post_meta( $post_id, 'tix_coupon', true ), ]; + $attendee_data = apply_filters( 'camptix_webhook_attendee_data', $attendee_data, $post_id ); + // Prepare webhook data. $response = wp_remote_post( $webhook_url, [ - 'body' => wp_json_encode( $attendee ), + 'body' => wp_json_encode( $attendee_data ), 'headers' => [ 'Content-Type' => 'application/json', ], ] ); + update_post_meta( $post_id, 'tix_webhook_triggered_number', $triggered_number + 1 ); + // Log the response. if ( is_wp_error( $response ) ) { $this->log( sprintf( 'Webhook failed: %s', $response->get_error_message() ), $post_id, $response ); @@ -158,7 +180,30 @@ public function trigger_webhook( $post_id ) { $this->log( 'Webhook triggered', $post_id, $response ); } - /** + /** + * Enqueue scripts for admin. + * + * @param mixed $hook Current page hook. + * @return void + */ + public function admin_enqueue_scripts( $hook ) { + if ( $hook !== 'tix_ticket_page_camptix_options' ) { + return; + } + + wp_enqueue_script( + 'camptix-webhook-admin', + plugin_dir_url( Webhook\BASE_FILE ) . 'js/camptix-webhook-admin.js', + [], + '1.0', + [ + 'strategy' => 'async', + 'footer' => true, + ] + ); + } + + /** * Write a log entry to CampTix. */ public function log( $message, $post_id = 0, $data = null ) { @@ -166,12 +211,10 @@ public function log( $message, $post_id = 0, $data = null ) { $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(); diff --git a/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php b/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php index 499bbc33e..c35a10dd6 100644 --- a/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php +++ b/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php @@ -2,16 +2,41 @@ /** * Plugin Name: CampTix - Webhook * Description: An addon for CampTix that allows 3rd party integration via webhook. - * Version: 0.1 + * Version: 1.0 * Author: Ivan Kristianto * Author URI: https://profiles.wordpress.org/ivankristianto/ */ + namespace CampTix\Webhook; + if ( ! defined( 'ABSPATH' ) ) { exit; } +const BASE_DIR = __DIR__; +const BASE_FILE = __FILE__; + +require_once( __DIR__ . '/addons/webhook.php' ); +require_once __DIR__ . '/inc/integration.php'; + +/** + * Bootstrap the plugin. + * @return void + */ +function bootstrap() { + add_action( 'camptix_load_addons', __NAMESPACE__ . '\\camptix_webhook_register' ); + + Integration\bootstrap(); +} + +/** + * Register the webhook addon. + * + * @return void + */ function camptix_webhook_register() { - require_once( plugin_dir_path( __FILE__ ) . 'addons/webhook.php' ); + Addon\CampTix_Webhook::register_addon(); } -add_action( 'camptix_load_addons', 'camptix_webhook_register' ); + +// Bootstrap the plugin. +bootstrap(); \ No newline at end of file diff --git a/public_html/wp-content/plugins/camptix-webhook/inc/integration.php b/public_html/wp-content/plugins/camptix-webhook/inc/integration.php new file mode 100644 index 000000000..b4e2cff1f --- /dev/null +++ b/public_html/wp-content/plugins/camptix-webhook/inc/integration.php @@ -0,0 +1,170 @@ + setup page. + */ +function test_webhook_action() { + + if ( ! isset( $_GET['test_webhook'] ) || $_GET['test_webhook'] !== '1' ) { + return; + } + + /** @var CampTix_Plugin $camptix */ + global $camptix; + + $camptix_options = $camptix->get_options(); + + $webhook_url = isset( $camptix_options['webhook-url'] ) ? $camptix_options['webhook-url'] : ''; + + // Get attendee data. + $attendee_data = [ + 'timestamp' => time(), + 'status' => 'publish', + 'is_new_entry' => true, + 'tix_email' => 'camptix-webhook-test@wordcamp.org', + 'tix_first_name' => 'Camptix Webhook', + 'tix_last_name' => 'Test', + 'tix_ticket_id' => '0000', + 'tix_coupon' => 'Coupon_XXX', + ]; + + // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. + $attendee_data = apply_filters( 'camptix_webhook_attendee_data', $attendee_data, -1 ); + + // Prepare webhook data. + $response = wp_remote_post( + $webhook_url, + [ + 'body' => wp_json_encode( $attendee_data ), + 'headers' => [ + 'Content-Type' => 'application/json', + ], + ] + ); + + if ( is_wp_error( $response ) ) { + add_settings_error( + 'camptix-webhook', + 'error', + __( 'Webhook test failed: ', 'wordcamporg' ) . $response->get_error_message(), + 'error' + ); + return; + } + + add_settings_error( + 'camptix-webhook', + 'success', + __( 'Webhook test success', 'wordcamporg' ), + 'success' + ); +} + +/** + * Add attendees admin flag to attendee data. + * + * @param array $attendee_data Array of attendee data. + * @param int $post_id Post ID. + * @return array + */ +function add_attendees_admin_flag( $attendee_data, $post_id ): array { + // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. + if ( $post_id === -1 ) { + $admin_flag = [ 'volunteer', 'speaker', 'organiser' ]; + $attendee_data['tix_admin_flag'] = $admin_flag[ array_rand( $admin_flag ) ]; + + return $attendee_data; + } + + // Admin flag meta could be more than 1. + $attendee_data['tix_admin_flag'] = get_post_meta( $post_id, 'camptix-admin-flag', false ); + + return $attendee_data; +} + +/** + * Add attendees meta to attendee data. + * + * @param array $attendee_data Array of attendee data. + * @param int $post_id Post ID. + * @return array + */ +function add_attendees_meta( $attendee_data, $post_id ): array { + $allowed_meta = [ + 'tix_accommodations', + 'tix_allergy', + 'tix_coupon', + 'tix_first_time_attending_wp_event' + ]; + + // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. + if ( $post_id === -1 ) { + $attendee_data['tix_accommodations'] = 'yes'; + $attendee_data['tix_allergy'] = 'no'; + $attendee_data['tix_coupon'] = 'test'; + $attendee_data['tix_first_time_attending_wp_event'] = 'test'; + + return $attendee_data; + } + + foreach ( $allowed_meta as $meta_key ) { + $attendee_data[ $meta_key ] = get_post_meta( $post_id, $meta_key, true ); + } + + return $attendee_data; +} + +/** + * Add attendees questions answered to attendee data. + * + * @param array $attendee_data Array of attendee data. + * @param int $post_id Post ID. + * @return array + */ +function add_attendees_questions_answered( $attendee_data, $post_id ): array { + // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. + if ( $post_id === -1 ) { + $attendee_data['answered'] = 'N/A'; + + return $attendee_data; + } + + global $camptix; + $ticket_id = intval( get_post_meta( $post_id, 'tix_ticket_id', true ) ); + $questions = $camptix->get_sorted_questions( $ticket_id ); + $answers = get_post_meta( $post_id, 'tix_questions', true ); + + $rows = []; + foreach ( $questions as $question ) { + if ( isset( $answers[ $question->ID ] ) ) { + $answer = $answers[ $question->ID ]; + if ( is_array( $answer ) ) + $answer = implode( ', ', $answer ); + $rows[] = [ $question->post_title, $answer ]; + } + } + + $attendee_data['answered'] = $rows; + + return $attendee_data; +} \ No newline at end of file From 6822565f6279bbec07c709dff56d1cf2f0009dd6 Mon Sep 17 00:00:00 2001 From: Ivan Kristianto Date: Sun, 18 Aug 2024 15:05:02 +0700 Subject: [PATCH 4/6] Add js file to test webhook --- .../js/camptix-webhook-admin.js | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 public_html/wp-content/plugins/camptix-webhook/js/camptix-webhook-admin.js diff --git a/public_html/wp-content/plugins/camptix-webhook/js/camptix-webhook-admin.js b/public_html/wp-content/plugins/camptix-webhook/js/camptix-webhook-admin.js new file mode 100644 index 000000000..470827adb --- /dev/null +++ b/public_html/wp-content/plugins/camptix-webhook/js/camptix-webhook-admin.js @@ -0,0 +1,30 @@ +/** + * Add event listener to test button. + */ +function addEventHandler() { + const testWebhookButton = document.getElementById( 'camptix-webhook-test-url' ); + + if ( ! testWebhookButton ) { + return; + } + + testWebhookButton.addEventListener( 'click', function ( event ) { + event.preventDefault(); + + try { + // Get current url. + const url = new URL( window.location.href ); + + // Add search param test_webhook to url. + url.searchParams.append( 'test_webhook', '1' ); + + // Redirect to new url. + window.location = url.href; + } catch ( error ) { + // Do nothing. + } + } ); +} + +// Run event listener when window load. +window.addEventListener( 'load', addEventHandler ); From 371aad65cf6ac9f044c1937e10bc7b86ded01707 Mon Sep 17 00:00:00 2001 From: Ivan Kristianto Date: Mon, 19 Aug 2024 08:49:50 +0700 Subject: [PATCH 5/6] Lint fixes --- .../camptix-webhook/addons/webhook.php | 32 +++++++------- .../camptix-webhook/camptix-webhook.php | 9 ++-- .../camptix-webhook/inc/integration.php | 43 ++++++++++--------- 3 files changed, 44 insertions(+), 40 deletions(-) diff --git a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php index 695a77a54..bbb1af50a 100644 --- a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php +++ b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php @@ -52,8 +52,9 @@ public function setup_sections( $sections ) { public function setup_controls( $section ) { global $camptix; - if ( 'webhook-ui' != $section ) + if ( 'webhook-ui' != $section ) { return; + } add_settings_section( 'general', esc_html__( 'Attendees Webhook', 'wordcamporg' ), array( $this, 'setup_controls_section' ), 'camptix_options' ); @@ -68,8 +69,9 @@ public function setup_controls( $section ) { * Runs whenever the CampTix option is updated. */ public function validate_options( $output, $input ) { - if ( isset( $input['webhook-enabled'] ) ) + 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'] ); @@ -103,14 +105,14 @@ public function setup_buttons_test_webhook() { * 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 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 ] ); + if ( ! wp_next_scheduled( 'camptix_webhook_trigger', array( $post_id ) ) ) { + wp_schedule_single_event( time() + 5, 'camptix_webhook_trigger', array( $post_id ) ); } } @@ -126,7 +128,7 @@ public function trigger_webhook( $post_id ) { $camptix_options = $camptix->get_options(); - $is_enabled = isset( $camptix_options['webhook-enabled'] ) ? $camptix_options['webhook-enabled'] : false; + $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 ) { @@ -146,7 +148,7 @@ public function trigger_webhook( $post_id ) { $triggered_number = absint( get_post_meta( $post_id, 'tix_webhook_triggered_number', true ) ); // Get attendee data. - $attendee_data = [ + $attendee_data = array( 'timestamp' => time(), 'status' => $post->post_status, 'is_new_entry' => $triggered_number === 0, @@ -155,19 +157,19 @@ public function trigger_webhook( $post_id ) { '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 ), - ]; + ); $attendee_data = apply_filters( 'camptix_webhook_attendee_data', $attendee_data, $post_id ); // Prepare webhook data. $response = wp_remote_post( $webhook_url, - [ + array( 'body' => wp_json_encode( $attendee_data ), - 'headers' => [ + 'headers' => array( 'Content-Type' => 'application/json', - ], - ] + ), + ) ); update_post_meta( $post_id, 'tix_webhook_triggered_number', $triggered_number + 1 ); @@ -194,12 +196,12 @@ public function admin_enqueue_scripts( $hook ) { wp_enqueue_script( 'camptix-webhook-admin', plugin_dir_url( Webhook\BASE_FILE ) . 'js/camptix-webhook-admin.js', - [], + array(), '1.0', - [ + array( 'strategy' => 'async', 'footer' => true, - ] + ) ); } diff --git a/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php b/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php index c35a10dd6..577ca0571 100644 --- a/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php +++ b/public_html/wp-content/plugins/camptix-webhook/camptix-webhook.php @@ -7,20 +7,21 @@ * Author URI: https://profiles.wordpress.org/ivankristianto/ */ - namespace CampTix\Webhook; +namespace CampTix\Webhook; if ( ! defined( 'ABSPATH' ) ) { exit; } -const BASE_DIR = __DIR__; +const BASE_DIR = __DIR__; const BASE_FILE = __FILE__; -require_once( __DIR__ . '/addons/webhook.php' ); +require_once __DIR__ . '/addons/webhook.php'; require_once __DIR__ . '/inc/integration.php'; /** * Bootstrap the plugin. + * * @return void */ function bootstrap() { @@ -39,4 +40,4 @@ function camptix_webhook_register() { } // Bootstrap the plugin. -bootstrap(); \ No newline at end of file +bootstrap(); diff --git a/public_html/wp-content/plugins/camptix-webhook/inc/integration.php b/public_html/wp-content/plugins/camptix-webhook/inc/integration.php index b4e2cff1f..f849798b7 100644 --- a/public_html/wp-content/plugins/camptix-webhook/inc/integration.php +++ b/public_html/wp-content/plugins/camptix-webhook/inc/integration.php @@ -37,7 +37,7 @@ function test_webhook_action() { $webhook_url = isset( $camptix_options['webhook-url'] ) ? $camptix_options['webhook-url'] : ''; // Get attendee data. - $attendee_data = [ + $attendee_data = array( 'timestamp' => time(), 'status' => 'publish', 'is_new_entry' => true, @@ -46,7 +46,7 @@ function test_webhook_action() { 'tix_last_name' => 'Test', 'tix_ticket_id' => '0000', 'tix_coupon' => 'Coupon_XXX', - ]; + ); // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. $attendee_data = apply_filters( 'camptix_webhook_attendee_data', $attendee_data, -1 ); @@ -54,12 +54,12 @@ function test_webhook_action() { // Prepare webhook data. $response = wp_remote_post( $webhook_url, - [ + array( 'body' => wp_json_encode( $attendee_data ), - 'headers' => [ + 'headers' => array( 'Content-Type' => 'application/json', - ], - ] + ), + ) ); if ( is_wp_error( $response ) ) { @@ -84,13 +84,13 @@ function test_webhook_action() { * Add attendees admin flag to attendee data. * * @param array $attendee_data Array of attendee data. - * @param int $post_id Post ID. + * @param int $post_id Post ID. * @return array */ function add_attendees_admin_flag( $attendee_data, $post_id ): array { // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. if ( $post_id === -1 ) { - $admin_flag = [ 'volunteer', 'speaker', 'organiser' ]; + $admin_flag = array( 'volunteer', 'speaker', 'organiser' ); $attendee_data['tix_admin_flag'] = $admin_flag[ array_rand( $admin_flag ) ]; return $attendee_data; @@ -106,22 +106,22 @@ function add_attendees_admin_flag( $attendee_data, $post_id ): array { * Add attendees meta to attendee data. * * @param array $attendee_data Array of attendee data. - * @param int $post_id Post ID. + * @param int $post_id Post ID. * @return array */ function add_attendees_meta( $attendee_data, $post_id ): array { - $allowed_meta = [ + $allowed_meta = array( 'tix_accommodations', 'tix_allergy', 'tix_coupon', - 'tix_first_time_attending_wp_event' - ]; + 'tix_first_time_attending_wp_event', + ); // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. if ( $post_id === -1 ) { - $attendee_data['tix_accommodations'] = 'yes'; - $attendee_data['tix_allergy'] = 'no'; - $attendee_data['tix_coupon'] = 'test'; + $attendee_data['tix_accommodations'] = 'yes'; + $attendee_data['tix_allergy'] = 'no'; + $attendee_data['tix_coupon'] = 'test'; $attendee_data['tix_first_time_attending_wp_event'] = 'test'; return $attendee_data; @@ -138,7 +138,7 @@ function add_attendees_meta( $attendee_data, $post_id ): array { * Add attendees questions answered to attendee data. * * @param array $attendee_data Array of attendee data. - * @param int $post_id Post ID. + * @param int $post_id Post ID. * @return array */ function add_attendees_questions_answered( $attendee_data, $post_id ): array { @@ -152,19 +152,20 @@ function add_attendees_questions_answered( $attendee_data, $post_id ): array { global $camptix; $ticket_id = intval( get_post_meta( $post_id, 'tix_ticket_id', true ) ); $questions = $camptix->get_sorted_questions( $ticket_id ); - $answers = get_post_meta( $post_id, 'tix_questions', true ); + $answers = get_post_meta( $post_id, 'tix_questions', true ); - $rows = []; + $rows = array(); foreach ( $questions as $question ) { if ( isset( $answers[ $question->ID ] ) ) { $answer = $answers[ $question->ID ]; - if ( is_array( $answer ) ) + if ( is_array( $answer ) ) { $answer = implode( ', ', $answer ); - $rows[] = [ $question->post_title, $answer ]; + } + $rows[] = array( $question->post_title, $answer ); } } $attendee_data['answered'] = $rows; return $attendee_data; -} \ No newline at end of file +} From 78d91915286efbf3fcc95d60c7ee6ccaa404852a Mon Sep 17 00:00:00 2001 From: Ivan Kristianto Date: Mon, 19 Aug 2024 08:56:20 +0700 Subject: [PATCH 6/6] Linter fixes --- .../plugins/camptix-webhook/addons/webhook.php | 6 +++--- .../plugins/camptix-webhook/inc/integration.php | 10 ++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php index bbb1af50a..c644c14ae 100644 --- a/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php +++ b/public_html/wp-content/plugins/camptix-webhook/addons/webhook.php @@ -58,7 +58,7 @@ public function setup_controls( $section ) { add_settings_section( 'general', esc_html__( 'Attendees Webhook', 'wordcamporg' ), array( $this, 'setup_controls_section' ), 'camptix_options' ); - // Fields + // 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__( 'Webhook URL including protocol such as https://', 'wordcamporg' ) ); @@ -151,7 +151,7 @@ public function trigger_webhook( $post_id ) { $attendee_data = array( 'timestamp' => time(), 'status' => $post->post_status, - 'is_new_entry' => $triggered_number === 0, + 'is_new_entry' => 0 === $triggered_number, '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 ), @@ -189,7 +189,7 @@ public function trigger_webhook( $post_id ) { * @return void */ public function admin_enqueue_scripts( $hook ) { - if ( $hook !== 'tix_ticket_page_camptix_options' ) { + if ( 'tix_ticket_page_camptix_options' !== $hook ) { return; } diff --git a/public_html/wp-content/plugins/camptix-webhook/inc/integration.php b/public_html/wp-content/plugins/camptix-webhook/inc/integration.php index f849798b7..1f9b52897 100644 --- a/public_html/wp-content/plugins/camptix-webhook/inc/integration.php +++ b/public_html/wp-content/plugins/camptix-webhook/inc/integration.php @@ -3,8 +3,6 @@ * Camptix Webhook Integration */ -namespace GG\RewriteRules\Rewrite; - namespace CampTix\Webhook\Integration; /** @@ -25,7 +23,7 @@ function bootstrap() { */ function test_webhook_action() { - if ( ! isset( $_GET['test_webhook'] ) || $_GET['test_webhook'] !== '1' ) { + if ( ! isset( $_GET['test_webhook'] ) || '1' !== $_GET['test_webhook'] ) { return; } @@ -89,7 +87,7 @@ function test_webhook_action() { */ function add_attendees_admin_flag( $attendee_data, $post_id ): array { // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. - if ( $post_id === -1 ) { + if ( -1 === $post_id ) { $admin_flag = array( 'volunteer', 'speaker', 'organiser' ); $attendee_data['tix_admin_flag'] = $admin_flag[ array_rand( $admin_flag ) ]; @@ -118,7 +116,7 @@ function add_attendees_meta( $attendee_data, $post_id ): array { ); // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. - if ( $post_id === -1 ) { + if ( -1 === $post_id ) { $attendee_data['tix_accommodations'] = 'yes'; $attendee_data['tix_allergy'] = 'no'; $attendee_data['tix_coupon'] = 'test'; @@ -143,7 +141,7 @@ function add_attendees_meta( $attendee_data, $post_id ): array { */ function add_attendees_questions_answered( $attendee_data, $post_id ): array { // Post ID: -1 will invalid when getting post object. We use this to indicate that this is a test webhook. - if ( $post_id === -1 ) { + if ( -1 === $post_id ) { $attendee_data['answered'] = 'N/A'; return $attendee_data;