<?php
/**
 * Pawfect Emoji Disable
 *
 * Disables WordPress emoji conversion to prevent external s.w.org resource loading.
 * This ensures GDPR compliance.
 *
 * @package Pawfect
 * @since 1.0.0
 */

// Exit if accessed directly.
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Disable WordPress Emoji Conversion
 * Prevents WP from converting native emojis to external s.w.org images
 */
function pawfect_disable_emojis() {
    remove_action('wp_head', 'print_emoji_detection_script', 7);
    remove_action('admin_print_scripts', 'print_emoji_detection_script');
    remove_action('wp_print_styles', 'print_emoji_styles');
    remove_action('admin_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');
    
    // Remove from TinyMCE
    add_filter('tiny_mce_plugins', 'pawfect_disable_emojis_tinymce');
    
    // Remove DNS prefetch for s.w.org
    add_filter('wp_resource_hints', 'pawfect_disable_emojis_dns_prefetch', 10, 2);
}
add_action('init', 'pawfect_disable_emojis');

/**
 * Remove wpemoji from TinyMCE plugins
 *
 * @param array $plugins TinyMCE plugins array.
 * @return array Modified plugins array.
 */
function pawfect_disable_emojis_tinymce($plugins) {
    if (is_array($plugins)) {
        return array_diff($plugins, array('wpemoji'));
    }
    return array();
}

/**
 * Remove emoji DNS prefetch
 *
 * @param array  $urls          URLs to prefetch.
 * @param string $relation_type Relation type.
 * @return array Modified URLs array.
 */
function pawfect_disable_emojis_dns_prefetch($urls, $relation_type) {
    if ('dns-prefetch' === $relation_type) {
        $urls = array_filter($urls, function($url) {
            return false === strpos($url, 'https://s.w.org/images/core/emoji/');
        });
    }
    return $urls;
}
