Skip to content

feat: refactor codebase to implement simple webhook action handling and and dispatching #235

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

Merged
merged 4 commits into from
Jun 5, 2025

Conversation

theodesp
Copy link
Member

@theodesp theodesp commented May 28, 2025

Description

Add support for minimal webhooks with customizable payloads and event handling. Implements webhook creation, event listening, and sending HTTP requests with filtered payloads.

Related Issue

closes #166

Dependant PRs

Type of Change

  • ✅ Bug fix (non-breaking change which fixes an issue)
  • ✨ New feature (non-breaking change which adds functionality)
  • 💥 Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactoring (no functional changes)
  • 📄 Example update (no functional changes)
  • 📝 Documentation update
  • 🔍 Performance improvement
  • 🧪 Test update

How Has This Been Tested?

  • Created webhooks via repository interface

  • Triggered events (e.g., post published) and confirmed HTTP POSTs sent

  • Verified payload customization via filter hooks

  • Checked no warnings or errors in logs

Screenshots

Screenshot 2025-06-04 at 14 30 06 pplicable -->

Checklist

  • I have read the CONTRIBUTING document
  • My code follows the project's coding standards
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation (if applicable)
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works (if applicable)
  • Any dependent changes have been highlighted, merged or published

Example Snippet to Create a Test Webhook

Run this snippet once:

function create_test_post_published_webhook() {
    // Get the repository instance from the plugin singleton
    $repository = \WPGraphQL\Webhooks\Plugin::instance()->get_repository();

    // Define webhook properties
    $name = 'Test Post Published Webhook';
    $event = 'post_published'; // Existing event handled by the plugin
    $url = 'https://webhook.site/YOUR_UNIQUE_URL_HERE'; // Replace with your unique URL
    $method = 'POST';
    $headers = [
        'X-Custom-Header' => 'TestHeaderValue',
    ];

    // Create the webhook
    $result = $repository->create( $name, $event, $url, $method, $headers );

    if ( is_wp_error( $result ) ) {
        error_log( 'Failed to create webhook: ' . $result->get_error_message() );
    } else {
        error_log( 'Webhook created successfully with ID: ' . $result );
    }
}

// Run once, e.g., on admin_init or manually trigger it
add_action( 'admin_init', 'create_test_post_published_webhook' );

Run this snippet all the time:

add_filter( 'graphql_webhooks_payload', function( array $payload, $webhook ) {
    // Example: Add a timestamp to all webhook payloads
    $payload['sent_at'] = current_time( 'mysql' );

    // Example: Customize payload for a specific webhook ID
    if ( $webhook->id === 123 ) {
        $payload['custom_data'] = 'This is custom data for webhook 123';
    }

    // Example: Add more context or modify existing data
    if ( isset( $payload['event'] ) && $payload['event'] === 'post_published' ) {
        $payload['extra_info'] = 'Additional info for post published event';
    }

    return $payload;
}, 10, 2 );

Note to Developer Reviewer

Please generate a new unique URL from https://webhook.site/ to replace YOUR_UNIQUE_URL_HERE in the snippet above. This will allow you to verify the webhook payloads sent when triggering events like publishing a post.

Copy link

ℹ️ Download the hwp-previews plugin artifact from this workflow run (see the 'Artifacts' section at the bottom).

@colinmurphy
Copy link
Member

@theodesp Nice :) 🆙

Copy link

ℹ️ Download the hwp-previews plugin artifact from this workflow run (see the 'Artifacts' section at the bottom).

@josephfusco
Copy link
Member

@theodesp I was seeing an error in

I was seeing this error which was blocking login,

Notice: Function _load_textdomain_just_in_time was called incorrectly. Translation loading for the wp-graphql-headless-webhooks domain was triggered too early. This is usually an indicator for some code in the plugin or theme running too early. Translations should be loaded at the init action or later. Please see [Debugging in WordPress](https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/) for more information. (This message was added in version 6.7.0.) in /var/www/html/wp-includes/functions.php on line 6121 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/functions.php:6121) in /var/www/html/wp-includes/pluggable.php on line 1450 Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/wp-includes/functions.php:6121) in /var/www/html/wp-includes/pluggable.php on line 1453

I pushed a fix to this branch (ea70bdd) which addresses this by:

  1. Replacing translation functions with plain strings in early-loading code
  2. Adding proper text domain loading at the init hook with priority 1
  3. Adding a text domain constant for consistency

This prevents the notice from being displayed and fixes the header issues that were blocking login functionality.

@theodesp theodesp marked this pull request as ready for review June 4, 2025 13:26
@theodesp theodesp requested a review from a team as a code owner June 4, 2025 13:26
Copy link

github-actions bot commented Jun 4, 2025

ℹ️ Download the hwp-previews plugin artifact from this workflow run (see the 'Artifacts' section at the bottom).

Copy link
Member

@colinmurphy colinmurphy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@theodesp LGTM 🚀 🚀 🚀

@theodesp theodesp enabled auto-merge June 5, 2025 14:29
@theodesp theodesp added this pull request to the merge queue Jun 5, 2025
Merged via the queue into main with commit cc32dea Jun 5, 2025
11 checks passed
@theodesp theodesp deleted the feat-webhooks-revamp-event-dispatch branch June 5, 2025 14:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: Develop Simple Webhook Delivery System
4 participants