[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/js/ -> wp-embed.js (source)

   1  /**
   2   * WordPress inline HTML embed
   3   *
   4   * @since 4.4.0
   5   * @output wp-includes/js/wp-embed.js
   6   *
   7   * This file cannot have ampersands in it. This is to ensure
   8   * it can be embedded in older versions of WordPress.
   9   * See https://core.trac.wordpress.org/changeset/35708.
  10   */
  11  (function ( window, document ) {
  12      'use strict';
  13  
  14      var supportedBrowser = false,
  15          loaded = false;
  16  
  17          if ( document.querySelector ) {
  18              if ( window.addEventListener ) {
  19                  supportedBrowser = true;
  20              }
  21          }
  22  
  23      /** @namespace wp */
  24      window.wp = window.wp || {};
  25  
  26      if ( !! window.wp.receiveEmbedMessage ) {
  27          return;
  28      }
  29  
  30      /**
  31       * Receive embed message.
  32       *
  33       * @param {MessageEvent} e
  34       */
  35      window.wp.receiveEmbedMessage = function( e ) {
  36          var data = e.data;
  37  
  38          if ( ! data ) {
  39              return;
  40          }
  41  
  42          if ( ! ( data.secret || data.message || data.value ) ) {
  43              return;
  44          }
  45  
  46          if ( /[^a-zA-Z0-9]/.test( data.secret ) ) {
  47              return;
  48          }
  49  
  50          var iframes = document.querySelectorAll( 'iframe[data-secret="' + data.secret + '"]' ),
  51              blockquotes = document.querySelectorAll( 'blockquote[data-secret="' + data.secret + '"]' ),
  52              i, source, height, sourceURL, targetURL;
  53  
  54          for ( i = 0; i < blockquotes.length; i++ ) {
  55              blockquotes[ i ].style.display = 'none';
  56          }
  57  
  58          for ( i = 0; i < iframes.length; i++ ) {
  59              source = iframes[ i ];
  60  
  61              if ( e.source !== source.contentWindow ) {
  62                  continue;
  63              }
  64  
  65              source.removeAttribute( 'style' );
  66  
  67              /* Resize the iframe on request. */
  68              if ( 'height' === data.message ) {
  69                  height = parseInt( data.value, 10 );
  70                  if ( height > 1000 ) {
  71                      height = 1000;
  72                  } else if ( ~~height < 200 ) {
  73                      height = 200;
  74                  }
  75  
  76                  source.height = height;
  77              }
  78  
  79              /* Link to a specific URL on request. */
  80              if ( 'link' === data.message ) {
  81                  sourceURL = document.createElement( 'a' );
  82                  targetURL = document.createElement( 'a' );
  83  
  84                  sourceURL.href = source.getAttribute( 'src' );
  85                  targetURL.href = data.value;
  86  
  87                  /* Only continue if link hostname matches iframe's hostname. */
  88                  if ( targetURL.host === sourceURL.host ) {
  89                      if ( document.activeElement === source ) {
  90                          window.top.location.href = data.value;
  91                      }
  92                  }
  93              }
  94          }
  95      };
  96  
  97  	function onLoad() {
  98          if ( loaded ) {
  99              return;
 100          }
 101  
 102          loaded = true;
 103  
 104          var isIE10 = -1 !== navigator.appVersion.indexOf( 'MSIE 10' ),
 105              isIE11 = !!navigator.userAgent.match( /Trident.*rv:11\./ ),
 106              iframes = document.querySelectorAll( 'iframe.wp-embedded-content' ),
 107              iframeClone, i, source, secret;
 108  
 109          for ( i = 0; i < iframes.length; i++ ) {
 110              /** @var {IframeElement} */
 111              source = iframes[ i ];
 112  
 113              secret = source.getAttribute( 'data-secret' );
 114              if ( ! secret ) {
 115                  /* Add secret to iframe */
 116                  secret = Math.random().toString( 36 ).substr( 2, 10 );
 117                  source.src += '#?secret=' + secret;
 118                  source.setAttribute( 'data-secret', secret );
 119              }
 120  
 121              /* Remove security attribute from iframes in IE10 and IE11. */
 122              if ( ( isIE10 || isIE11 ) ) {
 123                  iframeClone = source.cloneNode( true );
 124                  iframeClone.removeAttribute( 'security' );
 125                  source.parentNode.replaceChild( iframeClone, source );
 126              }
 127  
 128              /*
 129               * Let post embed window know that the parent is ready for receiving the height message, in case the iframe
 130               * loaded before wp-embed.js was loaded. When the ready message is received by the post embed window, the
 131               * window will then (re-)send the height message right away.
 132               */
 133              source.contentWindow.postMessage( {
 134                  message: 'ready',
 135                  secret: secret
 136              }, '*' );
 137          }
 138      }
 139  
 140      if ( supportedBrowser ) {
 141          window.addEventListener( 'message', window.wp.receiveEmbedMessage, false );
 142          document.addEventListener( 'DOMContentLoaded', onLoad, false );
 143          window.addEventListener( 'load', onLoad, false );
 144      }
 145  })( window, document );


Generated: Sat May 28 01:00:02 2022 Cross-referenced by PHPXref 0.7.1