Skip to content

feat: webhooks plugin core registration api #193

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

theodesp
Copy link
Member

@theodesp theodesp commented May 15, 2025

Description

This PR introduces the core Webhooks Registration API for WPGraphQL Headless Webhooks. It provides the WebhookRegistry singleton class responsible for managing webhook types and webhook instances as a custom post type (graphql_webhook). The API includes functions to register webhook types (register_webhook_type), create webhook instances (create_webhook), and retrieve registered webhook types (get_webhook_type, get_webhook_types).

Related Issue

#164

Dependant PRs

  • None at this time.

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?

  • Manual testing registering webhook types and creating webhook posts via the API functions.
  • Verified webhook types are registered correctly on the graphql_register_webhooks hook.
  • Confirmed webhook posts are created with correct meta data and post type.
  • Tested retrieval functions return expected webhook type data.
  • Confirmed no errors or warnings during plugin initialization and webhook registration.

Screenshots

  • Not applicable (backend API functionality).

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

#Testing snippet

Use this snippet to test the code:

// Make sure this runs early, e.g., in a plugin's main file or functions.php

// 1. Register multiple webhook types with unique identifiers.
register_webhook_type( 'custom_webhook_type_1', [
    'label'       => 'Custom Webhook Type 1',
    'description' => 'Triggered on event type 1.',
    'config'      => [
        'url'    => 'https://example.com/webhook-endpoint-1',
        'method' => 'POST',
    ],
] );

register_webhook_type( 'custom_webhook_type_2', [
    'label'       => 'Custom Webhook Type 2',
    'description' => 'Triggered on event type 2.',
    'config'      => [
        'url'    => 'https://example.com/webhook-endpoint-2',
        'method' => 'POST',
    ],
] );

// 2. Initialize the WebhookRegistry (usually done by the plugin automatically).
// \WPGraphQL\Webhooks\WebhookRegistry::init(); // Uncomment if needed.

// 3. Create new webhook instances with unique names.
add_action( 'admin_init', function() {
    $post_id_1 = create_webhook(
        'custom_webhook_type_1',
        'Webhook Instance for Type 1',
        [
            'url'    => 'https://example.com/receive-webhook-1',
            'secret' => 'secretkey1',
        ]
    );

    if ( is_wp_error( $post_id_1 ) ) {
        error_log( 'Webhook creation error (Type 1): ' . $post_id_1->get_error_message() );
    } else {
        error_log( 'Webhook created with ID (Type 1): ' . $post_id_1 );
    }

    $post_id_2 = create_webhook(
        'custom_webhook_type_2',
        'Webhook Instance for Type 2',
        [
            'url'    => 'https://example.com/receive-webhook-2',
            'secret' => 'secretkey2',
        ]
    );

    if ( is_wp_error( $post_id_2 ) ) {
        error_log( 'Webhook creation error (Type 2): ' . $post_id_2->get_error_message() );
    } else {
        error_log( 'Webhook created with ID (Type 2): ' . $post_id_2 );
    }
} );

// 4. Display all registered webhook types in admin notices.
add_action( 'admin_notices', function() {
    $webhook_types = get_webhook_types();

    if ( empty( $webhook_types ) ) {
        echo '<div class="notice notice-warning"><p>No webhook types registered.</p></div>';
        return;
    }

    echo '<div class="notice notice-success"><p><strong>Registered Webhook Types:</strong></p><ul>';
    foreach ( $webhook_types as $type => $args ) {
        echo '<li><strong>' . esc_html( $type ) . '</strong>: ' . esc_html( $args['label'] ) . ' - ' . esc_html( $args['description'] ) . '</li>';
    }
    echo '</ul></div>';
} );

@theodesp theodesp requested a review from a team as a code owner May 15, 2025 15:35
colinmurphy
colinmurphy previously approved these changes May 15, 2025
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 🚀 🚀 🚀

We probably should consider examples too maybe on how to use those hooks.

@colinmurphy
Copy link
Member

@theodesp

One small thing is I did try and test the snippet in your PR and I got the following error:

[15-May-2025 18:07:56 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function register_webhook_type() in /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-content/themes/twentytwentyfive/functions.php:16
Stack trace:
#0 /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-settings.php(695): include()
#1 /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-config.php(124): require_once('/Users/colin.mu...')
#2 /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-load.php(50): require_once('/Users/colin.mu...')
#3 /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-blog-header.php(13): require_once('/Users/colin.mu...')
#4 /Users/colin.murphy/Sites/local/single-test-site/app/public/index.php(17): require('/Users/colin.mu...')
#5 {main}
  thrown in /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-content/themes/twentytwentyfive/functions.php on line 16

Not sure if I was missing anything but the plugin was installed (symlinked from this PR)

@theodesp
Copy link
Member Author

@theodesp

One small thing is I did try and test the snippet in your PR and I got the following error:

[15-May-2025 18:07:56 UTC] PHP Fatal error:  Uncaught Error: Call to undefined function register_webhook_type() in /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-content/themes/twentytwentyfive/functions.php:16
Stack trace:
#0 /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-settings.php(695): include()
#1 /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-config.php(124): require_once('/Users/colin.mu...')
#2 /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-load.php(50): require_once('/Users/colin.mu...')
#3 /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-blog-header.php(13): require_once('/Users/colin.mu...')
#4 /Users/colin.murphy/Sites/local/single-test-site/app/public/index.php(17): require('/Users/colin.mu...')
#5 {main}
  thrown in /Users/colin.murphy/Sites/local/single-test-site/app/public/wp-content/themes/twentytwentyfive/functions.php on line 16

Not sure if I was missing anything but the plugin was installed (symlinked from this PR)

@colinmurphy that is strange as I copied those to the same location and does not have any issues

can you try using the https://wordpress.org/plugins/code-snippets/ plugin

*
* @internal
*/
class Autoloader {
Copy link
Member

Choose a reason for hiding this comment

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

That would be great for the team to discuss and find the common solution/way for the autoloading for our new plugins. And use unified for all new plugins.

/**
* Register the webhook CPT
*/
public function register_webhook_cpt(): void {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe there is a benefit to have Post registration functionality separately, in a separate class?

Also can we discuss what benefits we have when utilizing it as a Post Type vs Options/Settings?

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks @whoami-pwd . I will refactor. After discussion with @josephfusco I think using CPT provides several benefics over settings:

CPTs provide built-in admin UI, revision history, meta fields per webhook, and native REST/GraphQL integration. We can create multiple hooks as a separate database entry rather than bloating a single serialized options array. The CPT structure also enables more granular permissions as well. So Im not sure how scalable the Options/Settings variant will be in that case.

@theodesp theodesp changed the title Feat webhooks plugin core registration api feat: webhooks plugin core registration api May 19, 2025
colinmurphy
colinmurphy previously approved these changes May 19, 2025
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 dismissed colinmurphy’s stale review May 20, 2025 16:26

The merge-base changed after approval.

@@ -0,0 +1,64 @@
<?php
Copy link
Member Author

Choose a reason for hiding this comment

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

Separated this in a new class

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.

3 participants