diff --git a/scripts/patch-tool-catalog.php b/scripts/patch-tool-catalog.php new file mode 100644 index 0000000..e02dc2f --- /dev/null +++ b/scripts/patch-tool-catalog.php @@ -0,0 +1,185 @@ +getAllToolsRaw() as $t) { if (!empty($t['name'])) $live[$t['name']] = true; } + $labels = $this->ampPluginLabels(); + $groups = []; + foreach ($files as $f) { + $code = @file_get_contents($f); + if ($code === false || $code === '') continue; + if (!preg_match('/if\s*\(\s*!\s*(?:defined|class_exists)\s*\(\s*[\'"]\\\\?([A-Za-z0-9_\\\\]+)[\'"]\s*\)[^;\n]*\breturn\b/', $code, $gm)) continue; + $guard = ltrim($gm[1], '\\'); + if (!preg_match_all('/\$def\(\s*\'([a-z0-9_]+)\'\s*,\s*\'((?:\\\\.|[^\'\\\\])*)\'/', $code, $mm, PREG_SET_ORDER)) continue; + foreach ($mm as $m) { + $name = $m[1]; + if (isset($live[$name])) continue; // plugin active → already a real tool + $desc = str_replace(["\\'", "\\\\"], ["'", "\\"], $m[2]); + $info = $labels[$guard] ?? [$this->ampHumanizeGuard($guard), '']; + $gid = 'unavail_' . strtolower(preg_replace('/[^A-Za-z0-9]+/', '_', $guard)); + if (!isset($groups[$gid])) $groups[$gid] = ['label' => $info[0], 'plugin' => $info[0], 'tools' => []]; + $groups[$gid]['tools'][$name] = ['label' => $this->prettyLabel($name), 'description' => $desc]; + } + } + foreach ($groups as $gid => $g) { if (empty($g['tools'])) { unset($groups[$gid]); continue; } ksort($groups[$gid]['tools']); } + uasort($groups, fn($a, $b) => strcmp($a['label'], $b['label'])); + return $groups; + } + + private function ampPluginLabels(): array { + return [ + 'WPB_VC_VERSION' => ['WPBakery Page Builder', 'js_composer'], + 'ELEMENTOR_VERSION' => ['Elementor', 'elementor'], + 'WooCommerce' => ['WooCommerce', 'woocommerce'], + 'WOOCOMMERCE_VERSION' => ['WooCommerce', 'woocommerce'], + 'ICL_SITEPRESS_VERSION' => ['WPML', 'wpml'], + 'WPSEO_VERSION' => ['Yoast SEO', 'wordpress-seo'], + 'WORDFENCE_VERSION' => ['Wordfence Security', 'wordfence'], + 'LSCWP_V' => ['LiteSpeed Cache', 'litespeed-cache'], + 'LSCWP_CUR_V' => ['LiteSpeed Cache', 'litespeed-cache'], + 'WP_ROCKET_VERSION' => ['WP Rocket', 'wp-rocket'], + 'RANK_MATH_VERSION' => ['Rank Math SEO', 'seo-by-rank-math'], + 'AIOSEO_VERSION' => ['All in One SEO', 'all-in-one-seo-pack'], + 'SEOPRESS_VERSION' => ['SEOPress', 'wp-seopress'], + 'GFCommon' => ['Gravity Forms', 'gravityforms'], + 'FLUENTFORM_VERSION' => ['Fluent Forms', 'fluentform'], + 'ACF' => ['Advanced Custom Fields', 'advanced-custom-fields'], + 'TRP_PLUGIN_VERSION' => ['TranslatePress', 'translatepress-multilingual'], + 'POLYLANG_VERSION' => ['Polylang', 'polylang'], + 'ET_CORE_VERSION' => ['Divi', 'divi'], + ]; + } + + private function ampHumanizeGuard(string $g): string { + $g = preg_replace('/_(VERSION|VER|PLUGIN|CORE)$/i', '', $g); + $g = trim(str_replace(['_', '\\'], ' ', $g)); + return $g !== '' ? ucwords(strtolower($g)) : 'Add-on'; + } + + +PHP; + + $anchor = " private function prettyLabel(string \$n): string {"; + $n = substr_count($src, $anchor); + if ($n !== 1) { fwrite(STDERR, "prettyLabel anchor count=$n; aborting\n"); exit(2); } + $src = str_replace($anchor, $methods . $anchor, $src); + + $renderAnchor = " \n "; + $n = substr_count($src, $renderAnchor); + if ($n !== 1) { fwrite(STDERR, "render anchor count=$n; aborting\n"); exit(3); } + + $renderBlock = <<<'HTML' + + getUnavailableGrouped(); if (!empty($amp_unavail)): ?> +
These tools stay off because the plugin that drives them isn’t active on this site. Install & activate it and they switch on automatically — listed here so you can see the full catalog.
+Requires — install & activate it to enable these tools.
+
+
+