PluginProbe
Embed Privacy / 1.10.9
Embed Privacy v1.10.9
1.14.0 1.13.0 trunk 0.1 1.0.0 1.0.1 1.0.2 1.1.0 1.1.1 1.1.2 1.1.3 1.10.0 1.10.1 1.10.10 1.10.2 1.10.3 1.10.4 1.10.5 1.10.6 1.10.7 1.10.8 1.10.9 1.11.0 1.11.1 1.11.2 All 68 releases
embed-privacy / assets / js / embed-privacy.js

embed-privacy.js in Embed Privacy 1.10.9, at assets/js/embed-privacy.js

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