| 1 | <?php |
| 2 | |
| 3 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | exit; |
| 5 | } |
| 6 | |
| 7 | add_action( 'rest_api_init', 'camptix_register_attendees_fields' ); |
| 8 | add_filter( 'rest_tix_attendee_collection_params', 'camptix_attendees_endpoint_collection_params' ); |
| 9 | add_filter( 'rest_tix_attendee_query', 'camptix_attendees_endpoint_query_args', 10, 2 ); |
| 10 | |
| 11 | /** |
| 12 | * Register extra REST API fields for attendees CPT |
| 13 | */ |
| 14 | function camptix_register_attendees_fields() { |
| 15 | $public_attendee_fields = array( |
| 16 | 'tix_first_name' => array( |
| 17 | 'type' => 'string', |
| 18 | 'single' => true, |
| 19 | ), |
| 20 | 'tix_last_name' => array( |
| 21 | 'type' => 'string', |
| 22 | 'single' => true, |
| 23 | ) |
| 24 | ); |
| 25 | |
| 26 | wcorg_register_meta_only_on_endpoint( 'post', $public_attendee_fields, '/wp-json/wp/v2/tix_attendee/' ); |
| 27 | |
| 28 | $avatar_properties = array(); |
| 29 | |
| 30 | $avatar_sizes = rest_get_avatar_sizes(); |
| 31 | |
| 32 | foreach ( $avatar_sizes as $size ) { |
| 33 | $avatar_properties[ $size ] = array( |
| 34 | /* translators: %d: avatar image size in pixels */ |
| 35 | 'description' => sprintf( __( 'Avatar URL with image size of %d pixels.', 'camptix' ), $size ), |
| 36 | 'type' => 'string', |
| 37 | 'format' => 'uri', |
| 38 | 'context' => array( 'view' ), |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | register_rest_field( |
| 43 | 'tix_attendee', |
| 44 | 'avatar_urls', |
| 45 | array( |
| 46 | 'get_callback' => 'camptix_attendees_endpoint_get_avatars', |
| 47 | 'update_callback' => null, |
| 48 | 'schema' => array( |
| 49 | 'description' => __( 'Avatar URLs for the user.' ), |
| 50 | 'type' => 'object', |
| 51 | 'context' => array( 'view' ), |
| 52 | 'readonly' => true, |
| 53 | 'properties' => $avatar_properties, |
| 54 | ) |
| 55 | ) |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | add_action( 'rest_api_init', 'camptix_register_attendees_fields' ); |
| 60 | |
| 61 | |
| 62 | /** |
| 63 | * Modify collection parameters for the attendees endpoint controller. |
| 64 | * |
| 65 | * @param array $params JSON Schema-formatted collection parameters. |
| 66 | * |
| 67 | * @return array |
| 68 | */ |
| 69 | function camptix_attendees_endpoint_collection_params( $params ) { |
| 70 | // We are going to force per_page param |
| 71 | if ( isset( $params['per_page'] ) ) { |
| 72 | unset( $params['per_page'] ); |
| 73 | } |
| 74 | |
| 75 | $params['tickets'] = array( |
| 76 | 'description' => __( 'Limit result set to a set of tickets ID.', 'camptix' ), |
| 77 | 'type' => 'array', |
| 78 | 'items' => array( |
| 79 | 'type' => 'integer', |
| 80 | ), |
| 81 | 'default' => array(), |
| 82 | ); |
| 83 | |
| 84 | return $params; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Modify the query arguments for an attendee request. |
| 89 | * |
| 90 | * @param array $args Key value array of query var to query value. |
| 91 | * @param WP_REST_Request $request The request used. |
| 92 | * |
| 93 | * @return array |
| 94 | */ |
| 95 | function camptix_attendees_endpoint_query_args( $args, $request ) { |
| 96 | // Same value that the shortcode |
| 97 | $args['posts_per_page'] = 10000; |
| 98 | |
| 99 | // Do not show attendees markedd as private |
| 100 | if ( empty( $args['meta_query'] ) ) { |
| 101 | $args['meta_query'] = array(); |
| 102 | } |
| 103 | |
| 104 | $args['meta_query'][] = array( |
| 105 | 'relation' => 'OR', |
| 106 | array( |
| 107 | 'key' => 'tix_privacy', |
| 108 | 'value' => 'private', |
| 109 | 'compare' => '!=' |
| 110 | ), |
| 111 | array( |
| 112 | 'key' => 'tix_privacy', |
| 113 | 'compare' => 'NOT EXISTS' |
| 114 | ) |
| 115 | ); |
| 116 | |
| 117 | if ( is_array( $request['tickets'] ) && count( $request['tickets'] ) > 0 ) { |
| 118 | $args['meta_query'][] = array( |
| 119 | array( |
| 120 | 'key' => 'tix_ticket_id', |
| 121 | 'compare' => 'IN', |
| 122 | 'value' => $request['tickets'], |
| 123 | ) |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | return $args; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Get the avatar urls field for an attendee |
| 132 | * |
| 133 | * @param array $object Post object |
| 134 | * |
| 135 | * @return array |
| 136 | */ |
| 137 | function camptix_attendees_endpoint_get_avatars( $object ) { |
| 138 | $email = get_post_meta( $object['id'], 'tix_email', true ); |
| 139 | return rest_get_avatar_urls( $email ); |
| 140 | } |
| 141 | No newline at end of file |