Brak opisu

pinterest.php 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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: https://pinterest.com/pin/129056345550241149/
  8. * Second Example URL: https://uk.pinterest.com/annsawesomepins/travel/
  9. *
  10. * @package automattic/jetpack
  11. */
  12. wp_embed_register_handler(
  13. 'pinterest',
  14. '#'
  15. . 'https?://'
  16. . '(?:www\.)?'
  17. . '(?:[a-z]{2}\.)?'
  18. . 'pinterest\.[a-z.]+/'
  19. . '([^/]+)'
  20. . '(/[^/]+)?'
  21. . '#',
  22. 'pinterest_embed_handler'
  23. );
  24. /**
  25. * Callback to modify output of embedded Pinterest posts.
  26. *
  27. * @param array $matches Regex partial matches against the URL passed.
  28. * @param array $attr Attributes received in embed response.
  29. * @param array $url Requested URL to be embedded.
  30. */
  31. function pinterest_embed_handler( $matches, $attr, $url ) {
  32. // Pinterest's JS handles making the embed.
  33. $script_src = '//assets.pinterest.com/js/pinit.js';
  34. wp_enqueue_script( 'pinterest-embed', $script_src, array(), JETPACK__VERSION, true );
  35. $path = wp_parse_url( $url, PHP_URL_PATH );
  36. if ( 0 === strpos( $path, '/pin/' ) ) {
  37. $embed_type = 'embedPin';
  38. } elseif ( preg_match( '#^/([^/]+)/?$#', $path ) ) {
  39. $embed_type = 'embedUser';
  40. } elseif ( preg_match( '#^/([^/]+)/([^/]+)/?$#', $path ) ) {
  41. $embed_type = 'embedBoard';
  42. } else {
  43. if ( current_user_can( 'edit_posts' ) ) {
  44. return __( 'Sorry, that Pinterest URL was not recognized.', 'jetpack' );
  45. }
  46. return;
  47. }
  48. $return = sprintf( '<a data-pin-do="%s" href="%s"></a>', esc_attr( $embed_type ), esc_url( $url ) );
  49. // If we're generating an embed view for the WordPress Admin via ajax.
  50. if ( doing_action( 'wp_ajax_parse-embed' ) ) {
  51. $return .= sprintf(
  52. '<script src="%s"></script>', // phpcs:ignore WordPress.WP.EnqueuedResources.NonEnqueuedScript
  53. esc_url( $script_src )
  54. );
  55. }
  56. return $return;
  57. }