| @@ -1,0 +1,254 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Rest API Logs | |
| 4 | + * | |
| 5 | + * @package GamiPress\Rest_API\Logs | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.6.5 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +/** | |
| 13 | + * User Earning item schema | |
| 14 | + * | |
| 15 | + * @since 1.6.5 | |
| 16 | + * | |
| 17 | + * @param array $schema | |
| 18 | + * | |
| 19 | + * @return array | |
| 20 | + */ | |
| 21 | +function gamipress_user_earnings_rest_item_schema( $schema ) { | |
| 22 | + | |
| 23 | + // Properties | |
| 24 | + $schema['properties'] = array_merge( $schema['properties'], array( | |
| 25 | + 'title' => array( | |
| 26 | + 'description' => __( 'The title for the object.', 'gamipress' ), | |
| 27 | + 'type' => 'string', | |
| 28 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 29 | + ), | |
| 30 | + 'user_id' => array( | |
| 31 | + 'description' => __( 'The ID for the user of the object.', 'gamipress' ), | |
| 32 | + 'type' => 'integer', | |
| 33 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 34 | + ), | |
| 35 | + 'post_id' => array( | |
| 36 | + 'description' => __( 'The ID for the post of the object.', 'gamipress' ), | |
| 37 | + 'type' => 'integer', | |
| 38 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 39 | + ), | |
| 40 | + 'post_type' => array( | |
| 41 | + 'description' => __( 'The type for the post of the object.', 'gamipress' ), | |
| 42 | + 'type' => 'string', | |
| 43 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 44 | + ), | |
| 45 | + 'points' => array( | |
| 46 | + 'description' => __( 'The points amount of the object.', 'gamipress' ), | |
| 47 | + 'type' => 'integer', | |
| 48 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 49 | + ), | |
| 50 | + 'points_type' => array( | |
| 51 | + 'description' => __( 'The type for the points amount of the object.', 'gamipress' ), | |
| 52 | + 'type' => 'string', | |
| 53 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 54 | + ), | |
| 55 | + 'date' => array( | |
| 56 | + 'description' => __( 'The date the object was created, in the site\'s timezone.', 'gamipress' ), | |
| 57 | + 'type' => 'string', | |
| 58 | + 'format' => 'date-time', | |
| 59 | + 'context' => array( 'view', 'edit', 'embed' ), | |
| 60 | + ), | |
| 61 | + ) ); | |
| 62 | + | |
| 63 | + return $schema; | |
| 64 | + | |
| 65 | +} | |
| 66 | +add_filter( 'ct_rest_gamipress_user_earnings_schema', 'gamipress_user_earnings_rest_item_schema' ); | |
| 67 | + | |
| 68 | +/** | |
| 69 | + * User Earnings collection params | |
| 70 | + * | |
| 71 | + * @since 1.6.5 | |
| 72 | + * | |
| 73 | + * @param array $query_params | |
| 74 | + * @param CT_Table $ct_table | |
| 75 | + * | |
| 76 | + * @return array | |
| 77 | + */ | |
| 78 | +function gamipress_user_earnings_rest_collection_params( $query_params, $ct_table ) { | |
| 79 | + | |
| 80 | + // User ID | |
| 81 | + $query_params['user_id'] = array( | |
| 82 | + 'description' => __( 'Limit result set to earnings assigned to specific users.', 'gamipress' ), | |
| 83 | + 'type' => 'array', | |
| 84 | + 'items' => array( | |
| 85 | + 'type' => 'integer', | |
| 86 | + ), | |
| 87 | + 'default' => array(), | |
| 88 | + ); | |
| 89 | + | |
| 90 | + // Post ID | |
| 91 | + $query_params['post_id'] = array( | |
| 92 | + 'description' => __( 'Limit result set to earnings assigned to specific posts.', 'gamipress' ), | |
| 93 | + 'type' => 'array', | |
| 94 | + 'items' => array( | |
| 95 | + 'type' => 'integer', | |
| 96 | + ), | |
| 97 | + 'default' => array(), | |
| 98 | + ); | |
| 99 | + | |
| 100 | + // Post Type | |
| 101 | + $query_params['post_type'] = array( | |
| 102 | + 'description' => __( 'Limit result set to earnings assigned to specific post types.', 'gamipress' ), | |
| 103 | + 'type' => 'array', | |
| 104 | + 'items' => array( | |
| 105 | + 'type' => 'string', | |
| 106 | + ), | |
| 107 | + 'default' => array(), | |
| 108 | + ); | |
| 109 | + | |
| 110 | + // Points Type | |
| 111 | + $query_params['points_type'] = array( | |
| 112 | + 'description' => __( 'Limit result set to earnings assigned to specific points types.', 'gamipress' ), | |
| 113 | + 'type' => 'array', | |
| 114 | + 'items' => array( | |
| 115 | + 'type' => 'string', | |
| 116 | + ), | |
| 117 | + 'default' => array(), | |
| 118 | + ); | |
| 119 | + | |
| 120 | + // Force Types | |
| 121 | + $query_params['force_types'] = array( | |
| 122 | + 'description' => __( 'Setting it to true will force to limit results of the same post_type and points_type.' ), | |
| 123 | + 'type' => 'boolean', | |
| 124 | + 'default' => false, | |
| 125 | + ); | |
| 126 | + | |
| 127 | + // Exclude ( exclude => user_earning__not_in ) | |
| 128 | + $query_params['exclude'] = array( | |
| 129 | + 'description' => __( 'Ensure result set excludes specific IDs.', 'gamipress' ), | |
| 130 | + 'type' => 'array', | |
| 131 | + 'items' => array( | |
| 132 | + 'type' => 'integer', | |
| 133 | + ), | |
| 134 | + 'default' => array(), | |
| 135 | + ); | |
| 136 | + | |
| 137 | + // Include ( include => user_earning__in ) | |
| 138 | + $query_params['include'] = array( | |
| 139 | + 'description' => __( 'Limit result set to specific IDs.', 'gamipress' ), | |
| 140 | + 'type' => 'array', | |
| 141 | + 'items' => array( | |
| 142 | + 'type' => 'integer', | |
| 143 | + ), | |
| 144 | + 'default' => array(), | |
| 145 | + ); | |
| 146 | + | |
| 147 | + // After | |
| 148 | + $query_params['after'] = array( | |
| 149 | + 'description' => __( 'Limit response to user earnings after a given ISO8601 compliant date.', 'gamipress' ), | |
| 150 | + 'type' => 'string', | |
| 151 | + 'format' => 'date-time', | |
| 152 | + ); | |
| 153 | + | |
| 154 | + // Before | |
| 155 | + $query_params['before'] = array( | |
| 156 | + 'description' => __( 'Limit response to user earnings before a given ISO8601 compliant date.', 'gamipress' ), | |
| 157 | + 'type' => 'string', | |
| 158 | + 'format' => 'date-time', | |
| 159 | + ); | |
| 160 | + | |
| 161 | + | |
| 162 | + return $query_params; | |
| 163 | +} | |
| 164 | +add_filter( 'ct_rest_gamipress_user_earnings_collection_params', 'gamipress_user_earnings_rest_collection_params', 10, 2 ); | |
| 165 | + | |
| 166 | +/** | |
| 167 | + * Set custom parameters mapping | |
| 168 | + * | |
| 169 | + * @since 1.6.5 | |
| 170 | + * | |
| 171 | + * @param array $parameter_mappings Array of parameters to map. | |
| 172 | + * @param CT_Table $ct_table Table object. | |
| 173 | + * @param WP_REST_Request $request The request given. | |
| 174 | + * | |
| 175 | + * @return array | |
| 176 | + */ | |
| 177 | +function gamipress_user_earnings_rest_parameter_mappings( $parameter_mappings, $ct_table, $request ) { | |
| 178 | + | |
| 179 | + // Fields | |
| 180 | + $parameter_mappings['user_id'] = 'user_id'; | |
| 181 | + $parameter_mappings['post_id'] = 'post_id'; | |
| 182 | + $parameter_mappings['post_type'] = 'post_type'; | |
| 183 | + $parameter_mappings['points_type'] = 'points_type'; | |
| 184 | + | |
| 185 | + $parameter_mappings['force_types'] = 'force_types'; | |
| 186 | + | |
| 187 | + // Mappings | |
| 188 | + $parameter_mappings['exclude'] = 'user_earning__not_in'; | |
| 189 | + $parameter_mappings['include'] = 'user_earning__in'; | |
| 190 | + | |
| 191 | + // Date | |
| 192 | + $parameter_mappings['after'] = 'after'; | |
| 193 | + $parameter_mappings['before'] = 'before'; | |
| 194 | + | |
| 195 | + return $parameter_mappings; | |
| 196 | +} | |
| 197 | +add_filter( 'ct_rest_gamipress_user_earnings_parameter_mappings', 'gamipress_user_earnings_rest_parameter_mappings', 10, 3 ); | |
| 198 | + | |
| 199 | +/** | |
| 200 | + * Fields sanitization | |
| 201 | + * | |
| 202 | + * @since 1.6.5 | |
| 203 | + * | |
| 204 | + * @param mixed $value The field value given. | |
| 205 | + * @param string $field The field name. | |
| 206 | + * @param WP_REST_Request $request The request object. | |
| 207 | + * | |
| 208 | + * @return mixed|WP_Error | |
| 209 | + */ | |
| 210 | +function gamipress_user_earnings_rest_sanitize_field_value( $value, $field, $request ) { | |
| 211 | + | |
| 212 | + switch( $field ) { | |
| 213 | + case 'user_id': | |
| 214 | + // Validate user ID | |
| 215 | + $value = absint( $value ); | |
| 216 | + $user_obj = get_userdata( $value ); | |
| 217 | + | |
| 218 | + if ( ! $user_obj ) { | |
| 219 | + return new WP_Error( 'rest_invalid_field', __( 'Invalid user ID.', 'gamipress' ), array( 'status' => 400 ) ); | |
| 220 | + } | |
| 221 | + break; | |
| 222 | + case 'post_id': | |
| 223 | + // Validate post ID | |
| 224 | + $value = absint( $value ); | |
| 225 | + $post_obj = get_post( $value ); | |
| 226 | + | |
| 227 | + if ( ! $post_obj ) { | |
| 228 | + return new WP_Error( 'rest_invalid_field', __( 'Invalid post ID.', 'gamipress' ), array( 'status' => 400 ) ); | |
| 229 | + } | |
| 230 | + break; | |
| 231 | + case 'points_type': | |
| 232 | + // Validate points type | |
| 233 | + if( ! empty( $value ) ) { | |
| 234 | + if ( ! in_array( $value, gamipress_get_points_types_slugs() ) ) { | |
| 235 | + return new WP_Error( 'rest_invalid_field', __( 'Invalid points type.', 'gamipress' ), array( 'status' => 400 ) ); | |
| 236 | + } | |
| 237 | + } | |
| 238 | + break; | |
| 239 | + case 'date': | |
| 240 | + // Validate date. | |
| 241 | + $mm = substr( $value, 5, 2 ); | |
| 242 | + $jj = substr( $value, 8, 2 ); | |
| 243 | + $aa = substr( $value, 0, 4 ); | |
| 244 | + $valid_date = wp_checkdate( $mm, $jj, $aa, $value ); | |
| 245 | + | |
| 246 | + if ( ! $valid_date ) { | |
| 247 | + return new WP_Error( 'rest_invalid_field', __( 'Invalid date.', 'gamipress' ), array( 'status' => 400 ) ); | |
| 248 | + } | |
| 249 | + break; | |
| 250 | + } | |
| 251 | + | |
| 252 | + return $value; | |
| 253 | +} | |
| 254 | +add_filter( 'ct_rest_gamipress_user_earnings_sanitize_field_value', 'gamipress_user_earnings_rest_sanitize_field_value', 10, 3 ); | |