-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathhide-youtube-related-videos.php
106 lines (89 loc) · 4.05 KB
/
hide-youtube-related-videos.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
<?php /*
**************************************************************************
Plugin Name: Hide YouTube Related Videos
Plugin URI: http://wordpress.org/extend/plugins/hide-youtube-related-videos/
Description: This is a simple plugin to keep the YouTube oEmbed from showing related videos.
Author: SparkWeb Interactive, Inc.
Version: 1.4.2
Author URI: http://www.soapboxdave.com/
**************************************************************************
Copyright (C) 2015 SparkWeb Interactive, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**************************************************************************/
//The Filter That Does the Work
add_filter('oembed_result', 'hide_youtube_related_videos', 10, 3);
function hide_youtube_related_videos($data, $url, $args = array()) {
//Autoplay
$autoplay = strpos($url, "autoplay=1") !== false ? "&autoplay=1" : "";
//Setup the string to inject into the url
$str_to_add = apply_filters("hyrv_extra_querystring_parameters", "wmode=transparent&") . 'rel=0';
//Regular oembed
if (strpos($data, "feature=oembed") !== false) {
$data = str_replace('feature=oembed', $str_to_add . $autoplay . '&feature=oembed', $data);
//Playlist
} elseif (strpos($data, "list=") !== false) {
$data = str_replace('list=', $str_to_add . $autoplay . '&list=', $data);
}
//All Set
return $data;
}
//Disable the Jetpack
add_filter('jetpack_shortcodes_to_include', 'hyrv_remove_jetpack_shortcode_function');
function hyrv_remove_jetpack_shortcode_function( $shortcodes ) {
$jetpack_shortcodes_dir = WP_CONTENT_DIR . '/plugins/jetpack/modules/shortcodes/';
$shortcodes_to_unload = array('youtube.php');
foreach ($shortcodes_to_unload as $shortcode) {
if ($key = array_search($jetpack_shortcodes_dir . $shortcode, $shortcodes)) {
unset($shortcodes[$key]);
}
}
return $shortcodes;
}
//On Activation, all oembed caches are cleared
register_activation_hook(__FILE__, 'hyrv_clear_cache');
function hyrv_clear_cache() {
global $wpdb;
$post_ids = $wpdb->get_col("SELECT DISTINCT post_id FROM $wpdb->postmeta WHERE meta_key LIKE '_oembed_%'");
if ($post_ids) {
$postmetaids = $wpdb->get_col("SELECT meta_id FROM $wpdb->postmeta WHERE meta_key LIKE '_oembed_%'");
$in = implode(',', array_fill(1, count($postmetaids), '%d'));
do_action('delete_postmeta', $postmetaids);
$wpdb->query($wpdb->prepare("DELETE FROM $wpdb->postmeta WHERE meta_id IN ($in)", $postmetaids));
do_action('deleted_postmeta', $postmetaids);
foreach ($post_ids as $post_id) {
wp_cache_delete($post_id, 'post_meta');
}
return true;
}
}
//Display Clear Oembed Cache Link on Plugin Screen
add_filter('plugin_action_links', 'hyrv_plugin_action_links', 10, 2);
function hyrv_plugin_action_links($links, $file) {
static $this_plugin;
if (!$this_plugin) {
$this_plugin = "hide-youtube-related-videos/hide-youtube-related-videos.php";
}
if ($file == $this_plugin) {
$custom_link = '<a href="' . admin_url('plugins.php?hyrv_clear_cache=1') . '">Clear Oembed Cache</a>';
array_unshift($links, $custom_link);
}
return $links;
}
//Clear Cache On Admin Page Load
add_action('admin_notices', 'hyrv_execute_cache_reload');
function hyrv_execute_cache_reload() {
if (isset($_GET['hyrv_clear_cache'])) {
hyrv_clear_cache();
echo '<div class="updated"><p><strong>Success!</strong> Your oembed cache has been cleared.</p></div>';
}
}