No Description

kickstarter.php 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Kickstarter shortcode
  4. *
  5. * Usage:
  6. * [kickstarter url="https://www.kickstarter.com/projects/peaktoplateau/yak-wool-baselayers-from-tibet-to-the-world" width="480" height=""]
  7. *
  8. * @package automattic/jetpack
  9. */
  10. add_shortcode( 'kickstarter', 'jetpack_kickstarter_shortcode' );
  11. add_filter( 'pre_kses', 'jetpack_kickstarter_embed_to_shortcode' );
  12. /**
  13. * Parse shortcode arguments and render its output.
  14. *
  15. * @since 4.5.0
  16. *
  17. * @param array $atts Shortcode parameters.
  18. *
  19. * @return string
  20. */
  21. function jetpack_kickstarter_shortcode( $atts ) {
  22. if ( empty( $atts['url'] ) ) {
  23. return '';
  24. }
  25. $url = esc_url_raw( $atts['url'] );
  26. if ( ! preg_match( '#^(www\.)?kickstarter\.com$#i', wp_parse_url( $url, PHP_URL_HOST ) ) ) {
  27. return '<!-- Invalid Kickstarter URL -->';
  28. }
  29. global $wp_embed;
  30. return $wp_embed->shortcode( $atts, $url );
  31. }
  32. /**
  33. * Converts Kickstarter iframe embeds to a shortcode.
  34. *
  35. * EG: <iframe width="480" height="360" src="http://www.kickstarter.com/projects/deweymac/dewey-mac-kid-detective-book-make-diy-and-stem-spy/widget/video.html" frameborder="0" scrolling="no"> </iframe>
  36. *
  37. * @since 4.5.0
  38. *
  39. * @param string $content Entry content that possibly includes a Kickstarter embed.
  40. *
  41. * @return string
  42. */
  43. function jetpack_kickstarter_embed_to_shortcode( $content ) {
  44. if ( ! is_string( $content ) || false === stripos( $content, 'www.kickstarter.com/projects' ) ) {
  45. return $content;
  46. }
  47. $regexp = '!<iframe((?:\s+\w+=[\'"][^\'"]*[\'"])*)\s+src=[\'"](http://www\.kickstarter\.com/projects/[^/]+/[^/]+)/[^\'"]+[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)>[\s]*</iframe>!i';
  48. $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); // phpcs:ignore
  49. foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
  50. if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
  51. continue;
  52. }
  53. foreach ( $matches as $match ) {
  54. $url = esc_url( $match[2] );
  55. $params = $match[1] . $match[3];
  56. if ( 'regexp_ent' === $reg ) {
  57. $params = html_entity_decode( $params );
  58. }
  59. $params = wp_kses_hair( $params, array( 'http' ) );
  60. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  61. $shortcode = '[kickstarter url=' . $url . ( ( ! empty( $width ) ) ? " width=$width" : '' ) . ']';
  62. $content = str_replace( $match[0], $shortcode, $content );
  63. }
  64. }
  65. return $content;
  66. }