| | 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 | register_rest_field( |
| | 16 | 'tix_attendee', |
| | 17 | 'first_name', |
| | 18 | array( |
| | 19 | 'get_callback' => 'camptix_attendees_endpoint_get_field', |
| | 20 | 'update_callback' => null, |
| | 21 | 'schema' => array( |
| | 22 | 'description' => __( 'Attendee first name.', 'camptix' ), |
| | 23 | 'type' => 'string', |
| | 24 | 'readonly' => true, |
| | 25 | 'context' => array( 'view' ) |
| | 26 | ), |
| | 27 | ) |
| | 28 | ); |
| | 29 | |
| | 30 | register_rest_field( |
| | 31 | 'tix_attendee', |
| | 32 | 'last_name', |
| | 33 | array( |
| | 34 | 'get_callback' => 'camptix_attendees_endpoint_get_meta_field', |
| | 35 | 'update_callback' => null, |
| | 36 | 'schema' => array( |
| | 37 | 'description' => __( 'Attendee last name.', 'camptix' ), |
| | 38 | 'type' => 'string', |
| | 39 | 'readonly' => true, |
| | 40 | 'context' => array( 'view' ) |
| | 41 | ), |
| | 42 | ) |
| | 43 | ); |
| | 44 | |
| | 45 | $avatar_properties = array(); |
| | 46 | |
| | 47 | $avatar_sizes = rest_get_avatar_sizes(); |
| | 48 | |
| | 49 | foreach ( $avatar_sizes as $size ) { |
| | 50 | $avatar_properties[ $size ] = array( |
| | 51 | /* translators: %d: avatar image size in pixels */ |
| | 52 | 'description' => sprintf( __( 'Avatar URL with image size of %d pixels.', 'camptix' ), $size ), |
| | 53 | 'type' => 'string', |
| | 54 | 'format' => 'uri', |
| | 55 | 'context' => array( 'view' ), |
| | 56 | ); |
| | 57 | } |
| | 58 | |
| | 59 | register_rest_field( |
| | 60 | 'tix_attendee', |
| | 61 | 'avatar_urls', |
| | 62 | array( |
| | 63 | 'get_callback' => 'camptix_attendees_endpoint_get_avatars', |
| | 64 | 'update_callback' => null, |
| | 65 | 'schema' => array( |
| | 66 | 'description' => __( 'Avatar URLs for the user.' ), |
| | 67 | 'type' => 'object', |
| | 68 | 'context' => array( 'view' ), |
| | 69 | 'readonly' => true, |
| | 70 | 'properties' => $avatar_properties, |
| | 71 | ) |
| | 72 | ) |
| | 73 | ); |
| | 74 | } |
| | 75 | |
| | 76 | |
| | 77 | /** |
| | 78 | * Modify collection parameters for the attendees endpoint controller. |
| | 79 | * |
| | 80 | * @param array $params JSON Schema-formatted collection parameters. |
| | 81 | * |
| | 82 | * @return array |
| | 83 | */ |
| | 84 | function camptix_attendees_endpoint_collection_params( $params ) { |
| | 85 | // We are going to force per_page param |
| | 86 | if ( isset( $params['per_page'] ) ) { |
| | 87 | unset( $params['per_page'] ); |
| | 88 | } |
| | 89 | |
| | 90 | $params['tickets'] = array( |
| | 91 | 'description' => __( 'Limit result set to a set of tickets ID.', 'camptix' ), |
| | 92 | 'type' => 'array', |
| | 93 | 'items' => array( |
| | 94 | 'type' => 'integer', |
| | 95 | ), |
| | 96 | 'default' => array(), |
| | 97 | ); |
| | 98 | |
| | 99 | return $params; |
| | 100 | } |
| | 101 | |
| | 102 | /** |
| | 103 | * Modify the query arguments for an attendee request. |
| | 104 | * |
| | 105 | * @param array $args Key value array of query var to query value. |
| | 106 | * @param WP_REST_Request $request The request used. |
| | 107 | * |
| | 108 | * @return array |
| | 109 | */ |
| | 110 | function camptix_attendees_endpoint_query_args( $args, $request ) { |
| | 111 | // Same value that the shortcode |
| | 112 | $args['posts_per_page'] = 10000; |
| | 113 | |
| | 114 | // Do not show attendees markedd as private |
| | 115 | if ( empty( $args['meta_query'] ) ) { |
| | 116 | $args['meta_query'] = array(); |
| | 117 | } |
| | 118 | |
| | 119 | $args['meta_query'][] = array( |
| | 120 | 'relation' => 'OR', |
| | 121 | array( |
| | 122 | 'key' => 'tix_privacy', |
| | 123 | 'value' => 'private', |
| | 124 | 'compare' => '!=' |
| | 125 | ), |
| | 126 | array( |
| | 127 | 'key' => 'tix_privacy', |
| | 128 | 'compare' => 'NOT EXISTS' |
| | 129 | ) |
| | 130 | ); |
| | 131 | |
| | 132 | if ( is_array( $request['tickets'] ) && count( $request['tickets'] ) > 0 ) { |
| | 133 | $args['meta_query'][] = array( |
| | 134 | array( |
| | 135 | 'key' => 'tix_ticket_id', |
| | 136 | 'compare' => 'IN', |
| | 137 | 'value' => $request['tickets'], |
| | 138 | ) |
| | 139 | ); |
| | 140 | } |
| | 141 | |
| | 142 | return $args; |
| | 143 | } |
| | 144 | |
| | 145 | /** |
| | 146 | * Get a simple meta value for an attendee |
| | 147 | * |
| | 148 | * @param array $object Post object |
| | 149 | * @param string $field Name of the field |
| | 150 | * |
| | 151 | * @return mixed |
| | 152 | */ |
| | 153 | function camptix_attendees_endpoint_get_meta_field( $object, $field ) { |
| | 154 | $meta_key = 'tix_' . $field; |
| | 155 | return get_post_meta( $object['id'], $meta_key, true ); |
| | 156 | } |
| | 157 | |
| | 158 | /** |
| | 159 | * Get the avatar urls field for an attendee |
| | 160 | * |
| | 161 | * @param array $object Post object |
| | 162 | * |
| | 163 | * @return array |
| | 164 | */ |
| | 165 | function camptix_attendees_endpoint_get_avatars( $object ) { |
| | 166 | $email = get_post_meta( $object['id'], 'tix_email', true ); |
| | 167 | return rest_get_avatar_urls( $email ); |
| | 168 | } |
| | 169 | No newline at end of file |