| 1 |
<?php |
| 2 |
namespace epiphyt\Embed_Privacy; |
| 3 |
|
| 4 |
/** |
| 5 |
* Two click embed main class. |
| 6 |
* |
| 7 |
* @author Epiphyt |
| 8 |
* @license GPL2 |
| 9 |
* @package epiphyt\Embed_Privacy |
| 10 |
* @version 0.1 |
| 11 |
*/ |
| 12 |
class Embed_Privacy { |
| 13 |
/** |
| 14 |
* @var bool Determine if we use the cache |
| 15 |
*/ |
| 16 |
private $usecache = false; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var array The supported media providers |
| 20 |
*/ |
| 21 |
public $embed_providers = [ |
| 22 |
'animoto.com' => 'Animoto', |
| 23 |
'cloudup.com' => 'Cloudup', |
| 24 |
'collegehumor.com' => 'CollegeHumor', |
| 25 |
'dailymotion.com' => 'DailyMotion', |
| 26 |
'facebook.com' => 'Facebook', |
| 27 |
'flickr.com' => 'Flickr', |
| 28 |
'funnyordie.com' => 'Funny Or Die', |
| 29 |
'hulu.com' => 'Hulu', |
| 30 |
'imgur.com' => 'Imgur', |
| 31 |
'instagram.com' => 'Instagram', |
| 32 |
'issuu.com' => 'Issuu', |
| 33 |
'kickstarter.com' => 'Kickstarter', |
| 34 |
'meetup.com' => 'Meetup', |
| 35 |
'mixcloud.com' => 'Mixcloud', |
| 36 |
'photobucket.com' => 'Photobucket', |
| 37 |
'polldaddy.com' => 'Polldaddy.com', |
| 38 |
'reddit.com' => 'Reddit', |
| 39 |
'reverbnation.com' => 'ReverbNation', |
| 40 |
'scribd.com' => 'Scribd', |
| 41 |
'sketchfab.com' => 'Sketchfab', |
| 42 |
'slideshare.com' => 'SlideShare', |
| 43 |
'smugmug.com' => 'SmugMug', |
| 44 |
'soundcloud.com' => 'SoundCloud', |
| 45 |
'speakerdeck.com' => 'Speaker Deck', |
| 46 |
'spotify.com' => 'Spotify', |
| 47 |
'ted.com' => 'TED', |
| 48 |
'tumblr.com' => 'Tumblr', |
| 49 |
'twitter.com' => 'Twitter', |
| 50 |
'videopress.com' => 'VideoPres', |
| 51 |
'vimeo.com' => 'Vimeo', |
| 52 |
'wordpress.org/plugins' => 'WordPress.org', |
| 53 |
'wordpress.tv' => 'WordPress.tv', |
| 54 |
'youtu.be' => 'YouTube', |
| 55 |
'youtube.com' => 'YouTube', |
| 56 |
]; |
| 57 |
|
| 58 |
/** |
| 59 |
* Embed Privacy constructor. |
| 60 |
*/ |
| 61 |
public function __construct() { |
| 62 |
// actions |
| 63 |
\add_action( 'init', [ $this, 'load_textdomain' ] ); |
| 64 |
\add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_assets' ] ); |
| 65 |
|
| 66 |
$this->usecache = ! \is_admin(); |
| 67 |
|
| 68 |
// filters |
| 69 |
if ( ! $this->usecache ) { |
| 70 |
// set ttl to 0 in admin |
| 71 |
\add_filter( 'oembed_ttl', function( $time ) { |
| 72 |
return 0; |
| 73 |
}, 10, 1 ); |
| 74 |
} |
| 75 |
|
| 76 |
\add_filter( 'embed_oembed_html', [ $this, 'replace_embeds' ], 10, 3 ); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Embeds are cached in the postmeta database table and need to be removed |
| 81 |
* whenever the plugin will be enabled or disabled. |
| 82 |
*/ |
| 83 |
public function clear_embed_cache() { |
| 84 |
global $wpdb; |
| 85 |
|
| 86 |
// the query to delete cache |
| 87 |
$query = "DELETE FROM " . $wpdb->get_blog_prefix() . "postmeta |
| 88 |
WHERE meta_key LIKE '%_oembed_%'"; |
| 89 |
|
| 90 |
if ( \is_plugin_active_for_network( 'embed-privacy/embed-privacy.php' ) ) { |
| 91 |
// on networks we need to iterate through every site |
| 92 |
$sites = \get_sites( 99999 ); |
| 93 |
|
| 94 |
foreach ( $sites as $site ) { |
| 95 |
\switch_to_blog( $site ); |
| 96 |
|
| 97 |
$wpdb->query( $query ); |
| 98 |
} |
| 99 |
} |
| 100 |
else { |
| 101 |
$wpdb->query( $query ); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Enqueue our assets for the frontend. |
| 107 |
*/ |
| 108 |
public function enqueue_assets() { |
| 109 |
$suffix = ( \defined( 'DEBUG_MODE' ) && \DEBUG_MODE ? '' : '.min' ); |
| 110 |
$css_file = \EPI_EMBED_PRIVACY_BASE . 'assets/style/embed-privacy' . $suffix . '.css'; |
| 111 |
$css_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/style/embed-privacy' . $suffix . '.css'; |
| 112 |
|
| 113 |
\wp_enqueue_style( 'embed-privacy', $css_file_url, [], \filemtime( $css_file ) ); |
| 114 |
|
| 115 |
$js_file = \EPI_EMBED_PRIVACY_BASE . 'assets/js/embed-privacy' . $suffix . '.js'; |
| 116 |
$js_file_url = \EPI_EMBED_PRIVACY_URL . 'assets/js/embed-privacy' . $suffix . '.js'; |
| 117 |
|
| 118 |
\wp_enqueue_script( 'two-click_embed', $js_file_url, [], \filemtime( $js_file ) ); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* Load the translation files. |
| 123 |
*/ |
| 124 |
public function load_textdomain() { |
| 125 |
\load_plugin_textdomain( 'embed-privacy', false, \EPI_EMBED_PRIVACY_BASE . 'languages' ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Replace embeds with a container and hide the embed with an HTML comment. |
| 130 |
* |
| 131 |
* @param string $output |
| 132 |
* @param string $url |
| 133 |
* @param array $args |
| 134 |
* @return string |
| 135 |
*/ |
| 136 |
public function replace_embeds( $output, $url, $args ) { |
| 137 |
// don't do anything in admin |
| 138 |
if ( ! $this->usecache ) return $output; |
| 139 |
|
| 140 |
$embed_provider = ''; |
| 141 |
|
| 142 |
// get embed provider name |
| 143 |
foreach ( $this->embed_providers as $url_part => $name ) { |
| 144 |
// save name of provider and stop loop |
| 145 |
if ( \strpos( $url, $url_part ) !== false ) { |
| 146 |
$embed_provider = $name; |
| 147 |
break; |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// add two click to markup |
| 152 |
$embed_class = ' embed-' . ( ! empty( $embed_provider ) ? \sanitize_title( $embed_provider ) : 'default' ); |
| 153 |
$height = ( ! empty( $args['height'] ) && $args['height'] <= $args['width'] ? 'height: ' . $args['height'] . 'px;' : 'height: 300px;' ); |
| 154 |
$width = ( ! empty( $args['width'] ) ? 'width: ' . $args['width'] . 'px;' : '' ); |
| 155 |
$markup = '<div class="embed-container' . $embed_class . '">'; |
| 156 |
$markup .= '<div class="embed-overlay" style="' . $height . ' ' . $width . '">'; |
| 157 |
$markup .= '<h3>'; |
| 158 |
|
| 159 |
if ( ! empty( $embed_provider ) ) { |
| 160 |
/* translators: the embed provider */ |
| 161 |
$markup .= \sprintf( \esc_html__( 'Click here to display content from %s', 'embed-privacy' ), $embed_provider ); |
| 162 |
} |
| 163 |
else { |
| 164 |
$markup .= \esc_html__( 'Click here to display content from external service', 'embed-privacy' ); |
| 165 |
} |
| 166 |
|
| 167 |
$markup .= '</h3>'; |
| 168 |
$markup .= '</div>'; |
| 169 |
$markup .= '<div class="embed-content"><!-- ' . $output . ' --></div>'; |
| 170 |
$markup .= '</div>'; |
| 171 |
|
| 172 |
return $markup; |
| 173 |
} |
| 174 |
} |
| 175 |
|