kickstarter.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. add_shortcode( 'kickstarter', 'jetpack_kickstarter_shortcode' );
  9. add_filter( 'pre_kses', 'jetpack_kickstarter_embed_to_shortcode' );
  10. /**
  11. * Parse shortcode arguments and render its output.
  12. *
  13. * @since 4.5.0
  14. *
  15. * @param array $atts Shortcode parameters.
  16. *
  17. * @return string
  18. */
  19. function jetpack_kickstarter_shortcode( $atts ) {
  20. if ( empty( $atts['url'] ) ) {
  21. return '';
  22. }
  23. $url = esc_url_raw( $atts['url'] );
  24. if ( ! preg_match( '#^(www\.)?kickstarter\.com$#i', parse_url( $url, PHP_URL_HOST ) ) ) {
  25. return '<!-- Invalid Kickstarter URL -->';
  26. }
  27. global $wp_embed;
  28. return $wp_embed->shortcode( $atts, $url );
  29. }
  30. /**
  31. * Converts Kickstarter iframe embeds to a shortcode.
  32. *
  33. * 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>
  34. *
  35. * @since 4.5.0
  36. *
  37. * @param string $content Entry content that possibly includes a Kickstarter embed.
  38. *
  39. * @return string
  40. */
  41. function jetpack_kickstarter_embed_to_shortcode( $content ) {
  42. if ( ! is_string( $content ) || false === stripos( $content, 'www.kickstarter.com/projects' ) ) {
  43. return $content;
  44. }
  45. $regexp = '!<iframe((?:\s+\w+=[\'"][^\'"]*[\'"])*)\s+src=[\'"](http://www\.kickstarter\.com/projects/[^/]+/[^/]+)/[^\'"]+[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)>[\s]*</iframe>!i';
  46. $regexp_ent = str_replace( '&amp;#0*58;', '&amp;#0*58;|&#0*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) );
  47. foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) {
  48. if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) {
  49. continue;
  50. }
  51. foreach ( $matches as $match ) {
  52. $url = esc_url( $match[2] );
  53. $params = $match[1] . $match[3];
  54. if ( 'regexp_ent' == $reg ) {
  55. $params = html_entity_decode( $params );
  56. }
  57. $params = wp_kses_hair( $params, array( 'http' ) );
  58. $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0;
  59. $shortcode = '[kickstarter url=' . $url . ( ( ! empty( $width ) ) ? " width=$width" : '' ) . ']';
  60. $content = str_replace( $match[0], $shortcode, $content );
  61. }
  62. }
  63. return $content;
  64. }