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