Nav apraksta

getty.php 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * Getty shortcode
  4. *
  5. * [getty src="82278805" width="$width" height="$height"]
  6. * <div class="getty embed image" style="background-color:#fff;display:inline-block;font-family:'Helvetica Neue',Helvetica,Arial,sans-serif;color:#a7a7a7;font-size:11px;width:100%;max-width:462px;"><div style="padding:0;margin:0;text-align:left;"><a href="http://www.gettyimages.com/detail/82278805" target="_blank" style="color:#a7a7a7;text-decoration:none;font-weight:normal !important;border:none;display:inline-block;">Embed from Getty Images</a></div><div style="overflow:hidden;position:relative;height:0;padding:80.086580% 0 0 0;width:100%;"><iframe src="//embed.gettyimages.com/embed/82278805?et=jGiu6FXXSpJDGf1SnwLV2g&sig=TFVNFtqghwNw5iJQ1MFWnI8f4Y40_sfogfZLhai6SfA=" width="462" height="370" scrolling="no" frameborder="0" style="display:inline-block;position:absolute;top:0;left:0;width:100%;height:100%;"></iframe></div><p style="margin:0;"></p></div>
  7. *
  8. * @package automattic/jetpack
  9. */
  10. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  11. add_action( 'init', 'jetpack_getty_enable_embeds' );
  12. } else {
  13. jetpack_getty_enable_embeds();
  14. }
  15. /**
  16. * Register Getty as oembed provider. Add filter to reverse iframes to shortcode. Register [getty] shortcode.
  17. *
  18. * @since 4.5.0
  19. * @since 5.8.0 removed string parameter.
  20. */
  21. function jetpack_getty_enable_embeds() {
  22. // Support their oEmbed Endpoint.
  23. wp_oembed_add_provider( '#https?://www\.gettyimages\.com/detail/.*#i', 'https://embed.gettyimages.com/oembed/', true );
  24. wp_oembed_add_provider( '#https?://(www\.)?gty\.im/.*#i', 'https://embed.gettyimages.com/oembed/', true );
  25. // Allow iframes to be filtered to short code (so direct copy+paste can be done).
  26. add_filter( 'pre_kses', 'wpcom_shortcodereverse_getty' );
  27. // Actually display the Getty Embed.
  28. add_shortcode( 'getty', 'jetpack_getty_shortcode' );
  29. }
  30. /**
  31. * Filters the oEmbed provider URL for Getty URLs to include site URL host as
  32. * caller if available, falling back to "wordpress.com". Must be applied at
  33. * time of embed in case that `init` is too early (WP.com REST API).
  34. *
  35. * @module shortcodes
  36. *
  37. * @since 5.8.0
  38. *
  39. * @see WP_oEmbed::fetch
  40. *
  41. * @return string oEmbed provider URL
  42. */
  43. add_filter( 'oembed_fetch_url', 'getty_add_oembed_endpoint_caller' );
  44. /**
  45. * Filter the embeds to add a caller parameter.
  46. *
  47. * @param string $provider URL of the oEmbed provider.
  48. */
  49. function getty_add_oembed_endpoint_caller( $provider ) {
  50. // By time filter is called, original provider URL has had url, maxwidth,
  51. // maxheight query parameters added.
  52. if ( 0 !== strpos( $provider, 'https://embed.gettyimages.com/oembed/' ) ) {
  53. return $provider;
  54. }
  55. // Set the caller argument to pass to Getty's oembed provider.
  56. if ( defined( 'IS_WPCOM' ) && IS_WPCOM ) {
  57. // Only include caller for non-private sites.
  58. if ( ! function_exists( 'is_private_blog' ) || ! is_private_blog() ) {
  59. $host = wp_parse_url( get_bloginfo( 'url' ), PHP_URL_HOST );
  60. }
  61. // Fall back to WordPress.com.
  62. if ( empty( $host ) ) {
  63. $host = 'wordpress.com';
  64. }
  65. } else {
  66. $host = wp_parse_url( get_home_url(), PHP_URL_HOST );
  67. }
  68. return add_query_arg( 'caller', $host, $provider );
  69. }
  70. /**
  71. * Compose shortcode based on Getty iframes.
  72. *
  73. * @since 4.5.0
  74. *
  75. * @param string $content Post content.
  76. *
  77. * @return mixed
  78. */
  79. function wpcom_shortcodereverse_getty( $content ) {
  80. if ( ! is_string( $content ) || false === stripos( $content, '.gettyimages.com/' ) ) {
  81. return $content;
  82. }
  83. $regexp = '!<iframe\s+src=[\'"](https?:)?//embed\.gettyimages\.com/embed(/|/?\?assets=)([a-z0-9_-]+(,[a-z0-9_-]+)*)[^\'"]*?[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)((?:[\s\w]*))></iframe>!i';
  84. $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
  85. // Markup pattern for 2017 embed syntax with significant differences from the prior pattern.
  86. $regexp_2017 = '!<a.+?class=\'gie-(single|slideshow)\'.+?gie\.widgets\.load\({([^}]+)}\).+?embed-cdn\.gettyimages\.com/widgets\.js.+?</script>!';
  87. $regexp_2017_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp_2017, ENT_NOQUOTES ) );
  88. foreach ( compact( 'regexp_2017', 'regexp_2017_ent', 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
  89. if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
  90. continue;
  91. }
  92. foreach ( $matches as $match ) {
  93. if ( 'regexp_2017' === $reg || 'regexp_2017_ent' === $reg ) {
  94. // Extract individual keys from the matched JavaScript object.
  95. $params = $match[2];
  96. if ( ! preg_match_all( '!(?P<key>\w+)\s*:\s*([\'"](?P<value>[^\'"]*?)(px)?[\'"])!', $params, $key_matches, PREG_SET_ORDER ) ) {
  97. continue;
  98. }
  99. foreach ( $key_matches as $key_match ) {
  100. switch ( $key_match['key'] ) {
  101. case 'items':
  102. $ids = $key_match['value'];
  103. break;
  104. case 'w':
  105. $width = (int) $key_match['value'];
  106. break;
  107. case 'h':
  108. $height = (int) $key_match['value'];
  109. break;
  110. case 'tld':
  111. $tld = $key_match['value'];
  112. break;
  113. }
  114. }
  115. } else {
  116. $params = $match[5];
  117. if ( 'regexp_ent' === $reg ) {
  118. $params = html_entity_decode( $params );
  119. }
  120. $params = wp_kses_hair( $params, array( 'http' ) );
  121. $ids = esc_html( $match[3] );
  122. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  123. $height = isset( $params['height'] ) ? (int) $params['height']['value'] : 0;
  124. }
  125. if ( empty( $ids ) ) {
  126. continue;
  127. }
  128. $shortcode = '[getty src="' . esc_attr( $ids ) . '"';
  129. if ( ! empty( $width ) ) {
  130. $shortcode .= ' width="' . esc_attr( $width ) . '"';
  131. }
  132. if ( ! empty( $height ) ) {
  133. $shortcode .= ' height="' . esc_attr( $height ) . '"';
  134. }
  135. /*
  136. * While it does not appear to have any practical impact, Getty has
  137. * requested that we include TLD in the embed request
  138. */
  139. if ( ! empty( $tld ) ) {
  140. $shortcode .= ' tld="' . esc_attr( $tld ) . '"';
  141. }
  142. $shortcode .= ']';
  143. $content = str_replace( $match[0], $shortcode, $content );
  144. }
  145. }
  146. // strip out enclosing div and any other markup.
  147. $regexp = '%<div class="getty\s[^>]*+>.*?<div[^>]*+>(\[getty[^\]]*+\])\s*</div>.*?</div>%is';
  148. $regexp_ent = str_replace( array( '&amp;#0*58;', '[^&gt;]' ), array( '&amp;#0*58;|&#0*58;', '[^&]' ), htmlspecialchars( $regexp, ENT_NOQUOTES ) );
  149. foreach ( compact( 'regexp', 'regexp_ent' ) as $reg => $regexp ) {
  150. if ( ! preg_match_all( $regexp, $content, $matches, PREG_SET_ORDER ) ) {
  151. continue;
  152. }
  153. foreach ( $matches as $match ) {
  154. $content = str_replace( $match[0], $match[1], $content );
  155. }
  156. }
  157. /** This action is documented in modules/widgets/social-media-icons.php */
  158. do_action( 'jetpack_bump_stats_extras', 'html_to_shortcode', 'getty' );
  159. return $content;
  160. }
  161. /**
  162. * Parse shortcode arguments and render its output.
  163. *
  164. * @since 4.5.0
  165. *
  166. * @param array $atts Shortcode parameters.
  167. * @param string $content Content enclosed by shortcode tags.
  168. *
  169. * @return string
  170. */
  171. function jetpack_getty_shortcode( $atts, $content = '' ) {
  172. if ( ! empty( $content ) ) {
  173. $src = $content;
  174. } elseif ( ! empty( $atts['src'] ) ) {
  175. $src = $atts['src'];
  176. } elseif ( ! empty( $atts[0] ) ) {
  177. $src = $atts[0];
  178. } else {
  179. return '<!-- Missing Getty Source ID -->';
  180. }
  181. $src = preg_replace( '/^([\da-z-]+(,[\da-z-]+)*).*$/', '$1', $src );
  182. $params = array(
  183. 'width' => isset( $atts['width'] ) ? (int) $atts['width'] : null,
  184. 'height' => isset( $atts['height'] ) ? (int) $atts['height'] : null,
  185. );
  186. if ( ! empty( $atts['tld'] ) ) {
  187. $params['tld'] = $atts['tld'];
  188. }
  189. return wp_oembed_get( 'https://gty.im/' . $src, array_filter( $params ) );
  190. }