array( 'type' => 'string', 'description' => __( 'Module slug, e.g. "analytics-4" or "search-console". List them with site-kit/list-modules.', 'acrossai-abilities-manager' ), ), 'datapoint' => array( 'type' => 'string', 'description' => __( 'Datapoint name without the GET: prefix, e.g. "properties" or "webdatastreams". List them with site-kit/list-module-datapoints.', 'acrossai-abilities-manager' ), ), 'params' => array( 'type' => 'object', 'default' => array(), 'additionalProperties' => true, 'description' => __( 'Parameters for the datapoint, passed through to Site Kit unchanged. Which ones apply depends entirely on the datapoint.', 'acrossai-abilities-manager' ), ), ); } protected function output_properties(): array { return array( 'module' => array( 'type' => 'string' ), 'datapoint' => array( 'type' => 'string' ), 'data' => array( 'type' => array( 'object', 'array', 'string', 'number', 'boolean', 'null' ) ), ); } protected function required_input(): array { return array( 'module', 'datapoint' ); } /** * Not idempotent as a class: the datapoint reached is caller-chosen, and while * every GET datapoint is a read, several are reads of live figures that move. * Claiming idempotent here would be a promise this ability cannot keep. */ protected function annotations(): array { return array( 'readonly' => true, 'destructive' => false, 'idempotent' => false ); } /** * @param array $input * @return array|WP_Error */ protected function run( array $input ) { $slug = isset( $input['module'] ) ? sanitize_key( (string) $input['module'] ) : ''; if ( '' === $slug ) { return new WP_Error( 'invalid_input', __( 'module is required. List the available slugs with site-kit/list-modules.', 'acrossai-abilities-manager' ) ); } $datapoint = isset( $input['datapoint'] ) ? (string) $input['datapoint'] : ''; // Datapoint names are camelCase or hyphenated ('searchanalytics', // 'account-summaries', 'webdatastreams-batch'), so sanitize_key would mangle // them. Constrain the shape instead of normalising it. if ( '' === $datapoint || ! preg_match( '/^[A-Za-z0-9-]{1,64}$/', $datapoint ) ) { return new WP_Error( 'invalid_input', __( 'datapoint is required and must be a plain datapoint name such as "properties" or "account-summaries". Do not include the GET: prefix. List them with site-kit/list-module-datapoints.', 'acrossai-abilities-manager' ) ); } $params = isset( $input['params'] ) && is_array( $input['params'] ) ? $input['params'] : array(); $data = Report_Repository::module_data( $slug, $datapoint, $params ); if ( is_wp_error( $data ) ) { return $data; } return array( 'module' => $slug, 'datapoint' => $datapoint, 'data' => $data, 'message' => sprintf( /* translators: 1: datapoint name, 2: module slug */ __( 'Read "%1$s" from the Site Kit "%2$s" module.', 'acrossai-abilities-manager' ), $datapoint, $slug ), ); } }