本文整理汇总了PHP中WC_Webhook::set_api_version方法的典型用法代码示例。如果您正苦于以下问题:PHP WC_Webhook::set_api_version方法的具体用法?PHP WC_Webhook::set_api_version怎么用?PHP WC_Webhook::set_api_version使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类WC_Webhook
的用法示例。
在下文中一共展示了WC_Webhook::set_api_version方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: create
/**
* Create Webhook.
*/
private function create()
{
if (empty($_REQUEST['_wpnonce']) || !wp_verify_nonce($_REQUEST['_wpnonce'], 'create-webhook')) {
wp_die(__('Action failed. Please refresh the page and retry.', 'woocommerce'));
}
if (!current_user_can('publish_shop_webhooks')) {
wp_die(__('You don\'t have permissions to create Webhooks!', 'woocommerce'));
}
$webhook_id = wp_insert_post(array('post_type' => 'shop_webhook', 'post_status' => 'pending', 'ping_status' => 'closed', 'post_author' => get_current_user_id(), 'post_password' => strlen($password = uniqid('webhook_')) > 20 ? substr($password, 0, 20) : $password, 'post_title' => sprintf(__('Webhook created on %s', 'woocommerce'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce'))), 'comment_status' => 'open'));
if (is_wp_error($webhook_id)) {
wp_die($webhook_id->get_error_messages());
}
update_post_meta($webhook_id, '_webhook_pending_delivery', true);
$webhook = new WC_Webhook($webhook_id);
$webhook->set_api_version('wp_api_v1');
delete_transient('woocommerce_webhook_ids');
// Redirect to edit page
wp_redirect(admin_url('admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook_id . '&created=1'));
exit;
}
示例2: create_item
/**
* Create a single webhook.
*
* @param WP_REST_Request $request Full details about the request.
* @return WP_Error|WP_REST_Response
*/
public function create_item($request)
{
if (!empty($request['id'])) {
/* translators: %s: post type */
return new WP_Error("woocommerce_rest_{$this->post_type}_exists", sprintf(__('Cannot create existing %s.', 'woocommerce'), $this->post_type), array('status' => 400));
}
// Validate topic.
if (empty($request['topic']) || !wc_is_webhook_valid_topic(strtolower($request['topic']))) {
return new WP_Error("woocommerce_rest_{$this->post_type}_invalid_topic", __('Webhook topic is required and must be valid.', 'woocommerce'), array('status' => 400));
}
// Validate delivery URL.
if (empty($request['delivery_url']) || !wc_is_valid_url($request['delivery_url'])) {
return new WP_Error("woocommerce_rest_{$this->post_type}_invalid_delivery_url", __('Webhook delivery URL must be a valid URL starting with http:// or https://.', 'woocommerce'), array('status' => 400));
}
$post = $this->prepare_item_for_database($request);
if (is_wp_error($post)) {
return $post;
}
$post->post_type = $this->post_type;
$post_id = wp_insert_post($post, true);
if (is_wp_error($post_id)) {
if (in_array($post_id->get_error_code(), array('db_insert_error'))) {
$post_id->add_data(array('status' => 500));
} else {
$post_id->add_data(array('status' => 400));
}
return $post_id;
}
$post->ID = $post_id;
$webhook = new WC_Webhook($post_id);
// Set topic.
$webhook->set_topic($request['topic']);
// Set delivery URL.
$webhook->set_delivery_url($request['delivery_url']);
// Set secret.
$webhook->set_secret(!empty($request['secret']) ? $request['secret'] : '');
// Set API version to WP API integration v1.
$webhook->set_api_version('wp_api_v1');
// Set status.
if (!empty($request['status'])) {
$webhook->update_status($request['status']);
}
$post = get_post($post_id);
$this->update_additional_fields_for_object($post, $request);
/**
* Fires after a single item is created or updated via the REST API.
*
* @param WP_Post $post Inserted object.
* @param WP_REST_Request $request Request object.
* @param boolean $creating True when creating item, false when updating.
*/
do_action("woocommerce_rest_insert_{$this->post_type}", $post, $request, true);
$request->set_param('context', 'edit');
$response = $this->prepare_item_for_response($post, $request);
$response = rest_ensure_response($response);
$response->set_status(201);
$response->header('Location', rest_url(sprintf('/%s/%s/%d', $this->namespace, $this->rest_base, $post_id)));
// Send ping.
$webhook->deliver_ping();
// Clear cache.
delete_transient('woocommerce_webhook_ids');
return $response;
}
示例3: create_webhook
/**
* Create an webhook
*
* @since 2.2
* @param array $data parsed webhook data
* @return array
*/
public function create_webhook($data)
{
try {
if (!isset($data['webhook'])) {
throw new WC_API_Exception('woocommerce_api_missing_webhook_data', sprintf(__('No %1$s data specified to create %1$s', 'woocommerce'), 'webhook'), 400);
}
$data = $data['webhook'];
// permission check
if (!current_user_can('publish_shop_webhooks')) {
throw new WC_API_Exception('woocommerce_api_user_cannot_create_webhooks', __('You do not have permission to create webhooks.', 'woocommerce'), 401);
}
$data = apply_filters('woocommerce_api_create_webhook_data', $data, $this);
// validate topic
if (empty($data['topic']) || !wc_is_webhook_valid_topic(strtolower($data['topic']))) {
throw new WC_API_Exception('woocommerce_api_invalid_webhook_topic', __('Webhook topic is required and must be valid.', 'woocommerce'), 400);
}
// validate delivery URL
if (empty($data['delivery_url']) || !wc_is_valid_url($data['delivery_url'])) {
throw new WC_API_Exception('woocommerce_api_invalid_webhook_delivery_url', __('Webhook delivery URL must be a valid URL starting with http:// or https://', 'woocommerce'), 400);
}
$webhook_data = apply_filters('woocommerce_new_webhook_data', array('post_type' => 'shop_webhook', 'post_status' => 'publish', 'ping_status' => 'closed', 'post_author' => get_current_user_id(), 'post_password' => strlen($password = uniqid('webhook_')) > 20 ? substr($password, 0, 20) : $password, 'post_title' => !empty($data['name']) ? $data['name'] : sprintf(__('Webhook created on %s', 'woocommerce'), strftime(_x('%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce')))), $data, $this);
$webhook_id = wp_insert_post($webhook_data);
if (is_wp_error($webhook_id) || !$webhook_id) {
throw new WC_API_Exception('woocommerce_api_cannot_create_webhook', sprintf(__('Cannot create webhook: %s', 'woocommerce'), is_wp_error($webhook_id) ? implode(', ', $webhook_id->get_error_messages()) : '0'), 500);
}
$webhook = new WC_Webhook($webhook_id);
// set topic, delivery URL, and optional secret
$webhook->set_topic($data['topic']);
$webhook->set_delivery_url($data['delivery_url']);
// set secret if provided, defaults to API users consumer secret
$webhook->set_secret(!empty($data['secret']) ? $data['secret'] : '');
// Set API version to legacy v3.
$webhook->set_api_version('legacy_v3');
// send ping
$webhook->deliver_ping();
// HTTP 201 Created
$this->server->send_status(201);
do_action('woocommerce_api_create_webhook', $webhook->id, $this);
delete_transient('woocommerce_webhook_ids');
return $this->get_webhook($webhook->id);
} catch (WC_API_Exception $e) {
return new WP_Error($e->getErrorCode(), $e->getMessage(), array('status' => $e->getCode()));
}
}