-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.js
More file actions
253 lines (216 loc) · 8.48 KB
/
build.js
File metadata and controls
253 lines (216 loc) · 8.48 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
// const fs = require('fs');
const fs = require('fs-extra');
const path = require('path');
const metadata = require('./package.json');
function copyRecursiveSync(src, dest) {
const exists = fs.existsSync(src);
const stats = exists && fs.statSync(src);
const isDirectory = exists && stats.isDirectory();
if (isDirectory) {
if (!fs.existsSync(dest)) fs.mkdirSync(dest);
fs.readdirSync(src).forEach(childItemName => {
copyRecursiveSync(path.join(src, childItemName),
path.join(dest, childItemName));
});
} else {
fs.copyFileSync(src, dest);
}
}
const buildDirPath = path.join(__dirname, 'build');
const distDir = path.join(__dirname, metadata.name + '-dist');
const commonDistDir = path.join(distDir.toString(), 'common');
const indexPhpPath = path.join(__dirname, 'index.php');
const gearCommonFile = path.join(commonDistDir.toString(), 'gearCommonData.js');
const itemsToCopy = [
'build',
'index.php',
];
const blocks = fs.readdirSync(buildDirPath).filter(file =>
fs.statSync(path.join(buildDirPath, file)).isDirectory()
);
const blocksString = blocks.map(block => `'${block}'`).join(', ');
const indexPhpContent =`<?php
/**
* Plugin Name: Vara Components
* Description: Plugin with custom blocks to handle Vara network features
* Version: 1.0
* Author: Vara
*/
// 1. Register the option to store data in wordpress
function gear_plugin_register_settings() {
register_setting('gear_plugin_settings_group', 'gear_rpc_url');
register_setting('gear_plugin_settings_group', 'gear_server_url');
register_setting('gear_plugin_settings_group', 'gear_contract_id');
register_setting('gear_plugin_settings_group', 'gear_idl_content');
register_setting('gear_plugin_settings_group', 'gear_app_name');
}
add_action('admin_init', 'gear_plugin_register_settings');
// 2. Main menu and sub-menus
function gear_plugin_add_admin_menu() {
// Menú principal "Vara Components"
add_menu_page(
'Vara Components', // page title
'Vara Components', // manu title
'manage_options', // capacity
'gear_plugin', // Slug
'gear_plugin_main_page', // main page Callback
'dashicons-admin-generic', // Icon
80 // menu position
);
// Submenú "Settings"
add_submenu_page(
'gear_plugin', // menu parent Slug
'Settings', // page title
'Settings', // sub-menu title
'manage_options', // capacity
'gear_plugin_settings', // Slug
'gear_plugin_settings_page' // Callback
);
// Submenu "About" (optional)
add_submenu_page(
'gear_plugin',
'About',
'About',
'manage_options',
'gear_plugin_about',
'gear_plugin_about_page'
);
}
add_action('admin_menu', 'gear_plugin_add_admin_menu');
// 3. Main page (Vara components)
function gear_plugin_main_page() {
echo '<div class="wrap"><h1>Vara Components</h1><p>Vara network wordpress blocks.</p></div>';
}
// 4. Settings page
function gear_plugin_settings_page() {
?>
<div class="wrap">
<h1>Gear Plugin Settings</h1>
<?php if (isset($_GET['settings-updated']) && $_GET['settings-updated'] === 'true') : ?>
<div id="message" class="updated notice is-dismissible">
<p>Settings saved.</p>
</div>
<?php endif; ?>
<form method="post" action="options.php">
<?php settings_fields('gear_plugin_settings_group'); ?>
<?php do_settings_sections('gear_plugin_settings_group'); ?>
<table class="form-table">
<tr valign="top">
<th scope="row">VARA APP NAME</th>
<td>
<input type="text" name="gear_app_name" value="<?php echo esc_attr(get_option('gear_app_name', '')); ?>" size="50" />
<p class="description">Example: My_Project</p>
</td>
</tr>
<tr valign="top">
<th scope="row">RPC URL</th>
<td>
<input type="text" name="gear_rpc_url" value="<?php echo esc_attr(get_option('gear_rpc_url', '')); ?>" size="50" />
<p class="description">Example: wss://testnet.vara.network</p>
</td>
</tr>
<tr valign="top">
<th scope="row">BACKEND URL</th>
<td>
<input type="text" name="gear_server_url" value="<?php echo esc_attr(get_option('gear_server_url', '')); ?>" size="50" />
<p class="description">Example: http://localhost</p>
</td>
</tr>
<tr valign="top">
<th scope="row">CONTRACT ADDRESS</th>
<td>
<input type="text" name="gear_contract_id" value="<?php echo esc_attr(get_option('gear_contract_id', '')); ?>" size="50" />
<p class="description">Example: 0x03jf21...</p>
</td>
</tr>
<tr valign="top">
<th scope="row">IDL Content</th>
<td>
<textarea name="gear_idl_content" rows="10" cols="80"><?php echo esc_textarea(get_option('gear_idl_content', '')); ?></textarea>
<p class="description">Paste here your contract idl content.</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
// 5. About page
function gear_plugin_about_page() {
echo '<div class="wrap"><h1>About Vara Components</h1><p></p></div>';
}
// 6. Add settings link in the plugins section
function gear_plugin_action_links($links) {
$settings_link = '<a href="admin.php?page=gear_plugin_settings">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'gear_plugin_action_links');
// 7. register vara plugin blocks
function vara_wordpress_register_blocks() {
$blocks = [${blocksString}];
foreach ( $blocks as $block ) {
register_block_type( __DIR__ . "/build/$block" );
}
}
add_action('init', 'vara_wordpress_register_blocks');
// 8. Inject GearPluginSettins on JS (globally)
function vara_wordpress_register_common_gearapi_script() {
wp_register_script(
'vara-wordpress-common-gearapi-js',
plugins_url('common/gearCommonData.js', __FILE__), // be sure to have this file
array(),
'1.0.0',
true
);
wp_enqueue_script('vara-wordpress-common-gearapi-js');
wp_enqueue_script(
'sailscalls-global-api',
plugin_dir_url(__FILE__) . 'build/common/varaGearGlobalData.js',
array(),
'1.0.0',
true // Footer para asegurar que window esté listo
);
wp_localize_script(
'vara-wordpress-common-gearapi-js',
'GearPluginSettings',
array(
'rpcUrl' => get_option('gear_rpc_url', ''),
'backendUrl' => get_option('gear_server_url', ''),
'contractAddress' => get_option('gear_contract_id', ''),
'contractIdl' => get_option('gear_idl_content', ''),
'gearAppName' => get_option('gear_app_name', '')
)
);
}
add_action('wp_enqueue_scripts', 'vara_wordpress_register_common_gearapi_script');
// register_deactivation_hook(__FILE__, 'gear_plugin_deactivate');
// function gear_plugin_deactivate() {
// delete_option('gear_rpc_url');
// delete_option('gear_server_url');
// delete_option('gear_contract_id');
// delete_option('gear_idl_content');
// delete_option('gear_app_name');
// }`;
console.log(`\n📦 Blocks detected: ${blocks.join(', ')}`);
if (!fs.existsSync(distDir)) {
fs.mkdirSync(distDir);
}
if (fs.existsSync(indexPhpPath)) {
fs.rmSync(indexPhpPath);
}
fs.mkdirSync(commonDistDir);
try {
fs.writeFileSync(indexPhpPath, indexPhpContent);
fs.writeFileSync(gearCommonFile, '// hook file for wp_localize_script');
} catch(e) {
console.log("❌ Error while creating index.php file");
process.exit(-1);
}
itemsToCopy.forEach(item =>{
const src = path.join(__dirname, item);
const dest = path.join(distDir, item);
fs.move(src, dest);
});
console.log("✅ plugin packaged in:", distDir);