set_name( $name ); if ( ! $webhook->get_user_id() ) { $webhook->set_user_id( get_current_user_id() ); } // Status. $webhook->set_status( ! empty( $_POST['webhook_status'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_status'] ) ) : 'disabled' ); // WPCS: input var okay, CSRF ok. // Delivery URL. $delivery_url = ! empty( $_POST['webhook_delivery_url'] ) ? esc_url_raw( wp_unslash( $_POST['webhook_delivery_url'] ) ) : ''; // WPCS: input var okay, CSRF ok. if ( wc_is_valid_url( $delivery_url ) ) { $webhook->set_delivery_url( $delivery_url ); } // Secret. $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. $webhook->set_secret( $secret ); // Topic. if ( ! empty( $_POST['webhook_topic'] ) ) { // WPCS: input var okay, CSRF ok. $resource = ''; $event = ''; 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. } break; case 'action': $resource = 'action'; $event = ! empty( $_POST['webhook_action_event'] ) ? sanitize_text_field( wp_unslash( $_POST['webhook_action_event'] ) ) : ''; // WPCS: input var okay, CSRF ok. break; default: list( $resource, $event ) = explode( '.', sanitize_text_field( wp_unslash( $_POST['webhook_topic'] ) ) ); // WPCS: input var okay, CSRF ok. break; } $topic = $resource . '.' . $event; if ( wc_is_webhook_valid_topic( $topic ) ) { $webhook->set_topic( $topic ); } } // API version. $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. $webhook->save(); // Run actions. do_action( 'woocommerce_webhook_options_save', $webhook->get_id() ); // Ping the webhook at the first time that is activated. if ( isset( $_POST['webhook_status'] ) && 'active' === $_POST['webhook_status'] && $webhook->get_pending_delivery() ) { // WPCS: input var okay, CSRF ok. $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->get_id() . '&error=' . rawurlencode( $result->get_error_message() ) ) ); exit(); } } // 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' ) ); exit(); } /** * Bulk delete. * * @param array $webhooks List of webhooks IDs. */ private function bulk_delete( $webhooks ) { foreach ( $webhooks as $webhook_id ) { $webhook = new WC_Webhook( (int) $webhook_id ); $webhook->delete( true ); } $qty = count( $webhooks ); $status = isset( $_GET['status'] ) ? '&status=' . sanitize_text_field( wp_unslash( $_GET['status'] ) ) : ''; // WPCS: input var okay, CSRF ok. // Redirect to webhooks page. wp_safe_redirect( admin_url( 'admin.php?page=wc-settings&tab=api§ion=webhooks' . $status . '&deleted=' . $qty ) ); exit(); } /** * Delete webhook. */ private function delete() { check_admin_referer( 'delete-webhook' ); if ( isset( $_GET['delete'] ) ) { // WPCS: input var okay, CSRF ok. $webhook_id = absint( $_GET['delete'] ); // WPCS: input var okay, CSRF ok. if ( $webhook_id ) { $this->bulk_delete( array( $webhook_id ) ); } } } /** * Bulk actions. */ private function bulk_actions() { check_admin_referer( 'woocommerce-settings' ); if ( ! current_user_can( 'manage_woocommerce' ) ) { wp_die( esc_html__( 'You do not have permission to edit Webhooks', 'woocommerce' ) ); } 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. $action = sanitize_text_field( wp_unslash( $_REQUEST['action'] ) ); // WPCS: input var okay, CSRF ok. if ( 'delete' === $action ) { $this->bulk_delete( $webhooks ); } } } /** * Webhooks admin actions. */ public function actions() { if ( $this->is_webhook_settings_page() ) { // Save. if ( isset( $_POST['save'] ) && isset( $_POST['webhook_id'] ) ) { // WPCS: input var okay, CSRF ok. $this->save(); } // Bulk actions. if ( isset( $_REQUEST['action'] ) && isset( $_REQUEST['webhook'] ) ) { // WPCS: input var okay, CSRF ok. $this->bulk_actions(); } // Delete webhook. if ( isset( $_GET['delete'] ) ) { // WPCS: input var okay, CSRF ok. $this->delete(); } } } /** * Page output. */ public static function page_output() { // Hide the save button. $GLOBALS['hide_save_button'] = true; if ( isset( $_GET['edit-webhook'] ) ) { // WPCS: input var okay, CSRF ok. $webhook_id = absint( $_GET['edit-webhook'] ); // WPCS: input var okay, CSRF ok. $webhook = new WC_Webhook( $webhook_id ); include( 'settings/views/html-webhooks-edit.php' ); return; } self::table_list_output(); } /** * Notices. */ public static function notices() { if ( isset( $_GET['deleted'] ) ) { // WPCS: input var okay, CSRF ok. $deleted = absint( $_GET['deleted'] ); // WPCS: input var okay, CSRF ok. /* translators: %d: count */ WC_Admin_Settings::add_message( sprintf( _n( '%d webhook permanently deleted.', '%d webhooks permanently deleted.', $deleted, 'woocommerce' ), $deleted ) ); } if ( isset( $_GET['updated'] ) ) { // WPCS: input var okay, CSRF ok. WC_Admin_Settings::add_message( __( 'Webhook updated successfully.', 'woocommerce' ) ); } if ( isset( $_GET['created'] ) ) { // WPCS: input var okay, CSRF ok. WC_Admin_Settings::add_message( __( 'Webhook created successfully.', 'woocommerce' ) ); } 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. } } /** * Table list output. */ private static function table_list_output() { echo '