namespace = $rest_namespace; $this->rest_base = '/log'; } /** * Register the API endpoints. * * @return void */ public function register_routes() { $this->logger->log_debug( 'Hook executed', __FUNCTION__, __CLASS__ ); $store_id_args = array( self::PARAM_STORE_ID => $this->get_arg_string() ); $conf_args = array_merge( $store_id_args, array( self::PARAM_IS_ENABLED => $this->get_arg_bool(), self::PARAM_LOG_LEVEL => $this->get_arg_int(), ) ); $store_id = $this->url_param_pattern( self::PARAM_STORE_ID ); $this->register_get( "{$store_id}", 'get_logs', $store_id_args ); $this->register_delete( "{$store_id}", 'delete_logs', $store_id_args ); $this->register_get( "settings/{$store_id}", 'get_configuration', $store_id_args ); $this->register_post( "settings/{$store_id}", 'save_configuration', $conf_args ); } /** * GET logs. * * @param WP_REST_Request $request The request. * * @return WP_REST_Response|WP_Error */ public function get_logs( WP_REST_Request $request ) { $response = null; try { $response = $this->logger->get_content( $request->get_param( self::PARAM_STORE_ID ) ); } catch ( \Throwable $e ) { $response = new WP_Error( 'error', $e->getMessage() ); } return \rest_ensure_response( $response ); } /** * DELETE logs. * * @param WP_REST_Request $request The request. * * @return WP_REST_Response|WP_Error */ public function delete_logs( WP_REST_Request $request ) { $response = null; try { $this->logger->clear( $request->get_param( self::PARAM_STORE_ID ) ); $response = array(); } catch ( \Throwable $e ) { $response = new WP_Error( 'error', $e->getMessage() ); } return \rest_ensure_response( $response ); } /** * GET logs configuration. * * @return WP_REST_Response|WP_Error */ public function get_configuration() { $response = null; try { $response = $this->get_config_response(); } catch ( \Throwable $e ) { $response = new WP_Error( 'error', $e->getMessage() ); } return \rest_ensure_response( $response ); } /** * POST logs configuration. * * @param WP_REST_Request $request The request. * * @return WP_REST_Response|WP_Error */ public function save_configuration( WP_REST_Request $request ) { $response = null; try { $this->logger->enable( $request->get_param( self::PARAM_IS_ENABLED ) ); $this->logger->set_min_log_level( $request->get_param( self::PARAM_LOG_LEVEL ) ); $response = $this->get_config_response(); } catch ( \Throwable $e ) { $response = new WP_Error( 'error', $e->getMessage() ); } return \rest_ensure_response( $response ); } /** * Make the logger configuration response. * * @return array */ private function get_config_response(): array { return array( self::PARAM_IS_ENABLED => $this->logger->is_enabled(), self::PARAM_LOG_LEVEL => $this->logger->get_min_log_level(), ); } }