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