Skip to content
This repository was archived by the owner on Aug 23, 2022. It is now read-only.

Commit 577e14c

Browse files
author
Jonny Bull
committed
Add JavaScript flag support
1 parent 95b8a16 commit 577e14c

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,25 @@ if ( flagpole_flag_enabled( 'flag_key' ) ) {
6666

6767
Replace `flag_key` with the key used in the register function to check if it is enabled.
6868

69+
It is also possible to use the same function in JavaScript and get the same results:
70+
71+
```javascript
72+
if ( flagpole_flag_enabled( 'flag_key' ) ) {
73+
/* flag_key is enabled! */
74+
}
75+
```
76+
77+
However, checking the Flagpole function exists before expecting it to return a value is recommended. This prevents JavaScript files from breaking if the plugin is not enabled on your project.
78+
79+
```javascript
80+
const flagEnabled = typeof( flagpole_flag_enabled ) === 'function' && flagpole_flag_enabled( 'flag_key' );
81+
82+
if ( flagEnabled ) {
83+
/* flag_key is enabled! */
84+
}
85+
```
86+
87+
6988
#### Flag options/arguments
7089

7190

flagpole.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
}
2626

2727
use Flagpole\Flagpole;
28+
use Flagpole\JavaScript;
2829

2930
// Define plugin paths and url for global usage.
3031
define( 'FLAGPOLE_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
@@ -99,6 +100,7 @@ function flagpole_admin_imports( $hook ) {
99100
require plugin_dir_path( __FILE__ ) . 'includes/admin/settings-page.php';
100101
require plugin_dir_path( __FILE__ ) . 'includes/api/api.general.php';
101102
require plugin_dir_path( __FILE__ ) . 'includes/api/api.shortcode.php';
103+
require plugin_dir_path( __FILE__ ) . 'includes/javascript/class-javascript.php';
102104

103105
/**
104106
* AJAX Action toggling features from the WP admin area.
@@ -176,8 +178,12 @@ function flagpole_create_group() {
176178
$validation = array_filter( $validation );
177179

178180
if ( $validation ) {
179-
$result = Flagpole::init()->create_group( $validation['group-key'], $validation['group-name'],
180-
$validation['group-desc'], $validation['group-private'] );
181+
$result = Flagpole::init()->create_group(
182+
$validation['group-key'],
183+
$validation['group-name'],
184+
$validation['group-desc'],
185+
$validation['group-private']
186+
);
181187

182188
flagpole_operation_redirect( $result );
183189
}
@@ -338,3 +344,5 @@ function flagpole_operation_redirect( $error_code = false, $redirect = true ) {
338344
add_shortcode( 'debugFlagpole_flags', 'flagpole_shortcode_debug_flags' );
339345
add_shortcode( 'debugFlagpole_groups', 'flagpole_shortcode_debug_groups' );
340346
add_shortcode( 'debugFlagpole_db', 'flagpole_shortcode_debug_db' );
347+
348+
( new JavaScript() )->init();
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
/**
3+
* JavaScript Class
4+
*
5+
* Inserts feature flag object and a corresponding flagpole_flag_enabled function into the <head>.
6+
*
7+
* @package flagpole
8+
*/
9+
10+
namespace Flagpole;
11+
12+
/**
13+
* Class Flagpole_JavaScript.
14+
*
15+
* @package Peake\Plugins
16+
*/
17+
class JavaScript {
18+
/**
19+
* Initialise filters.
20+
*/
21+
public function init(): void {
22+
add_action( 'wp_head', array( $this, 'print_flagpole_js' ) );
23+
}
24+
25+
/**
26+
* Reduces the full array of flagpole flag objects into an array of enabled array keys.
27+
* Returns an empty array if the 'flagpole_flag_enabled' PHP function is not present.
28+
*
29+
* @param array $flag_list The defined flags.
30+
* @return array The keys of enabled flags.
31+
*/
32+
private function enabled_flag_filter( array $flag_list ): array {
33+
$filtered_list = array();
34+
35+
foreach ( $flag_list as $flag ) {
36+
if ( flagpole_flag_enabled( $flag->key ) ) {
37+
$filtered_list[] = $flag->key;
38+
}
39+
}
40+
41+
return $filtered_list;
42+
}
43+
44+
/**
45+
* Prints a flagpole_flag_enabled function.
46+
* The function returns 'true' if the flag is in our list of enabled flags.
47+
* Otherwise, it returns false.
48+
*
49+
* @return void
50+
*/
51+
public function print_flagpole_js(): void {
52+
$flagpole_available_flags = self::enabled_flag_filter( Flagpole::init()->get_flags() );
53+
?>
54+
<script>
55+
var flagpole_flag_enabled = function( ff ) {
56+
const flist = <?php echo wp_json_encode( $flagpole_available_flags ); ?>;
57+
return flist.indexOf( ff ) !== -1;
58+
}
59+
</script>
60+
<?php
61+
}
62+
}

0 commit comments

Comments
 (0)