This repository has been archived by the owner on Aug 23, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
flagpole.php
executable file
·340 lines (284 loc) · 9.34 KB
/
flagpole.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
/**
* Flagpole
* Easily register and work with feature flags in your themes.
*
* @package flagpole
* @subpackage flagpole/flagpole
* @author "James Williams <james@jamesrwilliams.ca>"
* @link https://github.com/jamesrwilliams/flagpole
* @copyright 2019-2021 James Williams
*
* @wordpress-plugin
* Plugin Name: Flagpole
* Description: Easily register and work with feature flags in your theme.
* Version: 0.1.3-beta
* Author: James Williams
* Requires PHP: 5.6
* Text Domain: flagpole
* Author URI: https://jamesrwilliams.ca/
*/
// If this file is called directly abort.
if ( ! defined( 'WPINC' ) ) {
wp_die();
}
use Flagpole\Flagpole;
// Define plugin paths and url for global usage.
define( 'FLAGPOLE_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
define( 'FLAGPOLE_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'FLAGPOLE_VERSION', '0.1.0' );
// Register admin page.
add_action(
'admin_init',
function () {
register_setting(
'ff-settings-page',
'ff_client_secret',
function ( $posted_data ) {
if ( ! $posted_data ) {
add_settings_error( 'ff_client_secret', 'ff_updated', 'Error Message', 'error' );
return false;
}
return $posted_data;
}
);
}
);
/**
* Plugin styles and scripts.
*
* @param string $hook The Hook string.
*/
function flagpole_admin_imports( $hook ) {
if ( 'tools_page_flagpole' !== $hook ) {
return;
}
wp_enqueue_style(
'flagpole-styles',
plugins_url(
'/assets/css/flagpole.css',
__FILE__
),
array(),
FLAGPOLE_VERSION
);
wp_register_script(
'flagpole-script',
plugins_url(
'/assets/js/flagpole.js',
__FILE__
),
array(),
FLAGPOLE_VERSION,
false
);
wp_localize_script(
'flagpole-script',
'ffwp',
array(
'ajax_nonce' => wp_create_nonce( 'featureFlagNonce' ),
)
);
wp_enqueue_script( 'flagpole-script' );
}
add_action( 'admin_enqueue_scripts', 'flagpole_admin_imports' );
// Includes.
require plugin_dir_path( __FILE__ ) . 'includes/class-flagpole.php';
require plugin_dir_path( __FILE__ ) . 'includes/admin/settings-page.php';
require plugin_dir_path( __FILE__ ) . 'includes/api/api.general.php';
require plugin_dir_path( __FILE__ ) . 'includes/api/api.shortcode.php';
/**
* AJAX Action toggling features from the WP admin area.
*/
add_action( 'wp_ajax_toggleFeatureFlag', 'flagpole_enable' );
/**
* Enable a feature Flag
*/
function flagpole_enable() {
if (
isset( $_POST['featureKey'] ) &&
check_ajax_referer( 'featureFlagNonce', 'security' )
) {
$response = array();
$feature_key = sanitize_text_field( wp_unslash( $_POST['featureKey'] ) );
if ( ! empty( $feature_key ) ) {
$response['response'] = $feature_key;
Flagpole::init()->toggle_feature_preview( $feature_key );
} else {
header( 'HTTP/1.1 500 Internal Server Error' );
$response['response'] = 'no feature key';
}
}
exit();
}
/**
* AJAX Action toggling features from the WP admin area.
*/
add_action( 'wp_ajax_togglePublishedFeature', 'flagpole_flag_publish' );
/**
* Publish a feature flag to be publicly visible.
*/
function flagpole_flag_publish() {
if ( isset( $_POST['featureKey'] ) && check_ajax_referer( 'featureFlagNonce', 'security' ) ) {
$response = array();
$feature_key = sanitize_text_field( wp_unslash( $_POST['featureKey'] ) );
if ( ! empty( $feature_key ) ) {
$response['response'] = Flagpole::init()->toggle_feature_publication( $feature_key );
} else {
header( 'HTTP/1.1 500 Internal Server Error' );
$response['response'] = 'no feature key';
}
header( 'Content-Type: application/json' );
echo wp_json_encode( $response );
}
exit();
}
// Groups - Create.
add_action( 'admin_post_ff_register_group', 'flagpole_create_group' );
add_action( 'admin_post_nopriv_ff_register_group', 'flagpole_create_group' );
/**
* Create group admin hook handler.
*/
function flagpole_create_group() {
$validation = array();
$validation['group-nonce'] = check_admin_referer( 'register-group' );
$validation['group-key'] = ( ! empty( $_GET['group-key'] ) ? sanitize_text_field( wp_unslash( $_GET['group-key'] ) ) : false );
$validation['group-name'] = ( ! empty( $_GET['group-name'] ) ? sanitize_text_field( wp_unslash( $_GET['group-name'] ) ) : false );
$validation['group-desc'] = ( ! empty( $_GET['group-description'] ) ? sanitize_textarea_field( wp_unslash( $_GET['group-description'] ) ) : '' );
$validation['group-private'] = ( ! empty( $_GET['group-private'] ) ? sanitize_textarea_field( wp_unslash( $_GET['group-private'] ) ) : false );
$validation = array_filter( $validation );
if ( $validation ) {
$result = Flagpole::init()->create_group( $validation['group-key'], $validation['group-name'],
$validation['group-desc'], $validation['group-private'] );
flagpole_operation_redirect( $result );
}
}
// Groups - Delete.
add_action( 'admin_post_ff_delete_group', 'flagpole_delete_group' );
add_action( 'admin_post_nopriv_ff_delete_group', 'flagpole_delete_group' );
/**
* Delete group admin hook handler.
*/
function flagpole_delete_group() {
if ( ! empty( $_GET['key'] ) && check_admin_referer( 'ff_delete_group' ) ) {
$key = sanitize_text_field( wp_unslash( $_GET['key'] ) );
} else {
$key = false;
}
$result = Flagpole::init()->delete_group( $key );
flagpole_operation_redirect( $result );
}
add_action( 'admin_post_ff_preview_group', 'flagpole_toggle_group_preview' );
add_action( 'admin_post_nopriv_ff_preview_group', 'flagpole_toggle_group_preview' );
/**
* Toggle group preview handler.
*/
function flagpole_toggle_group_preview() {
if ( ! empty( $_GET['group_key'] ) && check_admin_referer( 'ff_preview_group' ) ) {
$group_key = sanitize_text_field( wp_unslash( $_GET['group_key'] ) );
} else {
$group_key = false;
}
$result = Flagpole::init()->toggle_group_preview( $group_key );
flagpole_operation_redirect( $result );
}
// Groups - Add too group.
add_action( 'admin_post_ff_add_to_group', 'flagpole_add_to_group' );
add_action( 'admin_post_nopriv_ff_add_to_group', 'flagpole_add_to_group' );
/**
* Add to group admin hook handler.
*/
function flagpole_add_to_group() {
if (
! empty( $_GET['selected-flag'] ) &&
! empty( $_GET['selected-group'] ) &&
check_admin_referer( 'ff-add-to-group' )
) {
$flag = sanitize_text_field( wp_unslash( $_GET['selected-flag'] ) );
$group = sanitize_text_field( wp_unslash( $_GET['selected-group'] ) );
$response = Flagpole::init()->add_flag_to_group( $flag, $group );
flagpole_operation_redirect( $response );
} else {
flagpole_operation_redirect( 'fgae' );
}
}
add_action( 'admin_post_ff_remove_flag_from_group', 'flagpole_remove_from_group' );
add_action( 'admin_post_nopriv_remove_flag_from_group', 'flagpole_remove_from_group' );
/**
* Admin Post handler for removing a flag from a group.
*/
function flagpole_remove_from_group() {
if (
! empty( $_GET['flag'] ) &&
! empty( $_GET['group'] ) &&
check_admin_referer( 'ff_remove_flag_from_group' )
) {
$flag = sanitize_text_field( wp_unslash( $_GET['flag'] ) );
$group = sanitize_text_field( wp_unslash( $_GET['group'] ) );
$response = Flagpole::init()->remove_flag_from_group( $flag, $group );
flagpole_operation_redirect( $response );
} else {
flagpole_operation_redirect( 'fgae' );
}
}
add_action( 'template_redirect', 'flagpole_redirect_with_key' );
/**
* Redirect the user to the login form if they attempt to use a
* flag query string while logged out.
*/
function flagpole_redirect_with_key() {
$query = flagpole_find_query_string();
if (
isset( $_SERVER['REQUEST_URI'] ) &&
isset( $_SERVER['HTTP_HOST'] ) &&
! empty( $query ) && ! is_user_logged_in()
) {
if ( Flagpole::init()->is_private( $query ) ) {
$destination = esc_url_raw( wp_unslash( $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'] ) );
if ( filter_var( $destination, FILTER_VALIDATE_URL ) ) {
wp_safe_redirect( wp_login_url( $destination ) );
exit();
}
}
}
}
/**
* Check if there is a group query string.
*
* @return bool|string False if there isn't one, the group key if found.
*/
function flagpole_find_query_string() {
/* TODO: #21 - Make this a configurable key */
$query_string_key = 'group';
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET[ $query_string_key ] ) && '' !== $_GET[ $query_string_key ] ) {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
return sanitize_title( wp_unslash( $_GET[ $query_string_key ] ) );
} else {
return false;
}
}
/**
* Quickly generate return URLs with appropriate Error code messages.
*
* @param bool $error_code The error code string.
* @param bool $redirect Either safe_redirect or return the URL.
*
* @return null|string Depending on $redirect.
* @see FeatureFlags::get_admin_error_message() Method for generating error messages.
*/
function flagpole_operation_redirect( $error_code = false, $redirect = true ) {
$redirect_url = ( wp_get_referer() ? wp_get_referer() : get_home_url() );
if ( false !== $error_code && ! empty( $error_code ) ) {
$redirect_url = add_query_arg(
array(
'error' => $error_code,
),
$redirect_url
);
}
return ( $redirect ? wp_safe_redirect( $redirect_url ) : $redirect_url );
}
add_shortcode( 'debugFlagpole_flags', 'flagpole_shortcode_debug_flags' );
add_shortcode( 'debugFlagpole_groups', 'flagpole_shortcode_debug_groups' );
add_shortcode( 'debugFlagpole_db', 'flagpole_shortcode_debug_db' );