pinterest.php
| 1 | <?php |
| 2 | /** |
| 3 | * Pinterest embeds |
| 4 | * |
| 5 | * Based on "Board Widget" example here: http://business.pinterest.com/widget-builder/#code |
| 6 | */ |
| 7 | |
| 8 | // Example URL: http://pinterest.com/pinterest/pin-pets/ |
| 9 | // Second Example URL: https://uk.pinterest.com/annsawesomepins/travel/ |
| 10 | wp_embed_register_handler( 'pinterest', '#https?://(?:www\.)?(?:[a-z]{2}\.)?pinterest\.com/([^/]+)(/[^/]+)?#', 'pinterest_embed_handler' ); |
| 11 | |
| 12 | function pinterest_embed_handler( $matches, $attr, $url ) { |
| 13 | // Pinterest's JS handles making the embed |
| 14 | $script_src = '//assets.pinterest.com/js/pinit.js'; |
| 15 | wp_enqueue_script( 'pinterest-embed', $script_src, array(), false, true ); |
| 16 | |
| 17 | $path = parse_url( $url, PHP_URL_PATH ); |
| 18 | if ( 0 === strpos( $path, '/pin/' ) ) { |
| 19 | $embed_type = 'embedPin'; |
| 20 | } elseif ( preg_match( '#^/([^/]+)/?$#', $path ) ) { |
| 21 | $embed_type = 'embedUser'; |
| 22 | } elseif ( preg_match( '#^/([^/]+)/([^/]+)/?$#', $path ) ) { |
| 23 | $embed_type = 'embedBoard'; |
| 24 | } else { |
| 25 | if ( current_user_can( 'edit_posts' ) ) { |
| 26 | return __( 'Sorry, that Pinterest URL was not recognized.', 'jetpack' ); |
| 27 | } |
| 28 | return; |
| 29 | } |
| 30 | |
| 31 | $return = sprintf( '<a data-pin-do="%s" href="%s"></a>', esc_attr( $embed_type ), esc_url( $url ) ); |
| 32 | |
| 33 | // If we're generating an embed view for the WordPress Admin via ajax... |
| 34 | if ( doing_action( 'wp_ajax_parse-embed' ) ) { |
| 35 | $return .= sprintf( '<script src="%s"></script>', esc_url( $script_src ) ); |
| 36 | } |
| 37 | |
| 38 | return $return; |
| 39 | } |
| 40 |