Blocks
1 month ago
MenuService.php
1 month ago
SBR_About_Builder.php
1 month ago
SBR_Admin_Notice.php
1 month ago
SBR_Collections_Builder.php
1 month ago
SBR_New_User.php
1 month ago
SBR_Notifications.php
1 month ago
SBR_Plugin_Insltaller.php
1 month ago
SBR_Support_Builder.php
1 month ago
SBR_Support_Tool.php
1 month ago
SBR_Support_Tool.php
546 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * SBR_Support_Tool. |
| 5 | * |
| 6 | * @since X.X |
| 7 | */ |
| 8 | |
| 9 | namespace SmashBalloon\Reviews\Common\Admin; |
| 10 | |
| 11 | use Smashballoon\Stubs\Services\ServiceProvider; |
| 12 | |
| 13 | // Exit if accessed directly |
| 14 | if (!defined('ABSPATH')) { |
| 15 | exit; |
| 16 | } |
| 17 | |
| 18 | class SBR_Support_Tool extends ServiceProvider |
| 19 | { |
| 20 | /** |
| 21 | * Plugin Name |
| 22 | * @access private |
| 23 | * |
| 24 | * @var string |
| 25 | */ |
| 26 | private static $plugin_name = 'SmashBalloon Reviews'; |
| 27 | |
| 28 | /** |
| 29 | * Plugin |
| 30 | * @access private |
| 31 | * |
| 32 | * @var string |
| 33 | */ |
| 34 | private static $plugin = 'smash_sbr'; |
| 35 | |
| 36 | /** |
| 37 | * Temp User Name |
| 38 | * @access private |
| 39 | * |
| 40 | * @var string |
| 41 | */ |
| 42 | private static $name = 'SmashBalloonSBR'; |
| 43 | |
| 44 | /** |
| 45 | * Temp Last Name |
| 46 | * @access private |
| 47 | * |
| 48 | * @var string |
| 49 | */ |
| 50 | private static $last_name = 'Support'; |
| 51 | |
| 52 | |
| 53 | /** |
| 54 | * Temp Login UserName |
| 55 | * @access private |
| 56 | * |
| 57 | * @var string |
| 58 | */ |
| 59 | private static $username = 'SmashBalloon_SBRSupport'; |
| 60 | |
| 61 | /** |
| 62 | * Cron Job Name |
| 63 | * @access public |
| 64 | * |
| 65 | * @var string |
| 66 | */ |
| 67 | public static $cron_event_name = 'smash_sbr_delete_expired_user'; |
| 68 | |
| 69 | /** |
| 70 | * Temp User Role |
| 71 | * @access private |
| 72 | * |
| 73 | * @var string |
| 74 | */ |
| 75 | public static $role = '_support_role'; |
| 76 | |
| 77 | public function register() |
| 78 | { |
| 79 | $this->init(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * SBR_Support_Tool constructor. |
| 84 | * |
| 85 | * @since X.X |
| 86 | */ |
| 87 | public function init() |
| 88 | { |
| 89 | add_action('plugins_loaded', [$this, 'init_temp_login']); |
| 90 | |
| 91 | if (!is_admin()) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | $this->ini_ajax_calls(); |
| 96 | add_action('admin_menu', [$this, 'register_menu']); |
| 97 | add_action('admin_footer', ['\SmashBalloon\Reviews\Common\Admin\SBR_Support_Tool', 'delete_expired_users']); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Create New User Ajax Call |
| 102 | * |
| 103 | * @since X.X |
| 104 | * |
| 105 | * @return void |
| 106 | */ |
| 107 | public function ini_ajax_calls() |
| 108 | { |
| 109 | add_action('wp_ajax_sbr_create_temp_user', array($this, 'create_temp_user_ajax_call')); |
| 110 | add_action('wp_ajax_sbr_delete_temp_user', array($this, 'delete_temp_user_ajax_call')); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Create New User Ajax Call |
| 115 | * |
| 116 | * @since X.X |
| 117 | */ |
| 118 | public function delete_temp_user_ajax_call() |
| 119 | { |
| 120 | check_ajax_referer('sbr-admin', 'nonce'); |
| 121 | $cap = current_user_can('manage_reviews_feed_options') ? 'manage_reviews_feed_options' : 'manage_options'; |
| 122 | $cap = apply_filters('sbr_settings_pages_capability', $cap); |
| 123 | if (!current_user_can($cap)) { |
| 124 | wp_send_json_error(); // This auto-dies. |
| 125 | } |
| 126 | |
| 127 | if (!isset($_POST['userId'])) { |
| 128 | wp_send_json_error(); |
| 129 | } |
| 130 | |
| 131 | $user_id = sanitize_key($_POST['userId']); |
| 132 | $return = SBR_Support_Tool::delete_temporary_user($user_id); |
| 133 | echo wp_json_encode($return); |
| 134 | wp_die(); |
| 135 | } |
| 136 | /** |
| 137 | * Create New User Ajax Call |
| 138 | * |
| 139 | * @since X.X |
| 140 | */ |
| 141 | public function create_temp_user_ajax_call() |
| 142 | { |
| 143 | check_ajax_referer('sbr-admin', 'nonce'); |
| 144 | $cap = current_user_can('manage_reviews_feed_options') ? 'manage_reviews_feed_options' : 'manage_options'; |
| 145 | $cap = apply_filters('sbr_settings_pages_capability', $cap); |
| 146 | if (!current_user_can($cap)) { |
| 147 | wp_send_json_error(); // This auto-dies. |
| 148 | } |
| 149 | $return = SBR_Support_Tool::create_temporary_user(); |
| 150 | echo wp_json_encode($return); |
| 151 | wp_die(); |
| 152 | } |
| 153 | |
| 154 | /** |
| 155 | * Init Login |
| 156 | * |
| 157 | * @since X.X |
| 158 | */ |
| 159 | public function init_temp_login() |
| 160 | { |
| 161 | |
| 162 | $attr = SBR_Support_Tool::$plugin . '_token'; |
| 163 | if (empty($_GET[$attr])) { |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | |
| 168 | $token = sanitize_key($_GET[$attr]); // Input var okay. |
| 169 | $temp_user = SBR_Support_Tool::get_temporary_user_by_token($token); |
| 170 | if (!$temp_user) { |
| 171 | wp_die(esc_attr__("You Cannot connect user", 'instgaram-feed')); |
| 172 | } |
| 173 | |
| 174 | $user_id = $temp_user->ID; |
| 175 | $should_login = (is_user_logged_in() && $user_id !== get_current_user_id()) || !is_user_logged_in(); |
| 176 | |
| 177 | if ($should_login) { |
| 178 | if ($user_id !== get_current_user_id()) { |
| 179 | wp_logout(); |
| 180 | } |
| 181 | |
| 182 | $user_login = $temp_user->user_login; |
| 183 | |
| 184 | wp_set_current_user($user_id, $user_login); |
| 185 | wp_set_auth_cookie($user_id); |
| 186 | do_action('wp_login', $user_login, $temp_user); |
| 187 | $redirect_page = 'admin.php?page=' . SBR_Support_Tool::$plugin . '_tool'; |
| 188 | wp_safe_redirect(admin_url($redirect_page)); |
| 189 | exit(); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | /** |
| 194 | * Create New User. |
| 195 | * |
| 196 | * @return array |
| 197 | * |
| 198 | * @since X.X |
| 199 | */ |
| 200 | public static function create_temporary_user() |
| 201 | { |
| 202 | if (!current_user_can('create_users')) { |
| 203 | return [ |
| 204 | 'success' => false, |
| 205 | 'message' => __('You don\'t have enough permission to create users'), |
| 206 | ]; |
| 207 | } |
| 208 | $domain = str_replace([ |
| 209 | 'http://', 'https://', 'http://www.', 'https://www.', 'www.' |
| 210 | ], '', site_url()); |
| 211 | |
| 212 | $email = SBR_Support_Tool::$username . '@' . $domain; |
| 213 | $temp_user_args = [ |
| 214 | 'user_email' => $email, |
| 215 | 'user_pass' => SBR_Support_Tool::generate_temp_password(), |
| 216 | 'first_name' => SBR_Support_Tool::$name, |
| 217 | 'last_name' => SBR_Support_Tool::$last_name, |
| 218 | 'user_login' => SBR_Support_Tool::$username, |
| 219 | 'role' => SBR_Support_Tool::$plugin . SBR_Support_Tool::$role |
| 220 | ]; |
| 221 | |
| 222 | $temp_user_id = \wp_insert_user($temp_user_args); |
| 223 | $result = []; |
| 224 | |
| 225 | if (is_wp_error($temp_user_id)) { |
| 226 | $result = [ |
| 227 | 'success' => false, |
| 228 | 'message' => __('Cannot create user') |
| 229 | ]; |
| 230 | } else { |
| 231 | $creation_time = \current_time('timestamp'); |
| 232 | $expires = strtotime('+15 days', $creation_time); |
| 233 | $token = str_replace(['=', '&', '"', "'"], '', \sbr_encrypt_decrypt('encrypt', SBR_Support_Tool::generate_temp_password(35))); |
| 234 | |
| 235 | update_user_meta($temp_user_id, SBR_Support_Tool::$plugin . '_user', $temp_user_id); |
| 236 | update_user_meta($temp_user_id, SBR_Support_Tool::$plugin . '_token', $token); |
| 237 | update_user_meta($temp_user_id, SBR_Support_Tool::$plugin . '_create_time', $creation_time); |
| 238 | update_user_meta($temp_user_id, SBR_Support_Tool::$plugin . '_expires', $expires); |
| 239 | |
| 240 | $result = [ |
| 241 | 'success' => true, |
| 242 | 'message' => __('Temporary user created successfully'), |
| 243 | 'user' => SBR_Support_Tool::get_user_meta_data($temp_user_id) |
| 244 | ]; |
| 245 | } |
| 246 | return $result; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Delete Temp User. |
| 251 | * |
| 252 | * @param $user_id User ID to delete |
| 253 | * |
| 254 | * @return array |
| 255 | * |
| 256 | * @since X.X |
| 257 | */ |
| 258 | public static function delete_temporary_user($user_id) |
| 259 | { |
| 260 | require_once(ABSPATH . 'wp-admin/includes/user.php'); |
| 261 | |
| 262 | if (!current_user_can('delete_users')) { |
| 263 | return [ |
| 264 | 'success' => false, |
| 265 | 'message' => __('You don\'t have enough permission to delete users'), |
| 266 | ]; |
| 267 | } |
| 268 | if (!wp_delete_user($user_id)) { |
| 269 | return [ |
| 270 | 'success' => false, |
| 271 | 'message' => __('Cannot delete this user'), |
| 272 | ]; |
| 273 | } |
| 274 | |
| 275 | return [ |
| 276 | 'success' => true, |
| 277 | 'message' => __('User Deleted'), |
| 278 | ]; |
| 279 | } |
| 280 | |
| 281 | /** |
| 282 | * Get User Meta |
| 283 | * |
| 284 | * @param $user_id User ID to Get |
| 285 | * |
| 286 | * @return array/boolean |
| 287 | * |
| 288 | * @since X.X |
| 289 | */ |
| 290 | public static function get_user_meta_data($user_id) |
| 291 | { |
| 292 | $user = get_user_meta($user_id, SBR_Support_Tool::$plugin . '_user'); |
| 293 | if (!$user) { |
| 294 | return false; |
| 295 | } |
| 296 | $token = get_user_meta($user_id, SBR_Support_Tool::$plugin . '_token'); |
| 297 | $creation_time = get_user_meta($user_id, SBR_Support_Tool::$plugin . '_create_time'); |
| 298 | $expires = get_user_meta($user_id, SBR_Support_Tool::$plugin . '_expires'); |
| 299 | |
| 300 | $url = SBR_Support_Tool::$plugin . '_token=' . $token[0]; |
| 301 | return [ |
| 302 | 'id' => $user_id, |
| 303 | 'token' => $token[0], |
| 304 | 'creation_time' => $creation_time[0], |
| 305 | 'expires' => $expires[0], |
| 306 | 'expires_date' => SBR_Support_Tool::get_expires_days($expires[0]), |
| 307 | 'url' => admin_url('/?' . $url) |
| 308 | ]; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Get UDays before Expiring Token |
| 313 | * |
| 314 | * @param $expires timestamp |
| 315 | * |
| 316 | * @since X.X |
| 317 | */ |
| 318 | public static function get_expires_days($expires) |
| 319 | { |
| 320 | return ceil(($expires - time()) / 60 / 60 / 24); |
| 321 | } |
| 322 | |
| 323 | /** |
| 324 | * Get User By Token. |
| 325 | * |
| 326 | * @param $token Token to connect with |
| 327 | * |
| 328 | * @since X.X |
| 329 | */ |
| 330 | public static function get_temporary_user_by_token($token = '') |
| 331 | { |
| 332 | if (empty($token)) { |
| 333 | return false; |
| 334 | } |
| 335 | |
| 336 | $args = [ |
| 337 | 'fields' => 'all', |
| 338 | 'meta_query' => [ |
| 339 | [ |
| 340 | 'key' => SBR_Support_Tool::$plugin . '_token', |
| 341 | 'value' => sanitize_text_field($token), |
| 342 | 'compare' => '=', |
| 343 | ] |
| 344 | ] |
| 345 | ]; |
| 346 | |
| 347 | $users = new \WP_User_Query($args); |
| 348 | $users_result = $users->get_results(); |
| 349 | |
| 350 | if (empty($users_result)) { |
| 351 | return false; |
| 352 | } |
| 353 | |
| 354 | return $users_result[0]; |
| 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Check Temporary User Created |
| 359 | * |
| 360 | * @since X.X |
| 361 | */ |
| 362 | public static function check_temporary_user_exists() |
| 363 | { |
| 364 | $args = [ |
| 365 | 'fields' => 'all', |
| 366 | 'meta_query' => [ |
| 367 | [ |
| 368 | 'key' => SBR_Support_Tool::$plugin . '_token', |
| 369 | 'value' => null, |
| 370 | 'compare' => '!=', |
| 371 | ] |
| 372 | ] |
| 373 | ]; |
| 374 | $users = new \WP_User_Query($args); |
| 375 | $users_result = $users->get_results(); |
| 376 | if (empty($users_result)) { |
| 377 | return null; |
| 378 | } |
| 379 | return SBR_Support_Tool::get_user_meta_data($users_result[0]->ID); |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * Check & Delete Expired Users |
| 384 | * |
| 385 | * @since X.X |
| 386 | * |
| 387 | */ |
| 388 | public static function delete_expired_users() |
| 389 | { |
| 390 | $existing_user = SBR_Support_Tool::check_temporary_user_exists(); |
| 391 | if ($existing_user === null) { |
| 392 | return false; |
| 393 | } |
| 394 | $is_expired = intval($existing_user['expires']) - \current_time('timestamp') <= 0; |
| 395 | if (!$is_expired) { |
| 396 | return false; |
| 397 | } |
| 398 | require_once(ABSPATH . 'wp-admin/includes/user.php'); |
| 399 | \wp_delete_user($existing_user['id']); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * Delete Temp User |
| 404 | * |
| 405 | * @since X.X |
| 406 | * |
| 407 | */ |
| 408 | public static function delete_temp_user() |
| 409 | { |
| 410 | $existing_user = SBR_Support_Tool::check_temporary_user_exists(); |
| 411 | if ($existing_user === null) { |
| 412 | return false; |
| 413 | } |
| 414 | require_once(ABSPATH . 'wp-admin/includes/user.php'); |
| 415 | \wp_delete_user($existing_user['id']); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Register Menu. |
| 421 | * |
| 422 | * @since X.X |
| 423 | */ |
| 424 | public function register_menu() |
| 425 | { |
| 426 | $role_id = SBR_Support_Tool::$plugin . SBR_Support_Tool::$role; |
| 427 | $cap = $role_id; |
| 428 | $cap = apply_filters('sbr_settings_pages_capability', $cap); |
| 429 | |
| 430 | $support_tool_page = add_submenu_page( |
| 431 | 'sbr', |
| 432 | __('Support API tool', 'reviews-feed'), |
| 433 | __('Support API tool', 'reviews-feed'), |
| 434 | $cap, |
| 435 | SBR_Support_Tool::$plugin . '_tool', |
| 436 | array($this, 'render'), |
| 437 | 5 |
| 438 | ); |
| 439 | #add_action('load-' . $support_tool_page, array( $this, 'support_page_enqueue_assets')); |
| 440 | } |
| 441 | |
| 442 | |
| 443 | /** |
| 444 | * Generate Temp User Password |
| 445 | * |
| 446 | * @param $length Length of password |
| 447 | * |
| 448 | * @since X.X |
| 449 | * |
| 450 | * @return string |
| 451 | */ |
| 452 | public static function generate_temp_password($length = 20) |
| 453 | { |
| 454 | return wp_generate_password($length, true, true); |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /** |
| 459 | * Render the Api Tools Page |
| 460 | * |
| 461 | * @since X.X |
| 462 | * |
| 463 | * @return string |
| 464 | */ |
| 465 | public function render() |
| 466 | { |
| 467 | include_once SBR_PLUGIN_DIR . 'assets/admin/views/support/support-tools.php'; |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Available Endpoints |
| 472 | * |
| 473 | * @since 6.3 |
| 474 | * |
| 475 | * @return array |
| 476 | */ |
| 477 | public function available_endpoints() |
| 478 | { |
| 479 | return array( |
| 480 | 'source' => 'Source', |
| 481 | 'reviews' => 'Reviews' |
| 482 | ); |
| 483 | } |
| 484 | |
| 485 | |
| 486 | /** |
| 487 | * Render Validate Settings & return data |
| 488 | * |
| 489 | * @since X.X |
| 490 | * |
| 491 | * @return string |
| 492 | */ |
| 493 | public function validate_and_sanitize_support_settings($raw_post) |
| 494 | { |
| 495 | if (empty($raw_post['sb_reviews_support_source'])) { |
| 496 | return array(); |
| 497 | } |
| 498 | |
| 499 | $encryption = new \SmashBalloon\Reviews\Common\Helpers\Data_Encryption(); |
| 500 | $data_response_text = __('Cannot get info', 'reviews-feed'); |
| 501 | |
| 502 | $source_id = sanitize_key($raw_post['sb_reviews_support_source']); |
| 503 | $endpoint = sanitize_key($raw_post['sb_reviews_support_endpoint']); |
| 504 | $db = new \SmashBalloon\Reviews\Common\Customizer\Db(); |
| 505 | $sources = $db->get_single_source( |
| 506 | [ |
| 507 | 'id' => $source_id, |
| 508 | 'provider' => 'facebook' |
| 509 | ] |
| 510 | ); |
| 511 | |
| 512 | if (isset($sources[0])) { |
| 513 | $access_token = $encryption->maybe_decrypt($sources[0]['access_token']); |
| 514 | if ($endpoint === 'source') { |
| 515 | $data_response = \SmashBalloon\Reviews\Pro\Integrations\Providers\Facebook::get_source_header($source_id, $access_token, false); |
| 516 | } elseif ($endpoint === 'reviews') { |
| 517 | $data_response = \SmashBalloon\Reviews\Pro\Integrations\Providers\Facebook::get_reviews_list($source_id, $access_token); |
| 518 | } |
| 519 | $sanitized_results = wp_json_encode($data_response, true); |
| 520 | $data_response_text = str_replace($access_token, '{access_token}', $sanitized_results); |
| 521 | } |
| 522 | |
| 523 | echo '<h3>Results</h3>'; |
| 524 | echo '<pre>'; |
| 525 | // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r -- Support tool output |
| 526 | // nosemgrep: php-debug-print-r |
| 527 | print_r(esc_html($data_response_text)); |
| 528 | echo "\n\nDecoded:\n"; |
| 529 | // nosemgrep: php-debug-print-r |
| 530 | print_r(json_decode($data_response_text, true)); |
| 531 | echo '</pre>'; |
| 532 | echo '<hr>'; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 | |
| 540 | public function create_api_url($url, $settings) |
| 541 | { |
| 542 | } |
| 543 | |
| 544 | |
| 545 | } |
| 546 |