settings = Plugin::get_settings( 'security' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !empty( $this->settings['disable_embeds'] ) ) { if( $this->is_provider_disabled() ) { remove_action( 'wp_head', 'wp_oembed_add_discovery_links' ); remove_filter( 'rest_pre_serve_request', '_oembed_rest_pre_serve_request', 10 ); add_filter( 'rewrite_rules_array', [ $this, 'rewrite_rules_array' ] ); add_filter( 'query_vars', [ $this, 'query_vars' ] ); add_filter( 'rest_endpoints', [ $this, 'rest_endpoints' ] ); add_action( 'template_redirect', [ $this, 'template_redirect' ], 11 ); } if( $this->is_autoembed_disabled() ) { remove_action( 'wp_head', 'wp_oembed_add_host_js' ); add_action( 'wp_footer', [ $this, 'wp_footer' ], 1 ); add_action( 'init', [ $this, 'remove_autoembed' ], 20 ); add_filter( 'tiny_mce_plugins', [ $this, 'tiny_mce_plugins' ] ); } remove_filter( 'pre_oembed_result', 'wp_filter_pre_oembed_result', 10 ); add_filter( 'embed_oembed_discover', '__return_false' ); } } /** * Tells whether other sites are stopped from embedding this site's posts. * Sites that saved the switch before the split have no value yet, so they follow the main switch. * * @return bool */ private function is_provider_disabled(): bool { if( isset( $this->settings['disable_embeds_provider'] ) ) { return !empty( $this->settings['disable_embeds_provider'] ); } return !empty( $this->settings['disable_embeds'] ); } /** * Tells whether pasted links are kept as plain links. Off until the owner turns it on. * * @return bool */ private function is_autoembed_disabled(): bool { return !empty( $this->settings['disable_embeds_autoembed'] ); } /** * Stops WordPress from turning pasted links into embedded players in posts and widgets. * The [embed] shortcode keeps working. * * @return void */ public function remove_autoembed(): void { global $wp_embed; if( !$wp_embed instanceof \WP_Embed ) { return; } remove_filter( 'the_content', [ $wp_embed, 'autoembed' ], 8 ); remove_filter( 'widget_text_content', [ $wp_embed, 'autoembed' ], 8 ); remove_filter( 'widget_block_content', [ $wp_embed, 'autoembed' ], 8 ); } /** * Answers 404 when an embed URL still reaches the template loader. * * @return void */ public function template_redirect(): void { global $wp_query; if( is_embed() ) { $wp_query->set_404(); $wp_query->is_embed = false; status_header( 404 ); nocache_headers(); } } /** * Adds a settings field for disabling WordPress embeds to the provided array of fields. * This field allows users to toggle the embed functionality, which automatically converts * certain URLs into embedded content. Disabling embeds can enhance site performance and * privacy, as well as prevent other sites from embedding content without permission. * * @param array $fields An array of existing settings fields. * * @return array The modified array of settings fields, including the new 'disable embeds' option. */ public function adminease_settings_fields( array $fields ): array { $main_on = !empty( $this->settings['disable_embeds'] ); $fields['security']['fields'][] = [ 'type' => 'switch', 'id' => 'disable-embeds', 'name' => 'adminease[security][disable_embeds]', 'value' => $this->settings['disable_embeds'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Disable embeds', 'adminease' ), 'description' => __( 'WordPress can turn links from sites like YouTube or X into embedded players, and lets other sites embed your posts. Embeds can make pages heavier and send visitors to outside services. Turn this on, then choose which parts to switch off below.', 'adminease' ), 'child_fields' => [ [ 'type' => 'switch', 'id' => 'disable-embeds-provider', 'name' => 'adminease[security][disable_embeds_provider]', 'value' => $this->settings['disable_embeds_provider'] ?? $main_on, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Stop other sites embedding your posts', 'adminease' ), 'description' => __( 'Other sites can no longer show your posts as preview cards.', 'adminease' ), 'attributes' => [ 'data-parent' => 'disable-embeds', ], ], [ 'type' => 'switch', 'id' => 'disable-embeds-autoembed', 'name' => 'adminease[security][disable_embeds_autoembed]', 'value' => $this->settings['disable_embeds_autoembed'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input', 'wrapper_class' => 'form-group-child', 'label' => __( 'Show pasted links as plain links', 'adminease' ), 'description' => __( 'YouTube, X and similar links in your posts and widgets stay as links instead of becoming embedded players. Embeds inside block theme templates and patterns are not affected.', 'adminease' ), 'attributes' => [ 'data-parent' => 'disable-embeds', ], ], ], ]; return $fields; } /** * Removes the WordPress embed script from the footer. * This method dequeues the 'wp-embed' script, which is * responsible for handling oEmbed functionality in WordPress, * in order to prevent its loading on the site. * @return void */ public function wp_footer(): void { wp_dequeue_script( 'wp-embed' ); } /** * Filters and removes rewrite rules related to embedding functionality. * This method iterates through the existing WordPress rewrite rules * and removes any rules that contain the 'embed=true' query parameter, * effectively disabling embed-specific rewrite rules. * * @param array $rules The array of rewrite rules to be filtered. * * @return array The filtered array of rewrite rules with embed-related rules removed. */ public function rewrite_rules_array( array $rules ): array { foreach( $rules as $rule => $rewrite ) { if( false !== strpos( $rewrite, 'embed=true' ) ) { unset( $rules[ $rule ] ); } } return $rules; } /** * Filters the query variables to remove the 'embed' query variable. * This modification ensures that the 'embed' parameter is excluded * from the recognized query variables within WordPress, potentially * disabling embed functionalities. * * @param array $vars An array of existing query variables. * * @return array The filtered array of query variables. */ public function query_vars( array $vars ): array { return array_diff( $vars, [ 'embed' ] ); } /** * Filters the list of TinyMCE plugins to remove the embed functionality. * This method removes the 'wpembed' plugin from the list of TinyMCE plugins, * effectively disabling embed-related features in the TinyMCE editor. * * @param array $plugins The list of TinyMCE plugins currently enabled. * * @return array The filtered list of TinyMCE plugins with 'wpembed' removed. */ public function tiny_mce_plugins( array $plugins ): array { return array_diff( $plugins, [ 'wpembed' ] ); } /** * Removes specific oEmbed REST API endpoints from the list of registered endpoints. * This method unsets the public '/oembed/1.0/embed' endpoint. The '/oembed/1.0/proxy' * endpoint stays, because the block editor needs it to preview pasted links. * * @param array $endpoints An associative array of registered REST API endpoints. * * @return array The modified array of REST API endpoints with specific oEmbed endpoints removed. */ public function rest_endpoints( array $endpoints ): array { unset( $endpoints['/oembed/1.0/embed'] ); return $endpoints; } }