| @@ -1,0 +1,356 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Custom Tables | |
| 4 | + * | |
| 5 | + * @package GamiPress\Custom_Tables | |
| 6 | + * @author GamiPress <[email protected]>, Ruben Garcia <[email protected]> | |
| 7 | + * @since 1.2.8 | |
| 8 | + */ | |
| 9 | +// Exit if accessed directly | |
| 10 | +if( !defined( 'ABSPATH' ) ) exit; | |
| 11 | + | |
| 12 | +// Custom Tables | |
| 13 | +require_once GAMIPRESS_DIR . 'includes/custom-tables/logs.php'; | |
| 14 | +require_once GAMIPRESS_DIR . 'includes/custom-tables/user-earnings.php'; | |
| 15 | +// Rest API | |
| 16 | +require_once GAMIPRESS_DIR . 'includes/api/logs.php'; | |
| 17 | +require_once GAMIPRESS_DIR . 'includes/api/user-earnings.php'; | |
| 18 | + | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Register all GamiPress Custom DB Tables | |
| 22 | + * | |
| 23 | + * @since 1.2.8 | |
| 24 | + * @updated 1.4.3 User Earnings v2: Added the field title | |
| 25 | + * @updated 1.4.7 Logs v2: Added the field trigger_type | |
| 26 | + * @updated 1.5.1 Logs v3: Removed the field description and make type and trigger_type indexes | |
| 27 | + * | |
| 28 | + * @return void | |
| 29 | + */ | |
| 30 | +function gamipress_register_custom_tables() { | |
| 31 | + | |
| 32 | + // User Earnings Table | |
| 33 | + ct_register_table( 'gamipress_user_earnings', array( | |
| 34 | + 'show_ui' => true, | |
| 35 | + 'show_in_rest' => true, | |
| 36 | + 'rest_base' => 'gamipress-user-earnings', | |
| 37 | + 'version' => 4, | |
| 38 | + 'global' => gamipress_is_network_wide_active(), | |
| 39 | + 'capability' => gamipress_get_manager_capability(), | |
| 40 | + 'supports' => array( 'meta' ), | |
| 41 | + 'views' => array( | |
| 42 | + 'list' => array( | |
| 43 | + 'parent_slug' => 'gamipress', | |
| 44 | + 'priority' => 10, | |
| 45 | + ), | |
| 46 | + 'add' => false, | |
| 47 | + 'edit' => array( | |
| 48 | + 'show_in_menu' => false, | |
| 49 | + ), | |
| 50 | + ), | |
| 51 | + 'schema' => array( | |
| 52 | + 'user_earning_id' => array( | |
| 53 | + 'type' => 'bigint', | |
| 54 | + 'length' => '20', | |
| 55 | + 'auto_increment' => true, | |
| 56 | + 'primary_key' => true, | |
| 57 | + ), | |
| 58 | + 'title' => array( | |
| 59 | + 'type' => 'text', | |
| 60 | + ), | |
| 61 | + 'user_id' => array( | |
| 62 | + 'type' => 'bigint', | |
| 63 | + 'length' => '20', | |
| 64 | + 'key' => true, | |
| 65 | + ), | |
| 66 | + 'post_id' => array( | |
| 67 | + 'type' => 'bigint', | |
| 68 | + 'length' => '20', | |
| 69 | + 'key' => true, | |
| 70 | + ), | |
| 71 | + 'post_type' => array( | |
| 72 | + 'type' => 'varchar', | |
| 73 | + 'length' => '50', | |
| 74 | + ), | |
| 75 | + 'points' => array( | |
| 76 | + 'type' => 'bigint', | |
| 77 | + ), | |
| 78 | + 'points_type' => array( | |
| 79 | + 'type' => 'varchar', | |
| 80 | + 'length' => '50', | |
| 81 | + ), | |
| 82 | + 'date' => array( | |
| 83 | + 'type' => 'datetime', | |
| 84 | + 'default' => '0000-00-00 00:00:00' | |
| 85 | + ) | |
| 86 | + ), | |
| 87 | + ) ); | |
| 88 | + | |
| 89 | + // Logs Table | |
| 90 | + ct_register_table( 'gamipress_logs', array( | |
| 91 | + 'show_ui' => true, | |
| 92 | + 'show_in_rest' => true, | |
| 93 | + 'rest_base' => 'gamipress-logs', | |
| 94 | + 'version' => 6, | |
| 95 | + 'global' => gamipress_is_network_wide_active(), | |
| 96 | + 'capability' => gamipress_get_manager_capability(), | |
| 97 | + 'supports' => array( 'meta' ), | |
| 98 | + 'views' => array( | |
| 99 | + 'list' => array( | |
| 100 | + 'parent_slug' => 'gamipress', | |
| 101 | + 'priority' => 10, | |
| 102 | + ), | |
| 103 | + 'add' => false, | |
| 104 | + 'edit' => array( | |
| 105 | + 'show_in_menu' => false, | |
| 106 | + ), | |
| 107 | + ), | |
| 108 | + 'schema' => array( | |
| 109 | + 'log_id' => array( | |
| 110 | + 'type' => 'bigint', | |
| 111 | + 'length' => '20', | |
| 112 | + 'auto_increment' => true, | |
| 113 | + 'primary_key' => true, | |
| 114 | + ), | |
| 115 | + 'title' => array( | |
| 116 | + 'type' => 'text', | |
| 117 | + ), | |
| 118 | + 'type' => array( | |
| 119 | + 'type' => 'varchar', | |
| 120 | + 'length' => '50', | |
| 121 | + 'key' => true, | |
| 122 | + ), | |
| 123 | + 'trigger_type' => array( | |
| 124 | + 'type' => 'varchar', | |
| 125 | + 'length' => '255', | |
| 126 | + 'key' => true, | |
| 127 | + ), | |
| 128 | + 'access' => array( | |
| 129 | + 'type' => 'varchar', | |
| 130 | + 'length' => '50', | |
| 131 | + ), | |
| 132 | + 'user_id' => array( | |
| 133 | + 'type' => 'bigint', | |
| 134 | + 'length' => '20', | |
| 135 | + 'key' => true, | |
| 136 | + ), | |
| 137 | + 'points' => array( | |
| 138 | + 'type' => 'bigint', | |
| 139 | + 'length' => '20', | |
| 140 | + 'key' => true, | |
| 141 | + ), | |
| 142 | + 'points_type' => array( | |
| 143 | + 'type' => 'varchar', | |
| 144 | + 'length' => '255', | |
| 145 | + 'key' => true, | |
| 146 | + ), | |
| 147 | + 'date' => array( | |
| 148 | + 'type' => 'datetime', | |
| 149 | + 'default' => '0000-00-00 00:00:00', | |
| 150 | + ) | |
| 151 | + ), | |
| 152 | + ) ); | |
| 153 | + | |
| 154 | +} | |
| 155 | +add_action( 'ct_init', 'gamipress_register_custom_tables' ); | |
| 156 | + | |
| 157 | +/** | |
| 158 | + * Helper function to generate a where from a CT Query | |
| 159 | + * | |
| 160 | + * @since 1.0.0 | |
| 161 | + * | |
| 162 | + * @param array $query_vars The query vars | |
| 163 | + * @param string $field_id The field id | |
| 164 | + * @param string $table_field The table field key | |
| 165 | + * @param string $field_type The field type (string|integer) | |
| 166 | + * @param string $single_operator The single operator (=|!=) | |
| 167 | + * @param string $array_operator The array operator (IN|NOT IN) | |
| 168 | + * | |
| 169 | + * @return string | |
| 170 | + */ | |
| 171 | +function gamipress_custom_table_where( $query_vars, $field_id, $table_field, $field_type, $single_operator = '=', $array_operator = 'IN' ) { | |
| 172 | + | |
| 173 | + global $ct_table; | |
| 174 | + | |
| 175 | + $table_name = $ct_table->db->table_name; | |
| 176 | + | |
| 177 | + $where = ''; | |
| 178 | + | |
| 179 | + // Shorthand | |
| 180 | + $qv = $query_vars; | |
| 181 | + | |
| 182 | + // Type | |
| 183 | + if( isset( $qv[$field_id] ) && ! empty( $qv[$field_id] ) ) { | |
| 184 | + | |
| 185 | + if( is_array( $qv[$field_id] ) ) { | |
| 186 | + // Array values | |
| 187 | + | |
| 188 | + if( $field_type === 'string' || $field_type === 'text' ) { | |
| 189 | + | |
| 190 | + // Sanitize | |
| 191 | + $value = array_map( 'esc_sql', $qv[$field_id] ); | |
| 192 | + | |
| 193 | + // Join values by a comma-separated list of strings | |
| 194 | + $value = "'" . implode( "', '", $value ) . "'"; | |
| 195 | + | |
| 196 | + $where .= " AND {$table_name}.{$table_field} {$array_operator} ( {$value} )"; | |
| 197 | + | |
| 198 | + } else if( $field_type === 'integer' || $field_type === 'int' ) { | |
| 199 | + | |
| 200 | + // Sanitize | |
| 201 | + $value = array_map( 'absint', $qv[$field_id] ); | |
| 202 | + | |
| 203 | + // Join values by a comma-separated list of integers | |
| 204 | + $value = implode( ", ", $value ); | |
| 205 | + | |
| 206 | + $where .= " AND {$table_name}.{$table_field} {$array_operator} ( {$value} )"; | |
| 207 | + | |
| 208 | + } | |
| 209 | + } else { | |
| 210 | + // Single value | |
| 211 | + | |
| 212 | + if( $field_type === 'string' || $field_type === 'text' ) { | |
| 213 | + | |
| 214 | + $value = esc_sql( $qv[$field_id] ); | |
| 215 | + | |
| 216 | + $where .= " AND {$table_name}.{$table_field} {$single_operator} '{$value}'"; | |
| 217 | + | |
| 218 | + } else if( $field_type === 'integer' || $field_type === 'int' ) { | |
| 219 | + | |
| 220 | + $value = absint( $qv[$field_id] ); | |
| 221 | + | |
| 222 | + $where .= " AND {$table_name}.{$table_field} {$single_operator} {$value}"; | |
| 223 | + | |
| 224 | + } | |
| 225 | + } | |
| 226 | + | |
| 227 | + } | |
| 228 | + | |
| 229 | + return $where; | |
| 230 | + | |
| 231 | +} | |
| 232 | + | |
| 233 | +/** | |
| 234 | + * Helper function to sanitize a query args array | |
| 235 | + * | |
| 236 | + * @since 1.0.0 | |
| 237 | + * | |
| 238 | + * @param array $query_args The query args | |
| 239 | + * @param array $rules The rules applied to each query arg entry | |
| 240 | + * | |
| 241 | + * @return array | |
| 242 | + */ | |
| 243 | +function gamipress_sanitize_query_args( $query_args, $rules ) { | |
| 244 | + | |
| 245 | + $sanitized_query_args = array(); | |
| 246 | + | |
| 247 | + foreach( $rules as $field => $rule ) { | |
| 248 | + | |
| 249 | + if( ! isset( $query_args[$field] ) ) { | |
| 250 | + continue; | |
| 251 | + } | |
| 252 | + | |
| 253 | + // Parse type | |
| 254 | + switch ( $rule['type'] ) { | |
| 255 | + case 'string': | |
| 256 | + $query_args[$field] = sanitize_text_field( $query_args[$field] ); | |
| 257 | + break; | |
| 258 | + case 'integer': | |
| 259 | + case 'int': | |
| 260 | + $query_args[$field] = absint( $query_args[$field] ); | |
| 261 | + break; | |
| 262 | + case 'bool': | |
| 263 | + case 'boolean': | |
| 264 | + $query_args[$field] = (bool)( $query_args[$field] ); | |
| 265 | + break; | |
| 266 | + case 'array': | |
| 267 | + if( ! is_array( $query_args[$field] ) ) { | |
| 268 | + $query_args[$field] = array( $query_args[$field] ); | |
| 269 | + } | |
| 270 | + break; | |
| 271 | + } | |
| 272 | + | |
| 273 | + // Parse sanitize | |
| 274 | + if( isset( $rule['sanitize'] ) ) { | |
| 275 | + | |
| 276 | + switch ( $rule['sanitize'] ) { | |
| 277 | + case 'sanitize_text_field': | |
| 278 | + case 'text': | |
| 279 | + if( $rule['type'] === 'array' ) { | |
| 280 | + $query_args[$field] = array_map( 'sanitize_text_field', $query_args[$field] ); | |
| 281 | + } else { | |
| 282 | + $query_args[$field] = sanitize_text_field( $query_args[$field] ); | |
| 283 | + } | |
| 284 | + break; | |
| 285 | + case 'integer': | |
| 286 | + case 'int': | |
| 287 | + case 'absint': | |
| 288 | + if( $rule['type'] === 'array' ) { | |
| 289 | + $query_args[$field] = array_map( 'absint', $query_args[$field] ); | |
| 290 | + } else { | |
| 291 | + $query_args[$field] = absint( $query_args[$field] ); | |
| 292 | + } | |
| 293 | + break; | |
| 294 | + case 'uppercase': | |
| 295 | + case 'strtoupper': | |
| 296 | + if( $rule['type'] === 'array' ) { | |
| 297 | + $query_args[$field] = array_map( 'strtoupper', $query_args[$field] ); | |
| 298 | + } else { | |
| 299 | + $query_args[$field] = strtoupper( $query_args[$field] ); | |
| 300 | + } | |
| 301 | + break; | |
| 302 | + case 'lowercase': | |
| 303 | + case 'strtolower': | |
| 304 | + if( $rule['type'] === 'array' ) { | |
| 305 | + $query_args[$field] = array_map( 'strtolower', $query_args[$field] ); | |
| 306 | + } else { | |
| 307 | + $query_args[$field] = strtolower( $query_args[$field] ); | |
| 308 | + } | |
| 309 | + break; | |
| 310 | + } | |
| 311 | + | |
| 312 | + } | |
| 313 | + | |
| 314 | + // Parse allowed values | |
| 315 | + if( isset( $rule['allowed_values'] ) ) { | |
| 316 | + | |
| 317 | + if( ! is_array( $rule['allowed_values'] ) ) { | |
| 318 | + $rule['allowed_values'] = array( $rule['allowed_values'] ); | |
| 319 | + } | |
| 320 | + | |
| 321 | + if( $field === 'orderby' ) { | |
| 322 | + $rule['allowed_values'][] = 'rand'; | |
| 323 | + $rule['allowed_values'][] = 'none'; | |
| 324 | + } | |
| 325 | + | |
| 326 | + switch ( $rule['type'] ) { | |
| 327 | + case 'string': | |
| 328 | + case 'integer': | |
| 329 | + case 'int': | |
| 330 | + if( ! in_array( $query_args[$field], $rule['allowed_values'] ) ) { | |
| 331 | + $query_args[$field] = $rule['allowed_values'][0]; | |
| 332 | + }; | |
| 333 | + break; | |
| 334 | + case 'array': | |
| 335 | + $only_allowed = array(); | |
| 336 | + foreach( $query_args[$field] as $k => $v ) { | |
| 337 | + | |
| 338 | + if( in_array( $v, $rule['allowed_values'] ) ) { | |
| 339 | + $only_allowed[$k] = $v; | |
| 340 | + }; | |
| 341 | + | |
| 342 | + } | |
| 343 | + | |
| 344 | + $query_args[$field] = $only_allowed; | |
| 345 | + break; | |
| 346 | + } | |
| 347 | + | |
| 348 | + } | |
| 349 | + | |
| 350 | + $sanitized_query_args[$field] = $query_args[$field]; | |
| 351 | + | |
| 352 | + } | |
| 353 | + | |
| 354 | + return $sanitized_query_args; | |
| 355 | + | |
| 356 | +} | |