Geen omschrijving

googleapps.php 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * Google Docs and Google Calendar Shortcode
  4. *
  5. * Presentation:
  6. * <iframe src="https://docs.google.com/present/embed?id=dhfhrphh_123drp8s65c&interval=15&autoStart=true&loop=true&size=l" frameborder="0" width="700" height="559"></iframe>
  7. * <iframe src="https://docs.google.com/presentation/embed?id=13ItX4jV0SOSdr-ZjHarcpTh9Lr4omfsHAp87jpxv8-0&start=false&loop=false&delayms=3000" frameborder="0" width="960" height="749" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>
  8. *
  9. * Document:
  10. * <iframe src="https://docs.google.com/document/pub?id=1kDatklacdZ_tZUOpWtt_ONzY97Ldj2zFcuO9LBY2Ln4&amp;embedded=true"></iframe>
  11. * <iframe src="https://docs.google.com/document/d/1kDatklacdZ_tZUOpWtt_ONzY97Ldj2zFcuO9LBY2Ln4/pub?embedded=true"></iframe>
  12. * <iframe src="https://docs.google.com/document/d/e/2PACX-1vRkpIdasKL-eKXDjJgpEONduUspZTz0YmKaajfie0eJYnzikuyusuG1_V8X8T9XflN9l8A1oCM2sgEA/pub?embedded=true"></iframe>
  13. *
  14. * External document:
  15. * <iframe width=100% height=560px frameborder=0 src=https://docs.google.com/a/pranab.in/viewer?a=v&pid=explorer&chrome=false&embedded=true&srcid=1VTMwdgGiDMt8MCr75-YkQP-4u9WmEp1Qvf6C26KYBgFilxU2qndpd-VHhBIn&hl=en></iframe>
  16. *
  17. * Spreadsheet Form:
  18. * <iframe src="https://spreadsheets.google.com/embeddedform?formkey=dEVOYnMzZG5jMUpGbjFMYjFYNVB3NkE6MQ" width="760" height="710" frameborder="0" marginheight="0" marginwidth="0">Loading...</iframe>
  19. *
  20. * Spreadsheet Widget:
  21. * <iframe width='500' height='300' frameborder='0' src='https://spreadsheets1.google.com/a/petedavies.com/pub?hl=en&hl=en&key=0AjSij7nlnXvKdHNsNjRSWG12YmVfOEFwdlMxQ3J1S1E&single=true&gid=0&output=html&widget=true'></iframe>
  22. * <iframe width='500' height='300' frameborder='0' src='https://spreadsheets.google.com/spreadsheet/pub?hl=en&hl=en&key=0AhInIwfvYrIUdGJiTXhtUEhBSFVPUzdRZU5OMDlqdnc&output=html&widget=true'></iframe>
  23. *
  24. * Calendar:
  25. * <iframe src="https://www.google.com/calendar/embed?src=serjant%40gmail.com&ctz=Europe/Sofia" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>
  26. * <iframe src="http://www.google.com/calendar/hosted/belcastro.com/embed?src=n8nr8sd6v9hnus3nmlk7ed1238%40group.calendar.google.com&ctz=Europe/Zurich" style="border: 0" width="800" height="600" frameborder="0" scrolling="no"></iframe>
  27. *
  28. * Customized calendar:
  29. * <iframe src="https://www.google.com/calendar/embed?title=asdf&amp;showTitle=0&amp;showNav=0&amp;showDate=0&amp;showPrint=0&amp;showTabs=0&amp;showCalendars=0&amp;
  30. * showTz=0&amp;mode=AGENDA&amp;height=300&amp;wkst=2&amp;hl=fi&amp;bgcolor=%23ffcccc&amp;src=m52gdmbgelo3itf00u1v44g0ns%40group.calendar.google.com&amp;color=%234E5D6C&amp;
  31. * src=serjant%40gmail.com&amp;color=%235229A3&amp;ctz=Europe%2FRiga" style=" border:solid 1px #777 " width="500" height="300" frameborder="0" scrolling="no"></iframe>
  32. *
  33. * Generic
  34. * <iframe src="https://docs.google.com/file/d/0B0SIdZW7iu-zX1RWREJpMXVHZVU/preview" width="640" height="480"></iframe>
  35. *
  36. * @package automattic/jetpack
  37. */
  38. add_filter( 'pre_kses', 'googleapps_embed_to_shortcode' );
  39. add_shortcode( 'googleapps', 'googleapps_shortcode' );
  40. /**
  41. * Reverse iframe embed to shortcode mapping HTML attributes to shortcode attributes.
  42. *
  43. * @since 4.5.0
  44. *
  45. * @param string $content Post content.
  46. *
  47. * @return mixed
  48. */
  49. function googleapps_embed_to_shortcode( $content ) {
  50. if (
  51. ! is_string( $content )
  52. || false === stripos( $content, '<iframe' )
  53. && false === stripos( $content, '.google.com' )
  54. ) {
  55. return $content;
  56. }
  57. $regexp = '#<iframe((?:\s+\w+="[^"]*")*?)\s*src="https?://(docs|drive|spreadsheets\d*|calendar|www)*\.google\.com/(?!maps)([-\w\./]+)(?:\?)?([^"]+)?"\s*((?:\s+\w+="[^"]*")*?)>.*?</iframe>#i';
  58. $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
  59. $regexp_squot = str_replace( '"', "'", $regexp );
  60. $regexp_ent_squot = str_replace( '"', "'", $regexp_ent );
  61. $regexp_noquot = '!<iframe(.*?)src=https://(docs|drive)\.google\.com/[-\.\w/]*?(viewer)\?(.*?)>(.*?)</iframe>!';
  62. $regexp_ent_noquot = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp_noquot, ENT_NOQUOTES ) );
  63. foreach ( compact( 'regexp', 'regexp_ent', 'regexp_squot', 'regexp_ent_squot', 'regexp_noquot', 'regexp_ent_noquot' ) as $reg => $regexp ) {
  64. if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
  65. continue;
  66. }
  67. foreach ( $matches as $match ) {
  68. $params = $match[1] . $match[5];
  69. if ( in_array( $reg, array( 'regexp_ent', 'regexp_ent_squot' ), true ) ) {
  70. $params = html_entity_decode( $params );
  71. }
  72. $params = wp_kses_hair( $params, array( 'http' ) );
  73. $width = 0;
  74. $height = 0;
  75. if ( isset( $params['width'] ) ) {
  76. $width = (int) $params['width']['value'];
  77. }
  78. if ( isset( $params['height'] ) ) {
  79. $height = (int) $params['height']['value'];
  80. }
  81. // allow the user to specify width greater than 200 inside text widgets.
  82. if (
  83. $width > 400
  84. // We don't need to check a nonce here. A nonce is already checked "further up" in most code paths.
  85. // In the case where no nonce is ever checked, setting this $_POST parameter doesn't do anything the submitter couldn't already do (set the width/height).
  86. && isset( $_POST['widget-text'] ) // phpcs:ignore WordPress.Security.NonceVerification.Missing
  87. ) {
  88. $width = 200;
  89. $height = 200;
  90. }
  91. $attributes = '';
  92. if ( isset( $params['width'] ) && '100%' === $params['width']['value'] ) {
  93. $width = '100%';
  94. }
  95. if ( $width ) {
  96. $attributes = ' width="' . $width . '"';
  97. }
  98. if ( $height ) {
  99. $attributes .= ' height="' . $height . '"';
  100. }
  101. $domain = 'spreadsheets';
  102. if ( in_array( $match[2], array( 'docs', 'drive', 'www', 'calendar' ), true ) ) {
  103. $domain = $match[2];
  104. }
  105. // Make sure this is actually something that the shortcode supports. If it's not, leave the HTML alone.
  106. if ( ! googleapps_validate_domain_and_dir( $domain, $match[3] ) ) {
  107. continue;
  108. }
  109. /** This action is documented in modules/widgets/social-media-icons.php */
  110. do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', googleapps_service_name( $domain, $match[3] ) );
  111. $content = str_replace( $match[0], '[googleapps domain="' . $domain . '" dir="' . $match[3] . '" query="' . esc_attr( $match[4] ) . '"' . $attributes . ' /]', $content );
  112. }
  113. }
  114. return $content;
  115. }
  116. /**
  117. * Parse shortcode attributes and output a Google Docs embed.
  118. *
  119. * @since 4.5.0
  120. *
  121. * @param array $atts Shortcode attributes.
  122. *
  123. * @return string
  124. */
  125. function googleapps_shortcode( $atts ) {
  126. global $content_width;
  127. $attr = shortcode_atts(
  128. array(
  129. 'width' => '100%',
  130. 'height' => '560',
  131. 'domain' => 'docs',
  132. 'dir' => 'document',
  133. 'query' => '',
  134. 'src' => '',
  135. ),
  136. $atts
  137. );
  138. if ( isset( $content_width ) && is_numeric( $attr['width'] ) && $attr['width'] > $content_width ) {
  139. $attr['width'] = $content_width;
  140. }
  141. if ( isset( $content_width ) && '560' === $attr['height'] ) {
  142. $attr['height'] = floor( $content_width * 3 / 4 );
  143. }
  144. if ( isset( $atts[0] ) && $atts[0] ) {
  145. $attr['src'] = $atts[0];
  146. }
  147. if ( $attr['src'] && preg_match( '!https?://(docs|drive|spreadsheets\d*|calendar|www)*\.google\.com/([-\w\./]+)\?([^"]+)!', $attr['src'], $matches ) ) {
  148. $attr['domain'] = $matches[1];
  149. $attr['dir'] = $matches[2];
  150. parse_str( htmlspecialchars_decode( $matches[3] ), $query_ar );
  151. $query_ar['chrome'] = 'false';
  152. $query_ar['embedded'] = 'true';
  153. $attr['query'] = http_build_query( $query_ar );
  154. }
  155. if ( ! googleapps_validate_domain_and_dir( $attr['domain'], $attr['dir'] ) ) {
  156. return '<!-- Unsupported URL -->';
  157. }
  158. $attr['query'] = $attr['dir'] . '?' . $attr['query'];
  159. /** This action is documented in modules/widgets/social-media-icons.php */
  160. do_action( 'jetpack_bump_stats_extras', 'embeds', googleapps_service_name( $attr['domain'], $attr['dir'] ) );
  161. return sprintf(
  162. '<iframe src="%s" frameborder="0" width="%s" height="%s" marginheight="0" marginwidth="0" allowfullscreen="true" mozallowfullscreen="true" webkitallowfullscreen="true"></iframe>',
  163. esc_url( 'https://' . $attr['domain'] . '.google.com/' . $attr['query'] ),
  164. esc_attr( $attr['width'] ),
  165. esc_attr( $attr['height'] )
  166. );
  167. }
  168. /**
  169. * Check that the domain blogs to a Google Apps domain.
  170. *
  171. * @since 4.5.0
  172. *
  173. * @param string $domain Google subdomain.
  174. * @param string $dir Subdirectory of the shared URL.
  175. *
  176. * @return bool
  177. */
  178. function googleapps_validate_domain_and_dir( $domain, $dir ) {
  179. if ( ! in_array( $domain, array( 'docs', 'drive', 'www', 'spreadsheets', 'calendar' ), true ) ) {
  180. return false;
  181. }
  182. // Calendars.
  183. if ( ( 'www' === $domain || 'calendar' === $domain ) && 'calendar/' !== substr( $dir, 0, 9 ) ) {
  184. return false;
  185. }
  186. // Docs.
  187. if ( in_array( $domain, array( 'docs', 'drive' ), true ) && ! preg_match( '![-\.\w/]*(presentation/embed|presentation/d/(.*)|present/embed|document/pub|spreadsheets/d/(.*)|document/d/(e/)?[\w-]+/pub|file/d/[\w-]+/preview|viewer|forms/d/(.*)/viewform|spreadsheet/\w+)$!', $dir ) ) {
  188. return false;
  189. }
  190. // Spreadsheets.
  191. if ( 'spreadsheets' === $domain && ! preg_match( '!^([-\.\w/]+/pub|[-\.\w/]*embeddedform)$!', $dir ) ) {
  192. return false;
  193. }
  194. return true;
  195. }
  196. /**
  197. * Get the name of the service we'll be embedding.
  198. *
  199. * @since 4.5.0
  200. *
  201. * @param string $domain Google subdomain.
  202. * @param string $dir Subdirectory of the shared URL.
  203. *
  204. * @return string
  205. */
  206. function googleapps_service_name( $domain, $dir ) {
  207. switch ( $domain ) {
  208. case 'drive':
  209. case 'docs':
  210. $service_name = ( 'present/embed' === $dir ) ? 'googledocs_presentation' : 'googledocs_document';
  211. break;
  212. case 'spreadsheets':
  213. $service_name = ( 'embeddedform' === $dir ) ? 'googledocs_form' : 'googledocs_spreadsheet';
  214. break;
  215. case 'calendar':
  216. default:
  217. $service_name = 'google_calendar';
  218. }
  219. return $service_name;
  220. }