diff --git a/includes/Core/Modules/Module.php b/includes/Core/Modules/Module.php index 18fbcd89900..343be458fa3 100644 --- a/includes/Core/Modules/Module.php +++ b/includes/Core/Modules/Module.php @@ -338,6 +338,9 @@ final protected function execute_data_request( Data_Request $data ) { $datapoint = $this->get_datapoint_definition( "{$data->method}:{$data->datapoint}" ); $oauth_client = $this->get_oauth_client_for_datapoint( $datapoint ); + // Always reset this property first to ensure it is only set true for the current request. + $this->is_using_shared_credentials = false; + $this->validate_datapoint_scopes( $datapoint, $oauth_client ); $this->validate_base_scopes( $oauth_client ); diff --git a/includes/Core/Validation/Exception/Invalid_Report_Dimensions_Exception.php b/includes/Core/Validation/Exception/Invalid_Report_Dimensions_Exception.php new file mode 100644 index 00000000000..6ab3a690ad7 --- /dev/null +++ b/includes/Core/Validation/Exception/Invalid_Report_Dimensions_Exception.php @@ -0,0 +1,24 @@ +validate_report_dimensions( - array_map( - function ( $dimension_def ) { - return $dimension_def->getName(); - }, - $dimensions - ) - ); - - if ( isset( $invalid_dimensions_error_message ) ) { + try { + $this->validate_report_dimensions( $dimensions ); + } catch ( Invalid_Report_Dimensions_Exception $exception ) { return new WP_Error( - 'invalid_dimensions', - $invalid_dimensions_error_message, - array( 'status' => 400 ) + 'invalid_analytics_report_dimensions', + $exception->getMessage() ); } @@ -658,20 +652,12 @@ function ( $metric_def ) { ); if ( ! empty( $metrics ) ) { - $invalid_metrics_error_message = $this->validate_report_metrics( - array_map( - function ( $metric_def ) { - return $metric_def->getExpression(); - }, - $metrics - ) - ); - - if ( isset( $invalid_metrics_error_message ) ) { + try { + $this->validate_report_metrics( $metrics ); + } catch ( Invalid_Report_Metrics_Exception $exception ) { return new WP_Error( - 'invalid_metrics', - $invalid_metrics_error_message, - array( 'status' => 400 ) + 'invalid_analytics_report_metrics', + $exception->getMessage() ); } @@ -1370,9 +1356,8 @@ public function check_service_entity_access() { * * @since n.e.x.t * - * @param array $metrics The metrics to validate. - * - * @return null|WP_Error NULL if metrics are valid, otherwise WP_Error. + * @param Google_Service_AnalyticsReporting_Metric[] $metrics The metrics to validate. + * @throws Invalid_Report_Metrics_Exception Thrown if the metrics are invalid. */ protected function validate_report_metrics( $metrics ) { if ( false === $this->is_using_shared_credentials ) { @@ -1395,10 +1380,18 @@ protected function validate_report_metrics( $metrics ) { ) ); - $invalid_metrics = array_diff( $metrics, $valid_metrics ); + $invalid_metrics = array_diff( + array_map( + function ( $metric ) { + return $metric->getExpression(); + }, + $metrics + ), + $valid_metrics + ); if ( count( $invalid_metrics ) > 0 ) { - return sprintf( + $message = sprintf( /* translators: %s is replaced with a comma separated list of the invalid metrics. */ _n( 'Unsupported metric requested: %s', @@ -1406,11 +1399,15 @@ protected function validate_report_metrics( $metrics ) { count( $invalid_metrics ), 'google-site-kit' ), - implode( ', ', $invalid_metrics ) + join( + /* translators: used between list items, there is a space after the comma. */ + __( ', ', 'google-site-kit' ), + $invalid_metrics + ) ); - } - return null; + throw new Invalid_Report_Metrics_Exception( $message ); + } } /** @@ -1418,9 +1415,8 @@ protected function validate_report_metrics( $metrics ) { * * @since n.e.x.t * - * @param array $dimensions The dimensions to validate. - * - * @return null|WP_Error NULL if dimensions are valid, otherwise WP_Error. + * @param Google_Service_AnalyticsReporting_Dimension[] $dimensions The dimensions to validate. + * @throws Invalid_Report_Dimensions_Exception Thrown if the dimensions are invalid. */ protected function validate_report_dimensions( $dimensions ) { if ( false === $this->is_using_shared_credentials ) { @@ -1440,10 +1436,18 @@ protected function validate_report_dimensions( $dimensions ) { ) ); - $invalid_dimensions = array_diff( $dimensions, $valid_dimensions ); + $invalid_dimensions = array_diff( + array_map( + function ( $dimension ) { + return $dimension->getName(); + }, + $dimensions + ), + $valid_dimensions + ); if ( count( $invalid_dimensions ) > 0 ) { - return sprintf( + $message = sprintf( /* translators: %s is replaced with a comma separated list of the invalid dimensions. */ _n( 'Unsupported dimension requested: %s', @@ -1451,11 +1455,15 @@ protected function validate_report_dimensions( $dimensions ) { count( $invalid_dimensions ), 'google-site-kit' ), - implode( ', ', $invalid_dimensions ) + join( + /* translators: used between list items, there is a space after the comma. */ + __( ', ', 'google-site-kit' ), + $invalid_dimensions + ) ); - } - return null; + throw new Invalid_Report_Dimensions_Exception( $message ); + } } }