Share_Pinterest

The Jetpack by WordPress.com Share Pinterest class.

Defined (1)

The class is defined in the following location(s).

/modules/sharedaddy/sharing-sources.php  
  1. class Share_Pinterest extends Sharing_Source { 
  2. public $shortname = 'pinterest'; 
  3. public $genericon = '\f209'; 
  4.  
  5. public function __construct( $id, array $settings ) { 
  6. parent::__construct( $id, $settings ); 
  7. if ( 'official' == $this->button_style ) 
  8. $this->smart = true; 
  9. else 
  10. $this->smart = false; 
  11.  
  12. public function get_name() { 
  13. return __( 'Pinterest', 'jetpack' ); 
  14.  
  15. public function get_image( $post ) { 
  16. if ( class_exists( 'Jetpack_PostImages' ) ) { 
  17. $image = Jetpack_PostImages::get_image( $post->ID, array( 'fallback_to_avatars' => true ) ); 
  18. if ( ! empty( $image ) ) { 
  19. return $image['src']; 
  20.  
  21. /** 
  22. * Filters the default image used by the Pinterest Pin It share button. 
  23. * @since 3.6 
  24. * @param string $url Default image URL. 
  25. */ 
  26. return apply_filters( 'jetpack_sharing_pinterest_default_image', 'https://s0.wp.com/i/blank.jpg' ); 
  27.  
  28. public function get_external_url( $post ) { 
  29. $url = '//www.pinterest.com/pin/create/button/?url=' . rawurlencode( $this->get_share_url( $post->ID ) ) . '&media=' . rawurlencode( $this->get_image( $post ) ) . '&description=' . rawurlencode( $post->post_title ); 
  30.  
  31. /** 
  32. * Filters the Pinterest share URL used in sharing button output. 
  33. * @since 3.6 
  34. * @param string $url Pinterest share URL. 
  35. */ 
  36. return apply_filters( 'jetpack_sharing_pinterest_share_url', $url ); 
  37.  
  38. public function get_widget_type() { 
  39. /** 
  40. * Filters the Pinterest widget type. 
  41. * @since 3.6 
  42. * @link https://business.pinterest.com/en/widget-builder 
  43. * @param string $type Pinterest widget type. Default of 'buttonPin' for single-image selection. 'buttonBookmark' for multi-image modal. 
  44. */ 
  45. return apply_filters( 'jetpack_sharing_pinterest_widget_type', 'buttonPin' ); 
  46.  
  47. public function get_display( $post ) { 
  48. $display = ''; 
  49.  
  50. if ( $this->smart ) { 
  51. $display = sprintf( 
  52. '<div class="pinterest_button"><a href="%s" data-pin-do="%s" data-pin-config="beside"><img src="//assets.pinterest.com/images/pidgets/pinit_fg_en_rect_gray_20.png" /></a></div>',  
  53. esc_url( $this->get_external_url( $post ) ),  
  54. esc_attr( $this->get_widget_type() ) 
  55. ); 
  56. } else { 
  57. $display = $this->get_link( $this->get_process_request_url( $post->ID ), _x( 'Pinterest', 'share to', 'jetpack' ), __( 'Click to share on Pinterest', 'jetpack' ), 'share=pinterest', 'sharing-pinterest-' . $post->ID ); 
  58.  
  59. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) { 
  60. sharing_register_post_for_share_counts( $post->ID ); 
  61.  
  62. return $display; 
  63.  
  64. public function process_request( $post, array $post_data ) { 
  65. // Record stats 
  66. parent::process_request( $post, $post_data ); 
  67. // If we're triggering the multi-select panel, then we don't need to redirect to Pinterest 
  68. if ( !isset( $_GET['js_only'] ) ) { 
  69. $pinterest_url = esc_url_raw( $this->get_external_url( $post ) ); 
  70. wp_redirect( $pinterest_url ); 
  71. } else { 
  72. echo '// share count bumped'; 
  73. die(); 
  74.  
  75. public function display_footer() { 
  76. /** 
  77. * Filter the Pin it button appearing when hovering over images when using the official button style. 
  78. * @since 3.6.0 
  79. * @param bool $jetpack_pinit_over True by default, displays the Pin it button when hovering over images. 
  80. */ 
  81. $jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true ); 
  82. ?> 
  83. <?php if ( $this->smart ) : ?> 
  84. <script type="text/javascript"> 
  85. // Pinterest shared resources 
  86. var s = document.createElement("script"); 
  87. s.type = "text/javascript"; 
  88. s.async = true; 
  89. <?php if ( $jetpack_pinit_over ) echo "s.setAttribute('data-pin-hover', true);"; ?> 
  90. s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js"; 
  91. var x = document.getElementsByTagName("script")[0]; 
  92. x.parentNode.insertBefore(s, x); 
  93. // if 'Pin it' button has 'counts' make container wider 
  94. jQuery(window).load( function() { jQuery( 'li.share-pinterest a span:visible' ).closest( '.share-pinterest' ).width( '80px' ); } ); 
  95. </script> 
  96. <?php elseif ( 'buttonPin' != $this->get_widget_type() ) : ?> 
  97. <script type="text/javascript"> 
  98. jQuery(document).on('ready', function() { 
  99. jQuery('body').on('click', 'a.share-pinterest', function(e) { 
  100. e.preventDefault(); 
  101. // Load Pinterest Bookmarklet code 
  102. var s = document.createElement("script"); 
  103. s.type = "text/javascript"; 
  104. s.src = window.location.protocol + "//assets.pinterest.com/js/pinmarklet.js?r=" + ( Math.random() * 99999999 ); 
  105. var x = document.getElementsByTagName("script")[0]; 
  106. x.parentNode.insertBefore(s, x); 
  107. // Trigger Stats 
  108. var s = document.createElement("script"); 
  109. s.type = "text/javascript"; 
  110. s.src = this + ( this.toString().indexOf( '?' ) ? '&' : '?' ) + 'js_only=1'; 
  111. var x = document.getElementsByTagName("script")[0]; 
  112. x.parentNode.insertBefore(s, x); 
  113. }); 
  114. }); 
  115. </script> 
  116. <?php endif;