class.jetpack-iframe-embed.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. /**
  3. * Tweak the preview when rendered in an iframe
  4. */
  5. class Jetpack_Iframe_Embed {
  6. static function init() {
  7. if ( ! self::is_embedding_in_iframe() ) {
  8. return;
  9. }
  10. // Disable the admin bar
  11. if ( ! defined( 'IFRAME_REQUEST' ) ) {
  12. define( 'IFRAME_REQUEST', true );
  13. }
  14. // Prevent canonical redirects
  15. remove_filter( 'template_redirect', 'redirect_canonical' );
  16. add_action( 'wp_head', array( 'Jetpack_Iframe_Embed', 'noindex' ), 1 );
  17. add_action( 'wp_head', array( 'Jetpack_Iframe_Embed', 'base_target_blank' ), 1 );
  18. add_filter( 'shortcode_atts_video', array( 'Jetpack_Iframe_Embed', 'disable_autoplay' ) );
  19. add_filter( 'shortcode_atts_audio', array( 'Jetpack_Iframe_Embed', 'disable_autoplay' ) );
  20. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  21. wp_enqueue_script( 'jetpack-iframe-embed', WPMU_PLUGIN_URL . '/jetpack-iframe-embed/jetpack-iframe-embed.js', array( 'jquery' ) );
  22. } else {
  23. $ver = sprintf( '%s-%s', gmdate( 'oW' ), defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : '' );
  24. wp_enqueue_script( 'jetpack-iframe-embed', '//s0.wp.com/wp-content/mu-plugins/jetpack-iframe-embed/jetpack-iframe-embed.js', array( 'jquery' ), $ver );
  25. }
  26. wp_localize_script( 'jetpack-iframe-embed', '_previewSite', array( 'siteURL' => get_site_url() ) );
  27. }
  28. static function is_embedding_in_iframe() {
  29. return (
  30. self::has_iframe_get_param() && (
  31. self::has_preview_get_param() ||
  32. self::has_preview_theme_preview_param()
  33. )
  34. );
  35. }
  36. private static function has_iframe_get_param() {
  37. return isset( $_GET['iframe'] ) && $_GET['iframe'] === 'true';
  38. }
  39. private static function has_preview_get_param() {
  40. return isset( $_GET['preview'] ) && $_GET['preview'] === 'true';
  41. }
  42. private static function has_preview_theme_preview_param() {
  43. return isset( $_GET['theme_preview'] ) && $_GET['theme_preview'] === 'true';
  44. }
  45. /**
  46. * Disable `autoplay` shortcode attribute in context of an iframe
  47. * Added via `shortcode_atts_video` & `shortcode_atts_audio` in `init`
  48. *
  49. * @param array $atts The output array of shortcode attributes.
  50. *
  51. * @return array The output array of shortcode attributes.
  52. */
  53. static function disable_autoplay( $atts ) {
  54. return array_merge( $atts, array( 'autoplay' => false ) );
  55. }
  56. /**
  57. * We don't want search engines to index iframe previews
  58. * Added via `wp_head` action in `init`
  59. */
  60. static function noindex() {
  61. echo '<meta name="robots" content="noindex,nofollow" />';
  62. }
  63. /**
  64. * Make sure all links and forms open in a new window by default
  65. * (unless overridden on client-side by JS)
  66. * Added via `wp_head` action in `init`
  67. */
  68. static function base_target_blank() {
  69. echo '<base target="_blank" />';
  70. }
  71. }