WP-CLIwp easy-svg icons validate

wp easy-svg icons validate

Check a folder of SVG files against this site's sanitizer and hardener without importing anything, and fail a CI job with --strict when a file would be skipped.

WP-CLI command · Easy SVG Pro · Status: Stable

This command is a dry run. It reads every SVG in a folder, runs each one through exactly the sanitizer and hardener an import would use on this site, and tells you per file whether it would be kept. Nothing is written, nothing is registered. With --strict it fails as soon as one file would be skipped, which turns it into a quality gate for your icon repository.

Usage

wp easy-svg icons validate <dir> [--strict]

When to use it

  • Before you commit icons from a designer into a theme's assets/icons folder or a drop-in folder.
  • In CI, on every pull request that touches icons, so a file the site would refuse never reaches production.
  • Before wp easy-svg icons import, to see what would be copied and what would be skipped.
  • After you widened or narrowed the allow-list in the Konfigurator, to see which icons are affected on this site.

Do not use it to check a ZIP pack. It only reads loose .svg files in one folder. Import the pack on a staging site with wp easy-svg pack import instead.

Parameters

query
dirpositional
Required

The folder to check. Only *.svg files directly inside it are read, not subfolders. Relative paths are resolved from the directory WP-CLI runs in.

query
--strictflag

After printing the table, stop with an error and exit code 1 as soon as one row is not ok.

Return value

stdouttable

One row per file with the columns file, slug, status and reason. status is ok or skipped. reason is empty for ok, otherwise one of bad-name, duplicate, unreadable, not-svg or no-drawing.

exit codeint

0 without --strict, whatever the table says. With --strict, 1 when at least one file was skipped. Also 1 when the licence or the WP-CLI feature is off, when Easy SVG Support is inactive, or when it is too old to harden icons.

Default

Without --strict the command only reports and always exits 0 once it ran.

Examples

Check a folder by hand

wp easy-svg icons validate ./wp-content/themes/acme/assets/icons
+----------------------+----------+---------+------------+
| file                 | slug     | status  | reason     |
+----------------------+----------+---------+------------+
| .../icons/phone.svg  | phone    | ok      |            |
| .../icons/Phone.svg  | phone    | skipped | duplicate  |
| .../icons/empty.svg  | empty    | skipped | no-drawing |
+----------------------+----------+---------+------------+

Gate a pull request in CI

#!/usr/bin/env bash
set -euo pipefail

ICONS_DIR="wp-content/themes/acme/assets/icons"

# A wrong path would pass silently, so make sure there is something to check.
count=$(find "$ICONS_DIR" -maxdepth 1 -name '*.svg' | wc -l)
if [ "$count" -eq 0 ]; then
	echo "No SVG files found in $ICONS_DIR" >&2
	exit 1
fi

wp easy-svg icons validate "$ICONS_DIR" --strict

Check every subfolder of an icon repository

The command is not recursive, so loop over the folders yourself and collect the result:

status=0
for dir in icons/*/; do
	echo "== $dir"
	wp easy-svg icons validate "$dir" --strict || status=1
done
exit $status

Defensive: tell a broken setup apart from a bad icon

if ! wp plugin is-active easy-svg; then
	echo "Easy SVG Support is not active, nothing can be validated." >&2
	exit 2
fi
wp easy-svg icons validate ./icons --strict

How it works

  1. The guard checks that the licence is active and the WP-CLI feature is on. No user or capability is needed.
  2. The command looks up the site's sanitizer from Easy SVG Support and checks that easy_svg_harden_icon_markup() exists. Without either it stops, because a result from a weaker check would not match what an import stores.
  3. It lists <dir>/*.svg and handles each file in that order:
    • The slug is the file name without .svg, lower cased, with every run of other characters turned into -. A name that makes no valid slug is bad-name.
    • A second file with the same slug (Phone.svg and phone.svg) is duplicate. The first one wins.
    • An empty or unreadable file is unreadable.
    • The sanitizer refuses it: not-svg.
    • After hardening there is no <svg> with at least one child element left: no-drawing.
  4. The table is printed. With --strict the command then stops at the first row that is not ok.

The check uses this site's own allow-list, including tags or attributes added through the Konfigurator or the esw_svg_allowed_tags filter. The same folder can pass on one site and fail on another.

Pitfalls

  • A wrong path passes. A folder that does not exist or holds no .svg files gives an empty table and exit code 0, even with --strict. Check that the folder has files before you trust a green CI run.
  • Not recursive. Files in subfolders are not checked. Loop over subfolders yourself.
  • No size check. Files are not measured against easy_svg_pro_source_file_max_bytes (2 MB). A file that validates can still be refused as too-large when the folder is used as a source.
  • The manifest is ignored. manifest.json is not read. Names come from file names only, so a file the manifest renames is checked under its file name, and a broken manifest does not fail validation even though it would register nothing as a source. Use wp easy-svg icons problems on the live folder for manifest errors.
  • Without --strict CI stays green. The plain command always exits 0 once it ran.
  • Upper case extensions. The listing matches *.svg exactly, so ICON.SVG is not read on a case sensitive file system.