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( |
| 11 | 'pinterest', |
| 12 | '#' |
| 13 | . 'https?://' |
| 14 | . '(?:www\.)?' |
| 15 | . '(?:[a-z]{2}\.)?' |
| 16 | . 'pinterest\.[a-z.]+/' |
| 17 | . '([^/]+)' |
| 18 | . '(/[^/]+)?' |
| 19 | . '#', |
| 20 | 'pinterest_embed_handler' |
| 21 | ); |
| 22 | |
| 23 | function pinterest_embed_handler( $matches, $attr, $url ) { |
| 24 | // Pinterest's JS handles making the embed |
| 25 | $script_src = '//assets.pinterest.com/js/pinit.js'; |
| 26 | wp_enqueue_script( 'pinterest-embed', $script_src, array(), false, true ); |
| 27 | |
| 28 | $path = parse_url( $url, PHP_URL_PATH ); |
| 29 | if ( 0 === strpos( $path, '/pin/' ) ) { |
| 30 | $embed_type = 'embedPin'; |
| 31 | } elseif ( preg_match( '#^/([^/]+)/?$#', $path ) ) { |
| 32 | $embed_type = 'embedUser'; |
| 33 | } elseif ( preg_match( '#^/([^/]+)/([^/]+)/?$#', $path ) ) { |
| 34 | $embed_type = 'embedBoard'; |
| 35 | } else { |
| 36 | if ( current_user_can( 'edit_posts' ) ) { |
| 37 | return __( 'Sorry, that Pinterest URL was not recognized.', 'jetpack' ); |
| 38 | } |
| 39 | return; |
| 40 | } |
| 41 | |
| 42 | $return = sprintf( '<a data-pin-do="%s" href="%s"></a>', esc_attr( $embed_type ), esc_url( $url ) ); |
| 43 | |
| 44 | // If we're generating an embed view for the WordPress Admin via ajax... |
| 45 | if ( doing_action( 'wp_ajax_parse-embed' ) ) { |
| 46 | $return .= sprintf( '<script src="%s"></script>', esc_url( $script_src ) ); |
| 47 | } |
| 48 | |
| 49 | return $return; |
| 50 | } |
| 51 |