<?php
/**
 * Name: Disable Embeds
 * Description: Disable WordPress embed functionality and related scripts to improve page load speed by removing oEmbed discovery links and unnecessary JavaScript files from frontend
 * Category: Optimization
 * Icon: -
 * UseCases: [
 *   {
 *     "title": "Remove Unused Scripts",
 *     "description": "Disable WordPress embed functionality and wp-embed.min.js script when you never embed WordPress posts on your site."
 *   },
 *   {
 *     "title": "Security Hardening",
 *     "description": "Block oEmbed endpoint to prevent your site content from being embedded on external sites without permission."
 *   },
 *   {
 *     "title": "Faster Page Load",
 *     "description": "Eliminate embed detection scripts and rewrite rules to reduce HTTP requests and improve frontend performance."
 *   }
 * ]
 * Added: 2025-06-30
 * Tags: embeds, oembed, disable, performance, scripts
 * WhereToFind: {
 *   "location": "Page Source (Frontend)",
 *   "url": "/",
 *   "instructions": "View page source - wp-embed.min.js script should be removed from the <head> section",
 *   "newTab": true
 * }
 */

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

class SwitchboardDisableEmbeds {

    public function __construct() {
        add_action('init', [$this, 'disable_embeds'], 9999);
    }

    /**
     * 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_embeds() {
        global $wp;
        if (!empty($wp) && isset($wp->public_query_vars) && is_array($wp->public_query_vars)) {
            $wp->public_query_vars = array_diff($wp->public_query_vars, ['embed']);
        }

        remove_action('rest_api_init', 'wp_oembed_register_route');
        remove_filter('oembed_dataparse', 'wp_filter_oembed_result', 10);
        remove_action('wp_head', 'wp_oembed_add_discovery_links');
        remove_action('wp_head', 'wp_oembed_add_host_js');

        add_filter('embed_oembed_discover', '__return_false');
        add_filter('tiny_mce_plugins', [$this, 'disable_embeds_tiny_mce_plugin']);
        add_filter('rewrite_rules_array', [$this, 'disable_embeds_rewrites']);

        remove_filter('pre_oembed_result', 'wp_filter_pre_oembed_result', 10);
    }

    public function disable_embeds_tiny_mce_plugin($plugins) {
        if (is_array($plugins)) {
            return array_diff($plugins, ['wpembed']);
        }
        return $plugins;
    }

    public function disable_embeds_rewrites($rules) {
        if (!is_array($rules)) {
            return $rules;
        }

        foreach ($rules as $rule => $rewrite) {
            if (is_string($rewrite) && strpos($rewrite, 'embed=true') !== false) {
                unset($rules[$rule]);
            }
        }
        return $rules;
    }
}

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