-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoow-ai-codeblaze.php
More file actions
643 lines (595 loc) · 32.6 KB
/
oow-ai-codeblaze.php
File metadata and controls
643 lines (595 loc) · 32.6 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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
<?php
/**
* Plugin Name: OOW AI CodeBlaze
* Description: Visualize and share WordPress plugin files in an AI-optimized format with a Matrix-style output.
* Version: 1.0
* Author: oowpress
* Author URI: https://oowpress.com
* License: GPL-2.0+
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: oow-ai-codeblaze
* Domain Path: /languages
*/
if (!defined('ABSPATH')) {
exit; // Prevent direct access.
}
/**
* Main plugin class for OOW AI CodeBlaze.
*
* Handles the initialization, admin menu, and core functionality of the plugin.
*/
class Oow_AI_CodeBlaze {
/**
* Constructor.
*
* Sets up admin menu and enqueues styles/scripts.
*/
public function __construct() {
add_action('admin_menu', [$this, 'oow_add_admin_menu']);
add_action('admin_enqueue_scripts', [$this, 'oow_enqueue_styles']);
}
/**
* Adds the plugin's admin menu page.
*
* Registers a menu page in the WordPress admin dashboard.
*/
public function oow_add_admin_menu() {
add_menu_page(
__('OOW AI CodeBlaze', 'oow-ai-codeblaze'),
__('OOW AI CodeBlaze', 'oow-ai-codeblaze'),
'manage_options',
'oow-ai-codeblaze',
[$this, 'oow_render_admin_page'],
'dashicons-code-standards',
80
);
}
/**
* Enqueues admin styles and scripts.
*
* Loads CSS and JavaScript for the plugin's admin page, including Matrix-style output formatting.
*
* @param string $hook The current admin page hook.
*/
public function oow_enqueue_styles($hook) {
// Only load styles/scripts on our plugin page
if ($hook !== 'toplevel_page_oow-ai-codeblaze') {
return;
}
// Register and enqueue CSS
wp_register_style(
'oow-ai-codeblaze-admin',
plugin_dir_url(__FILE__) . 'assets/css/oow-ai-codeblaze-admin.css',
[],
'1.0'
);
wp_enqueue_style('oow-ai-codeblaze-admin');
// Register and enqueue JavaScript
wp_register_script(
'oow-ai-codeblaze-admin',
plugin_dir_url(__FILE__) . 'assets/js/oow-ai-codeblaze-admin.js',
['jquery'],
'1.0',
true
);
wp_enqueue_script('oow-ai-codeblaze-admin');
// Localize script for translations
wp_localize_script(
'oow-ai-codeblaze-admin',
'oowAICodeBlaze',
[
'copyText' => esc_js(__('Copy All', 'oow-ai-codeblaze')),
'copiedText' => esc_js(__('Copied!', 'oow-ai-codeblaze')),
]
);
}
/**
* Renders the admin page.
*
* Displays the plugin's admin interface, including plugin selection, file selection, and AI-optimized output.
*/
public function oow_render_admin_page() {
$plugins_dir = WP_PLUGIN_DIR;
$selected_plugin = '';
// Check nonce for plugin selection
$select_nonce = isset($_POST['oow_select_nonce']) ? sanitize_text_field(wp_unslash($_POST['oow_select_nonce'])) : '';
if ($select_nonce && wp_verify_nonce($select_nonce, 'oow_select_plugin')) {
if (isset($_POST['selected_plugin']) && !empty($_POST['selected_plugin'])) {
$selected_plugin = sanitize_text_field(wp_unslash($_POST['selected_plugin']));
}
}
// Check if selected_plugin is sent in file form submission
$generate_nonce = isset($_POST['oow_generate_nonce']) ? sanitize_text_field(wp_unslash($_POST['oow_generate_nonce'])) : '';
if ($generate_nonce && wp_verify_nonce($generate_nonce, 'oow_generate_output')) {
if (isset($_POST['selected_plugin']) && !empty($_POST['selected_plugin'])) {
$selected_plugin = sanitize_text_field(wp_unslash($_POST['selected_plugin']));
}
}
// Normalize plugin path
$plugin_path = realpath(trailingslashit($plugins_dir) . $selected_plugin);
if ($plugin_path === false) {
$plugin_path = trailingslashit($plugins_dir) . $selected_plugin;
}
?>
<div class="wrap">
<h1><?php esc_html_e('OOW AI CodeBlaze', 'oow-ai-codeblaze'); ?></h1>
<p class="description"><?php esc_html_e('Welcome to OOW AI CodeBlaze! This plugin is a must-have for developers, making it easy to inspect, debug, and share plugin code with AI tools. Select a plugin, customize the output, and copy it in a Matrix-style format perfect for analysis.', 'oow-ai-codeblaze'); ?></p>
<!-- Plugin selection form -->
<form method="post">
<?php wp_nonce_field('oow_select_plugin', 'oow_select_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="selected_plugin"><?php esc_html_e('Choose a plugin:', 'oow-ai-codeblaze'); ?></label></th>
<td>
<select name="selected_plugin" id="selected_plugin">
<option value=""><?php esc_html_e('-- Select --', 'oow-ai-codeblaze'); ?></option>
<?php
foreach (scandir($plugins_dir) as $plugin_folder) {
if ($plugin_folder === '.' || $plugin_folder === '..' || !is_dir($plugins_dir . '/' . $plugin_folder)) {
continue;
}
$selected = ($plugin_folder === $selected_plugin) ? 'selected' : '';
echo '<option value="' . esc_attr($plugin_folder) . '" ' . esc_attr($selected) . '>' . esc_html($plugin_folder) . '</option>';
}
?>
</select>
<button type="submit" class="button button-primary"><?php esc_html_e('Load', 'oow-ai-codeblaze'); ?></button>
<p class="description"><?php esc_html_e('Select a plugin to view its files and generate an AI-friendly output.', 'oow-ai-codeblaze'); ?></p>
</td>
</tr>
</table>
</form>
<!-- File selection form -->
<?php if ($selected_plugin && is_dir($plugin_path)) : ?>
<form method="post" id="file-form">
<?php wp_nonce_field('oow_generate_output', 'oow_generate_nonce'); ?>
<input type="hidden" name="selected_plugin" value="<?php echo esc_attr($selected_plugin); ?>">
<?php // translators: %s is the name of the selected plugin. ?>
<h2><?php printf(esc_html__('Files of %s', 'oow-ai-codeblaze'), '<code>' . esc_html($selected_plugin) . '</code>'); ?></h2>
<p class="description"><?php esc_html_e('Choose the files you want to include in the output and customize the AI analysis options below.', 'oow-ai-codeblaze'); ?></p>
<table class="form-table">
<tr>
<th scope="row"><label for="file-search"><?php esc_html_e('Search files:', 'oow-ai-codeblaze'); ?></label></th>
<td>
<input type="text" id="file-search" placeholder="<?php esc_attr_e('Search files...', 'oow-ai-codeblaze'); ?>">
<p class="description">
<a href="#" id="select-all"><?php esc_html_e('Select All', 'oow-ai-codeblaze'); ?></a> |
<a href="#" id="deselect-all"><?php esc_html_e('Deselect All', 'oow-ai-codeblaze'); ?></a>
<br>
<?php esc_html_e('Use the search box to filter files or select/deselect all files.', 'oow-ai-codeblaze'); ?>
</p>
</td>
</tr>
</table>
<h3><?php esc_html_e('AI Analysis Options', 'oow-ai-codeblaze'); ?></h3>
<table class="form-table">
<tr>
<th scope="row"><?php esc_html_e('Options', 'oow-ai-codeblaze'); ?></th>
<td>
<label><input type="checkbox" name="include_structure" value="on" <?php checked(isset($_POST['include_structure']) && $_POST['include_structure'] === 'on'); ?> checked> <?php esc_html_e('Include full directory structure', 'oow-ai-codeblaze'); ?></label><br>
<label><input type="checkbox" name="include_metadata" value="on" <?php checked(isset($_POST['include_metadata']) && $_POST['include_metadata'] === 'on'); ?>> <?php esc_html_e('Include plugin metadata', 'oow-ai-codeblaze'); ?></label><br>
<label><input type="checkbox" name="include_dependencies" value="on" <?php checked(isset($_POST['include_dependencies']) && $_POST['include_dependencies'] === 'on'); ?>> <?php esc_html_e('Include file dependencies', 'oow-ai-codeblaze'); ?></label><br>
<label><input type="checkbox" name="include_hooks" value="on" <?php checked(isset($_POST['include_hooks']) && $_POST['include_hooks'] === 'on'); ?>> <?php esc_html_e('Include WordPress hooks', 'oow-ai-codeblaze'); ?></label><br>
<label><input type="checkbox" name="group_by_type" value="on" <?php checked(isset($_POST['group_by_type']) && $_POST['group_by_type'] === 'on'); ?>> <?php esc_html_e('Group files by type', 'oow-ai-codeblaze'); ?></label><br>
<label><input type="checkbox" name="exclude_sensitive" value="on" <?php checked(isset($_POST['exclude_sensitive']) && $_POST['exclude_sensitive'] === 'on'); ?>> <?php esc_html_e('Exclude sensitive files', 'oow-ai-codeblaze'); ?></label><br>
<label><input type="checkbox" name="include_modification" value="on" <?php checked(isset($_POST['include_modification']) && $_POST['include_modification'] === 'on'); ?>> <?php esc_html_e('Include file modification dates', 'oow-ai-codeblaze'); ?></label><br>
<label><input type="checkbox" name="include_environment" value="on" <?php checked(isset($_POST['include_environment']) && $_POST['include_environment'] === 'on'); ?>> <?php esc_html_e('Include environment variables', 'oow-ai-codeblaze'); ?></label><br>
<label><input type="checkbox" name="include_database" value="on" <?php checked(isset($_POST['include_database']) && $_POST['include_database'] === 'on'); ?>> <?php esc_html_e('Include database details', 'oow-ai-codeblaze'); ?></label>
<p class="description"><?php esc_html_e('Check the options to customize the output. For example, include database details to share information about the database type and table storage.', 'oow-ai-codeblaze'); ?></p>
</td>
</tr>
</table>
<div id="pfv-file-tree">
<?php $this->oow_list_files_checkbox($plugin_path, $plugin_path); ?>
</div>
<p class="submit">
<button type="submit" name="display_selected" class="button button-primary"><?php esc_html_e('Generate Output', 'oow-ai-codeblaze'); ?></button>
</p>
</form>
<?php else: ?>
<?php if ($selected_plugin): ?>
<p class="description" style="color: red;"><?php esc_html_e('Error: The selected plugin directory does not exist or is not accessible.', 'oow-ai-codeblaze'); ?></p>
<?php endif; ?>
<?php endif; ?>
<!-- Output result -->
<?php
$generate_nonce = isset($_POST['oow_generate_nonce']) ? sanitize_text_field( wp_unslash( $_POST['oow_generate_nonce'] ) ) : '';
if (isset($_POST['display_selected']) && !empty($_POST['files']) && $generate_nonce && wp_verify_nonce($generate_nonce, 'oow_generate_output')) : ?>
<h2><?php esc_html_e('AI-Optimized Output', 'oow-ai-codeblaze'); ?></h2>
<p class="description"><?php esc_html_e('Copy the output below to share with an AI tool. It’s formatted for easy analysis with a Matrix-style look.', 'oow-ai-codeblaze'); ?></p>
<div class="pfv-clipboard-wrap">
<textarea id="pfv-clipboard"><?php
$files_to_process = array_map('sanitize_text_field', wp_unslash($_POST['files']));
// Debug: Afficher les informations de soumission
echo esc_html("Debug: POST selected_plugin: " . (isset($_POST['selected_plugin']) ? sanitize_text_field(wp_unslash($_POST['selected_plugin'])) : 'Not set') . "\n");
echo esc_html("Debug: Selected plugin: " . esc_html($selected_plugin) . "\n");
echo esc_html("Debug: Plugin path: " . esc_html($plugin_path) . "\n");
echo esc_html("Debug: Plugin path exists: " . (is_dir($plugin_path) ? 'Yes' : 'No') . "\n");
echo esc_html("Debug: Generate nonce valid: " . (wp_verify_nonce($generate_nonce, 'oow_generate_output') ? 'Yes' : 'No') . "\n");
echo esc_html("Debug: Selected files: ");
if (defined('WP_DEBUG') && WP_DEBUG) {
//echo esc_html(print_r($files_to_process, true));
} else {
echo esc_html(json_encode($files_to_process));
}
echo esc_html("\n\n");
// Environment variables
if (isset($_POST['include_environment']) && $_POST['include_environment'] === 'on') {
echo esc_html("=== Environment Variables ===\n");
$env_vars = $this->oow_get_environment_variables();
foreach ($env_vars as $key => $value) {
echo esc_html("# $key: ") . esc_html($value) . esc_html("\n");
}
echo esc_html("\n");
}
// Database details
if (isset($_POST['include_database']) && $_POST['include_database'] === 'on') {
echo esc_html("=== Database Details ===\n");
$db_vars = $this->oow_get_database_details();
foreach ($db_vars as $key => $value) {
echo esc_html("# $key: ") . esc_html($value) . esc_html("\n");
}
echo esc_html("\n");
}
// Plugin metadata
if (isset($_POST['include_metadata']) && $_POST['include_metadata'] === 'on') {
echo esc_html("=== Plugin Metadata ===\n");
$metadata = $this->oow_get_plugin_metadata($plugin_path);
foreach ($metadata as $key => $value) {
echo esc_html("# $key: ") . esc_html($value) . esc_html("\n");
}
echo esc_html("\n");
}
// Directory structure
if (isset($_POST['include_structure']) && $_POST['include_structure'] === 'on' && !empty($selected_plugin) && is_dir($plugin_path)) {
echo esc_html("=== Plugin Structure ===\n");
echo esc_html($this->oow_generate_directory_structure($plugin_path, $selected_plugin));
echo esc_html("\n");
}
// Process selected files
$grouped_files = isset($_POST['group_by_type']) && $_POST['group_by_type'] === 'on' ? $this->oow_group_files_by_type($files_to_process, $plugin_path) : ['all' => $files_to_process];
// Debug: Afficher les fichiers groupés
echo esc_html("Debug: Grouped files: ");
if (defined('WP_DEBUG') && WP_DEBUG) {
//echo esc_html(print_r($grouped_files, true));
} else {
echo esc_html(json_encode($grouped_files));
}
echo esc_html("\n\n");
foreach ($grouped_files as $type => $files) {
if ($type !== 'all') {
echo esc_html("=== Files (" . esc_html($type) . ") ===\n\n");
} else {
echo esc_html("=== Files ===\n\n");
}
foreach ($files as $file_rel_path) {
// Normalize relative path
$file_rel_path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $file_rel_path);
$file_rel_path = ltrim($file_rel_path, DIRECTORY_SEPARATOR);
$abs_path = trailingslashit($plugin_path) . $file_rel_path;
// Try to resolve the real path
$real_abs_path = realpath($abs_path);
if ($real_abs_path === false) {
$real_abs_path = $abs_path; // Fallback to original path
}
// Debug: Afficher les chemins
echo esc_html("Debug: Processing file: " . esc_html($abs_path) . "\n");
echo esc_html("Debug: Real path: " . esc_html($real_abs_path) . "\n");
echo esc_html("Debug: File exists: " . (file_exists($real_abs_path) ? 'Yes' : 'No') . "\n");
echo esc_html("Debug: File readable: " . (is_readable($real_abs_path) ? 'Yes' : 'No') . "\n");
// Verify file exists and is readable
if (!file_exists($real_abs_path)) {
echo esc_html("# Error: File does not exist: " . esc_html($file_rel_path) . "\n\n");
continue;
}
if (!is_readable($real_abs_path)) {
echo esc_html("# Error: File is not readable: " . esc_html($file_rel_path) . "\n\n");
continue;
}
// Exclude sensitive files
if (isset($_POST['exclude_sensitive']) && $_POST['exclude_sensitive'] === 'on' && $this->oow_is_sensitive_file($file_rel_path)) {
echo esc_html("# Skipped sensitive file: " . esc_html($file_rel_path) . "\n\n");
continue;
}
$file_info = pathinfo($real_abs_path);
$ext = isset($file_info['extension']) ? $file_info['extension'] : 'unknown';
$role = $this->oow_guess_file_role($file_rel_path, $ext);
echo esc_html("=== /" . esc_html($file_rel_path) . " ===\n");
echo esc_html("# File Type: " . esc_html($ext) . "\n");
echo esc_html("# Role: " . esc_html($role) . "\n");
// Modification date
if (isset($_POST['include_modification']) && $_POST['include_modification'] === 'on') {
echo esc_html("# Last Modified: " . esc_html(gmdate('Y-m-d H:i:s', filemtime($real_abs_path))) . "\n");
}
// Dependencies
if (isset($_POST['include_dependencies']) && $_POST['include_dependencies'] === 'on') {
$dependencies = $this->oow_get_file_dependencies($real_abs_path);
if (!empty($dependencies)) {
echo esc_html("# Dependencies: " . esc_html(implode(', ', array_map('esc_html', $dependencies))) . "\n");
}
}
// WordPress hooks
if (isset($_POST['include_hooks']) && $_POST['include_hooks'] === 'on') {
$hooks = $this->oow_get_wordpress_hooks($real_abs_path);
if (!empty($hooks)) {
echo esc_html("# WordPress Hooks: " . esc_html(implode(', ', array_map('esc_html', $hooks))) . "\n");
}
}
// Output file content
$content = file_get_contents($real_abs_path);
if ($content !== false) {
echo esc_html("\n```" . esc_html($ext) . "\n");
echo esc_html($content) . esc_html("\n```\n\n");
} else {
echo esc_html("# Error: Unable to read file content: " . esc_html($file_rel_path) . "\n\n");
}
}
}
?></textarea>
<p class="submit">
<button id="copy-all" class="button button-secondary"><?php esc_html_e('Copy All', 'oow-ai-codeblaze'); ?></button>
</p>
</div>
<?php endif; ?>
</div>
<?php
}
/**
* Lists files in a directory as checkboxes.
*
* Generates a hierarchical list of files and folders for selection in the admin interface.
*
* @param string $dir The directory to scan.
* @param string $base_path The base path for calculating relative paths.
*/
private function oow_list_files_checkbox($dir, $base_path) {
$files = scandir($dir);
echo '<ul class="pfv-folder">';
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$full_path = $dir . DIRECTORY_SEPARATOR . $file;
$relative_path = ltrim(str_replace($base_path, '', $full_path), DIRECTORY_SEPARATOR);
if (is_dir($full_path)) {
echo '<li class="pfv-folder">';
echo '<span class="pfv-folder-toggle">📁</span> <strong>' . esc_html($file) . '</strong>';
echo '<div class="pfv-folder-content">';
$this->oow_list_files_checkbox($full_path, $base_path);
echo '</div>';
echo '</li>';
} else {
echo '<li><label><input type="checkbox" name="files[]" value="' . esc_attr($relative_path) . '"> ' . esc_html($file) . '</label></li>';
}
}
echo '</ul>';
}
/**
* Guesses the role of a file based on its name and extension.
*
* Determines the purpose of a file (e.g., main plugin file, stylesheet) for output metadata.
*
* @param string $file_path The file path.
* @param string $ext The file extension.
* @return string The guessed role.
*/
private function oow_guess_file_role($file_path, $ext) {
$filename = basename($file_path);
if (preg_match('/^plugin-|^index|\.php$/', $filename)) {
return 'Main plugin file';
} elseif ($ext === 'css' || $ext === 'scss') {
return 'Stylesheet';
} elseif ($ext === 'js') {
return 'JavaScript';
} elseif (strpos($file_path, 'includes' . DIRECTORY_SEPARATOR) !== false) {
return 'Included functionality';
} elseif (strpos($file_path, 'admin' . DIRECTORY_SEPARATOR) !== false) {
return 'Admin-specific functionality';
}
return 'Unknown';
}
/**
* Generates a directory structure string.
*
* Creates a formatted string representing the folder and file structure of a plugin.
*
* @param string $dir The directory to scan.
* @param string $base_name The base name for relative paths.
* @param string $prefix The prefix for indentation.
* @return string The directory structure.
*/
private function oow_generate_directory_structure($dir, $base_name, $prefix = '') {
$output = '';
$files = scandir($dir);
foreach ($files as $file) {
if ($file === '.' || $file === '..') {
continue;
}
$full_path = $dir . DIRECTORY_SEPARATOR . $file;
$relative_path = ltrim(str_replace($dir, $base_name, $full_path), DIRECTORY_SEPARATOR);
if (is_dir($full_path)) {
$output .= $prefix . "📁 " . $relative_path . DIRECTORY_SEPARATOR . "\n";
$output .= $this->oow_generate_directory_structure($full_path, $base_name, $prefix . ' ');
} else {
$output .= $prefix . "📄 " . $relative_path . "\n";
}
}
return $output;
}
/**
* Retrieves plugin metadata from the main plugin file.
*
* Extracts metadata such as Plugin Name, Version, and Author from the plugin header.
*
* @param string $plugin_path The path to the plugin directory.
* @return array The metadata key-value pairs.
*/
private function oow_get_plugin_metadata($plugin_path) {
$main_file = $plugin_path . DIRECTORY_SEPARATOR . basename($plugin_path) . '.php';
$metadata = [];
if (file_exists($main_file)) {
$content = file_get_contents($main_file);
// Filter valid header fields
$valid_keys = [
'Plugin Name', 'Plugin URI', 'Description', 'Version', 'Author',
'Author URI', 'License', 'License URI', 'Text Domain', 'Domain Path'
];
preg_match_all('/^\s*\*?\s*([A-Za-z\s]+):\s*(.+)$/m', $content, $matches);
for ($i = 0; $i < count($matches[1]); $i++) {
$key = trim($matches[1][$i]);
$value = trim($matches[2][$i]);
if (in_array($key, $valid_keys) && !empty($key) && !empty($value)) {
$metadata[$key] = $value;
}
}
}
return $metadata;
}
/**
* Checks if a file is sensitive based on its name.
*
* Identifies files that may contain sensitive information (e.g., .env, config.php).
*
* @param string $file_path The file path to check.
* @return bool True if the file is sensitive, false otherwise.
*/
private function oow_is_sensitive_file($file_path) {
$sensitive_patterns = ['.env', 'config.php', 'wp-config.php', 'secret', 'key', 'password'];
foreach ($sensitive_patterns as $pattern) {
if (stripos($file_path, $pattern) !== false) {
return true;
}
}
return false;
}
/**
* Retrieves dependencies from a PHP file.
*
* Extracts require/include statements from a PHP file to identify dependencies.
*
* @param string $file_path The path to the PHP file.
* @return array List of dependency file names.
*/
private function oow_get_file_dependencies($file_path) {
$dependencies = [];
if (is_file($file_path) && in_array(pathinfo($file_path, PATHINFO_EXTENSION), ['php'])) {
$content = file_get_contents($file_path);
preg_match_all('/(require|include)(_once)?\s*\(?\s*[\'"](.+?)[\'"]\s*\)?;/i', $content, $matches);
if (!empty($matches[3])) {
$dependencies = array_map('basename', $matches[3]);
}
}
return $dependencies;
}
/**
* Retrieves WordPress hooks from a PHP file.
*
* Extracts add_action and add_filter hooks from a PHP file.
*
* @param string $file_path The path to the PHP file.
* @return array List of hook names.
*/
private function oow_get_wordpress_hooks($file_path) {
$hooks = [];
if (is_file($file_path) && in_array(pathinfo($file_path, PATHINFO_EXTENSION), ['php'])) {
$content = file_get_contents($file_path);
preg_match_all('/(add_action|add_filter)\s*\(\s*[\'"](.+?)[\'"]/i', $content, $matches);
if (!empty($matches[2])) {
$hooks = array_unique($matches[2]);
}
}
return $hooks;
}
/**
* Groups files by their extension.
*
* Organizes selected files into groups based on their file extensions.
*
* @param array $files List of file paths.
* @param string $plugin_path The plugin directory path.
* @return array Grouped files by extension.
*/
private function oow_group_files_by_type($files, $plugin_path) {
$grouped = [];
foreach ($files as $file) {
$file = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $file);
$file = ltrim($file, DIRECTORY_SEPARATOR);
$abs_path = trailingslashit($plugin_path) . $file;
$real_abs_path = realpath($abs_path);
$ext = pathinfo($real_abs_path ?: $abs_path, PATHINFO_EXTENSION) ?? 'unknown';
$grouped[$ext][] = $file;
}
return $grouped;
}
/**
* Retrieves environment variables.
*
* Collects system information such as WordPress version, PHP version, and server details.
*
* @return array Environment variables key-value pairs.
*/
private function oow_get_environment_variables() {
global $wp_version;
return [
'WordPress Version' => $wp_version,
'PHP Version' => phpversion(),
'Memory Limit' => ini_get('memory_limit'),
'Max Execution Time' => ini_get('max_execution_time') . ' seconds',
'Debug Mode' => defined('WP_DEBUG') && WP_DEBUG ? 'Enabled' : 'Disabled',
'Multisite' => is_multisite() ? 'Enabled' : 'Disabled',
'Server Software' => isset($_SERVER['SERVER_SOFTWARE']) ? sanitize_text_field(wp_unslash($_SERVER['SERVER_SOFTWARE'])) : 'Unknown',
];
}
/**
* Retrieves database details.
*
* Collects information about the database type, version, and table metadata (engine, size).
*
* @return array Database details key-value pairs.
*/
private function oow_get_database_details() {
global $wpdb;
$db_details = [
'Database Type' => $wpdb->db_server_info(),
'Database Version' => $wpdb->db_version(),
];
// Retrieve info for main tables
$tables = ['posts', 'options', 'users'];
foreach ($tables as $table) {
$table_name = $wpdb->prefix . $table;
$cache_key = 'oow_table_details_' . md5($table_name);
$cached = wp_cache_get($cache_key, 'oow_ai_codeblaze');
if ($cached !== false) {
$db_details["Table $table_name Engine"] = $cached['engine'];
$db_details["Table $table_name Size"] = $cached['size_mb'] . ' MB';
continue;
}
// Note: Direct query required to access information_schema.TABLES for table engine and size, as WordPress does not provide a built-in method to retrieve this metadata. The query is secured with $wpdb->prepare() to prevent SQL injection and cached to optimize performance.
$result = $wpdb->get_row(
$wpdb->prepare(
"
SELECT
ENGINE AS engine,
ROUND(DATA_LENGTH / 1024 / 1024, 2) AS size_mb
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = %s
AND TABLE_NAME = %s
",
DB_NAME,
$table_name
)
);
if ($result) {
$db_details["Table $table_name Engine"] = $result->engine;
$db_details["Table $table_name Size"] = $result->size_mb . ' MB';
wp_cache_set($cache_key, [
'engine' => $result->engine,
'size_mb' => $result->size_mb
], 'oow_ai_codeblaze', 3600); // Cache for 1 hour
}
}
return $db_details;
}
}
// Instantiate the class
new Oow_AI_CodeBlaze();