# yaymail/4.4.2/vendor-prefixed/src/Support/Slug.php

YayMail – WooCommerce Email Customizer, version 4.4.2. 43 lines.

- Page: https://pluginprobe.com/plugins/yaymail/4.4.2/code/vendor-prefixed/src/Support/Slug.php
- Raw: https://pluginprobe.com/plugins/yaymail/4.4.2/raw/vendor-prefixed/src/Support/Slug.php
- Modified: 2026-06-22T06:35:38+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/yaymail/4.4.2/code/vendor-prefixed/src/Support/Slug.php#L10-L20`.

```php
<?php

namespace YayMailScoped\YayCommerce\AdminShell\Support;

\defined('ABSPATH') || exit;
/**
 * Slug utilities.
 *
 * Plugin slugs can contain characters that are valid in wp_options keys
 * and URL paths but invalid as JavaScript identifiers (e.g. hyphens, '&').
 * This helper sanitizes a slug for use as a JS variable name while the
 * original slug stays intact for option-key and REST-URL contexts.
 * @internal
 */
class Slug
{
    /**
     * Convert a plugin slug into a valid JS identifier.
     *
     * Rules:
     *  - Replace any char outside [A-Za-z0-9_] with '_'
     *  - Collapse consecutive underscores
     *  - Trim leading/trailing underscores
     *  - Prefix '_' if the result starts with a digit
     *  - Return '_' for empty / all-special input (never empty string)
     *
     * Idempotent: to_var_name( to_var_name( $x ) ) === to_var_name( $x ).
     */
    public static function to_var_name(string $slug) : string
    {
        $name = \preg_replace('/[^A-Za-z0-9_]/', '_', $slug);
        $name = \preg_replace('/_+/', '_', (string) $name);
        $name = \trim((string) $name, '_');
        if ('' === $name) {
            return '_';
        }
        if (\preg_match('/^\\d/', $name)) {
            $name = '_' . $name;
        }
        return $name;
    }
}

```
