# yaymail/4.4.5/src/SocialIcons/SocialIconImageCache.php

YayMail – WooCommerce Email Customizer, version 4.4.5. 55 lines.

- Page: https://pluginprobe.com/plugins/yaymail/4.4.5/code/src/SocialIcons/SocialIconImageCache.php
- Raw: https://pluginprobe.com/plugins/yaymail/4.4.5/raw/src/SocialIcons/SocialIconImageCache.php
- Modified: 2026-08-23T09:24:58+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.5/code/src/SocialIcons/SocialIconImageCache.php#L10-L20`.

```php
<?php

namespace YayMail\SocialIcons;

/**
 * On-disk render cache for tinted social icon PNGs.
 *
 * Keyed by a caller-supplied hash (icon+color+size+mask-mtime), so a given
 * combination is generated by GD at most once per server; every later
 * request for the same combination is served straight off disk.
 */
class SocialIconImageCache {

    private const DIR_NAME = 'yaymail-social-icons';

    /**
     * Resolve (and lazily create) the on-disk cache path for a cache key.
     * Returns null when the uploads dir is unavailable/unwritable, in which
     * case the caller just falls back to generating on every request.
     *
     * @param string $cache_key Hash of icon+color+size+mask-mtime.
     */
    public static function get_path( $cache_key ) {
        $upload_dir = wp_upload_dir();
        if ( ! empty( $upload_dir['error'] ) ) {
            return null;
        }

        $dir = trailingslashit( $upload_dir['basedir'] ) . self::DIR_NAME . '/';
        if ( ! wp_mkdir_p( $dir ) ) {
            return null;
        }

        return $dir . $cache_key . '.png';
    }

    /**
     * Write PNG bytes to the cache atomically (temp file + rename) so a
     * concurrent request never reads a half-written file.
     *
     * @param string $cache_path Destination path from get_path().
     * @param string $png_data   Raw PNG bytes.
     */
    public static function write( $cache_path, $png_data ) {
        $tmp_path = $cache_path . '.' . uniqid( '', true ) . '.tmp';
        if ( false === file_put_contents( $tmp_path, $png_data ) ) { // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_put_contents
            return;
        }

        if ( ! rename( $tmp_path, $cache_path ) ) {
            wp_delete_file( $tmp_path );
        }
    }
}

```
