2015-01-09 17:42:01 +00:00
< ? php
/**
2015-11-03 13:53:50 +00:00
* WooCommerce Admin Webhooks Class
2015-01-09 17:42:01 +00:00
*
2017-10-10 20:15:37 +00:00
* @ author Automattic
2015-01-09 17:42:01 +00:00
* @ category Admin
* @ package WooCommerce / Admin
2017-11-09 14:07:44 +00:00
* @ version 3.3 . 0
2015-01-09 17:42:01 +00:00
*/
if ( ! defined ( 'ABSPATH' ) ) {
2017-10-10 20:15:37 +00:00
exit ; // Exit if accessed directly.
2015-01-09 17:42:01 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* WC_Admin_Webhooks .
2015-01-09 17:42:01 +00:00
*/
class WC_Admin_Webhooks {
/**
2015-11-03 12:28:01 +00:00
* Initialize the webhooks admin actions .
2015-01-09 17:42:01 +00:00
*/
public function __construct () {
2017-11-09 13:03:52 +00:00
add_action ( 'woocommerce_save_settings_api_webhooks' , array ( $this , 'allow_save_settings' ) );
2015-01-10 00:08:45 +00:00
add_action ( 'admin_init' , array ( $this , 'actions' ) );
}
/**
2017-11-09 13:03:52 +00:00
* Check if should allow save settings .
* This prevents " Your settings have been saved. " notices on the table list .
2015-01-10 00:08:45 +00:00
*
2017-11-09 13:03:52 +00:00
* @ param bool $allow If allow save settings .
2015-01-10 00:08:45 +00:00
* @ return bool
*/
2017-11-09 13:03:52 +00:00
public function allow_save_settings ( $allow ) {
if ( ! isset ( $_GET [ 'edit-webhook' ] ) ) { // WPCS: input var okay, CSRF ok.
return false ;
}
return $allow ;
2015-01-09 17:42:01 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Check if is webhook settings page .
2015-01-09 17:42:01 +00:00
*
2015-01-10 00:08:45 +00:00
* @ return bool
2015-01-09 17:42:01 +00:00
*/
2015-01-10 00:25:04 +00:00
private function is_webhook_settings_page () {
2017-11-07 18:23:26 +00:00
return isset ( $_GET [ 'page' ], $_GET [ 'tab' ], $_GET [ 'section' ] ) && 'wc-settings' === $_GET [ 'page' ] && 'api' === $_GET [ 'tab' ] && 'webhooks' === $_GET [ 'section' ]; // WPCS: input var okay, CSRF ok.
2015-01-09 17:42:01 +00:00
}
/**
2017-10-10 20:15:37 +00:00
* Save method .
2015-01-09 17:42:01 +00:00
*/
2017-10-10 20:15:37 +00:00
private function save () {
check_admin_referer ( 'woocommerce-settings' );
2015-01-09 17:42:01 +00:00
2017-10-10 20:15:37 +00:00
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
wp_die ( esc_html__ ( 'You do not have permission to update Webhooks' , 'woocommerce' ) );
}
2015-01-09 17:42:01 +00:00
2017-11-07 18:23:26 +00:00
$webhook_id = isset ( $_POST [ 'webhook_id' ] ) ? absint ( $_POST [ 'webhook_id' ] ) : 0 ; // WPCS: input var okay, CSRF ok.
2017-10-10 20:15:37 +00:00
$webhook = new WC_Webhook ( $webhook_id );
2015-01-09 17:42:01 +00:00
2017-10-10 20:15:37 +00:00
// Name.
2017-11-07 18:23:26 +00:00
if ( ! empty ( $_POST [ 'webhook_name' ] ) ) { // WPCS: input var okay, CSRF ok.
$name = sanitize_text_field ( wp_unslash ( $_POST [ 'webhook_name' ] ) ); // WPCS: input var okay, CSRF ok.
2017-10-10 20:15:37 +00:00
} else {
$name = sprintf (
/* translators: %s: date */
__ ( 'Webhook created on %s' , 'woocommerce' ),
// @codingStandardsIgnoreStart
strftime ( _x ( '%b %d, %Y @ %I:%M %p' , 'Webhook created on date parsed by strftime' , 'woocommerce' ) )
// @codingStandardsIgnoreEnd
);
}
$webhook -> set_name ( $name );
2017-11-28 20:03:55 +00:00
if ( ! $webhook -> get_user_id () ) {
$webhook -> set_user_id ( get_current_user_id () );
}
2017-10-10 20:15:37 +00:00
// Status.
2017-11-07 18:23:26 +00:00
$webhook -> set_status ( ! empty ( $_POST [ 'webhook_status' ] ) ? sanitize_text_field ( wp_unslash ( $_POST [ 'webhook_status' ] ) ) : 'disabled' ); // WPCS: input var okay, CSRF ok.
2017-10-10 20:15:37 +00:00
// Delivery URL.
2017-11-07 18:23:26 +00:00
$delivery_url = ! empty ( $_POST [ 'webhook_delivery_url' ] ) ? esc_url_raw ( wp_unslash ( $_POST [ 'webhook_delivery_url' ] ) ) : '' ; // WPCS: input var okay, CSRF ok.
2015-01-09 17:42:01 +00:00
if ( wc_is_valid_url ( $delivery_url ) ) {
$webhook -> set_delivery_url ( $delivery_url );
}
2017-10-10 20:15:37 +00:00
// Secret.
2017-11-07 18:23:26 +00:00
$secret = ! empty ( $_POST [ 'webhook_secret' ] ) ? sanitize_text_field ( wp_unslash ( $_POST [ 'webhook_secret' ] ) ) : wp_generate_password ( 50 , true , true ); // WPCS: input var okay, CSRF ok.
2015-01-09 17:42:01 +00:00
$webhook -> set_secret ( $secret );
2017-10-10 20:15:37 +00:00
// Topic.
2017-11-07 18:23:26 +00:00
if ( ! empty ( $_POST [ 'webhook_topic' ] ) ) { // WPCS: input var okay, CSRF ok.
2015-01-29 13:11:35 +00:00
$resource = '' ;
$event = '' ;
2017-11-07 18:23:26 +00:00
switch ( $_POST [ 'webhook_topic' ] ) { // WPCS: input var okay, CSRF ok.
case 'custom' :
if ( ! empty ( $_POST [ 'webhook_custom_topic' ] ) ) { // WPCS: input var okay, CSRF ok.
list ( $resource , $event ) = explode ( '.' , sanitize_text_field ( wp_unslash ( $_POST [ 'webhook_custom_topic' ] ) ) ); // WPCS: input var okay, CSRF ok.
2015-01-29 13:11:35 +00:00
}
break ;
2017-11-07 18:23:26 +00:00
case 'action' :
2015-01-29 13:11:35 +00:00
$resource = 'action' ;
2017-11-07 18:23:26 +00:00
$event = ! empty ( $_POST [ 'webhook_action_event' ] ) ? sanitize_text_field ( wp_unslash ( $_POST [ 'webhook_action_event' ] ) ) : '' ; // WPCS: input var okay, CSRF ok.
2015-01-29 13:11:35 +00:00
break ;
2017-11-07 18:23:26 +00:00
default :
list ( $resource , $event ) = explode ( '.' , sanitize_text_field ( wp_unslash ( $_POST [ 'webhook_topic' ] ) ) ); // WPCS: input var okay, CSRF ok.
2015-01-29 13:11:35 +00:00
break ;
2015-01-09 17:42:01 +00:00
}
$topic = $resource . '.' . $event ;
if ( wc_is_webhook_valid_topic ( $topic ) ) {
$webhook -> set_topic ( $topic );
}
}
2016-11-22 23:58:36 +00:00
// API version.
2017-11-07 18:23:26 +00:00
$webhook -> set_api_version ( ! empty ( $_POST [ 'webhook_api_version' ] ) ? sanitize_text_field ( wp_unslash ( $_POST [ 'webhook_api_version' ] ) ) : 'wp_api_v2' ); // WPCS: input var okay, CSRF ok.
2015-01-09 17:42:01 +00:00
2017-10-10 20:15:37 +00:00
$webhook -> save ();
2015-01-23 13:28:30 +00:00
2017-10-10 20:15:37 +00:00
// Run actions.
do_action ( 'woocommerce_webhook_options_save' , $webhook -> get_id () );
2015-12-04 12:47:30 +00:00
2017-10-10 20:15:37 +00:00
// Ping the webhook at the first time that is activated.
2017-11-07 18:23:26 +00:00
if ( isset ( $_POST [ 'webhook_status' ] ) && 'active' === $_POST [ 'webhook_status' ] && $webhook -> get_pending_delivery () ) { // WPCS: input var okay, CSRF ok.
2015-12-04 12:47:30 +00:00
$result = $webhook -> deliver_ping ();
if ( is_wp_error ( $result ) ) {
2017-10-10 20:15:37 +00:00
// Redirect to webhook edit page to avoid settings save actions.
2017-11-07 18:23:26 +00:00
wp_safe_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook -> get_id () . '&error=' . rawurlencode ( $result -> get_error_message () ) ) );
2015-12-04 12:47:30 +00:00
exit ();
}
}
2017-10-10 20:15:37 +00:00
// Redirect to webhook edit page to avoid settings save actions.
wp_safe_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook -> get_id () . '&updated=1' ) );
2015-01-10 00:08:45 +00:00
exit ();
}
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
/**
2017-11-09 14:07:44 +00:00
* Bulk delete .
2015-01-10 00:08:45 +00:00
*
2017-10-10 20:15:37 +00:00
* @ param array $webhooks List of webhooks IDs .
2015-01-09 18:21:19 +00:00
*/
2017-11-09 14:07:44 +00:00
private function bulk_delete ( $webhooks ) {
2015-01-10 00:08:45 +00:00
foreach ( $webhooks as $webhook_id ) {
2017-11-09 14:07:44 +00:00
$webhook = new WC_Webhook ( ( int ) $webhook_id );
$webhook -> delete ( true );
2015-01-10 00:08:45 +00:00
}
$qty = count ( $webhooks );
2017-11-07 18:23:26 +00:00
$status = isset ( $_GET [ 'status' ] ) ? '&status=' . sanitize_text_field ( wp_unslash ( $_GET [ 'status' ] ) ) : '' ; // WPCS: input var okay, CSRF ok.
2015-01-09 18:21:19 +00:00
2017-10-10 20:15:37 +00:00
// Redirect to webhooks page.
2017-11-09 14:07:44 +00:00
wp_safe_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks' . $status . '&deleted=' . $qty ) );
2015-01-10 00:08:45 +00:00
exit ();
}
/**
2017-11-09 14:07:44 +00:00
* Delete webhook .
2015-01-10 00:08:45 +00:00
*/
2017-11-09 14:07:44 +00:00
private function delete () {
check_admin_referer ( 'delete-webhook' );
2015-01-10 00:08:45 +00:00
2017-11-09 14:07:44 +00:00
if ( isset ( $_GET [ 'delete' ] ) ) { // WPCS: input var okay, CSRF ok.
$webhook_id = absint ( $_GET [ 'delete' ] ); // WPCS: input var okay, CSRF ok.
2015-05-18 19:01:51 +00:00
2017-11-09 14:07:44 +00:00
if ( $webhook_id ) {
$this -> bulk_delete ( array ( $webhook_id ) );
}
}
2015-01-10 00:08:45 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Bulk actions .
2015-01-10 00:08:45 +00:00
*/
private function bulk_actions () {
2017-10-10 20:15:37 +00:00
check_admin_referer ( 'woocommerce-settings' );
2015-05-18 19:01:51 +00:00
2017-10-10 20:15:37 +00:00
if ( ! current_user_can ( 'manage_woocommerce' ) ) {
wp_die ( esc_html__ ( 'You do not have permission to edit Webhooks' , 'woocommerce' ) );
2015-01-10 00:08:45 +00:00
}
2017-11-09 14:07:44 +00:00
if ( isset ( $_REQUEST [ 'action' ] ) ) { // WPCS: input var okay, CSRF ok.
$webhooks = isset ( $_REQUEST [ 'webhook' ] ) ? array_map ( 'absint' , ( array ) $_REQUEST [ 'webhook' ] ) : array (); // WPCS: input var okay, CSRF ok.
2015-01-10 00:08:45 +00:00
2017-11-09 14:07:44 +00:00
$action = sanitize_text_field ( wp_unslash ( $_REQUEST [ 'action' ] ) ); // WPCS: input var okay, CSRF ok.
2015-01-10 01:03:22 +00:00
2017-11-09 14:07:44 +00:00
if ( 'delete' === $action ) {
$this -> bulk_delete ( $webhooks );
}
2015-01-10 01:03:22 +00:00
}
}
2015-01-10 00:08:45 +00:00
/**
2015-11-03 12:28:01 +00:00
* Webhooks admin actions .
2015-01-10 00:08:45 +00:00
*/
public function actions () {
2015-01-10 00:25:04 +00:00
if ( $this -> is_webhook_settings_page () ) {
2017-10-10 20:15:37 +00:00
// Save.
2017-11-07 18:23:26 +00:00
if ( isset ( $_POST [ 'save' ] ) && isset ( $_POST [ 'webhook_id' ] ) ) { // WPCS: input var okay, CSRF ok.
2015-01-10 00:08:45 +00:00
$this -> save ();
2015-01-09 18:21:19 +00:00
}
2017-10-10 20:15:37 +00:00
// Bulk actions.
2017-11-09 14:07:44 +00:00
if ( isset ( $_REQUEST [ 'action' ] ) && isset ( $_REQUEST [ 'webhook' ] ) ) { // WPCS: input var okay, CSRF ok.
2015-01-10 00:08:45 +00:00
$this -> bulk_actions ();
}
2015-01-10 01:03:22 +00:00
2017-11-09 14:07:44 +00:00
// Delete webhook.
if ( isset ( $_GET [ 'delete' ] ) ) { // WPCS: input var okay, CSRF ok.
$this -> delete ();
2015-01-10 01:03:22 +00:00
}
2015-01-09 18:21:19 +00:00
}
}
2015-05-15 19:50:30 +00:00
/**
2015-11-03 12:28:01 +00:00
* Page output .
2015-05-15 19:50:30 +00:00
*/
2015-05-16 02:03:24 +00:00
public static function page_output () {
2017-10-10 20:15:37 +00:00
// Hide the save button.
2015-05-15 19:50:30 +00:00
$GLOBALS [ 'hide_save_button' ] = true ;
2017-11-07 18:23:26 +00:00
if ( isset ( $_GET [ 'edit-webhook' ] ) ) { // WPCS: input var okay, CSRF ok.
$webhook_id = absint ( $_GET [ 'edit-webhook' ] ); // WPCS: input var okay, CSRF ok.
2015-05-15 19:50:30 +00:00
$webhook = new WC_Webhook ( $webhook_id );
2017-11-09 14:07:44 +00:00
include ( 'settings/views/html-webhooks-edit.php' );
return ;
2015-05-15 19:50:30 +00:00
}
self :: table_list_output ();
}
/**
2015-05-16 02:03:24 +00:00
* Notices .
2015-05-15 19:50:30 +00:00
*/
2015-05-16 02:03:24 +00:00
public static function notices () {
2017-11-07 18:23:26 +00:00
if ( isset ( $_GET [ 'deleted' ] ) ) { // WPCS: input var okay, CSRF ok.
$deleted = absint ( $_GET [ 'deleted' ] ); // WPCS: input var okay, CSRF ok.
2015-05-15 19:50:30 +00:00
2016-10-29 10:16:03 +00:00
/* translators: %d: count */
WC_Admin_Settings :: add_message ( sprintf ( _n ( '%d webhook permanently deleted.' , '%d webhooks permanently deleted.' , $deleted , 'woocommerce' ), $deleted ) );
2015-05-15 19:50:30 +00:00
}
2017-11-07 18:23:26 +00:00
if ( isset ( $_GET [ 'updated' ] ) ) { // WPCS: input var okay, CSRF ok.
2015-05-15 19:50:30 +00:00
WC_Admin_Settings :: add_message ( __ ( 'Webhook updated successfully.' , 'woocommerce' ) );
}
2017-11-07 18:23:26 +00:00
if ( isset ( $_GET [ 'created' ] ) ) { // WPCS: input var okay, CSRF ok.
2015-05-15 19:50:30 +00:00
WC_Admin_Settings :: add_message ( __ ( 'Webhook created successfully.' , 'woocommerce' ) );
}
2015-12-04 12:47:30 +00:00
2017-11-07 18:23:26 +00:00
if ( isset ( $_GET [ 'error' ] ) ) { // WPCS: input var okay, CSRF ok.
WC_Admin_Settings :: add_error ( sanitize_text_field ( wp_unslash ( $_GET [ 'error' ] ) ) ); // WPCS: input var okay, CSRF ok.
2015-12-04 12:47:30 +00:00
}
2015-05-15 19:50:30 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Table list output .
2015-05-15 19:50:30 +00:00
*/
2015-05-16 02:03:24 +00:00
private static function table_list_output () {
2017-11-10 13:33:53 +00:00
echo '<h2>' . esc_html__ ( 'Webhooks' , 'woocommerce' ) . ' <a href="' . esc_url ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=0' ) ) . '" class="add-new-h2">' . esc_html__ ( 'Add webhook' , 'woocommerce' ) . '</a></h2>' ;
2015-05-15 19:50:30 +00:00
2017-10-10 20:15:37 +00:00
// Get the webhooks count.
2017-11-09 13:03:52 +00:00
$data_store = WC_Data_Store :: load ( 'webhook' );
$count = count ( $data_store -> get_webhooks_ids () );
2015-05-15 19:50:30 +00:00
2017-11-09 13:03:52 +00:00
if ( 0 < $count ) {
2017-03-17 22:49:19 +00:00
$webhooks_table_list = new WC_Admin_Webhooks_Table_List ();
$webhooks_table_list -> prepare_items ();
2015-05-15 19:50:30 +00:00
2017-03-17 22:49:19 +00:00
echo '<input type="hidden" name="page" value="wc-settings" />' ;
echo '<input type="hidden" name="tab" value="api" />' ;
echo '<input type="hidden" name="section" value="webhooks" />' ;
$webhooks_table_list -> views ();
$webhooks_table_list -> search_box ( __ ( 'Search webhooks' , 'woocommerce' ), 'webhook' );
$webhooks_table_list -> display ();
} else {
2017-04-27 11:23:16 +00:00
echo '<div class="woocommerce-BlankState woocommerce-BlankState--webhooks">' ;
2017-03-17 22:49:19 +00:00
?>
2017-10-10 20:15:37 +00:00
< h2 class = " woocommerce-BlankState-message " >< ? php esc_html_e ( 'Webhooks are event notifications sent to URLs of your choice. They can be used to integrate with third-party services which support them.' , 'woocommerce' ); ?> </h2>
2017-11-10 13:33:53 +00:00
< a class = " woocommerce-BlankState-cta button-primary button " href = " <?php echo esc_url( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=0' ) ); ?> " >< ? php esc_html_e ( 'Create a new webhook' , 'woocommerce' ); ?> </a>
2017-03-17 22:49:19 +00:00
2017-11-07 18:23:26 +00:00
< ? php
echo '<style type="text/css">#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions { display: none; } </style></div>' ;
2017-03-17 22:49:19 +00:00
}
2015-05-15 19:50:30 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Logs output .
2015-05-15 19:50:30 +00:00
*
2017-11-20 06:11:10 +00:00
* @ deprecated 3.3 . 0
2017-11-10 10:20:59 +00:00
* @ param WC_Webhook $webhook Deprecated .
2015-05-15 19:50:30 +00:00
*/
2017-11-10 06:54:29 +00:00
public static function logs_output ( $webhook = 'deprecated' ) {
2017-11-17 12:46:09 +00:00
wc_deprecated_function ( 'WC_Admin_Webhooks::logs_output' , '3.3' );
2015-05-15 19:50:30 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Get the webhook topic data .
2015-05-15 19:50:30 +00:00
*
2017-10-10 20:15:37 +00:00
* @ param WC_Webhook $webhook Webhook instance .
2017-05-15 11:50:52 +00:00
*
2015-05-15 19:50:30 +00:00
* @ return array
*/
public static function get_topic_data ( $webhook ) {
$topic = $webhook -> get_topic ();
$event = '' ;
$resource = '' ;
if ( $topic ) {
list ( $resource , $event ) = explode ( '.' , $topic );
if ( 'action' === $resource ) {
$topic = 'action' ;
2017-11-07 18:23:26 +00:00
} elseif ( ! in_array ( $resource , array ( 'coupon' , 'customer' , 'order' , 'product' ), true ) ) {
2015-05-15 19:50:30 +00:00
$topic = 'custom' ;
}
}
return array (
'topic' => $topic ,
'event' => $event ,
2016-08-27 01:46:45 +00:00
'resource' => $resource ,
2015-05-15 19:50:30 +00:00
);
}
/**
* Get the logs navigation .
*
2017-11-20 06:11:10 +00:00
* @ deprecated 3.3 . 0
* @ param int $total Deprecated .
* @ param WC_Webhook $webhook Deprecated .
2015-05-15 19:50:30 +00:00
*/
public static function get_logs_navigation ( $total , $webhook ) {
2017-11-10 06:54:29 +00:00
wc_deprecated_function ( 'WC_Admin_Webhooks::get_logs_navigation' , '3.3' );
2015-05-15 19:50:30 +00:00
}
2015-01-09 17:42:01 +00:00
}
new WC_Admin_Webhooks ();