PluginProbe
Faust.js / 1.0.2
Faust.js v1.0.2
1.8.12 1.8.11 1.4.1 1.8.0 1.8.8 1.8.9 trunk 0.7.0 0.7.1 0.7.10 0.7.11 0.7.3 0.7.4 0.7.5 0.7.6 0.7.7 0.7.8 0.7.9 0.8.0 0.8.1 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 All 40 releases
faustwp / includes / auth / callbacks.php

callbacks.php in Faust.js 1.0.2, at includes/auth/callbacks.php

80 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Redirect related callbacks.
4 *
5 * @package FaustWP
6 */
7
8 namespace WPE\FaustWP\Auth;
9
10 use function WPE\FaustWP\Settings\faustwp_get_setting;
11
12 if ( ! defined( 'ABSPATH' ) ) {
13 exit;
14 }
15
16 add_action( 'parse_request', __NAMESPACE__ . '\\handle_generate_endpoint' );
17 /**
18 * Callback for WordPress 'parse_request' action.
19 *
20 * Generate an authorization code and redirect to the requested url.
21 *
22 * @return void
23 */
24 function handle_generate_endpoint() {
25 $search_pattern = ':^' . site_url( '/generate', 'relative' ) . ':';
26
27 if ( ! preg_match( $search_pattern, $_SERVER['REQUEST_URI'] ) ) { // phpcs:ignore WordPress.Security
28 return;
29 }
30
31 if ( empty( $_GET['redirect_uri'] ) ) { // phpcs:ignore WordPress.Security
32 return;
33 }
34
35 $redirect_uri = wp_unslash( $_GET['redirect_uri'] ); // phpcs:ignore WordPress.Security
36
37 if ( ! is_user_logged_in() ) {
38 wp_safe_redirect(
39 wp_login_url( '/generate/?redirect_uri=' . rawurlencode( $redirect_uri ) )
40 );
41
42 exit;
43 }
44
45 $auth_code = generate_authorization_code(
46 wp_get_current_user(),
47 MINUTE_IN_SECONDS * 1
48 );
49
50 $redirect_uri = add_query_arg( 'code', rawurlencode( $auth_code ), $redirect_uri );
51
52 wp_safe_redirect( $redirect_uri );
53
54 exit;
55 }
56
57 add_filter( 'allowed_redirect_hosts', __NAMESPACE__ . '\\allowed_redirect_hosts', 10, 2 );
58 /**
59 * Callback for WordPress 'allowed_redirect_hosts' filter.
60 *
61 * Add frontend_uri host and development domains to allowed redirects.
62 *
63 * @link https://developer.wordpress.org/reference/hooks/allowed_redirect_hosts/
64 *
65 * @param string[] $hosts An array of allowed host names.
66 * @param string $host The host name of the redirect destination; empty string if not set.
67 *
68 * @return string[] An array of allowed host names.
69 */
70 function allowed_redirect_hosts( $hosts, $host ) {
71 $hosts = wp_parse_args( $hosts, array( 'localhost', '0.0.0.0' ) );
72 $frontend_host = wp_parse_url( faustwp_get_setting( 'frontend_uri' ), PHP_URL_HOST );
73
74 if ( $frontend_host ) {
75 $hosts[] = $frontend_host;
76 }
77
78 return $hosts;
79 }
80