[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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   * Single line comments should not be used since they will break
   8   * the script when inlined in get_post_embed_html(), specifically
   9   * when the comments are not stripped out due to SCRIPT_DEBUG
  10   * being turned on.
  11   */
  12  (function ( window, document ) {
  13      'use strict';
  14  
  15      /* Abort for ancient browsers. */
  16      if ( ! document.querySelector || ! window.addEventListener || typeof URL === 'undefined' ) {
  17          return;
  18      }
  19  
  20      /** @namespace wp */
  21      window.wp = window.wp || {};
  22  
  23      /* Abort if script was already executed. */
  24      if ( !! window.wp.receiveEmbedMessage ) {
  25          return;
  26      }
  27  
  28      /**
  29       * Receive embed message.
  30       *
  31       * @param {MessageEvent} e
  32       */
  33      window.wp.receiveEmbedMessage = function( e ) {
  34          var data = e.data;
  35  
  36          /* Verify shape of message. */
  37          if (
  38              ! ( data || data.secret || data.message || data.value ) ||
  39              /[^a-zA-Z0-9]/.test( data.secret )
  40          ) {
  41              return;
  42          }
  43  
  44          var iframes = document.querySelectorAll( 'iframe[data-secret="' + data.secret + '"]' ),
  45              blockquotes = document.querySelectorAll( 'blockquote[data-secret="' + data.secret + '"]' ),
  46              allowedProtocols = new RegExp( '^https?:$', 'i' ),
  47              i, source, height, sourceURL, targetURL;
  48  
  49          for ( i = 0; i < blockquotes.length; i++ ) {
  50              blockquotes[ i ].style.display = 'none';
  51          }
  52  
  53          for ( i = 0; i < iframes.length; i++ ) {
  54              source = iframes[ i ];
  55  
  56              if ( e.source !== source.contentWindow ) {
  57                  continue;
  58              }
  59  
  60              source.removeAttribute( 'style' );
  61  
  62              if ( 'height' === data.message ) {
  63                  /* Resize the iframe on request. */
  64                  height = parseInt( data.value, 10 );
  65                  if ( height > 1000 ) {
  66                      height = 1000;
  67                  } else if ( ~~height < 200 ) {
  68                      height = 200;
  69                  }
  70  
  71                  source.height = height;
  72              } else if ( 'link' === data.message ) {
  73                  /* Link to a specific URL on request. */
  74                  sourceURL = new URL( source.getAttribute( 'src' ) );
  75                  targetURL = new URL( data.value );
  76  
  77                  if (
  78                      allowedProtocols.test( targetURL.protocol ) &&
  79                      targetURL.host === sourceURL.host &&
  80                      document.activeElement === source
  81                  ) {
  82                      window.top.location.href = data.value;
  83                  }
  84              }
  85          }
  86      };
  87  
  88  	function onLoad() {
  89          var iframes = document.querySelectorAll( 'iframe.wp-embedded-content' ),
  90              i, source, secret;
  91  
  92          for ( i = 0; i < iframes.length; i++ ) {
  93              /** @var {IframeElement} */
  94              source = iframes[ i ];
  95  
  96              secret = source.getAttribute( 'data-secret' );
  97              if ( ! secret ) {
  98                  /* Add secret to iframe */
  99                  secret = Math.random().toString( 36 ).substring( 2, 12 );
 100                  source.src += '#?secret=' + secret;
 101                  source.setAttribute( 'data-secret', secret );
 102              }
 103  
 104              /*
 105               * Let post embed window know that the parent is ready for receiving the height message, in case the iframe
 106               * loaded before wp-embed.js was loaded. When the ready message is received by the post embed window, the
 107               * window will then (re-)send the height message right away.
 108               */
 109              source.contentWindow.postMessage( {
 110                  message: 'ready',
 111                  secret: secret
 112              }, '*' );
 113          }
 114      }
 115  
 116      window.addEventListener( 'message', window.wp.receiveEmbedMessage, false );
 117      document.addEventListener( 'DOMContentLoaded', onLoad, false );
 118  })( window, document );


Generated : Fri Apr 26 08:20:02 2024 Cross-referenced by PHPXref