.png); the dashboard never hot-links third-party * servers. Missing icons can be fetched once, on request, into the plugin's * own uploads folder: wp-content/uploads/aeoora/icons/ (resolved at runtime * with wp_upload_dir()). * * @package aeoora */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Favicons. */ class Aeoora_Favicons { /** * File slug for a domain. * * @param string $domain Domain. * @return string */ public static function slug( $domain ) { $domain = strtolower( preg_replace( '~^www\.~i', '', trim( (string) $domain ) ) ); return preg_replace( '~[^a-z0-9.-]~', '', $domain ); } /** * Local URL of a domain's icon, or '' (the UI then draws a monogram). * * @param string $domain Domain. * @return string */ public static function url( $domain ) { $slug = self::slug( $domain ); if ( '' === $slug || ! Aeoora_Settings::get( 'general', 'favicons', true ) ) { return ''; } foreach ( array( 'png', 'svg', 'ico' ) as $ext ) { if ( file_exists( AEOORA_DIR . 'assets/icons/' . $slug . '.' . $ext ) ) { return AEOORA_URL . 'assets/icons/' . $slug . '.' . $ext; } } $up = Aeoora_Util::upload_dir( 'icons' ); if ( $up ) { foreach ( array( 'png', 'ico' ) as $ext ) { if ( file_exists( $up['dir'] . '/' . $slug . '.' . $ext ) ) { return $up['url'] . '/' . $slug . '.' . $ext; } } } return ''; } /** * Fetch missing icons (admin action) directly from each domain's own site. * * @param int $max Max to fetch in one go. * @return array { fetched, missing } */ public static function fetch_missing( $max = 40 ) { $domains = array(); foreach ( Aeoora_Bots::all() as $bot ) { if ( $bot['domain'] ) { $domains[ self::slug( $bot['domain'] ) ] = $bot['domain']; } } foreach ( Aeoora_Referrals::sources() as $src ) { $domains[ self::slug( $src['domain'] ) ] = $src['domain']; } $up = Aeoora_Util::upload_dir( 'icons' ); if ( ! $up ) { return array( 'fetched' => 0, 'missing' => count( $domains ), ); } $fetched = 0; $missing = 0; foreach ( $domains as $slug => $domain ) { if ( '' !== self::url( $domain ) ) { continue; } if ( $fetched >= $max ) { $missing++; continue; } $saved = false; foreach ( array( 'https://' . $domain . '/favicon.ico', 'https://www.' . $domain . '/favicon.ico', 'https://' . $domain . '/apple-touch-icon.png' ) as $src ) { $res = wp_safe_remote_get( $src, array( 'timeout' => 8, 'redirection' => 3, 'limit_response_size' => 262144, ) ); if ( is_wp_error( $res ) || 200 !== (int) wp_remote_retrieve_response_code( $res ) ) { continue; } $body = (string) wp_remote_retrieve_body( $res ); $ext = self::sniff( $body ); if ( $ext ) { // $up['dir'] is wp_upload_dir()['basedir'] . '/aeoora/icons' (Aeoora_Util::upload_dir()). file_put_contents( trailingslashit( $up['dir'] ) . $slug . '.' . $ext, $body ); // phpcs:ignore WordPress.WP.AlternativeFunctions $saved = true; break; } } if ( $saved ) { $fetched++; } else { $missing++; } } return array( 'fetched' => $fetched, 'missing' => $missing, ); } /** * Accept only real PNG / ICO bytes (never SVG or HTML from remote). * * @param string $bytes Bytes. * @return string Extension or ''. */ private static function sniff( $bytes ) { if ( strlen( $bytes ) < 32 ) { return ''; } if ( "\x89PNG\r\n\x1a\n" === substr( $bytes, 0, 8 ) ) { return 'png'; } if ( "\x00\x00\x01\x00" === substr( $bytes, 0, 4 ) ) { return 'ico'; } return ''; } }