Tribe__Events__REST__V1__Endpoints__Archive_Organizer::get( WP_REST_Request $request )
Handles GET requests on the endpoint.
Contents
Parameters
- $request
-
(WP_REST_Request) (Required)
Return
(WP_Error|WP_REST_Response) An array containing the data on success or a WP_Error instance on failure.
Source
File: src/Tribe/REST/V1/Endpoints/Archive_Organizer.php
public function get( WP_REST_Request $request ) {
$args = array(
'posts_per_page' => $request['per_page'],
'paged' => $request['page'],
's' => $request['search'],
'event' => $request['event'],
'has_events' => $request['has_events'],
);
if ( null === $request['status'] ) {
$cap = get_post_type_object( Tribe__Events__Main::ORGANIZER_POST_TYPE )->cap->edit_posts;
$args['post_status'] = current_user_can( $cap ) ? 'any' : 'publish';
} else {
$args['post_status'] = $this->filter_post_status_list( $request['status'] );
}
/**
* Filters whether only organizers with upcoming events should be shown (`true`) or not (`false`) when
* the request parameter `only_with_upcoming` is not explicitly set.
*
* @param bool $default_only_with_upcoming
*
* @since 4.6
*/
$default_only_with_upcoming = apply_filters( 'tribe_rest_organizer_default_only_with_upcoming', false );
$only_with_upcoming = isset( $request['only_with_upcoming'] ) ? tribe_is_truthy( $request['only_with_upcoming'] ) : $default_only_with_upcoming;
unset( $args['only_with_upcoming'] );
if ( ! empty( $args['s'] ) ) {
if ( ! is_string( $args['s'] ) ) {
$message = $this->messages->get_message( 'archive-bad-search-string' );
return new WP_Error( 'archive-bad-search-string', $message, array( 'status' => 400 ) );
}
/** @var Tribe__Events__Organizer $linked_post */
$linked_post = tribe( 'tec.linked-posts.organizer' );
$matches = $linked_post->find_like( $args['s'] );
unset( $args['s'] );
if ( ! empty( $matches ) ) {
$args['post__in'] = $matches;
} else {
$organizers = array();
}
}
/** @var Tribe__Cache $cache */
$cache = tribe( 'cache' );
$cache_key = 'rest_get_organizers_data_' . get_current_user_id() . '_' . wp_json_encode( $args );
$data = $cache->get( $cache_key, 'save_post' );
if ( ! is_array( $data ) ) {
$posts_per_page = Tribe__Utils__Array::get( $args, 'posts_per_page', $this->get_default_posts_per_page() );
$organizers = isset( $organizers ) ? $organizers : tribe_get_organizers( $only_with_upcoming, $posts_per_page, true, $args );
unset( $args['fields'] );
$ids = wp_list_pluck( $organizers, 'ID' );
$data = array( 'organizers' => array() );
foreach ( $ids as $organizer_id ) {
$organizer = $this->repository->get_organizer_data( $organizer_id );
if ( $organizer && ! is_wp_error( $organizer ) ) {
$data['organizers'][] = $organizer;
}
}
$data['rest_url'] = $this->get_current_rest_url( $args );
$page = Tribe__Utils__Array::get( $args, 'paged', 1 );
if ( empty( $organizers ) && (int) $page > 1 ) {
$message = $this->messages->get_message( 'organizer-archive-page-not-found' );
return new WP_Error( 'organizer-archive-page-not-found', $message, array( 'status' => 404 ) );
}
if ( $this->has_next( $args, $page, $only_with_upcoming ) ) {
$data['next_rest_url'] = $this->get_next_rest_url( $data['rest_url'], $page );
}
if ( $this->has_previous( $page, $args, $only_with_upcoming ) ) {
$data['previous_rest_url'] = $this->get_previous_rest_url( $data['rest_url'], $page );;
}
$data['total'] = $total = $this->get_total( $args, $only_with_upcoming );
$data['total_pages'] = $this->get_total_pages( $total, $posts_per_page );
$cache->set( $cache_key, $data, Tribe__Cache::NON_PERSISTENT, 'save_post' );
}
$response = new WP_REST_Response( $data );
if ( isset( $data['total'] ) && isset( $data['total_pages'] ) ) {
$response->header( 'X-TEC-Total', $data['total'], true );
$response->header( 'X-TEC-TotalPages', $data['total_pages'], true );
}
return $response;
}
Changelog
| Version | Description |
|---|---|
| 4.6 | Introduced. |