array( 'type' => 'boolean', 'default' => false, 'description' => __( 'Return only the types that currently have rows to clean.', 'acrossai-abilities-manager' ), ), ); } protected function required_input(): array { return array(); } protected function output_properties(): array { return array( 'recommendations' => array( 'type' => 'array' ), 'total' => array( 'type' => 'integer' ), 'actionable' => array( 'type' => 'integer' ), ); } protected function annotations(): array { return array( 'readonly' => true, 'destructive' => false, 'idempotent' => true, ); } /** * @param array $input Input. * @return array */ protected function run( array $input ) { $only_actionable = ! empty( $input['only_actionable'] ); $advice = Database_Repository::recommendations(); // A list of rows, not a map keyed by type: an associative array encodes as a JSON object and // would fail this ability's own `array` output schema // (BUG-ARRAY-TYPED-OUTPUT-IS-A-JSON-OBJECT). $rows = array(); $total = 0; $actionable = 0; foreach ( Database_Repository::counts() as $row ) { $type = (string) $row['type']; $count = (int) $row['count']; $total += $count; if ( $count > 0 ) { ++$actionable; } if ( $only_actionable && 0 === $count ) { continue; } $rows[] = array( 'type' => $type, 'count' => $count, 'describes' => (string) $row['describes'], 'ability' => isset( $advice[ $type ]['ability'] ) ? (string) $advice[ $type ]['ability'] : '', 'safe' => isset( $advice[ $type ]['safe'] ) && (bool) $advice[ $type ]['safe'], 'how' => isset( $advice[ $type ]['how'] ) ? (string) $advice[ $type ]['how'] : '', ); } return array( 'recommendations' => $rows, 'total' => $total, 'actionable' => $actionable, 'message' => 0 === $total ? __( 'Nothing to clean up.', 'acrossai-abilities-manager' ) : sprintf( /* translators: 1: total rows, 2: number of cleanup types with rows */ __( '%1$d row(s) across %2$d cleanup type(s). Each row names the ability that can remove them.', 'acrossai-abilities-manager' ), $total, $actionable ), ); } }