*/ protected function input_properties(): array { return array( 'type' => array( 'type' => 'string', 'enum' => Bundle_Repository::TYPES, 'description' => __( 'Only plugin, theme or core bundles.', 'acrossai-abilities-manager' ), ), 'search' => array( 'type' => 'string', 'description' => __( 'Case-insensitive match against name, handle or slug.', 'acrossai-abilities-manager' ), ), 'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => 300, 'default' => 100, 'description' => __( 'Maximum rows to return. This site has around 150 bundles.', 'acrossai-abilities-manager' ), ), ); } /** * @since 0.0.34 * @return array */ protected function required_input(): array { return array(); } /** * @since 0.0.34 * @return array */ protected function output_properties(): array { return array( 'bundles' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'additionalProperties' => true ), ), 'count' => array( 'type' => 'integer' ), 'total' => array( 'type' => 'integer', 'description' => __( 'How many bundles exist before per_page was applied.', 'acrossai-abilities-manager' ), ), ); } /** * @since 0.0.34 * @return array */ protected function annotations(): array { return array( 'readonly' => true, 'destructive' => false, 'idempotent' => true ); } /** * @since 0.0.34 * @param array $input Input. * @return array|WP_Error */ protected function run( array $input ) { $bundles = Bundle_Repository::all( isset( $input['type'] ) ? (string) $input['type'] : '' ); if ( is_wp_error( $bundles ) ) { return $bundles; } $needle = isset( $input['search'] ) ? strtolower( trim( (string) $input['search'] ) ) : ''; $limit = isset( $input['per_page'] ) ? (int) $input['per_page'] : 100; $rows = array(); $total = 0; foreach ( $bundles as $bundle ) { $row = Bundle_Repository::shape( $bundle ); if ( '' !== $needle ) { $hay = strtolower( $row['name'] . ' ' . $row['handle'] . ' ' . $row['slug'] ); if ( false === strpos( $hay, $needle ) ) { continue; } } ++$total; if ( count( $rows ) < $limit ) { $rows[] = $row; } } return array( 'bundles' => $rows, 'count' => count( $rows ), 'total' => $total, ); } }