init_wp_recaptcha(); $this->hooks(); } } ); } private function hooks() { $template = WPF()->current_object['template']; ############################################################################### ############### Facebook & X.com API ######################################## ############################################################################### if( ! is_user_logged_in() ) { if( wpforo_setting( 'authorization', 'fb_login' ) ) { if( in_array( $template, [ 'login', 'register' ], true ) ) { add_action( 'wp_enqueue_scripts', [ $this, 'fb_enqueue' ] ); add_action( 'wpforo_bottom_hook', [ $this, 'fb_login_sdk' ], 9 ); } if( wpforo_setting( 'authorization', 'fb_api_id' ) && wpforo_setting( 'authorization', 'fb_api_secret' ) ) { if( wpforo_setting( 'authorization', 'fb_lb_on_lp' ) ) { add_action( 'wpforo_login_form_end', [ $this, 'fb_login_button' ] ); } if( wpforo_setting( 'authorization', 'fb_lb_on_rp' ) ) { add_action( 'wpforo_register_form_end', [ $this, 'fb_login_button' ] ); } } add_action( 'wp_ajax_wpforo_facebook_auth', [ $this, 'fb_auth' ] ); add_action( 'wp_ajax_nopriv_wpforo_facebook_auth', [ $this, 'fb_auth' ] ); } } if( is_wpforo_page() ) { if( apply_filters( 'wpforo_api_fb_load_sdk', true ) && wpforo_setting( 'social', 'sb', 'fb' ) ) { add_action( 'wpforo_bottom_hook', [ $this, 'fb_sdk' ], 10 ); } if( apply_filters( 'wpforo_api_tw_load_wjs', true ) && wpforo_setting( 'social', 'sb', 'tw' ) ) { add_action( 'wpforo_top_hook', [ $this, 'tw_wjs' ], 11 ); } if( apply_filters( 'wpforo_api_vk_load_js', true ) && wpforo_setting( 'social', 'sb', 'vk' ) ) { add_action( 'wpforo_top_hook', [ $this, 'vk_js' ], 13 ); } } ############################################################################### ############### reCAPTCHA API ################################################# ############################################################################### $site_key = wpforo_setting( 'recaptcha', 'site_key' ); $secret_key = wpforo_setting( 'recaptcha', 'secret_key' ); if( ! is_user_logged_in() && $site_key && $secret_key ) { $wpf_reg_form = wpforo_setting( 'recaptcha', 'wpf_reg_form' ); $wpf_login_form = wpforo_setting( 'recaptcha', 'wpf_login_form' ); $wpf_lostpass_form = wpforo_setting( 'recaptcha', 'wpf_lostpass_form' ); add_filter( 'script_loader_tag', [ &$this, 'rc_enqueue_async' ], 10, 3 ); //Verification Hooks: Login / Register / Reset Pass if( $wpf_login_form ) add_filter( 'wp_authenticate_user', [ $this, 'rc_verify_wp_login' ], 15, 2 ); if( $wpf_reg_form ) add_filter( 'registration_errors', [ $this, 'rc_verify_wp_register' ], 10, 3 ); if( $wpf_lostpass_form ) add_action( 'lostpassword_post', [ $this, 'rc_verify_wp_lostpassword' ], 10 ); //Load reCAPTCHA API on wpForo pages: Login / Register / Reset Pass if( in_array( $template, [ 'login', 'register', 'lostpassword' ], true ) ) { if( $wpf_reg_form || $wpf_login_form || $wpf_lostpass_form ) { add_action( 'wp_enqueue_scripts', [ $this, 'rc_enqueue' ] ); } } //Load reCAPTCHA Widget wpForo forms: Login / Register / Reset Pass if( $wpf_login_form && $template === 'login' ) add_action( 'login_form', [ $this, 'rc_widget' ] ); if( $wpf_reg_form && $template === 'register' ) add_action( 'register_form', [ $this, 'rc_widget' ] ); if( $wpf_lostpass_form && $template === 'lostpassword' ) { add_action( 'lostpassword_form', [ $this, 'rc_widget' ] ); } //Load reCAPTCHA API and Widget for Topic and Post Editor if( in_array( $template, [ 'forum', 'topic', 'post', 'add-topic' ], true ) ) { add_action( 'wp_enqueue_scripts', [ $this, 'rc_enqueue' ] ); add_action( 'wpforo_verify_form_end', [ $this, 'rc_verify' ] ); if( wpforo_setting( 'recaptcha', 'topic_editor' ) ) { add_action( 'wpforo_topic_form_extra_fields_after', [ $this, 'rc_widget' ] ); } if( wpforo_setting( 'recaptcha', 'post_editor' ) ) { add_action( 'wpforo_reply_form_extra_fields_after', [ $this, 'rc_widget' ] ); add_action( 'wpforo_portable_form_extra_fields_after', [ $this, 'rc_widget' ] ); } } } ############################################################################### } private function init_wp_recaptcha() { $template = WPF()->current_object['template']; $site_key = wpforo_setting( 'recaptcha', 'site_key' ); $secret_key = wpforo_setting( 'recaptcha', 'secret_key' ); if( ! is_user_logged_in() && $site_key && $secret_key ) { $reg_form = wpforo_setting( 'recaptcha', 'reg_form' ); $login_form = wpforo_setting( 'recaptcha', 'login_form' ); $lostpass_form = wpforo_setting( 'recaptcha', 'lostpass_form' ); //Verification Hooks: Login / Register / Reset Pass if( $login_form ) add_filter( 'wp_authenticate_user', [ $this, 'rc_verify_wp_login' ], 15, 2 ); if( $reg_form ) add_filter( 'registration_errors', [ $this, 'rc_verify_wp_register' ], 10, 3 ); if( $lostpass_form ) add_action( 'lostpassword_post', [ $this, 'rc_verify_wp_lostpassword' ], 10 ); //Load reCAPTCHA API and Widget on wp-login.php if( $reg_form || $login_form || $lostpass_form ) { add_action( 'login_enqueue_scripts', [ $this, 'rc_enqueue' ] ); add_action( 'login_enqueue_scripts', [ $this, 'rc_enqueue_css' ] ); if( $login_form && $template !== 'login' ) add_action( 'login_form', [ $this, 'rc_widget' ] ); if( $reg_form && $template !== 'register' ) add_action( 'register_form', [ $this, 'rc_widget' ] ); if( $lostpass_form && $template !== 'lostpassword' ) { add_action( 'lostpassword_form', [ $this, 'rc_widget' ] ); } } } } public function local( $api ) { $wplocal = get_locale(); $wplocal_iso = substr( $wplocal, 0, 2 ); if( $api === 'fb' ) { if( in_array( $wplocal, $this->fb_local ) ) { return $wplocal; } else { return $this->locale; } } elseif( $api === 'tw' ) { if( in_array( $wplocal_iso, $this->tw_local ) ) { return $wplocal_iso; } else { return $this->locale_iso; } } elseif( $api === 'vk' ) { return $wplocal_iso; } elseif( $api === 'ok' ) { if( in_array( $wplocal_iso, $this->ok_local ) ) { return $wplocal_iso; } else { return $this->locale_iso; } } return $wplocal; } public function fb_enqueue() { $app_id = wpforo_setting( 'authorization', 'fb_api_id' ); wp_register_script( 'wpforo-snfb', WPFORO_URL . '/assets/js/snfb.js', [ 'jquery' ], WPFORO_VERSION, false ); wp_enqueue_script( 'wpforo-snfb' ); wp_localize_script( 'wpforo-snfb', 'wpforo_fb', [ 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'site_url' => home_url(), 'scopes' => 'email,public_profile', 'appId' => $app_id, 'l18n' => [ 'chrome_ios_alert' => __( 'Please login into Facebook and then click connect button again', 'wpforo' ), ], ] ); } public function fb_auth() { $app_version = 'v2.10'; $app_secret = wpforo_setting( 'authorization', 'fb_api_secret' ); check_ajax_referer( 'wpforo-fb-nonce', 'security' ); $fb_token = isset( $_POST['fb_response']['authResponse']['accessToken'] ) ? $_POST['fb_response']['authResponse']['accessToken'] : ''; $fb_url = add_query_arg( [ 'fields' => 'id,first_name,last_name,email,link,about,locale,birthday', 'access_token' => $fb_token ], 'https://graph.facebook.com/' . $app_version . '/' . $_POST['fb_response']['authResponse']['userID'] ); ################################################################################################################### // Verifying Graph API Calls with appsecret_proof // Graph API calls can be made from clients or from your server on behalf of clients. // Calls from a server can be better secured by adding a parameter called appsecret_proof. // https://developers.facebook.com/docs/graph-api/securing-requests/ if( $app_secret ) { $appsecret_proof = hash_hmac( 'sha256', $fb_token, trim( (string) $app_secret ) ); $fb_url = add_query_arg( [ 'appsecret_proof' => $appsecret_proof ], $fb_url ); } ################################################################################################################### $fb_response = wp_remote_get( esc_url_raw( $fb_url ), [ 'timeout' => 30 ] ); if( is_wp_error( $fb_response ) ) wpforo_ajax_response( [ 'error' => $fb_response->get_error_message() ] ); $fb_user = json_decode( wp_remote_retrieve_body( $fb_response ), true ); if( isset( $fb_user['error'] ) ) { wpforo_ajax_response( [ 'error' => 'Error code: ' . $fb_user['error']['code'] . ' - ' . $fb_user['error']['message'] ] ); } if( empty( $fb_user['email'] ) ) { wpforo_ajax_response( [ 'error' => __( 'Your email is required to be able authorize you here. Please try loging again. ', 'wpforo' ), 'fb' => $fb_user, ] ); } $fb_user['link'] = ( isset( $fb_user['link'] ) ) ? $fb_user['link'] : ''; $fb_user['about'] = ( isset( $fb_user['about'] ) ) ? $fb_user['about'] : ''; $fb_user['locale'] = ( isset( $fb_user['locale'] ) ) ? $fb_user['locale'] : ''; $user = [ 'fb_user_id' => $fb_user['id'], 'first_name' => $fb_user['first_name'], 'last_name' => $fb_user['last_name'], 'user_email' => $fb_user['email'], 'user_url' => $fb_user['link'], 'user_pass' => wp_generate_password(), 'description' => $fb_user['about'], 'locale' => $fb_user['locale'], 'rich_editing' => 'true', ]; $message = [ 'error' => __( 'Invalid User', 'wpforo' ) ]; if( empty( $user['fb_user_id'] ) ) wpforo_ajax_response( $message ); $member = wpforo_get_fb_user( $user ); $meta_updated = false; if( $member ) { $user_id = $member->ID; $message = [ 'success' => $user_id, 'method' => 'login' ]; if( empty( $member->user_email ) ) { wp_update_user( [ 'ID' => $user_id, 'user_email' => $user['user_email'] ] ); } } else { if( ! wpforo_setting( 'authorization', 'user_register' ) ) { wpforo_ajax_response( [ 'error' => __( 'User registration is disabled', 'wpforo' ) ] ); } $username = wpforo_unique_username( $user['user_email'] ); $user['user_login'] = str_replace( '.', '', $username ); $user['user_nicename'] = sanitize_title( $username ); $user['display_name'] = ( $user['first_name'] || $user['last_name'] ) ? trim( $user['first_name'] . ' ' . $user['last_name'] ) : ucfirst( str_replace( '-', ' ', $user['user_nicename'] ) ); $user_id = wp_insert_user( $user ); if( ! is_wp_error( $user_id ) ) { wp_new_user_notification( $user_id, null, 'admin' ); wp_new_user_notification( $user_id, '', 'user' ); update_user_meta( $user_id, '_fb_user_id', $user['fb_user_id'] ); if( isset( $fb_user['birthday'] ) && $fb_user['birthday'] ) { update_user_meta( $user_id, '_fb_user_birthday', $fb_user['birthday'] ); } $meta_updated = true; $message = [ 'success' => $user_id, 'method' => 'registration' ]; } } if( wpforo_is_id( $user_id ) ) { wp_set_auth_cookie( $user_id, true ); if( ! $meta_updated ) update_user_meta( $user_id, '_fb_user_id', $user['fb_user_id'] ); } wpforo_ajax_response( $message ); } public function fb_redirect() { if( wpforo_setting( 'authorization', 'fb_redirect' ) === 'custom' && wpforo_setting( 'authorization', 'fb_redirect_url' ) != '' ) { return esc_url( (string) wpforo_setting( 'authorization', 'fb_redirect_url' ) ); } elseif( wpforo_setting( 'authorization', 'fb_redirect' ) === 'profile' ) { return wpforo_home_url( "account/" ); } else { return wpforo_home_url(); } } public function fb_sdk() { // @todo // FB SDK is disabled temporarily. return false; if( wpforo_setting( 'authorization', 'fb_api_id' ) ) { ?>
public_profile'; ?>