| @@ -1,388 +1,44 @@ | ||
| 1 | 1 | /** |
| 2 | 2 | * Embed Privacy JavaScript functions. |
| 3 | 3 | * |
| 4 | - * @author Epiphyt | |
| 5 | - * @license GPL2 | |
| 6 | - * @package epiphyt\Embed_Privacy | |
| 4 | + * @author Epiphyt | |
| 5 | + * @license GPL2 | |
| 6 | + * @package epiphyt\Embed_Privacy | |
| 7 | + * @version 0.1 | |
| 7 | 8 | */ |
| 8 | 9 | document.addEventListener( 'DOMContentLoaded', function() { |
| 9 | - var checkboxes = document.querySelectorAll( '.embed-privacy-inner .embed-privacy-input' ); | |
| 10 | - var labels = document.querySelectorAll( '.embed-privacy-inner .embed-privacy-label' ); | |
| 11 | - var overlays = document.querySelectorAll( '.embed-privacy-overlay' ); | |
| 12 | - var overlayLinks = document.querySelectorAll( '.embed-privacy-overlay a' ); | |
| 10 | + var overlays = document.querySelectorAll( '.embed-overlay' ); | |
| 13 | 11 | |
| 14 | - initOverlays( overlays, overlayLinks, checkboxes, labels ); | |
| 15 | - initBuddyPressActivityStream(); | |
| 16 | - optOut(); | |
| 17 | - setMinHeight(); | |
| 18 | - | |
| 19 | - window.addEventListener( 'resize', function() { | |
| 20 | - setMinHeight(); | |
| 21 | - } ); | |
| 22 | - | |
| 23 | - /** | |
| 24 | - * Clicking on a checkbox to always enable/disable an embed provider. | |
| 25 | - * | |
| 26 | - * @since 1.2.0 | |
| 27 | - * | |
| 28 | - * @param {HTMLElement} target Target element | |
| 29 | - */ | |
| 30 | - function checkboxActivation( target ) { | |
| 31 | - var embedProvider = target.getAttribute( 'data-embed-provider' ); | |
| 32 | - var cookie = ( get_cookie( 'embed-privacy' ) ? JSON.parse( get_cookie( 'embed-privacy' ) ) : '' ); | |
| 33 | - | |
| 34 | - if ( target.checked ) { | |
| 35 | - // add|update the cookie's value | |
| 36 | - if ( cookie !== null && Object.keys( cookie ).length !== 0 && cookie.constructor === Object ) { | |
| 37 | - cookie[ embedProvider ] = true; | |
| 38 | - | |
| 39 | - set_cookie( 'embed-privacy', JSON.stringify( cookie ), 365 ); | |
| 40 | - } | |
| 41 | - else { | |
| 42 | - set_cookie( 'embed-privacy', '{"' + embedProvider + '":true}', 365 ); | |
| 43 | - } | |
| 12 | + for ( var i = 0; i < overlays.length; i++ ) { | |
| 13 | + overlays[ i ].addEventListener( 'click', function( event ) { | |
| 14 | + var current_target = event.currentTarget; | |
| 15 | + var embed_content = current_target.nextElementSibling; | |
| 44 | 16 | |
| 45 | - enableAlwaysActiveProviders( document.querySelectorAll( '.embed-privacy-overlay' ) ); | |
| 46 | - // focus first element in container | |
| 47 | - target.closest( '.embed-privacy-container' ).querySelector( '.embed-privacy-content > :first-child' ).focus(); | |
| 48 | - } | |
| 49 | - else if ( cookie !== null ) { | |
| 50 | - delete cookie[ embedProvider ]; | |
| 17 | + // hide the embed overlay | |
| 18 | + current_target.style.display = 'none'; | |
| 19 | + // remove the HTML comments from the embed content | |
| 20 | + embed_content.innerHTML = embed_content.innerHTML.replace( /<!--/, '' ).replace( /-->/, '' ); | |
| 51 | 21 | |
| 52 | - if ( Object.keys( cookie ).length !== 0 ) { | |
| 53 | - set_cookie( 'embed-privacy', JSON.stringify( cookie ), 365 ); | |
| 54 | - } | |
| 55 | - else { | |
| 56 | - remove_cookie( 'embed-privacy' ); | |
| 57 | - } | |
| 58 | - } | |
| 59 | - } | |
| 60 | - | |
| 61 | - /** | |
| 62 | - * Check whether to enable an always active provider by default. | |
| 63 | - * | |
| 64 | - * @since 1.2.0 | |
| 65 | - * | |
| 66 | - * @param {HTMLElement[]} currentOverlays List of current overlays | |
| 67 | - */ | |
| 68 | - function enableAlwaysActiveProviders( currentOverlays ) { | |
| 69 | - var cookie = ( get_cookie( 'embed-privacy' ) ? JSON.parse( get_cookie( 'embed-privacy' ) ) : '' ); | |
| 70 | - | |
| 71 | - if ( ! currentOverlays ) { | |
| 72 | - currentOverlays = overlays; | |
| 73 | - } | |
| 74 | - | |
| 75 | - if ( cookie === null || ! Object.keys( cookie ).length ) { | |
| 76 | - return; | |
| 77 | - } | |
| 78 | - | |
| 79 | - var providers = Object.keys( cookie ); | |
| 80 | - | |
| 81 | - for ( var i = 0; i < currentOverlays.length; i++ ) { | |
| 82 | - var provider = currentOverlays[ i ].parentNode.getAttribute( 'data-embed-provider' ); | |
| 22 | + // get all script tags inside the embed | |
| 23 | + var script_tags = embed_content.querySelectorAll( 'script' ); | |
| 83 | 24 | |
| 84 | - if ( providers.includes( provider ) ) { | |
| 85 | - currentOverlays[ i ].click(); | |
| 86 | - } | |
| 87 | - } | |
| 88 | - } | |
| 89 | - | |
| 90 | - /** | |
| 91 | - * Get always active providers from cookie. | |
| 92 | - * | |
| 93 | - * @since 1.4.8 | |
| 94 | - * | |
| 95 | - * @return {string[]} List of always active providers | |
| 96 | - */ | |
| 97 | - function getAlwaysActiveProviders() { | |
| 98 | - const cookie = ( get_cookie( 'embed-privacy' ) ? JSON.parse( get_cookie( 'embed-privacy' ) ) : '' ); | |
| 99 | - | |
| 100 | - if ( ! cookie ) { | |
| 101 | - return []; | |
| 102 | - } | |
| 103 | - | |
| 104 | - return Object.keys( cookie ); | |
| 105 | - } | |
| 106 | - | |
| 107 | - /** | |
| 108 | - * Initialize overlay functionality. | |
| 109 | - * | |
| 110 | - * @since 1.11.0 | |
| 111 | - * | |
| 112 | - * @param {HTMLElement[]} embedOverlays List of embed overlays | |
| 113 | - * @param {HTMLElement[]} overlayLinks List of embed overlay links | |
| 114 | - * @param {HTMLElement[]} checkboxes List of embed overlay checkbox inputs | |
| 115 | - * @param {HTMLElement[]} labels List of embed overlay labels | |
| 116 | - */ | |
| 117 | - function initOverlays( embedOverlays, overlayLinks, checkboxes, labels ) { | |
| 118 | - for ( var i = 0; i < embedOverlays.length; i++ ) { | |
| 119 | - embedOverlays[ i ].addEventListener( 'click', function( event ) { | |
| 120 | - if ( event.currentTarget.tagName !== 'INPUT' ) { | |
| 121 | - overlayClick( event.currentTarget ); | |
| 122 | - } | |
| 123 | - } ); | |
| 124 | - | |
| 125 | - const button = embedOverlays[ i ].parentNode.querySelector( '.embed-privacy-enable' ); | |
| 126 | - | |
| 127 | - if ( ! button ) { | |
| 128 | - continue; | |
| 129 | - } | |
| 130 | - | |
| 131 | - button.addEventListener( 'click', function( event ) { | |
| 132 | - overlayClick( event.currentTarget.parentNode.querySelector( '.embed-privacy-overlay' ) ); | |
| 133 | - event.currentTarget.parentNode.removeChild( event.currentTarget ); // IE11 doesn't support .remove() | |
| 134 | - } ); | |
| 135 | - button.addEventListener( 'keypress', function( event ) { | |
| 136 | - if ( event.code === 'Enter' || event.code === 'Space' ) { | |
| 137 | - event.preventDefault(); // prevent space from scrolling the page | |
| 138 | - overlayClick( event.currentTarget.parentNode.querySelector( '.embed-privacy-overlay' ) ); | |
| 139 | - event.currentTarget.parentNode.removeChild( event.currentTarget ); // IE11 doesn't support .remove() | |
| 140 | - } | |
| 141 | - } ); | |
| 142 | - } | |
| 143 | - | |
| 144 | - for ( var i = 0; i < overlayLinks.length; i++ ) { | |
| 145 | - overlayLinks[ i ].addEventListener( 'click', function( event ) { | |
| 146 | - // don't trigger the overlays click | |
| 147 | - event.stopPropagation(); | |
| 148 | - } ); | |
| 149 | - } | |
| 150 | - | |
| 151 | - for ( var i = 0; i < checkboxes.length; i++ ) { | |
| 152 | - checkboxes[ i ].addEventListener( 'click', function( event ) { | |
| 153 | - // don't trigger the overlays click | |
| 154 | - event.stopPropagation(); | |
| 25 | + // insert every script tag inside the embed as a new script | |
| 26 | + // to execute it | |
| 27 | + for ( var n = 0; n < script_tags.length; n++ ) { | |
| 28 | + var element = document.createElement( 'script' ); | |
| 155 | 29 | |
| 156 | - checkboxActivation( event.currentTarget, [ event.currentTarget.closest( '.embed-privacy-overlay' ) ] ); | |
| 157 | - } ); | |
| 158 | - } | |
| 159 | - | |
| 160 | - for ( var i = 0; i < labels.length; i++ ) { | |
| 161 | - labels[ i ].addEventListener( 'click', function( event ) { | |
| 162 | - // don't trigger the overlays click | |
| 163 | - event.stopPropagation(); | |
| 164 | - } ); | |
| 165 | - } | |
| 166 | - | |
| 167 | - enableAlwaysActiveProviders( embedOverlays ); | |
| 168 | - } | |
| 169 | - | |
| 170 | - /** | |
| 171 | - * Initialize support for BuddyPress activity stream. | |
| 172 | - */ | |
| 173 | - function initBuddyPressActivityStream() { | |
| 174 | - const activityStream = document.getElementById( 'activity-stream' ); | |
| 175 | - | |
| 176 | - if ( ! activityStream ) { | |
| 177 | - return; | |
| 178 | - } | |
| 179 | - | |
| 180 | - const activityObserver = new MutationObserver( ( mutations ) => { | |
| 181 | - for ( const mutation of mutations ) { | |
| 182 | - const checkboxes = mutation.target.querySelectorAll( '.embed-privacy-inner .embed-privacy-input' ); | |
| 183 | - const labels = mutation.target.querySelectorAll( '.embed-privacy-inner .embed-privacy-label' ); | |
| 184 | - const overlays = mutation.target.querySelectorAll( '.embed-privacy-overlay' ); | |
| 185 | - const overlayLinks = mutation.target.querySelectorAll( '.embed-privacy-overlay a' ); | |
| 186 | - | |
| 187 | - if ( overlays.length ) { | |
| 188 | - initOverlays( overlays, overlayLinks, checkboxes, labels ); | |
| 30 | + if ( script_tags[ n ].src ) { | |
| 31 | + // if script tag has a src attribute | |
| 32 | + element.src = script_tags[ n ].src; | |
| 189 | 33 | } |
| 190 | - } | |
| 191 | - } ); | |
| 192 | - | |
| 193 | - activityObserver.observe( activityStream, { | |
| 194 | - childList: true, | |
| 195 | - } ); | |
| 196 | - } | |
| 197 | - | |
| 198 | - /** | |
| 199 | - * Opting in/out for embed providers. | |
| 200 | - * | |
| 201 | - * @since 1.2.0 | |
| 202 | - */ | |
| 203 | - function optOut() { | |
| 204 | - const optOutContainer = document.querySelector( '.embed-privacy-opt-out' ); | |
| 205 | - | |
| 206 | - if ( ! optOutContainer ) { | |
| 207 | - return; | |
| 208 | - } | |
| 209 | - | |
| 210 | - var optOutCheckboxes = optOutContainer.querySelectorAll( '.embed-privacy-opt-out-input' ); | |
| 211 | - const showAll = optOutContainer.getAttribute( 'data-show-all' ) === '1'; | |
| 212 | - | |
| 213 | - if ( ! optOutCheckboxes ) { | |
| 214 | - return; | |
| 215 | - } | |
| 216 | - | |
| 217 | - const alwaysActiveProviders = getAlwaysActiveProviders(); | |
| 218 | - | |
| 219 | - for ( var i = 0; i < optOutCheckboxes.length; i++ ) { | |
| 220 | - if ( alwaysActiveProviders.indexOf( optOutCheckboxes[ i ].getAttribute( 'data-embed-provider' ) ) !== -1 ) { | |
| 221 | - optOutCheckboxes[ i ].checked = true; | |
| 222 | - } | |
| 223 | - else if ( ! showAll ) { | |
| 224 | - optOutCheckboxes[ i ].parentNode.parentNode.classList.add( 'is-hidden' ); | |
| 225 | - | |
| 226 | - continue; | |
| 227 | - } | |
| 228 | - | |
| 229 | - optOutCheckboxes[ i ].addEventListener( 'click', function( event ) { | |
| 230 | - var currentTarget = event.currentTarget; | |
| 231 | - | |
| 232 | - if ( ! currentTarget ) { | |
| 233 | - return; | |
| 34 | + else { | |
| 35 | + // if script tag has content | |
| 36 | + element.innerHTML = script_tags[ n ].innerHTML; | |
| 234 | 37 | } |
| 235 | 38 | |
| 236 | - checkboxActivation( currentTarget ); | |
| 237 | - } ); | |
| 238 | - } | |
| 239 | - | |
| 240 | - const nonHiddenProviders = optOutContainer.querySelectorAll( '.embed-privacy-provider:not(.is-hidden)' ); | |
| 241 | - | |
| 242 | - // remove the container completely if there is no provider to display | |
| 243 | - if ( ! showAll && ! nonHiddenProviders.length ) { | |
| 244 | - optOutContainer.remove(); | |
| 245 | - } | |
| 246 | - } | |
| 247 | - | |
| 248 | - /** | |
| 249 | - * Clicking on an overlay. | |
| 250 | - * | |
| 251 | - * @since 1.2.0 | |
| 252 | - * | |
| 253 | - * @param {element} target Target element | |
| 254 | - */ | |
| 255 | - function overlayClick( target ) { | |
| 256 | - var embedContainer = target.parentNode; | |
| 257 | - var embedContent = target.nextElementSibling; | |
| 258 | - | |
| 259 | - embedContainer.classList.remove( 'is-disabled' ); | |
| 260 | - embedContainer.classList.add( 'is-enabled' ); | |
| 261 | - // hide the embed overlay | |
| 262 | - target.style.display = 'none'; | |
| 263 | - // get stored content from JavaScript | |
| 264 | - var embedObject = JSON.parse( window[ '_' + target.parentNode.getAttribute( 'data-embed-id' ) ] ); | |
| 265 | - | |
| 266 | - embedContent.innerHTML = htmlentities_decode( embedObject.embed ); | |
| 267 | - | |
| 268 | - // reset wrapper inline CSS set in setMinHeight() | |
| 269 | - var wrapper = embedContainer.parentNode; | |
| 270 | - | |
| 271 | - if ( wrapper.classList.contains( 'wp-block-embed__wrapper' ) ) { | |
| 272 | - wrapper.style.removeProperty( 'height' ); | |
| 273 | - } | |
| 274 | - | |
| 275 | - // get all script tags inside the embed | |
| 276 | - var scriptTags = embedContent.querySelectorAll( 'script' ); | |
| 277 | - | |
| 278 | - // insert every script tag inside the embed as a new script | |
| 279 | - // to execute it | |
| 280 | - for ( var n = 0; n < scriptTags.length; n++ ) { | |
| 281 | - var element = document.createElement( 'script' ); | |
| 282 | - | |
| 283 | - if ( scriptTags[ n ].src ) { | |
| 284 | - // if script tag has a src attribute | |
| 285 | - element.src = scriptTags[ n ].src; | |
| 39 | + // append it to body | |
| 40 | + embed_content.appendChild( element ); | |
| 286 | 41 | } |
| 287 | - else { | |
| 288 | - // if script tag has content | |
| 289 | - element.innerHTML = scriptTags[ n ].innerHTML; | |
| 290 | - } | |
| 291 | - | |
| 292 | - // append it to body | |
| 293 | - embedContent.appendChild( element ); | |
| 294 | - } | |
| 295 | - | |
| 296 | - // focus first element in container | |
| 297 | - embedContainer.querySelector( '.embed-privacy-content > :first-child' ).focus(); | |
| 298 | - | |
| 299 | - if ( typeof jQuery !== 'undefined' ) { | |
| 300 | - const videoShortcode = jQuery( '.wp-video-shortcode' ); | |
| 301 | - | |
| 302 | - if ( videoShortcode.length ) { | |
| 303 | - videoShortcode.mediaelementplayer(); | |
| 304 | - } | |
| 305 | - } | |
| 42 | + } ) | |
| 306 | 43 | } |
| 307 | - | |
| 308 | - /** | |
| 309 | - * Calculate min height of the embed wrapper depending of the overlay content. | |
| 310 | - */ | |
| 311 | - function setMinHeight() { | |
| 312 | - for ( var i = 0; i < overlays.length; i++ ) { | |
| 313 | - var wrapper = overlays[ i ].parentNode.parentNode; | |
| 314 | - | |
| 315 | - if ( ! wrapper.classList.contains( 'wp-block-embed__wrapper' ) ) { | |
| 316 | - continue; | |
| 317 | - } | |
| 318 | - | |
| 319 | - wrapper.style.removeProperty( 'height' ); | |
| 320 | - | |
| 321 | - if ( wrapper.offsetHeight < overlays[ i ].offsetHeight ) { | |
| 322 | - wrapper.style.height = overlays[ i ].offsetHeight + 'px'; | |
| 323 | - } | |
| 324 | - } | |
| 325 | - } | |
| 326 | -} ); | |
| 327 | - | |
| 328 | -/** | |
| 329 | - * Get a cookie. | |
| 330 | - * | |
| 331 | - * @link https://stackoverflow.com/a/24103596/3461955 | |
| 332 | - * | |
| 333 | - * @param {string} name The name of the cookie | |
| 334 | - */ | |
| 335 | -function get_cookie( name ) { | |
| 336 | - var nameEQ = name + '='; | |
| 337 | - var ca = document.cookie.split( ';' ); | |
| 338 | - for ( var i = 0; i < ca.length; i++ ) { | |
| 339 | - var c = ca[ i ]; | |
| 340 | - while ( c.charAt( 0 ) == ' ' ) c = c.substring( 1, c.length ); | |
| 341 | - if ( c.indexOf( nameEQ ) == 0 ) return c.substring( nameEQ.length, c.length ); | |
| 342 | - } | |
| 343 | - return null; | |
| 344 | -} | |
| 345 | - | |
| 346 | -/** | |
| 347 | - * Decode a string with HTML entities. | |
| 348 | - * | |
| 349 | - * @param {string} content The content to decode | |
| 350 | - * @return {string} The decoded content | |
| 351 | - */ | |
| 352 | -function htmlentities_decode( content ) { | |
| 353 | - var textarea = document.createElement( 'textarea' ); | |
| 354 | - | |
| 355 | - textarea.innerHTML = content; | |
| 356 | - | |
| 357 | - return textarea.value; | |
| 358 | -} | |
| 359 | - | |
| 360 | -/** | |
| 361 | - * Remove a cookie. | |
| 362 | - * | |
| 363 | - * @link https://stackoverflow.com/a/24103596/3461955 | |
| 364 | - * | |
| 365 | - * @param {string} name The name of the cookie | |
| 366 | - */ | |
| 367 | -function remove_cookie( name ) { | |
| 368 | - document.cookie = name + '=; expires=0; path=/'; | |
| 369 | -} | |
| 370 | - | |
| 371 | -/** | |
| 372 | - * Set a cookie. | |
| 373 | - * | |
| 374 | - * @link https://stackoverflow.com/a/24103596/3461955 | |
| 375 | - * | |
| 376 | - * @param {string} name The name of the cookie | |
| 377 | - * @param {string} value The value of the cookie | |
| 378 | - * @param {number} days The expiration in days | |
| 379 | - */ | |
| 380 | -function set_cookie( name, value, days ) { | |
| 381 | - var expires = ''; | |
| 382 | - if ( days ) { | |
| 383 | - var date = new Date(); | |
| 384 | - date.setTime( date.getTime() + ( days * 24 * 60 * 60 * 1000 ) ); | |
| 385 | - expires = '; expires=' + date.toUTCString(); | |
| 386 | - } | |
| 387 | - document.cookie = name + '=' + ( value || '' ) + expires + '; path=/'; | |
| 388 | -} | |
| 44 | +} ); | |