Edit and deleted webhooks
This commit is contained in:
parent
ba538ec36c
commit
2626275782
|
@ -87,7 +87,7 @@ class WC_Admin_Webhooks_Table_List extends WP_List_Table {
|
||||||
|
|
||||||
$actions['id'] = sprintf( __( 'ID: %d', 'woocommerce' ), $the_webhook->id );
|
$actions['id'] = sprintf( __( 'ID: %d', 'woocommerce' ), $the_webhook->id );
|
||||||
|
|
||||||
if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) ) {
|
if ( current_user_can( $post_type_object->cap->edit_post, $the_webhook->id ) && 'trash' !== $the_webhook->post_data->post_status ) {
|
||||||
$actions['edit'] = '<a href="' . esc_attr( $edit_link ) . '">' . __( 'Edit', 'woocommerce' ) . '</a>';
|
$actions['edit'] = '<a href="' . esc_attr( $edit_link ) . '">' . __( 'Edit', 'woocommerce' ) . '</a>';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,171 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* WooCommerce Admin Webhooks Class.
|
||||||
|
*
|
||||||
|
* @author WooThemes
|
||||||
|
* @category Admin
|
||||||
|
* @package WooCommerce/Admin
|
||||||
|
* @version 2.3.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
if ( ! defined( 'ABSPATH' ) ) {
|
||||||
|
exit; // Exit if accessed directly
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* WC_Admin_Webhooks
|
||||||
|
*/
|
||||||
|
class WC_Admin_Webhooks {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the webhooks admin actions
|
||||||
|
*/
|
||||||
|
public function __construct() {
|
||||||
|
// Save webhooks
|
||||||
|
add_action( 'admin_init', array( $this, 'save' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updated the Webhook name
|
||||||
|
*
|
||||||
|
* @param int $webhook_id
|
||||||
|
*/
|
||||||
|
private function update_name( $webhook_id ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$name = ! empty( $_POST['webhook_name'] ) ? $_POST['webhook_name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) );
|
||||||
|
$wpdb->update( $wpdb->posts, array( 'post_title' => $name ), array( 'ID' => $webhook_id ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updated the Webhook status
|
||||||
|
*
|
||||||
|
* @param WC_Webhook $webhook
|
||||||
|
*/
|
||||||
|
private function update_status( $webhook ) {
|
||||||
|
$status = ! empty( $_POST['webhook_status'] ) ? wc_clean( $_POST['webhook_status'] ) : '';
|
||||||
|
|
||||||
|
$webhook->update_status( $status );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updated the Webhook delivery URL
|
||||||
|
*
|
||||||
|
* @param WC_Webhook $webhook
|
||||||
|
*/
|
||||||
|
private function update_delivery_url( $webhook ) {
|
||||||
|
$delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? $_POST['webhook_delivery_url'] : '';
|
||||||
|
|
||||||
|
if ( wc_is_valid_url( $delivery_url ) ) {
|
||||||
|
$webhook->set_delivery_url( $delivery_url );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updated the Webhook secret
|
||||||
|
*
|
||||||
|
* @param WC_Webhook $webhook
|
||||||
|
*/
|
||||||
|
private function update_secret( $webhook ) {
|
||||||
|
$secret = ! empty( $_POST['webhook_secret'] ) ? $_POST['webhook_secret'] : get_user_meta( get_current_user_id(), 'woocommerce_api_consumer_secret', true );
|
||||||
|
|
||||||
|
$webhook->set_secret( $secret );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Updated the Webhook topic
|
||||||
|
*
|
||||||
|
* @param WC_Webhook $webhook
|
||||||
|
*/
|
||||||
|
private function update_topic( $webhook ) {
|
||||||
|
if ( ! empty( $_POST['webhook_topic'] ) ) {
|
||||||
|
list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_topic'] ) );
|
||||||
|
|
||||||
|
if ( 'action' === $resource ) {
|
||||||
|
$event = ! empty( $_POST['webhook_action_event'] ) ? wc_clean( $_POST['webhook_action_event'] ) : '';
|
||||||
|
} else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) && ! empty( $_POST['webhook_custom_topic'] ) ) {
|
||||||
|
list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_custom_topic'] ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$topic = $resource . '.' . $event;
|
||||||
|
|
||||||
|
if ( wc_is_webhook_valid_topic( $topic ) ) {
|
||||||
|
$webhook->set_topic( $topic );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set Webhook post data.
|
||||||
|
*
|
||||||
|
* @param int $webhook_id
|
||||||
|
*/
|
||||||
|
private function set_post_data( $webhook_id ) {
|
||||||
|
global $wpdb;
|
||||||
|
|
||||||
|
$password = uniqid( 'webhook_' );
|
||||||
|
$password = strlen( $password ) > 20 ? substr( $password, 0, 20 ) : $password;
|
||||||
|
|
||||||
|
$wpdb->update(
|
||||||
|
$wpdb->posts,
|
||||||
|
array(
|
||||||
|
'post_password' => $password,
|
||||||
|
'ping_status' => 'closed',
|
||||||
|
'comment_status' => 'open'
|
||||||
|
),
|
||||||
|
array( 'ID' => $webhook_id )
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Save method
|
||||||
|
*/
|
||||||
|
public function save() {
|
||||||
|
if ( isset( $_GET['page'] ) && 'wc-settings' == $_GET['page'] && isset( $_GET['tab'] ) && 'webhooks' == $_GET['tab'] && isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) {
|
||||||
|
|
||||||
|
if ( empty( $_REQUEST['_wpnonce'] ) || ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'woocommerce-settings' ) ) {
|
||||||
|
die( __( 'Action failed. Please refresh the page and retry.', 'woocommerce' ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
$webhook_id = absint( $_POST['webhook_id'] );
|
||||||
|
|
||||||
|
if ( ! current_user_can( 'edit_shop_webhook', $webhook_id ) ) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$webhook = new WC_Webhook( $webhook_id );
|
||||||
|
|
||||||
|
// Name
|
||||||
|
$this->update_name( $webhook->id );
|
||||||
|
|
||||||
|
// Status
|
||||||
|
$this->update_status( $webhook );
|
||||||
|
|
||||||
|
// Delivery URL
|
||||||
|
$this->update_delivery_url( $webhook );
|
||||||
|
|
||||||
|
// Secret
|
||||||
|
$this->update_secret( $webhook );
|
||||||
|
|
||||||
|
// Topic
|
||||||
|
$this->update_topic( $webhook );
|
||||||
|
|
||||||
|
// Webhook Created
|
||||||
|
if ( isset( $_POST['original_post_status'] ) && 'auto-draft' === $_POST['original_post_status'] ) {
|
||||||
|
// Set Post data like ping status and password
|
||||||
|
$this->set_post_data( $webhook->id );
|
||||||
|
|
||||||
|
// Ping webhook
|
||||||
|
$webhook->deliver_ping();
|
||||||
|
}
|
||||||
|
|
||||||
|
do_action( 'woocommerce_webhook_options_save', $webhook->id );
|
||||||
|
|
||||||
|
// Redirect to webhook edit page to avoid settings save actions
|
||||||
|
wp_redirect( admin_url( 'admin.php?page=wc-settings&tab=webhooks&edit-webhook=' . $webhook->id . '&updated=1' ) );
|
||||||
|
exit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
new WC_Admin_Webhooks();
|
|
@ -47,6 +47,7 @@ class WC_Admin {
|
||||||
include( 'class-wc-admin-welcome.php' );
|
include( 'class-wc-admin-welcome.php' );
|
||||||
include( 'class-wc-admin-notices.php' );
|
include( 'class-wc-admin-notices.php' );
|
||||||
include( 'class-wc-admin-assets.php' );
|
include( 'class-wc-admin-assets.php' );
|
||||||
|
include( 'class-wc-admin-webhooks.php' );
|
||||||
|
|
||||||
// Help
|
// Help
|
||||||
if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) {
|
if ( apply_filters( 'woocommerce_enable_admin_help_tab', true ) ) {
|
||||||
|
|
|
@ -42,7 +42,12 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
|
||||||
*/
|
*/
|
||||||
public function form_method( $method ) {
|
public function form_method( $method ) {
|
||||||
if ( isset( $_GET['edit-webhook'] ) ) {
|
if ( isset( $_GET['edit-webhook'] ) ) {
|
||||||
return 'post';
|
$webhook_id = absint( $_GET['edit-webhook'] );
|
||||||
|
$webhook = new WC_Webhook( $webhook_id );
|
||||||
|
|
||||||
|
if ( 'trash' != $webhook->post_data->post_status ) {
|
||||||
|
return 'post';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return 'get';
|
return 'get';
|
||||||
|
@ -69,6 +74,12 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
|
||||||
|
|
||||||
WC_Admin_Settings::add_message( sprintf( _n( '1 webhook permanently deleted.', '%d webhooks permanently deleted.', $deleted, 'woocommerce' ), $deleted ) );
|
WC_Admin_Settings::add_message( sprintf( _n( '1 webhook permanently deleted.', '%d webhooks permanently deleted.', $deleted, 'woocommerce' ), $deleted ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( isset( $_GET['updated'] ) ) {
|
||||||
|
$updated = absint( $_GET['updated'] );
|
||||||
|
|
||||||
|
WC_Admin_Settings::add_message( __( 'Webhook saved successfully.', 'woocommerce' ) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -87,11 +98,10 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Edit webhook output
|
* Edit webhook output
|
||||||
|
*
|
||||||
|
* @param WC_Webhook $webhook
|
||||||
*/
|
*/
|
||||||
private function edit_output() {
|
private function edit_output( $webhook ) {
|
||||||
$webhook_id = absint( $_GET['edit-webhook'] );
|
|
||||||
$webhook = new WC_Webhook( $webhook_id );
|
|
||||||
|
|
||||||
include_once( 'views/html-webhooks-edit.php' );
|
include_once( 'views/html-webhooks-edit.php' );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -136,10 +146,16 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
|
||||||
$GLOBALS['hide_save_button'] = true;
|
$GLOBALS['hide_save_button'] = true;
|
||||||
|
|
||||||
if ( isset( $_GET['edit-webhook'] ) ) {
|
if ( isset( $_GET['edit-webhook'] ) ) {
|
||||||
$this->edit_output();
|
$webhook_id = absint( $_GET['edit-webhook'] );
|
||||||
} else {
|
$webhook = new WC_Webhook( $webhook_id );
|
||||||
$this->table_list_output();
|
|
||||||
|
if ( 'trash' != $webhook->post_data->post_status ) {
|
||||||
|
$this->edit_output( $webhook );
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$this->table_list_output();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,98 +185,6 @@ class WC_Settings_Webhooks extends WC_Settings_Page {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Updated the Webhook name
|
|
||||||
*
|
|
||||||
* @param int $webhook_id
|
|
||||||
*/
|
|
||||||
private function update_name( $webhook_id ) {
|
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
$name = ! empty( $_POST['webhook_name'] ) ? $_POST['webhook_name'] : sprintf( __( 'Webhook created on %s', 'woocommerce' ), strftime( _x( '%b %d, %Y @ %I:%M %p', 'Webhook created on date parsed by strftime', 'woocommerce' ) ) );
|
|
||||||
$wpdb->update( $wpdb->posts, array( 'post_title' => $name ), array( 'ID' => $webhook_id ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updated the Webhook status
|
|
||||||
*
|
|
||||||
* @param WC_Webhook $webhook
|
|
||||||
*/
|
|
||||||
private function update_status( $webhook ) {
|
|
||||||
$status = ! empty( $_POST['webhook_status'] ) ? wc_clean( $_POST['webhook_status'] ) : '';
|
|
||||||
|
|
||||||
$webhook->update_status( $status );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updated the Webhook delivery URL
|
|
||||||
*
|
|
||||||
* @param WC_Webhook $webhook
|
|
||||||
*/
|
|
||||||
private function update_delivery_url( $webhook ) {
|
|
||||||
$delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? $_POST['webhook_delivery_url'] : '';
|
|
||||||
|
|
||||||
if ( wc_is_valid_url( $delivery_url ) ) {
|
|
||||||
$webhook->set_delivery_url( $delivery_url );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updated the Webhook secret
|
|
||||||
*
|
|
||||||
* @param WC_Webhook $webhook
|
|
||||||
*/
|
|
||||||
private function update_secret( $webhook ) {
|
|
||||||
$secret = ! empty( $_POST['webhook_secret'] ) ? $_POST['webhook_secret'] : get_user_meta( get_current_user_id(), 'woocommerce_api_consumer_secret', true );
|
|
||||||
|
|
||||||
$webhook->set_secret( $secret );
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Updated the Webhook topic
|
|
||||||
*
|
|
||||||
* @param WC_Webhook $webhook
|
|
||||||
*/
|
|
||||||
private function update_topic( $webhook ) {
|
|
||||||
if ( ! empty( $_POST['webhook_topic'] ) ) {
|
|
||||||
list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_topic'] ) );
|
|
||||||
|
|
||||||
if ( 'action' === $resource ) {
|
|
||||||
$event = ! empty( $_POST['webhook_action_event'] ) ? wc_clean( $_POST['webhook_action_event'] ) : '';
|
|
||||||
} else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) && ! empty( $_POST['webhook_custom_topic'] ) ) {
|
|
||||||
list( $resource, $event ) = explode( '.', wc_clean( $_POST['webhook_custom_topic'] ) );
|
|
||||||
}
|
|
||||||
|
|
||||||
$topic = $resource . '.' . $event;
|
|
||||||
|
|
||||||
if ( wc_is_webhook_valid_topic( $topic ) ) {
|
|
||||||
$webhook->set_topic( $topic );
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Set Webhook post data.
|
|
||||||
*
|
|
||||||
* @param int $webhook_id
|
|
||||||
*/
|
|
||||||
private function set_post_data( $webhook_id ) {
|
|
||||||
global $wpdb;
|
|
||||||
|
|
||||||
$password = uniqid( 'webhook_' );
|
|
||||||
$password = strlen( $password ) > 20 ? substr( $password, 0, 20 ) : $password;
|
|
||||||
|
|
||||||
$wpdb->update(
|
|
||||||
$wpdb->posts,
|
|
||||||
array(
|
|
||||||
'post_password' => $password,
|
|
||||||
'ping_status' => 'closed',
|
|
||||||
'comment_status' => 'open'
|
|
||||||
),
|
|
||||||
array( 'ID' => $webhook_id )
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the logs navigation.
|
* Get the logs navigation.
|
||||||
*
|
*
|
||||||
|
|
|
@ -112,8 +112,8 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
<?php do_action( 'woocommerce_webhook_options' ); ?>
|
<?php do_action( 'woocommerce_webhook_options' ); ?>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div id="webhook-info" class="settings-panel">
|
<div id="webhook-actions" class="settings-panel">
|
||||||
<h3><?php _e( 'Webhook Info', 'woocommerce' ); ?></h3>
|
<h3><?php _e( 'Webhook Actions', 'woocommerce' ); ?></h3>
|
||||||
<table class="form-table">
|
<table class="form-table">
|
||||||
<tbody>
|
<tbody>
|
||||||
<?php if ( '0000-00-00 00:00:00' != $webhook->post_data->post_modified_gmt ) : ?>
|
<?php if ( '0000-00-00 00:00:00' != $webhook->post_data->post_modified_gmt ) : ?>
|
||||||
|
@ -145,6 +145,16 @@ if ( ! defined( 'ABSPATH' ) ) {
|
||||||
</tr>
|
</tr>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
<?php endif; ?>
|
<?php endif; ?>
|
||||||
|
<tr valign="top">
|
||||||
|
<td colspan="2" scope="row" style="padding-left: 0;">
|
||||||
|
<p class="submit">
|
||||||
|
<?php if ( current_user_can( 'delete_post', $webhook->id ) ) : ?>
|
||||||
|
<a style="color: #a00; text-decoration: none; margin-right: 10px;" href="<?php echo esc_url( get_delete_post_link( $webhook->id ) ); ?>"><?php echo ( ! EMPTY_TRASH_DAYS ) ? __( 'Delete Permanently', 'woocommerce' ) : __( 'Move to Trash', 'woocommerce' ); ?></a>
|
||||||
|
<?php endif; ?>
|
||||||
|
<input type="submit" class="button button-primary button-large" name="save" id="publish" accesskey="p" value="<?php _e( 'Save Webhook', 'woocommerce' ); ?>" data-tip="<?php _e( 'Save/update the Webhook', 'woocommerce' ); ?>" />
|
||||||
|
</p>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
|
Loading…
Reference in New Issue