pinterest.php 1.4 KB

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