No Description

vr.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. /**
  3. * VR Viewer Shortcode
  4. * converts [vr] shortcode to an iframe viewer hosted on vr.me.sh
  5. *
  6. * @package automattic/jetpack
  7. */
  8. /**
  9. * Scrub URL paramaters for VR viewer
  10. *
  11. * @param array $params {
  12. * parameter array which is passed to the jetpack_vr_viewer.
  13. *
  14. * @type string $url url of 360 media
  15. * @type string $guid guid for videopress
  16. * @type string $view cinema, 360 - controls if panaroma view, or 360
  17. * @type string $rotation number for rotating media
  18. * @type string $preview show preview image or not
  19. * }
  20. *
  21. * @return array|false $url_params Array of URL parameters.
  22. */
  23. function jetpack_vr_viewer_get_viewer_url_params( $params ) {
  24. $url_params = array();
  25. if ( isset( $params['rotation'] ) ) {
  26. $url_params['rotation'] = (int) $params['rotation'];
  27. }
  28. if ( isset( $params['view'] ) && in_array( $params['view'], array( 'cinema', '360' ), true ) ) {
  29. $url_params['view'] = $params['view'];
  30. }
  31. if ( isset( $params['preview'] ) && $params['preview'] ) {
  32. $url_params['preview'] = 1;
  33. }
  34. if ( isset( $params['url'] ) ) {
  35. return array_merge( $url_params, array( 'url' => $params['url'] ) );
  36. } elseif ( isset( $params['guid'] ) ) {
  37. return array_merge( $url_params, array( 'guid' => $params['guid'] ) );
  38. }
  39. return false;
  40. }
  41. /**
  42. * Get padding for IFRAME depending on view type
  43. *
  44. * @param string $view string cinema, 360 - default cinema.
  45. *
  46. * @return string $css padding
  47. */
  48. function jetpack_vr_viewer_iframe_padding( $view ) {
  49. if ( '360' === $view ) {
  50. return '100%'; // 1:1 square aspect for 360
  51. }
  52. return '50%'; // 2:1 panorama aspect
  53. }
  54. /**
  55. * Create HTML for VR Viewer IFRAME and wrapper
  56. * The viewer code is hosted on vr.me.sh site which is then displayed
  57. * within posts via an IFRAME. This function returns the IFRAME html.
  58. *
  59. * @param array $url_params {
  60. * parameter array which is passed to the jetpack_vr_viewer.
  61. *
  62. * @type string $url url of 360 media
  63. * @type string $guid guid for videopress
  64. * @type string $view cinema, 360 - controls if panaroma view, or 360
  65. * @type string $rotation number for rotating media
  66. * @type string $preview show preview image or not
  67. * }
  68. *
  69. * @return string $rtn an iframe for viewer.
  70. */
  71. function jetpack_vr_viewer_get_html( $url_params ) {
  72. global $content_width;
  73. $iframe = add_query_arg( $url_params, 'https://vr.me.sh/view/' );
  74. // set some defaults.
  75. $maxwidth = ( isset( $content_width ) ) ? $content_width : 720;
  76. $view = ( isset( $url_params['view'] ) ) ? $url_params['view'] : 'cinema';
  77. // If the shortcode is displayed in a WPCOM notification, display a simple link only.
  78. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  79. require_once WP_CONTENT_DIR . '/lib/display-context.php';
  80. $context = A8C\Display_Context\get_current_context();
  81. if ( A8C\Display_Context\NOTIFICATIONS === $context ) {
  82. return sprintf(
  83. '<a href="%1$s" target="_blank" rel="noopener noreferrer">%1$s</a>',
  84. esc_url( $iframe )
  85. );
  86. }
  87. }
  88. $rtn = '<div style="position: relative; max-width: ' . $maxwidth . 'px; margin-left: auto; margin-right: auto; overflow: hidden; margin-bottom: 1em;">';
  89. $rtn .= '<div style="padding-top: ' . jetpack_vr_viewer_iframe_padding( $view ) . ';"></div>';
  90. $rtn .= '<iframe style="position: absolute; top: 0; right: 0; bottom: 0; left: 0; height: 100%" allowfullscreen="true" frameborder="0" width="100%" height="300" src="' . esc_url( $iframe ) . '">';
  91. $rtn .= '</iframe>';
  92. $rtn .= '</div>';
  93. return $rtn;
  94. }
  95. /**
  96. * Convert [vr] shortcode to viewer
  97. *
  98. * Shortcode example:
  99. * [vr url="https://en-blog.files.wordpress.com/2016/12/regents_park.jpg" view="360"]
  100. *
  101. * VR Viewer embed code:
  102. * <div style="position: relative; max-width: 720px; margin-left: auto; margin-right: auto; overflow: hidden;">
  103. * <div style="padding-top: 100%;"></div>
  104. * <iframe style="position: absolute; top: 0; right: 0; bottom: 0; left: 0; height: 100%" allowfullscreen="true" frameborder="0" width="100%" height="400" src="https://vr.me.sh/view/?view=360&amp;url=https://en-blog.files.wordpress.com/2016/12/regents_park.jpg">
  105. * </iframe>
  106. * </div>
  107. *
  108. * @param array $atts Shortcode attributes.
  109. *
  110. * @return html - complete vr viewer html
  111. */
  112. function jetpack_vr_viewer_shortcode( $atts ) {
  113. $params = shortcode_atts(
  114. array(
  115. 0 => null,
  116. 'url' => null,
  117. 'src' => null,
  118. 'guid' => null,
  119. 'rotation' => null,
  120. 'view' => null,
  121. 'preview' => false,
  122. ),
  123. $atts
  124. );
  125. // We offer a few ways to specify the URL.
  126. if ( $params[0] ) {
  127. $params['url'] = $params[0];
  128. } elseif ( $params['src'] ) {
  129. $params['url'] = $params['src'];
  130. }
  131. $url_params = jetpack_vr_viewer_get_viewer_url_params( $params );
  132. if ( $url_params ) {
  133. return jetpack_vr_viewer_get_html( $url_params );
  134. }
  135. // add check for user.
  136. if ( current_user_can( 'edit_posts' ) ) {
  137. return '[vr] shortcode requires a data source to be given';
  138. } else {
  139. return '';
  140. }
  141. }
  142. add_shortcode( 'vr', 'jetpack_vr_viewer_shortcode' );