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
*
* @ author WooThemes
* @ category Admin
* @ package WooCommerce / Admin
2015-05-15 19:12:11 +00:00
* @ version 2.4 . 0
2015-01-09 17:42:01 +00:00
*/
if ( ! defined ( 'ABSPATH' ) ) {
exit ; // Exit if accessed directly
}
/**
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 () {
2015-01-10 00:08:45 +00:00
add_action ( 'admin_init' , array ( $this , 'actions' ) );
}
/**
2015-11-03 12:28:01 +00:00
* Check if is webhook settings page .
2015-01-10 00:08:45 +00:00
*
* @ return bool
*/
2015-01-10 00:25:04 +00:00
private function is_webhook_settings_page () {
2017-06-16 12:58:28 +00:00
return isset ( $_GET [ 'page' ], $_GET [ 'tab' ], $_GET [ 'section' ] )
2017-06-16 12:53:57 +00:00
&& 'wc-settings' === $_GET [ 'page' ]
&& 'api' === $_GET [ 'tab' ]
2017-06-16 12:58:28 +00:00
&& 'webhooks' === $_GET [ 'section' ];
2015-01-09 17:42:01 +00:00
}
/**
2015-11-03 12:28:01 +00:00
* Updated the Webhook name .
2015-01-09 17:42:01 +00:00
*
* @ param int $webhook_id
*/
private function update_name ( $webhook_id ) {
global $wpdb ;
2016-09-01 20:50:14 +00:00
// @codingStandardsIgnoreStart
2016-10-29 10:16:03 +00:00
/* translators: %s: date` */
2015-01-09 17:42:01 +00:00
$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' ) ) );
2016-09-01 20:50:14 +00:00
// @codingStandardsIgnoreEnd
2015-01-09 17:42:01 +00:00
$wpdb -> update ( $wpdb -> posts , array ( 'post_title' => $name ), array ( 'ID' => $webhook_id ) );
}
/**
2015-11-03 12:28:01 +00:00
* Updated the Webhook status .
2015-01-09 17:42:01 +00:00
*
* @ param WC_Webhook $webhook
*/
private function update_status ( $webhook ) {
$status = ! empty ( $_POST [ 'webhook_status' ] ) ? wc_clean ( $_POST [ 'webhook_status' ] ) : '' ;
$webhook -> update_status ( $status );
}
/**
2015-11-03 12:28:01 +00:00
* Updated the Webhook delivery URL .
2015-01-09 17:42:01 +00:00
*
* @ 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 );
}
}
/**
2015-11-03 12:28:01 +00:00
* Updated the Webhook secret .
2015-01-09 17:42:01 +00:00
*
* @ param WC_Webhook $webhook
*/
private function update_secret ( $webhook ) {
2016-06-28 00:55:54 +00:00
$secret = ! empty ( $_POST [ 'webhook_secret' ] ) ? $_POST [ 'webhook_secret' ] : wp_generate_password ( 50 , true , true );
2015-01-09 17:42:01 +00:00
$webhook -> set_secret ( $secret );
}
/**
2015-11-03 12:28:01 +00:00
* Updated the Webhook topic .
2015-01-09 17:42:01 +00:00
*
* @ param WC_Webhook $webhook
*/
private function update_topic ( $webhook ) {
if ( ! empty ( $_POST [ 'webhook_topic' ] ) ) {
2015-01-29 13:11:35 +00:00
$resource = '' ;
$event = '' ;
switch ( $_POST [ 'webhook_topic' ] ) {
case 'custom' :
if ( ! empty ( $_POST [ 'webhook_custom_topic' ] ) ) {
list ( $resource , $event ) = explode ( '.' , wc_clean ( $_POST [ 'webhook_custom_topic' ] ) );
}
break ;
case 'action' :
$resource = 'action' ;
$event = ! empty ( $_POST [ 'webhook_action_event' ] ) ? wc_clean ( $_POST [ 'webhook_action_event' ] ) : '' ;
break ;
default :
list ( $resource , $event ) = explode ( '.' , wc_clean ( $_POST [ 'webhook_topic' ] ) );
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
/**
* Update webhook api version .
*
* @ param WC_Webhook $webhook Webhook instance .
*/
private function update_api_version ( $webhook ) {
2017-04-02 06:07:09 +00:00
$version = ! empty ( $_POST [ 'webhook_api_version' ] ) ? wc_clean ( $_POST [ 'webhook_api_version' ] ) : 'wp_api_v2' ;
2016-11-22 23:58:36 +00:00
$webhook -> set_api_version ( $version );
}
2015-01-09 17:42:01 +00:00
/**
2015-11-03 12:28:01 +00:00
* Save method .
2015-01-09 17:42:01 +00:00
*/
2015-01-10 00:08:45 +00:00
private function save () {
if ( empty ( $_REQUEST [ '_wpnonce' ] ) || ! wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'woocommerce-settings' ) ) {
2015-05-18 19:01:51 +00:00
wp_die ( __ ( 'Action failed. Please refresh the page and retry.' , 'woocommerce' ) );
2015-01-10 00:08:45 +00:00
}
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
$webhook_id = absint ( $_POST [ 'webhook_id' ] );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
if ( ! current_user_can ( 'edit_shop_webhook' , $webhook_id ) ) {
return ;
}
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
$webhook = new WC_Webhook ( $webhook_id );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Name
$this -> update_name ( $webhook -> id );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Status
$this -> update_status ( $webhook );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Delivery URL
$this -> update_delivery_url ( $webhook );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Secret
$this -> update_secret ( $webhook );
2015-01-09 17:42:01 +00:00
2015-01-10 00:08:45 +00:00
// Topic
$this -> update_topic ( $webhook );
2015-01-09 17:42:01 +00:00
2016-11-22 23:58:36 +00:00
// API version.
$this -> update_api_version ( $webhook );
2016-05-24 20:05:50 +00:00
// Update date.
wp_update_post ( array ( 'ID' => $webhook -> id , 'post_modified' => current_time ( 'mysql' ) ) );
2015-12-04 12:47:30 +00:00
// Run actions
2015-01-10 00:08:45 +00:00
do_action ( 'woocommerce_webhook_options_save' , $webhook -> id );
2015-01-09 17:42:01 +00:00
2015-01-23 13:28:30 +00:00
delete_transient ( 'woocommerce_webhook_ids' );
2015-12-04 12:47:30 +00:00
// Ping the webhook at the first time that is activated
$pending_delivery = get_post_meta ( $webhook -> id , '_webhook_pending_delivery' , true );
if ( isset ( $_POST [ 'webhook_status' ] ) && 'active' === $_POST [ 'webhook_status' ] && $pending_delivery ) {
$result = $webhook -> deliver_ping ();
if ( is_wp_error ( $result ) ) {
// 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 -> id . '&error=' . urlencode ( $result -> get_error_message () ) ) );
exit ();
}
}
2015-01-10 00:08:45 +00:00
// Redirect to webhook edit page to avoid settings save actions
2015-12-04 12:47:30 +00:00
wp_safe_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook -> 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
/**
2015-11-03 12:28:01 +00:00
* Create Webhook .
2015-01-10 00:08:45 +00:00
*/
private function create () {
2015-05-18 19:01:51 +00:00
if ( empty ( $_REQUEST [ '_wpnonce' ] ) || ! wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'create-webhook' ) ) {
wp_die ( __ ( 'Action failed. Please refresh the page and retry.' , 'woocommerce' ) );
}
2015-01-10 00:08:45 +00:00
if ( ! current_user_can ( 'publish_shop_webhooks' ) ) {
2017-08-14 16:25:08 +00:00
wp_die ( __ ( 'You do not have permission to create Webhooks' , 'woocommerce' ) );
2015-01-09 17:42:01 +00:00
}
2015-01-10 00:08:45 +00:00
$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 ,
2016-09-01 20:50:14 +00:00
// @codingStandardsIgnoreStart
2016-10-29 10:16:03 +00:00
/* translators: %s: date */
2015-01-10 00:08:45 +00:00
'post_title' => sprintf ( __ ( 'Webhook created on %s' , 'woocommerce' ), strftime ( _x ( '%b %d, %Y @ %I:%M %p' , 'Webhook created on date parsed by strftime' , 'woocommerce' ) ) ),
2016-09-01 20:50:14 +00:00
// @codingStandardsIgnoreEnd
2016-08-27 01:46:45 +00:00
'comment_status' => 'open' ,
2015-01-10 00:08:45 +00:00
) );
if ( is_wp_error ( $webhook_id ) ) {
wp_die ( $webhook_id -> get_error_messages () );
}
update_post_meta ( $webhook_id , '_webhook_pending_delivery' , true );
2016-11-22 23:58:36 +00:00
$webhook = new WC_Webhook ( $webhook_id );
2017-04-02 06:07:09 +00:00
$webhook -> set_api_version ( 'wp_api_v2' );
2015-01-10 00:08:45 +00:00
2015-01-23 13:28:30 +00:00
delete_transient ( 'woocommerce_webhook_ids' );
2015-01-10 00:08:45 +00:00
// Redirect to edit page
2015-05-15 19:12:11 +00:00
wp_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook_id . '&created=1' ) );
2015-01-10 00:08:45 +00:00
exit ();
2015-01-09 17:42:01 +00:00
}
2015-01-09 18:21:19 +00:00
/**
2015-11-03 12:28:01 +00:00
* Bulk trash / delete .
2015-01-10 00:08:45 +00:00
*
* @ param array $webhooks
* @ param bool $delete
2015-01-09 18:21:19 +00:00
*/
2015-01-10 00:08:45 +00:00
private function bulk_trash ( $webhooks , $delete = false ) {
foreach ( $webhooks as $webhook_id ) {
if ( $delete ) {
wp_delete_post ( $webhook_id , true );
} else {
wp_trash_post ( $webhook_id );
2015-01-09 18:21:19 +00:00
}
2015-01-10 00:08:45 +00:00
}
$type = ! EMPTY_TRASH_DAYS || $delete ? 'deleted' : 'trashed' ;
$qty = count ( $webhooks );
$status = isset ( $_GET [ 'status' ] ) ? '&status=' . sanitize_text_field ( $_GET [ 'status' ] ) : '' ;
2015-01-09 18:21:19 +00:00
2015-05-18 19:01:51 +00:00
delete_transient ( 'woocommerce_webhook_ids' );
2015-01-10 00:08:45 +00:00
// Redirect to webhooks page
2015-05-15 19:12:11 +00:00
wp_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks' . $status . '&' . $type . '=' . $qty ) );
2015-01-10 00:08:45 +00:00
exit ();
}
/**
2015-11-03 12:28:01 +00:00
* Bulk untrash .
2015-01-10 00:08:45 +00:00
*
* @ param array $webhooks
*/
private function bulk_untrash ( $webhooks ) {
foreach ( $webhooks as $webhook_id ) {
wp_untrash_post ( $webhook_id );
}
$qty = count ( $webhooks );
2015-05-18 19:01:51 +00:00
delete_transient ( 'woocommerce_webhook_ids' );
2015-01-10 00:08:45 +00:00
// Redirect to webhooks page
2015-05-15 19:12:11 +00:00
wp_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&status=trash&untrashed=' . $qty ) );
2015-01-10 00:08:45 +00:00
exit ();
}
/**
2015-11-03 12:28:01 +00:00
* Bulk actions .
2015-01-10 00:08:45 +00:00
*/
private function bulk_actions () {
2015-05-18 19:01:51 +00:00
if ( empty ( $_REQUEST [ '_wpnonce' ] ) || ! wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'woocommerce-settings' ) ) {
wp_die ( __ ( 'Action failed. Please refresh the page and retry.' , 'woocommerce' ) );
}
2015-01-10 00:08:45 +00:00
if ( ! current_user_can ( 'edit_shop_webhooks' ) ) {
2017-08-14 16:25:08 +00:00
wp_die ( __ ( 'You do not have permission to edit Webhooks' , 'woocommerce' ) );
2015-01-10 00:08:45 +00:00
}
$webhooks = array_map ( 'absint' , ( array ) $_GET [ 'webhook' ] );
switch ( $_GET [ 'action' ] ) {
case 'trash' :
$this -> bulk_trash ( $webhooks );
break ;
case 'untrash' :
$this -> bulk_untrash ( $webhooks );
break ;
case 'delete' :
$this -> bulk_trash ( $webhooks , true );
break ;
default :
break ;
}
}
2015-01-10 01:03:22 +00:00
/**
2015-11-03 12:28:01 +00:00
* Empty Trash .
2015-01-10 01:03:22 +00:00
*/
2015-05-15 19:50:30 +00:00
private function empty_trash () {
2015-05-18 19:01:51 +00:00
if ( empty ( $_REQUEST [ '_wpnonce' ] ) || ! wp_verify_nonce ( $_REQUEST [ '_wpnonce' ], 'empty_trash' ) ) {
wp_die ( __ ( 'Action failed. Please refresh the page and retry.' , 'woocommerce' ) );
}
2015-01-10 01:03:22 +00:00
if ( ! current_user_can ( 'delete_shop_webhooks' ) ) {
2017-08-14 16:25:08 +00:00
wp_die ( __ ( 'You do not have permission to delete Webhooks' , 'woocommerce' ) );
2015-01-10 01:03:22 +00:00
}
$webhooks = get_posts ( array (
'post_type' => 'shop_webhook' ,
'ignore_sticky_posts' => true ,
'nopaging' => true ,
'post_status' => 'trash' ,
2016-08-27 01:46:45 +00:00
'fields' => 'ids' ,
2015-01-10 01:03:22 +00:00
) );
foreach ( $webhooks as $webhook_id ) {
wp_delete_post ( $webhook_id , true );
}
$qty = count ( $webhooks );
// Redirect to webhooks page
2015-05-15 19:12:11 +00:00
wp_redirect ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&deleted=' . $qty ) );
2015-01-10 01:03:22 +00:00
exit ();
}
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 () ) {
2015-01-10 00:08:45 +00:00
// Save
if ( isset ( $_POST [ 'save' ] ) && isset ( $_POST [ 'webhook_id' ] ) ) {
$this -> save ();
2015-01-09 18:21:19 +00:00
}
2015-01-10 00:08:45 +00:00
// Create
if ( isset ( $_GET [ 'create-webhook' ] ) ) {
$this -> create ();
}
2015-01-09 18:21:19 +00:00
2015-01-10 01:03:22 +00:00
// Bulk actions
2015-01-10 00:08:45 +00:00
if ( isset ( $_GET [ 'action' ] ) && isset ( $_GET [ 'webhook' ] ) ) {
$this -> bulk_actions ();
}
2015-01-10 01:03:22 +00:00
2015-05-18 19:01:51 +00:00
// Empty trash
2015-01-10 01:03:22 +00:00
if ( isset ( $_GET [ 'empty_trash' ] ) ) {
$this -> empty_trash ();
}
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 () {
2015-05-15 19:50:30 +00:00
// Hide the save button
$GLOBALS [ 'hide_save_button' ] = true ;
if ( isset ( $_GET [ 'edit-webhook' ] ) ) {
$webhook_id = absint ( $_GET [ 'edit-webhook' ] );
$webhook = new WC_Webhook ( $webhook_id );
if ( 'trash' != $webhook -> post_data -> post_status ) {
2015-05-16 02:03:24 +00:00
include ( 'settings/views/html-webhooks-edit.php' );
2015-05-15 19:50:30 +00:00
return ;
}
}
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 () {
2015-05-15 19:50:30 +00:00
if ( isset ( $_GET [ 'trashed' ] ) ) {
$trashed = absint ( $_GET [ 'trashed' ] );
2016-10-29 10:16:03 +00:00
/* translators: %d: count */
WC_Admin_Settings :: add_message ( sprintf ( _n ( '%d webhook moved to the Trash.' , '%d webhooks moved to the Trash.' , $trashed , 'woocommerce' ), $trashed ) );
2015-05-15 19:50:30 +00:00
}
if ( isset ( $_GET [ 'untrashed' ] ) ) {
$untrashed = absint ( $_GET [ 'untrashed' ] );
2016-10-29 10:16:03 +00:00
/* translators: %d: count */
WC_Admin_Settings :: add_message ( sprintf ( _n ( '%d webhook restored from the Trash.' , '%d webhooks restored from the Trash.' , $untrashed , 'woocommerce' ), $untrashed ) );
2015-05-15 19:50:30 +00:00
}
if ( isset ( $_GET [ 'deleted' ] ) ) {
$deleted = absint ( $_GET [ 'deleted' ] );
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
}
if ( isset ( $_GET [ 'updated' ] ) ) {
WC_Admin_Settings :: add_message ( __ ( 'Webhook updated successfully.' , 'woocommerce' ) );
}
if ( isset ( $_GET [ 'created' ] ) ) {
WC_Admin_Settings :: add_message ( __ ( 'Webhook created successfully.' , 'woocommerce' ) );
}
2015-12-04 12:47:30 +00:00
if ( isset ( $_GET [ 'error' ] ) ) {
WC_Admin_Settings :: add_error ( wc_clean ( $_GET [ 'error' ] ) );
}
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 () {
2016-10-12 10:16:30 +00:00
echo '<h2>' . __ ( 'Webhooks' , 'woocommerce' ) . ' <a href="' . esc_url ( wp_nonce_url ( admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&create-webhook=1' ), 'create-webhook' ) ) . '" class="add-new-h2">' . __ ( 'Add webhook' , 'woocommerce' ) . '</a></h2>' ;
2015-05-15 19:50:30 +00:00
2017-03-17 22:49:19 +00:00
// Get the webhooks count
$count = array_sum ( ( array ) wp_count_posts ( 'shop_webhook' , 'readable' ) );
2015-05-15 19:50:30 +00:00
2017-03-17 22:49:19 +00:00
if ( absint ( $count ) && $count > 0 ) {
$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-04-27 11:23:16 +00:00
< h2 class = " woocommerce-BlankState-message " >< ? php _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>
< a class = " woocommerce-BlankState-cta button-primary button " href = " <?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks&create-webhook=1' ), 'create-webhook' ) ); ?> " >< ? php _e ( 'Create a new webhook' , 'woocommerce' ); ?> </a>
2017-03-17 22:49:19 +00:00
< ? php echo '<style type="text/css">#posts-filter .wp-list-table, #posts-filter .tablenav.top, .tablenav.bottom .actions { display: none; } </style></div>' ;
}
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
*
* @ param WC_Webhook $webhook
*/
public static function logs_output ( $webhook ) {
$current = isset ( $_GET [ 'log_page' ] ) ? absint ( $_GET [ 'log_page' ] ) : 1 ;
$args = array (
'post_id' => $webhook -> id ,
'status' => 'approve' ,
'type' => 'webhook_delivery' ,
2016-08-27 01:46:45 +00:00
'number' => 10 ,
2015-05-15 19:50:30 +00:00
);
if ( 1 < $current ) {
$args [ 'offset' ] = ( $current - 1 ) * 10 ;
}
remove_filter ( 'comments_clauses' , array ( 'WC_Comments' , 'exclude_webhook_comments' ), 10 , 1 );
$logs = get_comments ( $args );
add_filter ( 'comments_clauses' , array ( 'WC_Comments' , 'exclude_webhook_comments' ), 10 , 1 );
if ( $logs ) {
2016-07-27 10:58:43 +00:00
include_once ( dirname ( __FILE__ ) . '/settings/views/html-webhook-logs.php' );
2015-05-15 19:50:30 +00:00
} else {
echo '<p>' . __ ( 'This Webhook has no log yet.' , 'woocommerce' ) . '</p>' ;
}
}
/**
2015-11-03 12:28:01 +00:00
* Get the webhook topic data .
2015-05-15 19:50:30 +00:00
*
2017-05-15 11:50:52 +00:00
* @ param WC_Webhook $webhook
*
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' ;
2016-09-01 20:50:14 +00:00
} elseif ( ! in_array ( $resource , array ( 'coupon' , 'customer' , 'order' , 'product' ) ) ) {
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 .
*
* @ param int $total
2017-05-15 11:50:52 +00:00
* @ param WC_Webhook $webhook
2015-05-15 19:50:30 +00:00
*
* @ return string
*/
public static function get_logs_navigation ( $total , $webhook ) {
$pages = ceil ( $total / 10 );
$current = isset ( $_GET [ 'log_page' ] ) ? absint ( $_GET [ 'log_page' ] ) : 1 ;
$html = '<div class="webhook-logs-navigation">' ;
$html .= '<p class="info" style="float: left;"><strong>' ;
2016-10-29 13:10:55 +00:00
/* translators: 1: items count (i.e. 8 items) 2: current page 3: total pages */
2016-10-29 10:16:03 +00:00
$html .= sprintf (
2016-11-23 00:47:58 +00:00
__ ( '%1$s – Page %2$d of %3$d' , 'woocommerce' ),
2016-10-29 10:16:03 +00:00
sprintf ( _n ( '%d item' , '%d items' , $total , 'woocommerce' ), $total ),
$current ,
$pages
);
2015-05-15 19:50:30 +00:00
$html .= '</strong></p>' ;
if ( 1 < $pages ) {
$html .= '<p class="tools" style="float: right;">' ;
if ( 1 == $current ) {
$html .= '<button class="button-primary" disabled="disabled">' . __ ( '‹ Previous' , 'woocommerce' ) . '</button> ' ;
} else {
$html .= '<a class="button-primary" href="' . admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook -> id . '&log_page=' . ( $current - 1 ) ) . '#webhook-logs">' . __ ( '‹ Previous' , 'woocommerce' ) . '</a> ' ;
}
if ( $pages == $current ) {
$html .= '<button class="button-primary" disabled="disabled">' . __ ( 'Next ›' , 'woocommerce' ) . '</button>' ;
} else {
$html .= '<a class="button-primary" href="' . admin_url ( 'admin.php?page=wc-settings&tab=api§ion=webhooks&edit-webhook=' . $webhook -> id . '&log_page=' . ( $current + 1 ) ) . '#webhook-logs">' . __ ( 'Next ›' , 'woocommerce' ) . '</a>' ;
}
$html .= '</p>' ;
}
$html .= '<div class="clear"></div></div>' ;
return $html ;
}
2015-01-09 17:42:01 +00:00
}
new WC_Admin_Webhooks ();