kickstarter.php
| 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 | |
| 9 | add_shortcode( 'kickstarter', 'jetpack_kickstarter_shortcode' ); |
| 10 | add_filter( 'pre_kses', 'jetpack_kickstarter_embed_to_shortcode' ); |
| 11 | |
| 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 | |
| 26 | $url = esc_url_raw( $atts['url'] ); |
| 27 | if ( ! preg_match( '#^(www\.)?kickstarter\.com$#i', parse_url( $url, PHP_URL_HOST ) ) ) { |
| 28 | return '<!-- Invalid Kickstarter URL -->'; |
| 29 | } |
| 30 | |
| 31 | global $wp_embed; |
| 32 | return $wp_embed->shortcode( $atts, $url ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Converts Kickstarter iframe embeds to a shortcode. |
| 37 | * |
| 38 | * 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> |
| 39 | * |
| 40 | * @since 4.5.0 |
| 41 | * |
| 42 | * @param string $content Entry content that possibly includes a Kickstarter embed. |
| 43 | * |
| 44 | * @return string |
| 45 | */ |
| 46 | function jetpack_kickstarter_embed_to_shortcode( $content ) { |
| 47 | if ( ! is_string( $content ) || false === stripos( $content, 'www.kickstarter.com/projects' ) ) { |
| 48 | return $content; |
| 49 | } |
| 50 | |
| 51 | $regexp = '!<iframe((?:\s+\w+=[\'"][^\'"]*[\'"])*)\s+src=[\'"](http://www\.kickstarter\.com/projects/[^/]+/[^/]+)/[^\'"]+[\'"]((?:\s+\w+=[\'"][^\'"]*[\'"])*)>[\s]*</iframe>!i'; |
| 52 | $regexp_ent = str_replace( '&#0*58;', '&#0*58;|�*58;', htmlspecialchars( $regexp, ENT_NOQUOTES ) ); |
| 53 | |
| 54 | foreach ( array( 'regexp', 'regexp_ent' ) as $reg ) { |
| 55 | if ( ! preg_match_all( $$reg, $content, $matches, PREG_SET_ORDER ) ) { |
| 56 | continue; |
| 57 | } |
| 58 | |
| 59 | foreach ( $matches as $match ) { |
| 60 | $url = esc_url( $match[2] ); |
| 61 | |
| 62 | $params = $match[1] . $match[3]; |
| 63 | |
| 64 | if ( 'regexp_ent' == $reg ) { |
| 65 | $params = html_entity_decode( $params ); |
| 66 | } |
| 67 | |
| 68 | $params = wp_kses_hair( $params, array( 'http' ) ); |
| 69 | |
| 70 | $width = isset( $params['width'] ) ? (int) $params['width']['value'] : 0; |
| 71 | |
| 72 | $shortcode = '[kickstarter url=' . $url . ( ( ! empty( $width ) ) ? " width=$width" : '' ) . ']'; |
| 73 | $content = str_replace( $match[0], $shortcode, $content ); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return $content; |
| 78 | } |