-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathheadless-mode.php
136 lines (116 loc) · 4.04 KB
/
headless-mode.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
<?php
/*
* Plugin Name: Headless Mode
* Plugin URI:
* Description: This plugin disables access to the front end of your site unless the logged-in user can edit posts. It also automatically accepts requests to REST API or WP_GRAPHQL endpoints.
* Version: 0.4.0
* Author: Josh Pollock, Jason Bahl, and Ben Meredith
* Author URI: https://github.com/Shelob9/headless-mode
* License: GPL V2
* Text Domain: headless-mode
*
*/
if( ! defined( 'HEADLESS_MODE_CLIENT_URL' ) ) {
define( 'HEADLESS_MODE_CLIENT_URL', 'https://hiroy.club' );
};
/**
* Creates a simple settings page to display the constant name and instruct users on how to change it.
*
*/
add_action( 'admin_menu', 'headless_mode_settings' );
function headless_mode_settings() {
add_submenu_page(
'options-general.php',
__( 'Headless Mode set up', 'headless-mode' ),
__( 'Headless Mode', 'headless-mode' ),
'manage_options',
'headless-mode',
'headless_mode_settings_output'
);
}
function headless_mode_settings_output() {
$clientUrl = HEADLESS_MODE_CLIENT_URL !== 'https://hiroy.club' ? HEADLESS_MODE_CLIENT_URL : false;
?>
<div class="wrap">
<h2>
<?php _e( 'Headless Mode', 'headless-mode' ); ?>
</h2>
<p> <?php _e( 'Your site is currently set to redirect to:', 'headless-mode'); ?>
<p> <code><?php
if( $clientUrl ){
echo esc_url($clientUrl);
}else {
echo __( 'Your site is not redirecting.', 'headless-mode' );
} ?></code>
<p> <?php _e( 'Add the following to your wp-config.php file to redirect all traffic to the new front end of the site (change the URL before pasting!):', 'headless-mode' ); ?>
<p> <code> define( 'HEADLESS_MODE_CLIENT_URL', 'https://hiroy.club' );</code></p>
<p> <em> <?php _e( 'If after saving the wp-config.php file, your site is still not redirecting, make sure you\'ve replaced <code>https://hiroy.club</code> above with your front end web address.', 'headless-mode' ); ?> </em></p>
</div>
<?php
}
/**
*
* @see https://stackoverflow.com/a/768472/1469799
*
* @param $url
* @param bool $permanent
*/
function headless_mode_redirect($url, $permanent = false)
{
if ( HEADLESS_MODE_CLIENT_URL ==='https://hiroy.club' ){
return;
}
header('Location: ' . $url, true, $permanent ? 301 : 302);
exit();
}
/**
* Based on https://gist.github.com/jasonbahl/5dd6c046cd5a5d39bda9eaaf7e32a09d
*/
add_action( 'parse_request', 'headless_mode_disable_front_end', 99 );
function headless_mode_disable_front_end() {
/**
* Filters whether the current user has access to the front-end.
*
* By default, the front-end is disabled if the user doesn't
* have the capability to "edit_posts".
*
* Return true if you want the front-end to be disabled and
* the current user to be redirected to headless mode.
*
* @param bool $disabled True if the current user doesn't have the capability to "edit_posts".
*/
$disable_front_end = apply_filters( 'headless_mode_disable_front_end', ! current_user_can( 'edit_posts' ) );
if ( false === $disable_front_end ) {
return;
}
global $wp;
/**
* If the request is not part of a CRON, REST Request, GraphQL Request or Admin request,
* output some basic, blank markup
*/
if (
! defined( 'DOING_CRON' ) &&
! defined( 'REST_REQUEST' ) &&
// prevents the case of a new user activating the plugin but not yet setting the constant. Added in 0.3.0
HEADLESS_MODE_CLIENT_URL !== 'https://hiroy.club' &&
! is_admin() &&
(
empty( $wp->query_vars['rest_oauth1'] ) &&
! defined( 'GRAPHQL_HTTP_REQUEST' )
)
) {
// adds the rest of the request to the new URL.
$new_url = trailingslashit( HEADLESS_MODE_CLIENT_URL ) . $wp->request;
/**
* We should redirect. But will we?
*
* This filter allows you to do something else, when a redirect normally would occur.
*
* @param bool $will_redirct If truthy redirect will happen. If not, it will not. Do your own thing. Be you.
*/
if( apply_filters( 'headless_mode_will_redirect', true, $new_url ) ){
headless_mode_redirect( $new_url, true );
exit;
}
}
}