Installer
3 years ago
AdminNotices.php
1 month ago
AjaxHandler.php
1 year ago
Autologin.php
9 months ago
BlockRegistrations.php
10 months ago
BuddyPressBbPress.php
3 years ago
DisableConcurrentLogins.php
2 years ago
EditUserProfile.php
5 months ago
ExtensionManager.php
1 month ago
FileUploader.php
3 months ago
FormPreviewHandler.php
5 months ago
FormRepository.php
1 year ago
FormShortcodeDefaults.php
1 month ago
GDPR.php
3 years ago
Geolocation.php
3 years ago
GlobalSiteAccess.php
3 years ago
ImageUploader.php
1 year ago
LoginAuth.php
1 year ago
Miscellaneous.php
3 years ago
ModifyRedirectDefaultLinks.php
5 months ago
PPRESS_Session.php
1 year ago
PROFILEPRESS_sql.php
1 year ago
PasswordReset.php
1 year ago
ProfileUrlRewrite.php
4 years ago
RegistrationAuth.php
1 week ago
SendEmail.php
3 years ago
ShortcodeThemeFactory.php
5 years ago
UserAvatar.php
1 year ago
UserSignupLocationListingPage.php
3 years ago
UsernameEmailRestrictLogin.php
4 years ago
WPProfileFieldParserTrait.php
1 year ago
WelcomeEmailAfterSignup.php
1 year ago
default-email-template.php
1 year ago
index.php
3 years ago
PROFILEPRESS_sql.php
601 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\Classes; |
| 4 | |
| 5 | use ProfilePress\Core\Base; |
| 6 | |
| 7 | class PROFILEPRESS_sql |
| 8 | { |
| 9 | /** @param $meta_key |
| 10 | * @param $meta_value |
| 11 | * @param string $flag |
| 12 | * |
| 13 | * @return bool|int |
| 14 | */ |
| 15 | public static function add_meta_data($meta_key, $meta_value, $flag = '') |
| 16 | { |
| 17 | global $wpdb; |
| 18 | |
| 19 | $insert = $wpdb->insert( |
| 20 | Base::meta_data_db_table(), |
| 21 | [ |
| 22 | 'meta_key' => $meta_key, |
| 23 | 'meta_value' => serialize($meta_value), |
| 24 | 'flag' => $flag, |
| 25 | ], |
| 26 | [ |
| 27 | '%s', |
| 28 | '%s', |
| 29 | '%s', |
| 30 | ] |
| 31 | ); |
| 32 | |
| 33 | return ! $insert ? false : $wpdb->insert_id; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param $meta_key |
| 38 | * @param $meta_value |
| 39 | * @param $flag |
| 40 | * |
| 41 | * @return bool|int |
| 42 | */ |
| 43 | public static function update_meta_data($id, $meta_key, $meta_value, $flag = '') |
| 44 | { |
| 45 | global $wpdb; |
| 46 | |
| 47 | $insert = $wpdb->update( |
| 48 | Base::meta_data_db_table(), |
| 49 | [ |
| 50 | 'meta_key' => $meta_key, |
| 51 | 'meta_value' => serialize($meta_value), |
| 52 | 'flag' => $flag, |
| 53 | ], |
| 54 | ['id' => $id], |
| 55 | [ |
| 56 | '%s', |
| 57 | '%s', |
| 58 | '%s', |
| 59 | ], |
| 60 | ['%d'] |
| 61 | ); |
| 62 | |
| 63 | return ! $insert ? false : $wpdb->insert_id; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * @param $meta_id |
| 68 | * @param $meta_key |
| 69 | * @param $meta_value |
| 70 | * |
| 71 | * @return false|int |
| 72 | */ |
| 73 | public static function update_meta_value($meta_id, $meta_key, $meta_value) |
| 74 | { |
| 75 | global $wpdb; |
| 76 | |
| 77 | return $wpdb->update( |
| 78 | Base::meta_data_db_table(), |
| 79 | ['meta_value' => serialize($meta_value)], |
| 80 | ['id' => $meta_id, 'meta_key' => $meta_key], |
| 81 | ['%s'], |
| 82 | ['%d', '%s'] |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * @param $meta_id |
| 88 | * @param $meta_key |
| 89 | * |
| 90 | * @return bool|mixed |
| 91 | */ |
| 92 | public static function get_meta_value($meta_id, $meta_key) |
| 93 | { |
| 94 | global $wpdb; |
| 95 | |
| 96 | $table = Base::meta_data_db_table(); |
| 97 | |
| 98 | $sql = "SELECT meta_value FROM $table WHERE id = %d AND meta_key = %s"; |
| 99 | |
| 100 | // get the profile fields row for the id and save as array |
| 101 | $result = $wpdb->get_var($wpdb->prepare($sql, $meta_id, $meta_key)); |
| 102 | |
| 103 | return ! is_null($result) ? unserialize($result, ['allowed_classes' => false]) : false; |
| 104 | } |
| 105 | |
| 106 | public static function get_meta_data_by_key($meta_key) |
| 107 | { |
| 108 | return ppress_cache_transform('meta_data_by_key_' . $meta_key, function () use ($meta_key) { |
| 109 | |
| 110 | global $wpdb; |
| 111 | |
| 112 | $table = Base::meta_data_db_table(); |
| 113 | |
| 114 | $sql = "SELECT * FROM $table WHERE meta_key = %s"; |
| 115 | |
| 116 | $result = $wpdb->get_results($wpdb->prepare($sql, $meta_key), 'ARRAY_A'); |
| 117 | |
| 118 | if (empty($result)) return false; |
| 119 | |
| 120 | $output = []; |
| 121 | foreach ($result as $key => $meta) { |
| 122 | $output[$key] = array_reduce(array_keys($meta), function ($carry, $item) use ($meta) { |
| 123 | $carry[$item] = ($item == 'meta_value') ? unserialize($meta[$item], ['allowed_classes' => false]) : $meta[$item]; |
| 124 | |
| 125 | return $carry; |
| 126 | }); |
| 127 | } |
| 128 | |
| 129 | return $output; |
| 130 | }); |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * @param $id |
| 135 | * |
| 136 | * @return array|false |
| 137 | */ |
| 138 | public static function get_meta_data_by_id($id) |
| 139 | { |
| 140 | global $wpdb; |
| 141 | |
| 142 | $table = Base::meta_data_db_table(); |
| 143 | |
| 144 | $sql = "SELECT * FROM $table WHERE id = %d"; |
| 145 | |
| 146 | $result = $wpdb->get_results($wpdb->prepare($sql, $id), 'ARRAY_A'); |
| 147 | |
| 148 | if (empty($result)) return false; |
| 149 | |
| 150 | $output = []; |
| 151 | foreach ($result as $key => $meta) { |
| 152 | $output[$key] = array_reduce(array_keys($meta), function ($carry, $item) use ($meta) { |
| 153 | $carry[$item] = ($item == 'meta_value') ? unserialize($meta[$item], ['allowed_classes' => false]) : $meta[$item]; |
| 154 | |
| 155 | return $carry; |
| 156 | }); |
| 157 | } |
| 158 | |
| 159 | return $output; |
| 160 | } |
| 161 | |
| 162 | public static function get_meta_data_by_flag($flag) |
| 163 | { |
| 164 | global $wpdb; |
| 165 | |
| 166 | $table = Base::meta_data_db_table(); |
| 167 | |
| 168 | $sql = "SELECT * FROM $table WHERE flag = %s"; |
| 169 | |
| 170 | $result = $wpdb->get_results($wpdb->prepare($sql, $flag), 'ARRAY_A'); |
| 171 | |
| 172 | if (empty($result)) return false; |
| 173 | |
| 174 | $output = []; |
| 175 | foreach ($result as $key => $meta) { |
| 176 | $output[$key] = array_reduce(array_keys($meta), function ($carry, $item) use ($meta) { |
| 177 | $carry[$item] = ($item == 'meta_value') ? unserialize($meta[$item], ['allowed_classes' => false]) : $meta[$item]; |
| 178 | |
| 179 | return $carry; |
| 180 | }); |
| 181 | } |
| 182 | |
| 183 | return $output; |
| 184 | } |
| 185 | |
| 186 | public static function delete_meta_data($meta_id) |
| 187 | { |
| 188 | global $wpdb; |
| 189 | |
| 190 | $result = $wpdb->delete(Base::meta_data_db_table(), ['id' => $meta_id], ['%d']); |
| 191 | |
| 192 | return $result !== false; |
| 193 | } |
| 194 | |
| 195 | public static function delete_meta_data_by_flag($flag) |
| 196 | { |
| 197 | global $wpdb; |
| 198 | |
| 199 | $result = $wpdb->delete(Base::meta_data_db_table(), ['flag' => $flag], ['%s']); |
| 200 | |
| 201 | return $result !== false; |
| 202 | } |
| 203 | |
| 204 | public static function delete_meta_data_by_meta_key($meta_key) |
| 205 | { |
| 206 | global $wpdb; |
| 207 | |
| 208 | $result = $wpdb->delete(Base::meta_data_db_table(), ['meta_key' => $meta_key], ['%s']); |
| 209 | |
| 210 | return $result !== false; |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Query for profile placement if user can view the his profile |
| 215 | * |
| 216 | * @return mixed |
| 217 | */ |
| 218 | public static function get_profile_custom_fields() |
| 219 | { |
| 220 | if ( ! ExtensionManager::is_premium()) return []; |
| 221 | |
| 222 | global $wpdb; |
| 223 | |
| 224 | return $wpdb->get_results( |
| 225 | sprintf("SELECT * FROM %s ORDER BY id", Base::profile_fields_db_table()), |
| 226 | 'ARRAY_A' |
| 227 | ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Retrieve the profile field row of an ID |
| 232 | * |
| 233 | * @param int $id |
| 234 | * |
| 235 | * @return array |
| 236 | */ |
| 237 | public static function get_profile_custom_field_by_id($id) |
| 238 | { |
| 239 | global $wpdb; |
| 240 | |
| 241 | if ( ! ExtensionManager::is_premium()) return []; |
| 242 | |
| 243 | $table = Base::profile_fields_db_table(); |
| 244 | |
| 245 | return $wpdb->get_row( |
| 246 | $wpdb->prepare("SELECT * FROM $table WHERE id = %d", $id), |
| 247 | 'ARRAY_A' |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Retrieve the profile custom field by field key |
| 253 | * |
| 254 | * @param $field_key |
| 255 | * |
| 256 | * @return array |
| 257 | */ |
| 258 | public static function get_profile_custom_field_by_key($field_key) |
| 259 | { |
| 260 | if ( ! ExtensionManager::is_premium()) return []; |
| 261 | |
| 262 | global $wpdb; |
| 263 | |
| 264 | $table = Base::profile_fields_db_table(); |
| 265 | |
| 266 | return $wpdb->get_row( |
| 267 | $wpdb->prepare("SELECT * FROM $table WHERE field_key = %s", $field_key), |
| 268 | 'ARRAY_A' |
| 269 | ); |
| 270 | } |
| 271 | |
| 272 | public static function get_profile_custom_fields_by_types($types) |
| 273 | { |
| 274 | if ( ! ExtensionManager::is_premium()) return []; |
| 275 | |
| 276 | global $wpdb; |
| 277 | |
| 278 | $sql = sprintf("SELECT * FROM %s", Base::profile_fields_db_table()); |
| 279 | |
| 280 | $sql .= " WHERE type IN(" . implode(', ', array_fill(0, count($types), '%s')) . ")"; |
| 281 | |
| 282 | $sql = call_user_func_array([$wpdb, 'prepare'], array_merge([$sql], $types)); |
| 283 | |
| 284 | return $wpdb->get_results($sql); |
| 285 | } |
| 286 | |
| 287 | public static function delete_profile_custom_field($id) |
| 288 | { |
| 289 | global $wpdb; |
| 290 | |
| 291 | $delete_sql = $wpdb->delete( |
| 292 | Base::profile_fields_db_table(), |
| 293 | ['id' => $id], |
| 294 | ['%d'] |
| 295 | ); |
| 296 | |
| 297 | return $delete_sql; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Return a list of created custom profile IDs. |
| 302 | * |
| 303 | * @return array |
| 304 | */ |
| 305 | public static function get_profile_field_ids() |
| 306 | { |
| 307 | if ( ! ExtensionManager::is_premium()) return []; |
| 308 | |
| 309 | global $wpdb; |
| 310 | |
| 311 | $table = Base::profile_fields_db_table(); |
| 312 | |
| 313 | return $wpdb->get_col("SELECT id FROM $table ORDER BY id"); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Check if a profile field's key exist in the database. |
| 318 | * |
| 319 | * @param int $field_key |
| 320 | * |
| 321 | * @return bool |
| 322 | */ |
| 323 | public static function is_profile_field_key_exist($field_key) |
| 324 | { |
| 325 | if ( ! ExtensionManager::is_premium()) return false; |
| 326 | |
| 327 | global $wpdb; |
| 328 | |
| 329 | $table = Base::profile_fields_db_table(); |
| 330 | |
| 331 | $response = $wpdb->get_var( |
| 332 | $wpdb->prepare("SELECT id FROM $table WHERE field_key = %s", $field_key) |
| 333 | ); |
| 334 | |
| 335 | return ! is_null($response); |
| 336 | } |
| 337 | |
| 338 | /** |
| 339 | * Add custom field to DB |
| 340 | * |
| 341 | * @param string $label_name |
| 342 | * @param string $key |
| 343 | * @param string $description |
| 344 | * @param string $type |
| 345 | * @param string $options |
| 346 | * |
| 347 | * @return bool|int |
| 348 | */ |
| 349 | public static function add_profile_field($label_name, $key, $description, $type, $options) |
| 350 | { |
| 351 | global $wpdb; |
| 352 | |
| 353 | $insert = $wpdb->insert( |
| 354 | Base::profile_fields_db_table(), |
| 355 | array( |
| 356 | 'label_name' => $label_name, |
| 357 | 'field_key' => $key, |
| 358 | 'description' => $description, |
| 359 | 'type' => $type, |
| 360 | 'options' => $options, |
| 361 | ), |
| 362 | array( |
| 363 | '%s', |
| 364 | '%s', |
| 365 | '%s', |
| 366 | '%s', |
| 367 | '%s', |
| 368 | ) |
| 369 | ); |
| 370 | |
| 371 | return ! $insert ? false : $wpdb->insert_id; |
| 372 | } |
| 373 | |
| 374 | |
| 375 | /** |
| 376 | * Update custom field in DB |
| 377 | * |
| 378 | * @param $id |
| 379 | * @param string $label_name |
| 380 | * @param string $key |
| 381 | * @param string $description |
| 382 | * @param string $type |
| 383 | * @param string $options |
| 384 | * |
| 385 | * @return bool|int |
| 386 | */ |
| 387 | public static function update_profile_field($id, $label_name, $key, $description, $type, $options) |
| 388 | { |
| 389 | global $wpdb; |
| 390 | |
| 391 | return $wpdb->update( |
| 392 | Base::profile_fields_db_table(), |
| 393 | [ |
| 394 | 'label_name' => $label_name, |
| 395 | 'field_key' => $key, |
| 396 | 'description' => $description, |
| 397 | 'type' => $type, |
| 398 | 'options' => $options, |
| 399 | ], |
| 400 | ['id' => $id], |
| 401 | [ |
| 402 | '%s', |
| 403 | '%s', |
| 404 | '%s', |
| 405 | '%s', |
| 406 | '%s', |
| 407 | |
| 408 | ], |
| 409 | ['%d'] |
| 410 | ); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | /*** |
| 415 | * Mark a select field as multi selectable. |
| 416 | * |
| 417 | * @param string $key |
| 418 | * |
| 419 | * @param int $id must have a value. |
| 420 | * |
| 421 | * @return bool |
| 422 | */ |
| 423 | public static function add_multi_selectable($key, $id = 0) |
| 424 | { |
| 425 | $old_data = get_option('ppress_cpf_select_multi_selectable', array()); |
| 426 | $new_data = [$key => $id]; |
| 427 | |
| 428 | return update_option( |
| 429 | 'ppress_cpf_select_multi_selectable', |
| 430 | array_merge($old_data, $new_data) |
| 431 | ); |
| 432 | } |
| 433 | |
| 434 | /*** |
| 435 | * Remove a select field as multi selectable. |
| 436 | * |
| 437 | * @param string $key |
| 438 | * |
| 439 | * @return bool |
| 440 | */ |
| 441 | public static function delete_multi_selectable($key) |
| 442 | { |
| 443 | $old_data = get_option('ppress_cpf_select_multi_selectable', array()); |
| 444 | unset($old_data[$key]); |
| 445 | |
| 446 | return update_option('ppress_cpf_select_multi_selectable', array_unique($old_data)); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * get radio buttons options of an added custom field |
| 451 | */ |
| 452 | public static function get_field_option_values($field_key) |
| 453 | { |
| 454 | if ( ! ExtensionManager::is_premium()) return ''; |
| 455 | |
| 456 | global $wpdb; |
| 457 | |
| 458 | $table = Base::profile_fields_db_table(); |
| 459 | |
| 460 | return $wpdb->get_var( |
| 461 | $wpdb->prepare("SELECT options FROM $table WHERE field_key = %s", $field_key) |
| 462 | ); |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * Get radio buttons options of an added custom field |
| 467 | */ |
| 468 | public static function get_field_label($field_key) |
| 469 | { |
| 470 | if ( ! ExtensionManager::is_premium()) return ''; |
| 471 | |
| 472 | global $wpdb; |
| 473 | |
| 474 | $table = Base::profile_fields_db_table(); |
| 475 | |
| 476 | return $wpdb->get_var($wpdb->prepare("SELECT label_name FROM $table WHERE field_key = %s", $field_key)); |
| 477 | } |
| 478 | |
| 479 | public static function get_field_type($field_key) |
| 480 | { |
| 481 | $cache_key = 'get_field_type_' . $field_key; |
| 482 | |
| 483 | static $cache_bucket = []; |
| 484 | |
| 485 | if ( ! array_key_exists($cache_key, $cache_bucket)) { |
| 486 | |
| 487 | $cache_bucket[$cache_key] = ''; |
| 488 | |
| 489 | if (ExtensionManager::is_premium()) { |
| 490 | |
| 491 | global $wpdb; |
| 492 | |
| 493 | $table = Base::profile_fields_db_table(); |
| 494 | |
| 495 | $cache_bucket[$cache_key] = $wpdb->get_var($wpdb->prepare("SELECT type FROM $table WHERE field_key = %s", $field_key)); |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | return $cache_bucket[$cache_key]; |
| 500 | } |
| 501 | |
| 502 | public static function get_contact_info_fields() |
| 503 | { |
| 504 | return get_option(PPRESS_CONTACT_INFO_OPTION_NAME, []); |
| 505 | } |
| 506 | |
| 507 | public static function get_contact_info_field_label($field_key) |
| 508 | { |
| 509 | return ppress_var(self::get_contact_info_fields(), $field_key); |
| 510 | } |
| 511 | |
| 512 | /** One time Passwordless login */ |
| 513 | public static function passwordless_insert_record($user_id, $token, $expiration) |
| 514 | { |
| 515 | global $wpdb; |
| 516 | |
| 517 | $table = Base::passwordless_login_db_table(); |
| 518 | |
| 519 | // check if a passwordless record already exist for the user |
| 520 | // if true update the row else add a new row record. |
| 521 | $id = $wpdb->get_var($wpdb->prepare("SELECT user_id FROM $table WHERE user_id = %d", $user_id)); |
| 522 | |
| 523 | if (is_null($id)) { |
| 524 | |
| 525 | $prepared_statement = $wpdb->prepare( |
| 526 | " |
| 527 | INSERT INTO $table |
| 528 | ( user_id, token, expires ) |
| 529 | VALUES ( %d, %s, %d ) |
| 530 | ", |
| 531 | array( |
| 532 | $user_id, |
| 533 | $token, |
| 534 | $expiration, |
| 535 | ) |
| 536 | ); |
| 537 | } else { |
| 538 | $prepared_statement = $wpdb->prepare( |
| 539 | " |
| 540 | UPDATE $table |
| 541 | SET token = %s, expires = %d |
| 542 | WHERE user_id = %d |
| 543 | ", |
| 544 | array( |
| 545 | $token, |
| 546 | $expiration, |
| 547 | $user_id, |
| 548 | ) |
| 549 | ); |
| 550 | } |
| 551 | |
| 552 | return $wpdb->query($prepared_statement); |
| 553 | } |
| 554 | |
| 555 | /** |
| 556 | * Delete OTP record for a user. |
| 557 | * |
| 558 | * @param int $user_id |
| 559 | * |
| 560 | * @return false|int |
| 561 | */ |
| 562 | public static function passwordless_delete_record($user_id) |
| 563 | { |
| 564 | global $wpdb; |
| 565 | |
| 566 | return $wpdb->delete(Base::passwordless_login_db_table(), array('user_id' => $user_id), array('%d')); |
| 567 | } |
| 568 | |
| 569 | /** |
| 570 | * Get the passwordless token of a user by ID |
| 571 | * |
| 572 | * @param int $user_id ID of user |
| 573 | * |
| 574 | * @return null|string |
| 575 | */ |
| 576 | public static function passwordless_get_user_token($user_id) |
| 577 | { |
| 578 | global $wpdb; |
| 579 | |
| 580 | $table = Base::passwordless_login_db_table(); |
| 581 | |
| 582 | return $wpdb->get_var($wpdb->prepare("SELECT token FROM $table WHERE user_id = %d", $user_id)); |
| 583 | } |
| 584 | |
| 585 | /** |
| 586 | * Get the expiration time |
| 587 | * |
| 588 | * @param int $user_id |
| 589 | * |
| 590 | * @return null|string |
| 591 | */ |
| 592 | public static function passwordless_get_expiration($user_id) |
| 593 | { |
| 594 | global $wpdb; |
| 595 | |
| 596 | $table = Base::passwordless_login_db_table(); |
| 597 | |
| 598 | return $wpdb->get_var($wpdb->prepare("SELECT expires FROM $table WHERE user_id = %d", $user_id)); |
| 599 | } |
| 600 | } |
| 601 |