From e159e7ff4f67d6da2b128f446288c0bd200c882b Mon Sep 17 00:00:00 2001 From: Matt Sherman Date: Wed, 8 Nov 2023 16:43:07 -0500 Subject: [PATCH] API for updating the block template logging threshold --- plugins/woocommerce-beta-tester/api/api.php | 2 +- .../set-block-template-logging-threshold.php | 102 ++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 plugins/woocommerce-beta-tester/api/tools/set-block-template-logging-threshold.php diff --git a/plugins/woocommerce-beta-tester/api/api.php b/plugins/woocommerce-beta-tester/api/api.php index df138398cc9..2f8a0c9de77 100644 --- a/plugins/woocommerce-beta-tester/api/api.php +++ b/plugins/woocommerce-beta-tester/api/api.php @@ -58,4 +58,4 @@ require 'features/features.php'; require 'rest-api-filters/class-wca-test-helper-rest-api-filters.php'; require 'rest-api-filters/hook.php'; require 'live-branches/manifest.php'; -require 'live-branches/install.php'; +require 'tools/set-block-template-logging-threshold.php'; diff --git a/plugins/woocommerce-beta-tester/api/tools/set-block-template-logging-threshold.php b/plugins/woocommerce-beta-tester/api/tools/set-block-template-logging-threshold.php new file mode 100644 index 00000000000..86640461afe --- /dev/null +++ b/plugins/woocommerce-beta-tester/api/tools/set-block-template-logging-threshold.php @@ -0,0 +1,102 @@ + 'GET', + ) +); + +register_woocommerce_admin_test_helper_rest_route( + '/tools/get-block-template-logging-threshold/v1', + 'tools_get_block_template_logging_threshold', + array( + 'methods' => 'GET', + ) +); + +register_woocommerce_admin_test_helper_rest_route( + '/tools/update-block-template-logging-threshold/v1', + 'tools_update_block_template_logging_threshold', + array( + 'methods' => 'POST', + 'args' => array( + 'threshold' => array( + 'description' => 'Logging threshold', + 'type' => 'string', + 'sanitize_callback' => 'sanitize_text_field', + ), + ), + ) +); + +/** + * Get the list of logging levels. + */ +function tools_get_logging_levels() { + $levels = array( + array( + 'label' => 'Emergency', + 'value' => \WC_Log_Levels::EMERGENCY, + ), + array( + 'label' => 'Alert', + 'value' => \WC_Log_Levels::ALERT, + ), + array( + 'label' => 'Critical', + 'value' => \WC_Log_Levels::CRITICAL, + ), + array( + 'label' => 'Error', + 'value' => \WC_Log_Levels::ERROR, + ), + array( + 'label' => 'Warning', + 'value' => \WC_Log_Levels::WARNING, + ), + array( + 'label' => 'Notice', + 'value' => \WC_Log_Levels::NOTICE, + ), + array( + 'label' => 'Info', + 'value' => \WC_Log_Levels::INFO, + ), + array( + 'label' => 'Debug', + 'value' => \WC_Log_Levels::DEBUG, + ), + ); + + return new WP_REST_Response( $levels, 200 ); +} + +/** + * Get the block template logging threshold. + */ +function tools_get_block_template_logging_threshold() { + $threshold = get_option( 'woocommerce_block_template_logging_threshold', \WC_Log_Levels::INFO ); + + return new WP_REST_Response( $threshold, 200 ); +} + +/** + * Update the block template logging threshold. + * + * @param WP_REST_Request $request The full request data. + */ +function tools_update_block_template_logging_threshold( $request ) { + $threshold = $request->get_param( 'threshold' ); + + if ( ! isset( $threshold ) || ! \WC_Log_Levels::is_valid_level( $threshold ) ) { + return new WP_REST_Response( 'Invalid threshold', 400 ); + } + + update_option( 'woocommerce_block_template_logging_threshold', $threshold ); + + return new WP_REST_Response( $threshold, 200 ); +}