-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdlx-pmpro-turnstile.php
207 lines (182 loc) · 4.9 KB
/
dlx-pmpro-turnstile.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
/**
* Plugin Name: Paid Memberships Pro - Add Cloudflare Turnstile
* Plugin URI: https://dlxplugins.com/plugins/pmpro-turnstile/
* Description: Add Cloudflare Turnstile to your PMPro login and checkout forms.
* Version: 1.0.7
* Requires at least: 6.1
* Requires PHP: 7.2
* Author: DLX Plugins
* Author URI: https://dlxplugins.com/plugins/pmpro-turnstile/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: alerts-dlx
* Domain Path: /languages
*
* @package PMProTurnstile
*/
namespace DLXPlugins\PMProTurnstile;
define( 'DLX_PMPRO_TURNSTILE_VERSION', '1.0.7' );
define( 'DLX_PMPRO_TURNSTILE_FILE', __FILE__ );
define( 'DLX_PMPRO_TURNSTILE_PRODUCT_ID', 36132 );
// Support for site-level autoloading.
if ( file_exists( __DIR__ . '/lib/autoload.php' ) ) {
require_once __DIR__ . '/lib/autoload.php';
}
/**
* Turnstile class.
*/
class PMProTurnstile {
/**
* Holds the class instance.
*
* @var PMProTurnstile $instance
*/
private static $instance = null;
/**
* Return an instance of the class
*
* Return an instance of the ReflectorDLX Class.
*
* @since 1.0.0
*
* @return PMProTurnstile class instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Class initializer.
*/
public function plugins_loaded() {
load_plugin_textdomain(
'dlx-pmpro-turnstile',
false,
basename( __DIR__ ) . '/languages'
);
// Enqueue cloudflare scripts.
$enqueue = new Enqueue();
$enqueue->run();
// Set up Turnstile interface and actions.
$turnstile = new Turnstile();
$turnstile->run();
// Set up the admin and options.
$admin = new Admin();
$admin->run();
// Set up admin bar.
$admin_bar = new Admin_Bar();
$admin_bar->run();
/**
* When Turnstile can be extended.
*
* Filter when Turnstile can be extended.
*
* @since 1.0.0
*/
do_action( 'dlx_pmpro_turnstile_loaded' );
}
/**
* Init all the things.
*/
public function login_init() {
// We're only here for the login portion.
$turnstile = new Turnstile();
$turnstile->run();
}
}
/**
* Start your engines!
*/
add_action(
'plugins_loaded',
function () {
// Check for pmpro activation.
if ( ! Functions::is_activated( 'paid-memberships-pro/paid-memberships-pro.php' ) && ! function_exists( 'pmpro_getOption' ) ) {
return;
}
// Begin loading in the plugin.
$pmpro_turnstile = PMPROTurnstile::get_instance();
$pmpro_turnstile->plugins_loaded();
}
);
/**
* For hooking into the default wp login screen (non-pmpro).
*/
add_action(
'login_init',
function () {
$pmpro_turnstile = PMPROTurnstile::get_instance();
$pmpro_turnstile->login_init();
}
);
/* Setup plugin activation and redirection */
register_activation_hook( __FILE__, __NAMESPACE__ . '\on_plugin_activation' );
add_action( 'admin_init', __NAMESPACE__ . '\activate_redirect' );
/**
* Determine if a user can be redirected or not.
*
* @return true if the user can be redirected. false if not.
*/
function can_redirect_on_activation() {
/**
* Filter whether to redirect on plugin activation.
*
* @param bool $can_redirect Whether to redirect on plugin activation. Pass `false` to prevent redirect.
*/
$can_redirect = apply_filters( 'dlxplugins/pmproturnstile/can_redirect', true );
if ( false === $can_redirect ) {
return false;
}
// If plugin is activated in network admin options, skip redirect.
if ( is_network_admin() ) {
return false;
}
// Skip redirect if WP_DEBUG is enabled.
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
return false;
}
// Check for dev mode.
if ( function_exists( 'wp_get_development_mode' ) ) {
if ( ! empty( wp_get_development_mode() ) ) {
return false;
}
}
// Determine if multi-activation is enabled.
$maybe_multi = filter_input( INPUT_GET, 'activate-multi', FILTER_VALIDATE_BOOLEAN );
if ( $maybe_multi ) {
return false;
}
// See if cloudflare keys are already set.
$options = Options::get_options();
$site_key = $options['siteKey'];
$secret_key = $options['secretKey'];
if ( ! empty( $site_key ) && ! empty( $secret_key ) ) {
return false;
}
// All is well. Can redirect.
return true;
}
/**
* Callback when the plugin is activated.
*/
function on_plugin_activation() {
if ( can_redirect_on_activation() ) {
// Add option for whether to redirect.
add_option( 'dlx_pmpro_turnstile_redirect', sanitize_text_field( __FILE__ ) );
}
}
/**
* Redirect in the admin upon plugin activation.
*/
function activate_redirect() {
if ( can_redirect_on_activation() && is_admin() ) {
if ( __FILE__ === get_option( 'dlx_pmpro_turnstile_redirect' ) ) {
delete_option( 'dlx_pmpro_turnstile_redirect' );
wp_safe_redirect( esc_url_raw( add_query_arg( array( 'first_time_install' => true ), Functions::get_settings_url() ) ) );
exit;
}
}
}