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