array( 'type' => 'array', 'items' => array( 'type' => 'string' ), 'maxItems' => Key_Metrics_Repository::MAX_SLUGS, 'description' => __( 'The complete tile selection, replacing whatever is there. Get the recognised slugs from site-kit/get-key-metrics. Omit to leave the selection alone and only change hide_widget_area.', 'acrossai-abilities-manager' ), ), 'hide_widget_area' => array( 'type' => 'boolean', 'description' => __( 'True to hide the Key Metrics row on this user\'s dashboard, false to show it. Omit to leave it as it is.', 'acrossai-abilities-manager' ), ), ); } protected function output_properties(): array { return array( 'widget_slugs' => array( 'type' => 'array' ), 'widget_area_hidden' => array( 'type' => 'boolean' ), 'previous_slugs' => array( 'type' => 'array' ), 'changed' => array( 'type' => 'boolean' ), 'unrecognised_slugs' => array( 'type' => 'array' ), 'current_user_id' => array( 'type' => 'integer' ), ); } protected function required_input(): array { return array(); } protected function annotations(): array { return array( 'readonly' => false, 'destructive' => false, 'idempotent' => true ); } /** * @param array $input * @return array|WP_Error */ protected function run( array $input ) { $slugs = null; if ( array_key_exists( 'widget_slugs', $input ) ) { if ( ! is_array( $input['widget_slugs'] ) ) { return new WP_Error( 'invalid_input', __( 'widget_slugs must be an array of tile slugs.', 'acrossai-abilities-manager' ) ); } $slugs = $input['widget_slugs']; } $hidden = array_key_exists( 'hide_widget_area', $input ) ? (bool) $input['hide_widget_area'] : null; $result = Key_Metrics_Repository::update( $slugs, $hidden ); if ( is_wp_error( $result ) ) { return $result; } $parts = array(); $parts[] = $result['changed'] ? sprintf( /* translators: %d: number of tiles now selected */ __( 'This user\'s Site Kit dashboard now shows %d Key Metrics tiles.', 'acrossai-abilities-manager' ), count( $result['widget_slugs'] ) ) : __( 'Nothing changed — the selection already matched.', 'acrossai-abilities-manager' ); if ( $result['widget_area_hidden'] ) { $parts[] = __( 'The Key Metrics row is hidden, so the tiles are saved but not displayed.', 'acrossai-abilities-manager' ); } if ( array() !== $result['unrecognised_slugs'] ) { $parts[] = sprintf( /* translators: %s: comma-separated list of unrecognised slugs */ __( 'Saved but not recognised by this plugin: %s. Either a tile from a newer Site Kit, or a typo — a tile Site Kit does not know renders nothing.', 'acrossai-abilities-manager' ), implode( ', ', $result['unrecognised_slugs'] ) ); } $result['message'] = implode( ' ', $parts ); return $result; } }