LLMS_REST_Controller::prepare_collection_query_search_args( array $prepared, WP_REST_Request $request )

Format search query arguments to retrieve a collection of objects.


Parameters Parameters

$prepared

(array) (Required) Array of collection arguments.

$request

(WP_REST_Request) (Required) Request object.


Top ↑

Return Return

(array|WP_Error)


Top ↑

Source Source

File: libraries/lifterlms-rest/includes/abstracts/class-llms-rest-controller.php

	protected function prepare_collection_query_search_args( $prepared, $request ) {

		// Search?
		if ( ! empty( $prepared['search'] ) ) {

			if ( ! empty( $this->search_columns_mapping ) ) {

				if ( empty( $prepared['search_columns'] ) ) {
					return llms_rest_bad_request_error( __( 'You must provide a valid set of columns to search into.', 'lifterlms' ) );
				}

				// Filter search columns by context.
				$search_columns = array_keys( $this->filter_response_by_context( array_flip( $prepared['search_columns'] ), $request['context'] ) );

				// Check if one of more unallowed search columns have been provided as request query params (not merged with defaults).
				if ( ! empty( $request->get_query_params()['search_columns'] ) ) {

					$forbidden_columns = array_diff( $prepared['search_columns'], $search_columns );

					if ( ! empty( $forbidden_columns ) ) {
						return llms_rest_authorization_required_error(
							sprintf(
								// Translators: %1$s comma separated list of search columns.
								__( 'You are not allowed to search into the provided column(s): %1$s', 'lifterlms' ),
								implode( ',', $forbidden_columns )
							)
						);
					}
				}

				$prepared['search_columns'] = array();

				// Map our search columns into query compatible ones.
				foreach ( $search_columns as $search_column ) {
					if ( isset( $this->search_columns_mapping[ $search_column ] ) ) {
						$prepared['search_columns'][] = $this->search_columns_mapping[ $search_column ];
					}
				}

				if ( empty( $prepared['search_columns'] ) ) {
					return llms_rest_bad_request_error( __( 'You must provide a valid set of columns to search into.', 'lifterlms' ) );
				}
			}

			$prepared['search'] = '*' . $prepared['search'] . '*';

		} else {

			// Ensure a search string is set in case the orderby is set to 'relevance'.
			if ( ! empty( $request['orderby'] ) && 'relevance' === $request['orderby'] ) {
				return llms_rest_bad_request_error(
					__( 'You need to define a search term to order by relevance.', 'lifterlms' )
				);
			}
		}

		return $prepared;
	}


Top ↑

Changelog Changelog

Changelog
Version Description
1.0.0-beta.21 Return an error if requesting a list ordered by 'relevance' without providing a search string.
1.0.0-beta.12 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.