<?php
/**
 * Name: Disable Emoji Support
 * Description: Remove emoji scripts, styles, and DNS prefetch from frontend to improve page load speed by eliminating unnecessary JavaScript files and requests
 * Category: Optimization
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Page Speed Improvement",
 *     "description": "Remove emoji JavaScript and CSS files to eliminate unnecessary HTTP requests on business sites that never use emojis."
 *   },
 *   {
 *     "title": "Professional Site Performance",
 *     "description": "Clean up corporate websites by removing emoji scripts that add bloat without providing value."
 *   },
 *   {
 *     "title": "Core Web Vitals",
 *     "description": "Reduce JavaScript execution time by removing unused emoji detection script on e-commerce sites focused on speed."
 *   }
 * ]
 * Added: 2025-06-08
 * Tags: emoji, emoticons, performance, scripts, optimization
 * WhereToFind: {
 *   "location": "Works on frontend automatically",
 *   "url": "",
 *   "instructions": "Emoji scripts and styles removed from all pages",
 *   "newTab": false
 * }
 */

// Prevent direct access
if (!defined('ABSPATH')) exit;

class SwitchboardDisableEmoji {

    public function __construct() {
        // Remove emoji support immediately
        $this->disable_emoji();
    }

    /**
     * Register data locations for cleanup system
     *
     * @return array Data storage locations
     */
    public function register_data_locations() {
        return [
            'options' => [],
            'option_patterns' => [],
            'tables' => [],
            'post_meta' => [],
            'user_meta' => [],
        ];
    }

    public function disable_emoji() {
        // Remove emoji support from frontend only
        remove_action('wp_head', 'print_emoji_detection_script', 7);
        remove_action('wp_print_styles', 'print_emoji_styles');
        remove_filter('the_content_feed', 'wp_staticize_emoji');
        remove_filter('comment_text_rss', 'wp_staticize_emoji');
        remove_filter('wp_mail', 'wp_staticize_emoji_for_email');

        add_filter('wp_resource_hints', [$this, 'disable_emojis_dns_prefetch'], 10, 2);

        // Remove emoji scripts from embed
        add_filter('emoji_svg_url', '__return_false'); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WordPress filter
    }

    public function disable_emojis_dns_prefetch($urls, $relation_type) {
        if ($relation_type === 'dns-prefetch' && is_array($urls)) {
            $emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/'); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Core WordPress filter
            $urls = array_diff($urls, [$emoji_svg_url]);
        }
        return $urls;
    }
}

// Initialize and register with data system
$switchboard_instance = new SwitchboardDisableEmoji();
Switchboard_Data_Registry::get_instance()->auto_register($switchboard_instance, __FILE__);
