strtolower( wp_generate_uuid4() ), 'uuid' => Codexta_Connect::site_uuid(), 'event' => $event, 'at' => gmdate( 'Y-m-d\TH:i:s\Z' ), ) ); $response = wp_remote_post( $url . '/integrations/woocommerce/webhooks', array( 'timeout' => 5, 'redirection' => 0, 'headers' => array( 'Content-Type' => 'application/json', 'X-Codexta-Signature' => base64_encode( hash_hmac( 'sha256', $body, $secret, true ) ), ), 'body' => $body, ) ); return ! is_wp_error( $response ) && 200 <= wp_remote_retrieve_response_code( $response ) && 300 > wp_remote_retrieve_response_code( $response ); } /** * Delete the WooCommerce API key Codexta was given, so a disconnected site leaves no live credential behind. * * @param bool $keep_newest Keep the newest one: the key a connection has just been made with, while the ones of earlier connections go. */ public static function revoke_api_keys( $keep_newest = false ) { global $wpdb; $suppress = $wpdb->suppress_errors( true ); $table = $wpdb->prefix . 'woocommerce_api_keys'; $like = $wpdb->esc_like( 'Codexta Assistant' ) . '%'; if ( $keep_newest ) { $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE description LIKE %s AND key_id < (SELECT newest FROM (SELECT MAX(key_id) AS newest FROM %i WHERE description LIKE %s) AS k)', $table, $like, $table, $like ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } else { $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE description LIKE %s', $table, $like ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } $wpdb->suppress_errors( $suppress ); } /** * Delete the WooCommerce webhooks Codexta created in this shop (they point at Codexta's /integrations/woocommerce/webhooks). The plugin does it * itself, not through Codexta: Codexta's own cleanup would need the API key that is revoked here. */ public static function remove_webhooks() { global $wpdb; $suppress = $wpdb->suppress_errors( true ); $like = '%' . $wpdb->esc_like( '/integrations/woocommerce/webhooks?u=' ) . '%'; $ids = $wpdb->get_col( $wpdb->prepare( 'SELECT webhook_id FROM %i WHERE delivery_url LIKE %s', $wpdb->prefix . 'wc_webhooks', $like ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery foreach ( (array) $ids as $id ) { $hook = function_exists( 'wc_get_webhook' ) ? wc_get_webhook( (int) $id ) : null; if ( $hook ) { $hook->delete( true ); } else { $wpdb->delete( $wpdb->prefix . 'wc_webhooks', array( 'webhook_id' => (int) $id ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery } } $wpdb->suppress_errors( $suppress ); } }