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

Commit a8277cb

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

3 files changed

Lines changed: 92 additions & 2 deletions

File tree

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: 11 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__ ) );
@@ -100,6 +101,8 @@ function flagpole_admin_imports( $hook ) {
100101
require plugin_dir_path( __FILE__ ) . 'includes/api/api.general.php';
101102
require plugin_dir_path( __FILE__ ) . 'includes/api/api.shortcode.php';
102103

104+
require_once( plugin_dir_path( __FILE__ ) . 'includes/javascript/class-javascript.php' );
105+
103106
/**
104107
* AJAX Action toggling features from the WP admin area.
105108
*/
@@ -176,8 +179,12 @@ function flagpole_create_group() {
176179
$validation = array_filter( $validation );
177180

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

182189
flagpole_operation_redirect( $result );
183190
}
@@ -338,3 +345,5 @@ function flagpole_operation_redirect( $error_code = false, $redirect = true ) {
338345
add_shortcode( 'debugFlagpole_flags', 'flagpole_shortcode_debug_flags' );
339346
add_shortcode( 'debugFlagpole_groups', 'flagpole_shortcode_debug_groups' );
340347
add_shortcode( 'debugFlagpole_db', 'flagpole_shortcode_debug_db' );
348+
349+
( 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 FeatureFlags
16+
*/
17+
class JavaScript {
18+
/**
19+
* Initialise filters.
20+
*/
21+
public function init() {
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 ) {
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() {
52+
$available_flags = self::enabled_flag_filter( Flagpole::init()->get_flags() );
53+
?>
54+
<script>
55+
var flagpole_flag_enabled = function( ff ) {
56+
const flist = <?= wp_json_encode( $available_flags ); ?>;
57+
return flist.indexOf( ff ) !== -1;
58+
}
59+
</script>
60+
<?php
61+
}
62+
}

0 commit comments

Comments
 (0)