Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
120 changes: 120 additions & 0 deletions includes/Api/FlagOptions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php
/**
* API class for feature flags options
*
* @package mr-feature-flags
* @since 1.0.0
*/

declare(strict_types=1);

namespace MR\FeatureFlags\Api;

/**
* Class Settings
*
* @package mr-feature-flags
* @since 1.0.0
*/
class FlagOptions {

/**
* Name in options table.
*
* @var string $option_name
*/
public static $option_name = 'mr_feature_flags';

/**
* Register feature flag endpoints.
*
* @return void
* @since 1.0.0
*/
public function register_flags_endpoints() {
add_action(
'rest_api_init',
function () {
register_rest_route(
'feature-flags/v1',
'flags',
[
[
'methods' => \WP_REST_SERVER::READABLE,
'callback' => [ $this, 'get_all_flags' ],
'permission_callback' => '__return_true',
],
[
'methods' => \WP_REST_SERVER::EDITABLE,
'callback' => [ $this, 'post_flags' ],
'permission_callback' => '__return_true',
],
]
);

}
);
}

/**
* Get all flags from options
*
* @return mixed List of flags.
*/
public function get_all_flags() {
$flags = get_option( self::$option_name );

if ( empty( $flags ) ) {
return new \WP_Error( 'no_flags', 'Flags not found', array( 'status' => 404 ) );
}

return rest_ensure_response( $flags );
}

/**
* Insert / Update flags in options table.
*
* @return mixed List of flags.
*/
public function post_flags( $request ) {
$flags = $request->get_json_params();

if ( is_array( $flags ) ) {
$result = update_option( self::$option_name, $flags );
return rest_ensure_response(
array(
'status' => 200,
'success' => true,
),
);

} else {
return new \WP_Error( 'invalid_input', 'Cannot update flags', array( 'status' => 400 ) );
}
}

/**
* Register settings action method.
*
* @return void
* @since 1.0.0
*/
public function register_settings() {

add_menu_page(
'Feature Flags',
'Feature Flags',
'manage_options',
'mr-feature-flags',
[ $this, 'render_page' ],
'data:image/svg+xml;base64,' . base64_encode( '<svg width="15" height="18" viewBox="0 0 2000 1792" xmlns="http://www.w3.org/2000/svg"><path fill="black" d="M0 896q0-130 51-248.5t136.5-204 204-136.5 248.5-51h768q130 0 248.5 51t204 136.5 136.5 204 51 248.5-51 248.5-136.5 204-204 136.5-248.5 51h-768q-130 0-248.5-51t-204-136.5-136.5-204-51-248.5zm1408 512q104 0 198.5-40.5t163.5-109.5 109.5-163.5 40.5-198.5-40.5-198.5-109.5-163.5-163.5-109.5-198.5-40.5-198.5 40.5-163.5 109.5-109.5 163.5-40.5 198.5 40.5 198.5 109.5 163.5 163.5 109.5 198.5 40.5z"/></svg>' )
);
}

/**
* Render page
*/
public function render_page() {
echo '<div id="mr_feature_flags_settings_screen"></div>';
}
}
11 changes: 10 additions & 1 deletion includes/FeatureFlags.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,18 @@ public static function add_flag( string $flag ): bool {
throw new \Error( "Flag \"{$flag}\" already exists" );
}

$flag_key = 1;

if ( is_array( $flags ) && count( $flags ) ) {
$flag_key = count( $flags ) + 1;
}

// $flag_key = count( $flags ) ? count( $flags ) + 1 : 1;
// ddd( $flag_key );
$new_flag = [
'id' => $flag_key,
'name' => $flag,
'enabled' => true,
'enabled' => false,
];

if ( $flags ) {
Expand Down
55 changes: 55 additions & 0 deletions includes/Settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Admin setting page for feature flags
*
* @package mr-feature-flags
* @since 1.0.0
*/

declare(strict_types=1);

namespace MR\FeatureFlags;

/**
* Class Settings
*
* @package mr-feature-flags
* @since 1.0.0
*/
class Settings {

/**
* Register feature flag settings page.
*
* @return void
* @since 1.0.0
*/
public function register_feature_settings() {
add_action( 'admin_menu', [ $this, 'register_settings' ] );
}

/**
* Register settings action method.
*
* @return void
* @since 1.0.0
*/
public function register_settings() {

add_menu_page(
'Feature Flags',
'Feature Flags',
'manage_options',
'mr-feature-flags',
[ $this, 'render_page' ],
'data:image/svg+xml;base64,' . base64_encode( '<svg width="15" height="18" viewBox="0 0 2000 1792" xmlns="http://www.w3.org/2000/svg"><path fill="black" d="M0 896q0-130 51-248.5t136.5-204 204-136.5 248.5-51h768q130 0 248.5 51t204 136.5 136.5 204 51 248.5-51 248.5-136.5 204-204 136.5-248.5 51h-768q-130 0-248.5-51t-204-136.5-136.5-204-51-248.5zm1408 512q104 0 198.5-40.5t163.5-109.5 109.5-163.5 40.5-198.5-40.5-198.5-109.5-163.5-163.5-109.5-198.5-40.5-198.5 40.5-163.5 109.5-109.5 163.5-40.5 198.5 40.5 198.5 109.5 163.5 163.5 109.5 198.5 40.5z"/></svg>' )
);
}

/**
* Render page
*/
public function render_page() {
echo '<div id="mr_feature_flags_settings_screen"></div>';
}
}
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
"author": "Mohan Raj <https://mohanraj.dev>",
"license": "ISC",
"dependencies": {
"@types/wordpress__components": "^23.0.1",
"@wordpress/api-fetch": "^6.26.0",
"@wordpress/components": "^23.6.0",
"@wordpress/data": "^8.6.0",
"@wordpress/i18n": "^4.29.0",
"@wordpress/notices": "^3.29.0",
"ts-loader": "^9.4.2",
"typescript": "^5.0.2"
}
Expand Down
101 changes: 88 additions & 13 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
declare( strict_types = 1 );

namespace MR\FeatureFlags;
use MR\FeatureFlags\Api\FlagOptions;

// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
Expand Down Expand Up @@ -49,12 +50,6 @@ function(): void {
true
);

wp_enqueue_style(
'mr-feature-flags-style',
$plugin_url . 'build/index.css',
[],
$script_asset_file['version']
);

wp_localize_script(
'mr-feature-flags-script',
Expand All @@ -69,7 +64,52 @@ function(): void {

add_action(
'admin_enqueue_scripts',
function(): void {
function( string $page ): void {
if ( 'toplevel_page_mr-feature-flags' === $page ) {
load_settings_scripts();
}
}
);

/**
* Load settings page assets
*
* @return void
*/
function load_settings_scripts(): void {
$plugin_url = plugin_dir_url( MR_FEATURE_FLAGS_PLUGIN_PATH );
$settings_asset_file = require_once plugin_dir_path( MR_FEATURE_FLAGS_PLUGIN_PATH ) . 'build/settings.asset.php'; // @phpcs:ignore

wp_enqueue_script(
'mr-feature-flags',
$plugin_url . 'build/settings.js',
$settings_asset_file['dependencies'],
$settings_asset_file['version'],
true
);

wp_enqueue_style( 'wp-edit-blocks' );

wp_localize_script(
'mr-feature-flags',
'mrFeatureFlags',
[
'flags' => get_option( FeatureFlags::$option_name ),
]
);

wp_enqueue_style(
'mr-feature-flags',
$plugin_url . 'build/settings.css',
[],
$settings_asset_file['version']
);

}

add_action(
'admin_enqueue_scripts',
function(string $page): void {
$plugin_url = plugin_dir_url( MR_FEATURE_FLAGS_PLUGIN_PATH );
$script_asset_file = include_once plugin_dir_path( MR_FEATURE_FLAGS_PLUGIN_PATH ) . 'build/index.asset.php';

Expand All @@ -81,12 +121,7 @@ function(): void {
true
);

wp_enqueue_style(
'mr-feature-flags-style',
$plugin_url . 'build/index.css',
[],
$script_asset_file['version']
);


wp_localize_script(
'mr-feature-flags-script',
Expand All @@ -98,3 +133,43 @@ function(): void {

}
);

$mr_feature_flags_admin_settings = new Settings();
$mr_feature_flags_admin_settings->register_feature_settings();

$mr_feature_flags_register_api = new FlagOptions();
$mr_feature_flags_register_api->register_flags_endpoints();


// add_action ('init', function() {

// $request = new \WP_REST_Request( 'GET', '/feature-flags/v1/flags' );
// $request->set_query_params( [] );
// $response = rest_do_request( $request );
// ddd(rest_get_server()->response_to_data( $response, false ));

// });


add_filter( 'plugin_action_links_mr-feature-flags/plugin.php', function ( $links )
{
// Build and escape the URL.
$url = esc_url(
add_query_arg(
'page',
'mr-feature-flags',
get_admin_url() . 'admin.php'
)
);
// Create the link.
$settings_link = "<a href='$url'>" . __( 'Settings', 'mr-feature-flags' ) . '</a>';
// Adds the link to the end of the array.
array_push(
$links,
$settings_link
);
return $links;
}
);

// FeatureFlags::add_flag('Registration');
Binary file added src/.DS_Store
Binary file not shown.
Loading