query_vars['adscaleUpdate'] = sanitize_key( wp_unslash( $_GET['adscaleUpdate'] ) ); } // make sure endpoint request if ( ! isset( $wp->query_vars['adscaleUpdate'] ) ) { return; } try { ob_start(); Helper::nocache_headers(); // Check Request (Guards) self::checkRequest(); ob_end_clean(); // Handle Request self::handleRequest(); // Default error Helper::send_response_formatted_error( self::makeErrorBody() ); } catch ( ServiceApiException $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getApiErrorCode(), $ex->getMessage(), $ex->getApiErrorDetails() ) ); } catch ( \Exception $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getCode(), $ex->getMessage() ) ); } } public static function checkRequest() { parent::checkRequest(); self::initParams(); // Missing Action guard if ( ! self::checkIfRequestParamExists( 'action' ) ) { throw new ServiceApiException( 'Missing argument', 'M001', 'action' ); } $action = self::$requestParams['action']; // Action is not a string guard if ( ! is_string( $action ) ) { throw new ServiceApiException( 'Invalid argument value', 'I001', 'action' ); } // Unknown Action guard if ( ! in_array( $action, self::$allowedActions, true ) ) { throw new ServiceApiException( 'Invalid argument value', 'I001', 'action' ); } if ( 'modify' === $action ) { // Missing Property guard if ( ! self::checkIfRequestParamExists( 'property' ) ) { throw new ServiceApiException( 'Missing argument', 'M001', 'property' ); } $property = self::$requestParams['property']; // Empty Property guard if ( ! is_string( $property ) || ! $property ) { throw new ServiceApiException( 'Invalid argument value', 'I001', 'property' ); } // Missing Value guard if ( ! self::checkIfRequestParamExists( 'value' ) ) { throw new ServiceApiException( 'Missing argument', 'M001', 'value' ); } } if ( 'delete_changes' === $action ) { // Missing max_id guard if ( ! self::checkIfRequestParamExists( 'max_id' ) ) { throw new ServiceApiException( 'Missing argument', 'M001', 'max_id' ); } $max_id = self::$requestParams['max_id']; // invalid max_id guard if ( ! Helper::check_id_is_valid( $max_id ) ) { throw new ServiceApiException( 'Invalid argument value', 'I001', 'max_id' ); } } } public static function initParams() { self::$requestParams['action'] = Helper::get_post_body_data_value( 'action','sanitize_text_field' ); self::$requestParams['property'] = Helper::get_post_body_data_value( 'property','sanitize_text_field' ); self::$requestParams['value'] = Helper::get_post_body_data_value( 'value','sanitize_text_field' ); self::$requestParams['max_id'] = Helper::get_post_body_data_value( 'max_id' ,'absint'); } /** * Request handler * * @throws ServiceApiException */ public static function handleRequest() { UpdateHandler::actionProcessor( self::$requestParams ); } }