| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Disable Embeds |
| 4 |
* Description: Don't like the enhanced embeds in WordPress 4.4? Easily disable the feature using this plugin. |
| 5 |
* Version: 1.5.0 |
| 6 |
* Author: Pascal Birchler |
| 7 |
* Author URI: https://pascalbirchler.com |
| 8 |
* License: GPLv2+ |
| 9 |
* |
| 10 |
* @package disable-embeds |
| 11 |
*/ |
| 12 |
|
| 13 |
/** |
| 14 |
* Disable embeds on init. |
| 15 |
* |
| 16 |
* - Removes the needed query vars. |
| 17 |
* - Disables oEmbed discovery. |
| 18 |
* - Completely removes the related JavaScript. |
| 19 |
* - Disables the core-embed/wordpress block type (WordPress 5.0+) |
| 20 |
* |
| 21 |
* @since 1.0.0 |
| 22 |
*/ |
| 23 |
function disable_embeds_init() { |
| 24 |
/* @var WP $wp */ |
| 25 |
global $wp; |
| 26 |
|
| 27 |
// Remove the embed query var. |
| 28 |
$wp->public_query_vars = array_diff( $wp->public_query_vars, array( |
| 29 |
'embed', |
| 30 |
) ); |
| 31 |
|
| 32 |
// Remove the oembed/1.0/embed REST route. |
| 33 |
add_filter( 'rest_endpoints', 'disable_embeds_remove_embed_endpoint' ); |
| 34 |
|
| 35 |
// Disable handling of internal embeds in oembed/1.0/proxy REST route. |
| 36 |
add_filter( 'oembed_response_data', 'disable_embeds_filter_oembed_response_data' ); |
| 37 |
|
| 38 |
// Turn off oEmbed auto discovery. |
| 39 |
add_filter( 'embed_oembed_discover', '__return_false' ); |
| 40 |
|
| 41 |
// Don't filter oEmbed results. |
| 42 |
remove_filter( 'oembed_dataparse', 'wp_filter_oembed_result', 10 ); |
| 43 |
|
| 44 |
// Remove oEmbed discovery links. |
| 45 |
remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); |
| 46 |
|
| 47 |
// Remove oEmbed-specific JavaScript from the front-end and back-end. |
| 48 |
remove_action( 'wp_head', 'wp_oembed_add_host_js' ); |
| 49 |
add_filter( 'tiny_mce_plugins', 'disable_embeds_tiny_mce_plugin' ); |
| 50 |
|
| 51 |
// Remove all embeds rewrite rules. |
| 52 |
add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' ); |
| 53 |
|
| 54 |
// Remove filter of the oEmbed result before any HTTP requests are made. |
| 55 |
remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 ); |
| 56 |
|
| 57 |
// Load block editor JavaScript. |
| 58 |
add_action( 'enqueue_block_editor_assets', 'disable_embeds_enqueue_block_editor_assets' ); |
| 59 |
|
| 60 |
// Remove wp-embed dependency of wp-edit-post script handle. |
| 61 |
add_action( 'wp_default_scripts', 'disable_embeds_remove_script_dependencies' ); |
| 62 |
} |
| 63 |
|
| 64 |
add_action( 'init', 'disable_embeds_init', 9999 ); |
| 65 |
|
| 66 |
/** |
| 67 |
* Removes the 'wpembed' TinyMCE plugin. |
| 68 |
* |
| 69 |
* @since 1.0.0 |
| 70 |
* |
| 71 |
* @param array $plugins List of TinyMCE plugins. |
| 72 |
* @return array The modified list. |
| 73 |
*/ |
| 74 |
function disable_embeds_tiny_mce_plugin( $plugins ) { |
| 75 |
return array_diff( $plugins, array( 'wpembed' ) ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Remove all rewrite rules related to embeds. |
| 80 |
* |
| 81 |
* @since 1.2.0 |
| 82 |
* |
| 83 |
* @param array $rules WordPress rewrite rules. |
| 84 |
* @return array Rewrite rules without embeds rules. |
| 85 |
*/ |
| 86 |
function disable_embeds_rewrites( $rules ) { |
| 87 |
foreach ( $rules as $rule => $rewrite ) { |
| 88 |
if ( false !== strpos( $rewrite, 'embed=true' ) ) { |
| 89 |
unset( $rules[ $rule ] ); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
return $rules; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Remove embeds rewrite rules on plugin activation. |
| 98 |
* |
| 99 |
* @since 1.2.0 |
| 100 |
*/ |
| 101 |
function disable_embeds_remove_rewrite_rules() { |
| 102 |
add_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' ); |
| 103 |
flush_rewrite_rules( false ); |
| 104 |
} |
| 105 |
|
| 106 |
register_activation_hook( __FILE__, 'disable_embeds_remove_rewrite_rules' ); |
| 107 |
|
| 108 |
/** |
| 109 |
* Flush rewrite rules on plugin deactivation. |
| 110 |
* |
| 111 |
* @since 1.2.0 |
| 112 |
*/ |
| 113 |
function disable_embeds_flush_rewrite_rules() { |
| 114 |
remove_filter( 'rewrite_rules_array', 'disable_embeds_rewrites' ); |
| 115 |
flush_rewrite_rules( false ); |
| 116 |
} |
| 117 |
|
| 118 |
register_deactivation_hook( __FILE__, 'disable_embeds_flush_rewrite_rules' ); |
| 119 |
|
| 120 |
/** |
| 121 |
* Removes the oembed/1.0/embed REST route. |
| 122 |
* |
| 123 |
* @since 1.4.0 |
| 124 |
* |
| 125 |
* @param array $endpoints Registered REST API endpoints. |
| 126 |
* @return array Filtered REST API endpoints. |
| 127 |
*/ |
| 128 |
function disable_embeds_remove_embed_endpoint( $endpoints ) { |
| 129 |
unset( $endpoints['/oembed/1.0/embed'] ); |
| 130 |
|
| 131 |
return $endpoints; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* Disables sending internal oEmbed response data in proxy endpoint. |
| 136 |
* |
| 137 |
* @since 1.4.0 |
| 138 |
* |
| 139 |
* @param array $data The response data. |
| 140 |
* @return array|false Response data or false if in a REST API context. |
| 141 |
*/ |
| 142 |
function disable_embeds_filter_oembed_response_data( $data ) { |
| 143 |
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 144 |
return false; |
| 145 |
} |
| 146 |
|
| 147 |
return $data; |
| 148 |
} |
| 149 |
|
| 150 |
/** |
| 151 |
* Enqueues JavaScript for the block editor. |
| 152 |
* |
| 153 |
* @since 1.4.0 |
| 154 |
* |
| 155 |
* This is used to unregister the `core-embed/wordpress` block type. |
| 156 |
*/ |
| 157 |
function disable_embeds_enqueue_block_editor_assets() { |
| 158 |
$asset_file = plugin_dir_path( __FILE__ ) . 'build/index.asset.php'; |
| 159 |
$asset = is_readable( $asset_file ) ? require $asset_file : []; |
| 160 |
|
| 161 |
$asset['dependencies'] = isset( $asset['dependencies'] ) ? $asset['dependencies'] : []; |
| 162 |
$asset['version'] = isset( $asset['version'] ) ? $asset['version'] : ''; |
| 163 |
|
| 164 |
wp_enqueue_script( |
| 165 |
'disable-embeds', |
| 166 |
plugins_url( 'build/index.js', __FILE__ ), |
| 167 |
$asset['dependencies'], |
| 168 |
$asset['version'], |
| 169 |
true |
| 170 |
); |
| 171 |
} |
| 172 |
|
| 173 |
/** |
| 174 |
* Removes wp-embed dependency of core packages. |
| 175 |
* |
| 176 |
* @since 1.4.0 |
| 177 |
* |
| 178 |
* @param WP_Scripts $scripts WP_Scripts instance, passed by reference. |
| 179 |
*/ |
| 180 |
function disable_embeds_remove_script_dependencies( $scripts ) { |
| 181 |
if ( ! empty( $scripts->registered['wp-edit-post'] ) ) { |
| 182 |
$scripts->registered['wp-edit-post']->deps = array_diff( |
| 183 |
$scripts->registered['wp-edit-post']->deps, |
| 184 |
array( 'wp-embed' ) |
| 185 |
); |
| 186 |
} |
| 187 |
} |
| 188 |
|