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
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95b8a16
commit 2990ec4
Showing
3 changed files
with
91 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php | ||
/** | ||
* JavaScript Class | ||
* | ||
* Inserts feature flag object and a corresponding flagpole_flag_enabled function into the <head>. | ||
* | ||
* @package flagpole | ||
*/ | ||
|
||
namespace Flagpole; | ||
|
||
/** | ||
* Class Flagpole_JavaScript. | ||
* | ||
* @package Peake\Plugins | ||
*/ | ||
class JavaScript { | ||
/** | ||
* Initialise filters. | ||
*/ | ||
public function init() { | ||
add_action( 'wp_head', array( $this, 'print_flagpole_js' ) ); | ||
} | ||
|
||
/** | ||
* Reduces the full array of flagpole flag objects into an array of enabled array keys. | ||
* Returns an empty array if the 'flagpole_flag_enabled' PHP function is not present. | ||
* | ||
* @param array $flag_list The defined flags. | ||
* @return array The keys of enabled flags. | ||
*/ | ||
private function enabled_flag_filter( array $flag_list ) { | ||
$filtered_list = array(); | ||
|
||
foreach ( $flag_list as $flag ) { | ||
if ( flagpole_flag_enabled( $flag->key ) ) { | ||
$filtered_list[] = $flag->key; | ||
} | ||
} | ||
|
||
return $filtered_list; | ||
} | ||
|
||
/** | ||
* Prints a flagpole_flag_enabled function. | ||
* The function returns 'true' if the flag is in our list of enabled flags. | ||
* Otherwise, it returns false. | ||
* | ||
* @return void | ||
*/ | ||
public function print_flagpole_js() { | ||
$flagpole_available_flags = self::enabled_flag_filter( Flagpole::init()->get_flags() ); | ||
?> | ||
<script> | ||
var flagpole_flag_enabled = function( ff ) { | ||
const flist = <?php echo wp_json_encode( $flagpole_available_flags ); ?>; | ||
return flist.indexOf( ff ) !== -1; | ||
} | ||
</script> | ||
<?php | ||
} | ||
} |