| 1 |
<?php |
| 2 |
/** |
| 3 |
* Rest API Logs |
| 4 |
* |
| 5 |
* @package GamiPress\Rest_API\Logs |
| 6 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gmail.com> |
| 7 |
* @since 1.6.5 |
| 8 |
*/ |
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Logs item schema |
| 14 |
* |
| 15 |
* @since 1.6.5 |
| 16 |
* |
| 17 |
* @param array $schema |
| 18 |
* |
| 19 |
* @return array |
| 20 |
*/ |
| 21 |
function gamipress_logs_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 |
'type' => array( |
| 31 |
'description' => __( 'Type of log for the object.', 'gamipress' ), |
| 32 |
'type' => 'string', |
| 33 |
'context' => array( 'view', 'edit', 'embed' ), |
| 34 |
), |
| 35 |
'trigger_type' => array( |
| 36 |
'description' => __( 'Trigger of log for the object.', 'gamipress' ), |
| 37 |
'type' => 'string', |
| 38 |
'context' => array( 'view', 'edit', 'embed' ), |
| 39 |
), |
| 40 |
'access' => array( |
| 41 |
'description' => __( 'Access of log for the object.', 'gamipress' ), |
| 42 |
'type' => 'string', |
| 43 |
'context' => array( 'view', 'edit', 'embed' ), |
| 44 |
), |
| 45 |
'user_id' => array( |
| 46 |
'description' => __( 'The ID for the user of the object.', 'gamipress' ), |
| 47 |
'type' => 'integer', |
| 48 |
'context' => array( 'view', 'edit', 'embed' ), |
| 49 |
), |
| 50 |
'date' => array( |
| 51 |
'description' => __( 'The date the object was created, in the site\'s timezone.', 'gamipress' ), |
| 52 |
'type' => 'string', |
| 53 |
'format' => 'date-time', |
| 54 |
'context' => array( 'view', 'edit', 'embed' ), |
| 55 |
), |
| 56 |
) ); |
| 57 |
|
| 58 |
return $schema; |
| 59 |
|
| 60 |
} |
| 61 |
add_filter( 'ct_rest_gamipress_logs_schema', 'gamipress_logs_rest_item_schema' ); |
| 62 |
|
| 63 |
/** |
| 64 |
* Logs collection params |
| 65 |
* |
| 66 |
* @since 1.6.5 |
| 67 |
* |
| 68 |
* @param array $query_params |
| 69 |
* @param CT_Table $ct_table |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
function gamipress_logs_rest_collection_params( $query_params, $ct_table ) { |
| 74 |
|
| 75 |
// Type |
| 76 |
$log_types = gamipress_get_log_types(); |
| 77 |
// Allow empty type |
| 78 |
$log_types = array_merge( array( '' ), array_keys( $log_types ) ); |
| 79 |
|
| 80 |
$query_params['type'] = array( |
| 81 |
'description' => __( 'Limit result set to logs with a specific type.', 'gamipress' ), |
| 82 |
'type' => 'string', |
| 83 |
'default' => '', |
| 84 |
'enum' => $log_types, |
| 85 |
); |
| 86 |
|
| 87 |
// Trigger Type |
| 88 |
$query_params['trigger_type'] = array( |
| 89 |
'description' => __( 'Limit result set to logs with a specific trigger.' ), |
| 90 |
'type' => 'array', |
| 91 |
'items' => array( |
| 92 |
'type' => 'string', |
| 93 |
), |
| 94 |
'default' => array(), |
| 95 |
); |
| 96 |
|
| 97 |
// Access |
| 98 |
$query_params['access'] = array( |
| 99 |
'description' => __( 'Limit result set to logs with a specific access.', 'gamipress' ), |
| 100 |
'type' => 'string', |
| 101 |
'default' => '', |
| 102 |
'enum' => array( |
| 103 |
'', |
| 104 |
'public', |
| 105 |
'private' |
| 106 |
), |
| 107 |
); |
| 108 |
|
| 109 |
// User ID |
| 110 |
$query_params['user_id'] = array( |
| 111 |
'description' => __( 'Limit result set to logs assigned to specific users.', 'gamipress' ), |
| 112 |
'type' => 'array', |
| 113 |
'items' => array( |
| 114 |
'type' => 'integer', |
| 115 |
), |
| 116 |
'default' => array(), |
| 117 |
); |
| 118 |
|
| 119 |
// Exclude ( exclude => log__not_in ) |
| 120 |
$query_params['exclude'] = array( |
| 121 |
'description' => __( 'Ensure result set excludes specific IDs.', 'gamipress' ), |
| 122 |
'type' => 'array', |
| 123 |
'items' => array( |
| 124 |
'type' => 'integer', |
| 125 |
), |
| 126 |
'default' => array(), |
| 127 |
); |
| 128 |
|
| 129 |
// Include ( include => log__in ) |
| 130 |
$query_params['include'] = array( |
| 131 |
'description' => __( 'Limit result set to specific IDs.', 'gamipress' ), |
| 132 |
'type' => 'array', |
| 133 |
'items' => array( |
| 134 |
'type' => 'integer', |
| 135 |
), |
| 136 |
'default' => array(), |
| 137 |
); |
| 138 |
|
| 139 |
// After |
| 140 |
$query_params['after'] = array( |
| 141 |
'description' => __( 'Limit response to logs after a given ISO8601 compliant date.', 'gamipress' ), |
| 142 |
'type' => 'string', |
| 143 |
'format' => 'date-time', |
| 144 |
); |
| 145 |
|
| 146 |
// Before |
| 147 |
$query_params['before'] = array( |
| 148 |
'description' => __( 'Limit response to logs before a given ISO8601 compliant date.', 'gamipress' ), |
| 149 |
'type' => 'string', |
| 150 |
'format' => 'date-time', |
| 151 |
); |
| 152 |
|
| 153 |
|
| 154 |
return $query_params; |
| 155 |
} |
| 156 |
add_filter( 'ct_rest_gamipress_logs_collection_params', 'gamipress_logs_rest_collection_params', 10, 2 ); |
| 157 |
|
| 158 |
/** |
| 159 |
* Set custom parameters mapping |
| 160 |
* |
| 161 |
* @since 1.6.5 |
| 162 |
* |
| 163 |
* @param array $parameter_mappings Array of parameters to map. |
| 164 |
* @param CT_Table $ct_table Table object. |
| 165 |
* @param WP_REST_Request $request The request given. |
| 166 |
* |
| 167 |
* @return array |
| 168 |
*/ |
| 169 |
function gamipress_logs_rest_parameter_mappings( $parameter_mappings, $ct_table, $request ) { |
| 170 |
|
| 171 |
// Fields |
| 172 |
$parameter_mappings['type'] = 'type'; |
| 173 |
$parameter_mappings['trigger_type'] = 'trigger_type'; |
| 174 |
$parameter_mappings['access'] = 'access'; |
| 175 |
$parameter_mappings['user_id'] = 'user_id'; |
| 176 |
|
| 177 |
// Mappings |
| 178 |
$parameter_mappings['exclude'] = 'log__not_in'; |
| 179 |
$parameter_mappings['include'] = 'log__in'; |
| 180 |
|
| 181 |
// Date |
| 182 |
$parameter_mappings['after'] = 'after'; |
| 183 |
$parameter_mappings['before'] = 'before'; |
| 184 |
|
| 185 |
return $parameter_mappings; |
| 186 |
} |
| 187 |
add_filter( 'ct_rest_gamipress_logs_parameter_mappings', 'gamipress_logs_rest_parameter_mappings', 10, 3 ); |
| 188 |
|
| 189 |
/** |
| 190 |
* Fields sanitization |
| 191 |
* |
| 192 |
* @since 1.6.5 |
| 193 |
* |
| 194 |
* @param mixed $value The field value given. |
| 195 |
* @param string $field The field name. |
| 196 |
* @param WP_REST_Request $request The request object. |
| 197 |
* |
| 198 |
* @return mixed|WP_Error |
| 199 |
*/ |
| 200 |
function gamipress_logs_rest_sanitize_field_value( $value, $field, $request ) { |
| 201 |
|
| 202 |
switch( $field ) { |
| 203 |
case 'user_id': |
| 204 |
// Validate user ID |
| 205 |
$value = absint( $value ); |
| 206 |
$user_obj = get_userdata( $value ); |
| 207 |
|
| 208 |
if ( ! $user_obj ) { |
| 209 |
return new WP_Error( 'rest_invalid_field', __( 'Invalid user ID.', 'gamipress' ), array( 'status' => 400 ) ); |
| 210 |
} |
| 211 |
break; |
| 212 |
case 'date': |
| 213 |
// Validate date. |
| 214 |
$mm = substr( $value, 5, 2 ); |
| 215 |
$jj = substr( $value, 8, 2 ); |
| 216 |
$aa = substr( $value, 0, 4 ); |
| 217 |
$valid_date = wp_checkdate( $mm, $jj, $aa, $value ); |
| 218 |
|
| 219 |
if ( ! $valid_date ) { |
| 220 |
return new WP_Error( 'rest_invalid_field', __( 'Invalid date.', 'gamipress' ), array( 'status' => 400 ) ); |
| 221 |
} |
| 222 |
break; |
| 223 |
} |
| 224 |
|
| 225 |
return $value; |
| 226 |
} |
| 227 |
add_filter( 'ct_rest_gamipress_logs_sanitize_field_value', 'gamipress_logs_rest_sanitize_field_value', 10, 3 ); |
| 228 |
|
| 229 |
|