Skip to content

Commit b33d940

Browse files
committed
refactor function and use correct one in code
1 parent 8cba468 commit b33d940

File tree

2 files changed

+33
-26
lines changed

2 files changed

+33
-26
lines changed

packages/wp-mu-plugin/stretch-extra/stretch-extra/inc/secondary-plugin-dir.php

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
\update_option('extendify_insights_stop', true, true);
3131

3232
// Initialize the active plugins option as an empty array
33-
foreach (get_custom_plugins() as $plugin_info) {
33+
foreach (get_installed_custom_plugins() as $plugin_info) {
3434
// Activate all custom plugins by default on first run
3535
// @TODO: activate all in one update_option call
3636
activate_custom_plugin($plugin_info['key']);
@@ -127,10 +127,9 @@ function deactivate_custom_plugin($plugin_key)
127127
}
128128

129129
/**
130-
* Get all custom plugins from the custom plugins directory (excluding deleted ones)
131-
* Returns an array of plugin info: ['key' => plugin_key, 'file' => plugin_file, 'data' => plugin_data]
130+
* Get all custom plugins from the custom plugins directory
132131
*/
133-
function get_custom_plugins(bool $include_deleted = false): array
132+
function get_all_custom_plugins(): array
134133
{
135134
static $all_custom_plugins = null;
136135

@@ -139,10 +138,15 @@ function get_custom_plugins(bool $include_deleted = false): array
139138
$all_custom_plugins = $bundle_config['plugins'];
140139
}
141140

142-
if ($include_deleted) {
143-
return $all_custom_plugins;
144-
}
141+
return $all_custom_plugins;
142+
}
145143

144+
/**
145+
* Get installed (not deleted) custom plugins
146+
*/
147+
function get_installed_custom_plugins(): array
148+
{
149+
$all_custom_plugins = get_all_custom_plugins();
146150
// Filter out deleted plugins
147151
$deleted_plugins = get_deleted_custom_plugins();
148152
return array_filter($all_custom_plugins, function ($plugin_info) use ($deleted_plugins) {
@@ -154,7 +158,7 @@ function get_custom_plugins(bool $include_deleted = false): array
154158
* Inject activated custom plugins
155159
*/
156160
\add_action('plugins_loaded', function () {
157-
$custom_plugins = get_custom_plugins();
161+
$custom_plugins = get_installed_custom_plugins();
158162
$active_plugins = get_active_custom_plugins();
159163

160164
foreach ($custom_plugins as $plugin_info) {
@@ -194,7 +198,7 @@ function get_custom_plugins(bool $include_deleted = false): array
194198

195199
switch ($pagenow) {
196200
case 'plugins.php':
197-
$custom_plugins = get_custom_plugins();
201+
$custom_plugins = get_installed_custom_plugins();
198202
$deleted_plugins = get_deleted_custom_plugins();
199203

200204
foreach ($custom_plugins as $plugin_info) {
@@ -226,7 +230,7 @@ function get_custom_plugins(bool $include_deleted = false): array
226230
return $result;
227231
}
228232

229-
$custom_plugins = get_custom_plugins();
233+
$custom_plugins = get_installed_custom_plugins();
230234
$custom_installed_plugin_slugs = array_column($custom_plugins, 'slug');
231235
$custom_installed_plugin_slugs = array_merge(
232236
array_map(fn ($slug) => basename(dirname($slug)), get_deleted_custom_plugins()),
@@ -265,7 +269,7 @@ function get_custom_plugins(bool $include_deleted = false): array
265269
*/
266270
\add_filter('upgrader_pre_install', function ($response, $hook_extra) {
267271
if (isset($hook_extra['plugin'])) {
268-
$custom_plugins = get_custom_plugins();
272+
$custom_plugins = get_installed_custom_plugins();
269273
$custom_slugs = array_column($custom_plugins, 'slug');
270274

271275
// Extract slug from plugin path
@@ -520,7 +524,7 @@ function get_custom_plugins(bool $include_deleted = false): array
520524
'slug' => $slug,
521525
];
522526

523-
$custom_plugin = array_find(get_custom_plugins(), fn ($_) => $_['slug'] === $slug);
527+
$custom_plugin = array_find(get_installed_custom_plugins(), fn ($_) => $_['slug'] === $slug);
524528

525529
if ($custom_plugin===null) {
526530
// not a custom plugin, fallback to default handler
@@ -576,7 +580,7 @@ function get_custom_plugins(bool $include_deleted = false): array
576580

577581
$slug = \wp_unslash($_POST['slug']);
578582

579-
$custom_plugin = array_find(get_custom_plugins(), fn ($_) => $_['slug'] === $slug);
583+
$custom_plugin = array_find(get_installed_custom_plugins(), fn ($_) => $_['slug'] === $slug);
580584

581585
if ($custom_plugin===null) {
582586
// not a custom plugin, fallback to default handler
@@ -644,7 +648,7 @@ function get_custom_plugins(bool $include_deleted = false): array
644648
*/
645649
\add_filter('plugin_install_action_links', function ($links, $plugin) {
646650
$custom_plugin = array_find(
647-
get_custom_plugins(),
651+
get_installed_custom_plugins(),
648652
// CAVEAT: we cannot name it $custom plugin since rector will name it also $custom_plugin_*
649653
// fn ($custom_plugin) => $custom_plugin['slug'] === $plugin['slug'],
650654
fn ($_) => $_['slug'] === $plugin['slug'],

packages/wp-plugin/ionos-essentials/ionos-essentials/inc/wpscan/index.php

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace ionos\essentials\wpscan;
44

55
defined('ABSPATH') || exit();
6-
use function ionos\stretch_extra\secondary_plugin_dir\get_custom_plugins;
6+
use function ionos\stretch_extra\secondary_plugin_dir\get_all_custom_plugins;
77
require_once __DIR__ . '/controller/class-wpscan.php';
88
require_once __DIR__ . '/controller/class-wpscanmiddleware.php';
99
require_once __DIR__ . '/views/summary.php';
@@ -40,19 +40,22 @@ function instant_check()
4040
\wp_send_json_error(null, 500);
4141
}
4242

43-
$custom_plugins = get_custom_plugins(true);
44-
$custom_installed_plugin_slugs = array_column($custom_plugins, 'slug');
43+
if (function_exists('get_all_custom_plugins')) {
44+
$custom_plugins = get_all_custom_plugins();
45+
$custom_installed_plugin_slugs = array_column($custom_plugins, 'slug');
4546

46-
if ( in_array($slug, $custom_installed_plugin_slugs)) {
47-
\wp_send_json_success("nothing_found", 200);
48-
} else {
49-
$middleware = new WPScanMiddleware();
50-
$issue_type = $middleware->get_instant_data($type, $slug);
51-
if (false === $issue_type) {
52-
\wp_send_json_error(null, 500);
53-
}
54-
\wp_send_json_success($issue_type, 200);
47+
if ( in_array($slug, $custom_installed_plugin_slugs)) {
48+
\wp_send_json_success("nothing_found", 200);
49+
}
50+
}
51+
52+
$middleware = new WPScanMiddleware();
53+
54+
$issue_type = $middleware->get_instant_data($type, $slug);
55+
if (false === $issue_type) {
56+
\wp_send_json_error(null, 500);
5557
}
58+
\wp_send_json_success($issue_type, 200);
5659
}
5760

5861
function recommended_action(\WP_REST_Request $request)

0 commit comments

Comments
 (0)