PHP Class WC_Webhook

This class handles storing and retrieving webhook data from the associated. shop_webhook custom post type, as well as delivery logs from the webhook_delivery. comment type. Webhooks are enqueued to their associated actions, delivered, and logged.
Since: 2.2
Author: WooThemes
Mostrar archivo Open project: woocommerce/woocommerce Class Usage Examples

Public Properties

Property Type Description
$id webhook ID (post ID)

Public Methods

Method Description
__construct ( string | integer $id ) Setup webhook & load post data.
__get ( string $key ) : mixed Magic get, wraps get_post_meta() for all keys except $status.
__isset ( string $key ) : boolean Magic isset as a wrapper around metadata_exists().
deliver ( mixed $arg ) Deliver the webhook payload using wp_safe_remote_request().
deliver_ping ( ) : boolean | WP_Error Send a test ping to the delivery URL, sent when the webhook is first created.
enqueue ( ) Enqueue the hooks associated with the webhook.
generate_signature ( string $payload ) : string Generate a base64-encoded HMAC-SHA256 signature of the payload body so the.
get_api_version ( ) : string API version.
get_delivery_log ( integer $delivery_id ) : boolean | array Get the delivery log specified by the ID. The delivery log includes:
get_delivery_logs ( ) : array Get the delivery logs for this webhook.
get_delivery_url ( ) : string Get the delivery URL.
get_event ( ) : string Get the event for the webhook, e.g. created.
get_failure_count ( ) : integer Get the failure count.
get_hooks ( ) : array Get the hook names for the webhook.
get_i18n_status ( ) : string Get the webhook i18n status.
get_name ( ) : string Get the friendly name for the webhook.
get_new_delivery_id ( ) : integer Create a new comment for log the delivery request/response and.
get_post_data ( ) : null | WP_Post Get the post data for the webhook.
get_resource ( ) : string Get the resource for the webhook, e.g. order.
get_secret ( ) : string Get the secret used for generating the HMAC-SHA256 signature.
get_status ( ) : string Get the webhook status:
get_topic ( ) : string Get the webhook topic, e.g. order.created.
get_user_id ( ) : integer | string Get the user ID for this webhook.
log_delivery ( integer $delivery_id, array $request, array | WP_Error $response, float $duration ) Log the delivery request/response.
process ( mixed $arg ) Process the webhook for delivery by verifying that it should be delivered.
set_api_version ( string $version ) Set API version.
set_delivery_url ( string $url ) Set the delivery URL.
set_secret ( string $secret ) Set the secret used for generating the HMAC-SHA256 signature.
set_topic ( string $topic ) Set the webhook topic and associated hooks. The topic resource & event.
update_status ( $status ) Update the webhook status, see get_status() for valid statuses.

Private Methods

Method Description
build_payload ( mixed $resource_id ) : mixed Build the payload data for the webhook.
failed_delivery ( ) Track consecutive delivery failures and automatically disable the webhook.
get_legacy_api_payload ( string $resource, integer $resource_id, string $event ) : array Get Legacy API payload.
get_topic_hooks ( string $topic ) : array Get the associated hook names for a topic.
get_wp_api_payload ( string $resource, integer $resource_id, string $event ) : array Get WP API integration payload.
should_deliver ( mixed $arg ) : boolean Helper to check if the webhook should be delivered, as some hooks.

Method Details

__construct() public method

Setup webhook & load post data.
Since: 2.2
public __construct ( string | integer $id )
$id string | integer

__get() public method

Magic get, wraps get_post_meta() for all keys except $status.
Since: 2.2
public __get ( string $key ) : mixed
$key string
return mixed value

__isset() public method

Magic isset as a wrapper around metadata_exists().
Since: 2.2
public __isset ( string $key ) : boolean
$key string
return boolean true if $key isset, false otherwise

deliver() public method

Deliver the webhook payload using wp_safe_remote_request().
Since: 2.2
public deliver ( mixed $arg )
$arg mixed First hook argument.

deliver_ping() public method

Send a test ping to the delivery URL, sent when the webhook is first created.
Since: 2.2
public deliver_ping ( ) : boolean | WP_Error
return boolean | WP_Error

enqueue() public method

Enqueue the hooks associated with the webhook.
Since: 2.2
public enqueue ( )

generate_signature() public method

recipient can verify the authenticity of the webhook. Note that the signature. is calculated after the body has already been encoded (JSON by default).
Since: 2.2
public generate_signature ( string $payload ) : string
$payload string payload data to hash
return string hash

get_api_version() public method

API version.
Since: 2.7.0
public get_api_version ( ) : string
return string

get_delivery_log() public method

+ duration + summary + request method/url + request headers/body + response code/message/headers/body
Since: 2.2
public get_delivery_log ( integer $delivery_id ) : boolean | array
$delivery_id integer
return boolean | array false if invalid delivery ID, array of log data otherwise

get_delivery_logs() public method

Get the delivery logs for this webhook.
Since: 2.2
public get_delivery_logs ( ) : array
return array

get_delivery_url() public method

Get the delivery URL.
Since: 2.2
public get_delivery_url ( ) : string
return string

get_event() public method

Get the event for the webhook, e.g. created.
Since: 2.2
public get_event ( ) : string
return string

get_failure_count() public method

Get the failure count.
Since: 2.2
public get_failure_count ( ) : integer
return integer

get_hooks() public method

Get the hook names for the webhook.
Since: 2.2
public get_hooks ( ) : array
return array hook names

get_i18n_status() public method

Get the webhook i18n status.
public get_i18n_status ( ) : string
return string

get_name() public method

Get the friendly name for the webhook.
Since: 2.2
public get_name ( ) : string
return string

get_new_delivery_id() public method

return the ID for inclusion in the webhook request.
Since: 2.2
public get_new_delivery_id ( ) : integer
return integer delivery (comment) ID

get_post_data() public method

Get the post data for the webhook.
Since: 2.2
public get_post_data ( ) : null | WP_Post
return null | WP_Post

get_resource() public method

Get the resource for the webhook, e.g. order.
Since: 2.2
public get_resource ( ) : string
return string

get_secret() public method

Get the secret used for generating the HMAC-SHA256 signature.
Since: 2.2
public get_secret ( ) : string
return string

get_status() public method

+ active - delivers payload. + paused - does not deliver payload, paused by admin. + disabled - does not delivery payload, paused automatically due to. consecutive failures.
Since: 2.2
public get_status ( ) : string
return string status

get_topic() public method

Get the webhook topic, e.g. order.created.
Since: 2.2
public get_topic ( ) : string
return string

get_user_id() public method

Get the user ID for this webhook.
Since: 2.2
public get_user_id ( ) : integer | string
return integer | string user ID

log_delivery() public method

Log the delivery request/response.
Since: 2.2
public log_delivery ( integer $delivery_id, array $request, array | WP_Error $response, float $duration )
$delivery_id integer previously created comment ID
$request array request data
$response array | WP_Error response data
$duration float request duration

process() public method

and scheduling the delivery (in the background by default, or immediately).
Since: 2.2
public process ( mixed $arg )
$arg mixed the first argument provided from the associated hooks

set_api_version() public method

Set API version.
Since: 2.7.0
public set_api_version ( string $version )
$version string REST API version.

set_delivery_url() public method

Set the delivery URL.
Since: 2.2
public set_delivery_url ( string $url )
$url string

set_secret() public method

Set the secret used for generating the HMAC-SHA256 signature.
Since: 2.2
public set_secret ( string $secret )
$secret string

set_topic() public method

are also saved separately.
Since: 2.2
public set_topic ( string $topic )
$topic string

update_status() public method

Update the webhook status, see get_status() for valid statuses.
Since: 2.2
public update_status ( $status )
$status

Property Details

$id public_oe property

webhook ID (post ID)
public $id