-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.php
164 lines (136 loc) · 3.79 KB
/
plugin.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
<?php
/**
* The plugin bootstrap file
*
* @since 0.1.0
* @package codeb-feature-flags
*
* @wordpress-plugin
* Plugin Name: Feature Flags
* Plugin URI: https://github.com/codebtech/wp-feature-flags
* Description: Allows developers to enable / disable features based on flags.
* Version: 0.3.2
* Requires at least: 6.4
* Requires PHP: 8.1
* Author: Mohan Raj
* Author URI: https://mohanraj.dev
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: codeb-feature-flags
*/
declare( strict_types = 1 );
namespace CodeB\FeatureFlags;
use CodeB\FeatureFlags\Api\Flags;
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* API and Plugin version constants.
*/
define( 'CODEB_FEATURE_FLAGS_PLUGIN_PATH', __FILE__ );
if ( ! file_exists( Flag::class ) ) {
include_once __DIR__ . '/vendor/autoload.php';
}
// Enqueue scripts, styles in settings page.
add_action(
'admin_enqueue_scripts',
static function ( string $page ): void {
if ( 'toplevel_page_codeb-feature-flags' === $page ) {
codeb_feature_flags_load_settings_scripts();
}
}
);
/**
* Load settings page assets
*
* @return void
*/
function codeb_feature_flags_load_settings_scripts(): void {
$plugin_url = plugin_dir_url( CODEB_FEATURE_FLAGS_PLUGIN_PATH );
$settings_asset_file = require_once plugin_dir_path( CODEB_FEATURE_FLAGS_PLUGIN_PATH ) . 'build/settings.asset.php'; // @phpcs:ignore
wp_enqueue_script(
'codeb-feature-flags',
$plugin_url . 'build/settings.js',
$settings_asset_file['dependencies'],
$settings_asset_file['version'],
true
);
wp_enqueue_style( 'wp-edit-blocks' );
wp_enqueue_style(
'codeb-feature-flags',
$plugin_url . 'build/settings.css',
[],
$settings_asset_file['version']
);
}
// Enqueue scripts and styles for front end.
add_action(
'wp_enqueue_scripts',
__NAMESPACE__ . '\codeb_feature_flags_scripts_enqueue'
);
// Enqueue scripts and styles for wp-admin.
add_action(
'admin_enqueue_scripts',
__NAMESPACE__ . '\codeb_feature_flags_scripts_enqueue'
);
/**
* Enqueue scripts and assets for admin and front end
*/
function codeb_feature_flags_scripts_enqueue(): void {
$plugin_url = plugin_dir_url( CODEB_FEATURE_FLAGS_PLUGIN_PATH );
$script_asset_file = include_once plugin_dir_path( CODEB_FEATURE_FLAGS_PLUGIN_PATH ) . 'build/index.asset.php';
wp_enqueue_script(
'codeb-feature-flags-script',
$plugin_url . 'build/index.js',
$script_asset_file['dependencies'],
$script_asset_file['version'],
true
);
$feature_flag_meta = get_option( Flag::$option_name );
$flags_list = [];
if ( is_array( $feature_flag_meta ) ) {
$flags_list = $feature_flag_meta;
}
wp_localize_script(
'codeb-feature-flags-script',
'codebFeatureFlags',
[
'flags' => $flags_list,
]
);
}
// Registers feature flags admin setting page.
$codeb_feature_flags_admin_settings = new Settings();
$codeb_feature_flags_admin_settings->register_feature_settings();
// Registers feature flags API's.
$codeb_feature_flags_register_api = new Flags();
$codeb_feature_flags_register_api->register();
// Displays setting page link in plugin page.
add_filter(
'plugin_action_links_codeb-feature-flags/plugin.php',
static function ( $links ) {
$url = esc_url(
add_query_arg(
'page',
'codeb-feature-flags',
get_admin_url() . 'admin.php'
)
);
$settings_link = "<a href='$url'>" . __( 'Settings', 'codeb-feature-flags' ) . '</a>';
array_push(
$links,
$settings_link
);
return $links;
}
);
register_deactivation_hook( __FILE__, __NAMESPACE__ . '\codeb_feature_flags_uninstall' );
/**
* Uninstall method for the plugin.
*
* @return void
*/
function codeb_feature_flags_uninstall(): void {
delete_option( Flag::$option_name );
}