id = 'webhooks'; $this->label = __( 'Webhooks', 'woocommerce' ); add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); add_action( 'woocommerce_settings_form_method_tab_' . $this->id, array( $this, 'form_method' ) ); $this->notices(); } /** * Form method * * @param string $method * * @return string */ public function form_method( $method ) { if ( isset( $_GET['edit-webhook'] ) ) { $webhook_id = absint( $_GET['edit-webhook'] ); $webhook = new WC_Webhook( $webhook_id ); if ( 'trash' != $webhook->post_data->post_status ) { return 'post'; } } return 'get'; } /** * Notices. */ private function notices() { if ( isset( $_GET['trashed'] ) ) { $trashed = absint( $_GET['trashed'] ); WC_Admin_Settings::add_message( sprintf( _n( '1 webhook moved to the Trash.', '%d webhooks moved to the Trash.', $trashed, 'woocommerce' ), $trashed ) ); } if ( isset( $_GET['untrashed'] ) ) { $untrashed = absint( $_GET['untrashed'] ); WC_Admin_Settings::add_message( sprintf( _n( '1 webhook restored from the Trash.', '%d webhooks restored from the Trash.', $untrashed, 'woocommerce' ), $untrashed ) ); } if ( isset( $_GET['deleted'] ) ) { $deleted = absint( $_GET['deleted'] ); WC_Admin_Settings::add_message( sprintf( _n( '1 webhook permanently deleted.', '%d webhooks permanently deleted.', $deleted, 'woocommerce' ), $deleted ) ); } 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' ) ); } } /** * Table list output */ private function table_list_output() { echo '

' . __( 'Webhooks', 'woocommerce' ) . ' ' . __( 'Add Webhook', 'woocommerce' ) . '

'; $webhooks_table_list = new WC_Admin_Webhooks_Table_List(); $webhooks_table_list->prepare_items(); echo ''; echo ''; $webhooks_table_list->views(); $webhooks_table_list->search_box( __( 'Search Webhooks', 'woocommerce' ), 'webhook' ); $webhooks_table_list->display(); } /** * Edit webhook output * * @param WC_Webhook $webhook */ private function edit_output( $webhook ) { include_once( 'views/html-webhooks-edit.php' ); } /** * Logs output * * @param WC_Webhook $webhook */ private 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', 'number' => 10 ); 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 ) { include_once( 'views/html-webhook-logs.php' ); } else { echo '

' . __( 'This Webhook has no log yet.', 'woocommerce' ) . '

'; } } /** * Output the settings */ public function output() { global $current_section; // 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 ) { $this->edit_output( $webhook ); return; } } $this->table_list_output(); } /** * Get the webhook topic data * * @return array */ private function get_topic_data( $webhook ) { $topic = $webhook->get_topic(); $event = ''; $resource = ''; if ( $topic ) { list( $resource, $event ) = explode( '.', $topic ); if ( 'action' === $resource ) { $topic = 'action'; } else if ( ! in_array( $resource, array( 'coupon', 'customer', 'order', 'product' ) ) ) { $topic = 'custom'; } } return array( 'topic' => $topic, 'event' => $event, 'resource' => $resource ); } /** * Get the logs navigation. * * @param int $total * * @return string */ private function get_logs_navigation( $total, $webhook ) { $pages = ceil( $total / 10 ); $current = isset( $_GET['log_page'] ) ? absint( $_GET['log_page'] ) : 1; $html = '
'; $html .= '

'; $html .= sprintf( '%s – Page %d of %d', _n( '1 item', sprintf( '%d items', $total ), $total, 'woocommerce' ), $current, $pages ); $html .= '

'; if ( 1 < $pages ) { $html .= '

'; if ( 1 == $current ) { $html .= ' '; } else { $html .= '' . __( '‹ Previous', 'woocommerce' ) . ' '; } if ( $pages == $current ) { $html .= ''; } else { $html .= '' . __( 'Next ›', 'woocommerce' ) . ''; } $html .= '

'; } $html .= '
'; return $html; } } endif; return new WC_Settings_Webhooks();