Automatic WordPress security: local firewall, malware and file-integrity scanning, vulnerability protection, quarantine, scheduled backups, an optional page cache, and automatic global threat intelligence. See README.md for installation, update, and uninstall instructions.
91 lines
2.8 KiB
PHP
91 lines
2.8 KiB
PHP
<?php
|
|
/**
|
|
* One-time production-cleanup pass: removes explanatory development
|
|
* comments from the plugin source, keeping only what WordPress or
|
|
* PHPCS actually need. Run once against the source tree, not part of
|
|
* the release build pipeline (that's bin/strip-comments.php, which
|
|
* does a separate whitespace-minification pass over a packaged copy).
|
|
*
|
|
* Kept:
|
|
* - the main plugin bootstrap file's header DocBlock (Plugin Name: ...),
|
|
* required for WordPress to recognise the plugin at all.
|
|
* - single-line `// phpcs:ignore` / `phpcs:disable` / `phpcs:enable`
|
|
* directives, which suppress specific static-analysis false
|
|
* positives rather than narrate implementation decisions.
|
|
* Removed: every other // and /* *\/ comment, including docblocks.
|
|
*
|
|
* Usage: php bin/strip-dev-comments.php <file-or-dir> [<file-or-dir> ...]
|
|
*/
|
|
|
|
function argus_should_keep_comment( string $text, bool $is_first_token_in_bootstrap_file ): bool {
|
|
if ( $is_first_token_in_bootstrap_file && false !== strpos( $text, 'Plugin Name:' ) ) {
|
|
return true;
|
|
}
|
|
$trimmed = ltrim( $text, "/ \t" );
|
|
return 0 === stripos( $trimmed, 'phpcs:' );
|
|
}
|
|
|
|
function argus_strip_file( string $path ): void {
|
|
$source = file_get_contents( $path );
|
|
$tokens = token_get_all( $source );
|
|
$is_bootstrap = ( 'argus-wordpress-defence.php' === basename( $path ) );
|
|
|
|
$out = '';
|
|
$seen_first_comment = false;
|
|
foreach ( $tokens as $token ) {
|
|
if ( is_array( $token ) ) {
|
|
list( $id, $text ) = $token;
|
|
if ( T_COMMENT === $id || T_DOC_COMMENT === $id ) {
|
|
$is_first = $is_bootstrap && ! $seen_first_comment;
|
|
$seen_first_comment = true;
|
|
if ( argus_should_keep_comment( $text, $is_first ) ) {
|
|
$out .= $text;
|
|
}
|
|
// else: drop the comment text entirely (whitespace/newlines
|
|
// around it are separate whitespace tokens, left untouched).
|
|
continue;
|
|
}
|
|
$out .= $text;
|
|
} else {
|
|
$out .= $token;
|
|
}
|
|
}
|
|
|
|
// Collapse runs of 3+ blank lines left behind by removed comment
|
|
// blocks down to a single blank line, purely cosmetic.
|
|
$out = preg_replace( "/\n{3,}/", "\n\n", $out );
|
|
|
|
file_put_contents( $path, $out );
|
|
}
|
|
|
|
function argus_collect_php_files( array $paths ): array {
|
|
$files = array();
|
|
foreach ( $paths as $p ) {
|
|
if ( is_dir( $p ) ) {
|
|
$it = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $p, FilesystemIterator::SKIP_DOTS ) );
|
|
foreach ( $it as $f ) {
|
|
if ( 'php' === strtolower( $f->getExtension() ) ) {
|
|
$files[] = $f->getPathname();
|
|
}
|
|
}
|
|
} elseif ( is_file( $p ) ) {
|
|
$files[] = $p;
|
|
}
|
|
}
|
|
return $files;
|
|
}
|
|
|
|
$args = array_slice( $argv, 1 );
|
|
if ( empty( $args ) ) {
|
|
fwrite( STDERR, "Usage: php bin/strip-dev-comments.php <file-or-dir> [...]\n" );
|
|
exit( 1 );
|
|
}
|
|
|
|
$files = argus_collect_php_files( $args );
|
|
$count = 0;
|
|
foreach ( $files as $file ) {
|
|
argus_strip_file( $file );
|
|
$count++;
|
|
}
|
|
echo "Stripped development comments from {$count} PHP files.\n";
|