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. * @module sharedaddy 
  24. * @since 3.6.0 
  25. * @param string $url Default image URL. 
  26. */ 
  27. return apply_filters( 'jetpack_sharing_pinterest_default_image', 'https://s0.wp.com/i/blank.jpg' ); 
  28.  
  29. public function get_external_url( $post ) { 
  30. $url = 'https://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 ); 
  31.  
  32. /** 
  33. * Filters the Pinterest share URL used in sharing button output. 
  34. * @module sharedaddy 
  35. * @since 3.6.0 
  36. * @param string $url Pinterest share URL. 
  37. */ 
  38. return apply_filters( 'jetpack_sharing_pinterest_share_url', $url ); 
  39.  
  40. public function get_widget_type() { 
  41. /** 
  42. * Filters the Pinterest widget type. 
  43. * @see https://business.pinterest.com/en/widget-builder 
  44. * @module sharedaddy 
  45. * @since 3.6.0 
  46. * @param string $type Pinterest widget type. Default of 'buttonPin' for single-image selection. 'buttonBookmark' for multi-image modal. 
  47. */ 
  48. return apply_filters( 'jetpack_sharing_pinterest_widget_type', 'buttonPin' ); 
  49.  
  50. public function get_display( $post ) { 
  51. $display = ''; 
  52.  
  53. if ( $this->smart ) { 
  54. $display = sprintf( 
  55. '<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>',  
  56. esc_url( $this->get_external_url( $post ) ),  
  57. esc_attr( $this->get_widget_type() ) 
  58. ); 
  59. } else { 
  60. $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 ); 
  61.  
  62. /** This filter is already documented in modules/sharedaddy/sharing-sources.php */ 
  63. if ( apply_filters( 'jetpack_register_post_for_share_counts', true, $post->ID, 'linkedin' ) ) { 
  64. sharing_register_post_for_share_counts( $post->ID ); 
  65.  
  66. return $display; 
  67.  
  68. public function process_request( $post, array $post_data ) { 
  69. // Record stats 
  70. parent::process_request( $post, $post_data ); 
  71. // If we're triggering the multi-select panel, then we don't need to redirect to Pinterest 
  72. if ( !isset( $_GET['js_only'] ) ) { 
  73. $pinterest_url = esc_url_raw( $this->get_external_url( $post ) ); 
  74. wp_redirect( $pinterest_url ); 
  75. } else { 
  76. echo '// share count bumped'; 
  77. die(); 
  78.  
  79. public function display_footer() { 
  80. /** 
  81. * Filter the Pin it button appearing when hovering over images when using the official button style. 
  82. * @module sharedaddy 
  83. * @since 3.6.0 
  84. * @param bool $jetpack_pinit_over True by default, displays the Pin it button when hovering over images. 
  85. */ 
  86. $jetpack_pinit_over = apply_filters( 'jetpack_pinit_over_button', true ); 
  87. ?> 
  88. <?php if ( $this->smart ) : ?> 
  89. <script type="text/javascript"> 
  90. // Pinterest shared resources 
  91. var s = document.createElement("script"); 
  92. s.type = "text/javascript"; 
  93. s.async = true; 
  94. <?php if ( $jetpack_pinit_over ) echo "s.setAttribute('data-pin-hover', true);"; ?> 
  95. s.src = window.location.protocol + "//assets.pinterest.com/js/pinit.js"; 
  96. var x = document.getElementsByTagName("script")[0]; 
  97. x.parentNode.insertBefore(s, x); 
  98. // if 'Pin it' button has 'counts' make container wider 
  99. jQuery(window).load( function() { jQuery( 'li.share-pinterest a span:visible' ).closest( '.share-pinterest' ).width( '80px' ); } ); 
  100. </script> 
  101. <?php elseif ( 'buttonPin' != $this->get_widget_type() ) : ?> 
  102. <script type="text/javascript"> 
  103. jQuery(document).on('ready', function() { 
  104. jQuery('body').on('click', 'a.share-pinterest', function(e) { 
  105. e.preventDefault(); 
  106. // Load Pinterest Bookmarklet code 
  107. var s = document.createElement("script"); 
  108. s.type = "text/javascript"; 
  109. s.src = window.location.protocol + "//assets.pinterest.com/js/pinmarklet.js?r=" + ( Math.random() * 99999999 ); 
  110. var x = document.getElementsByTagName("script")[0]; 
  111. x.parentNode.insertBefore(s, x); 
  112. // Trigger Stats 
  113. var s = document.createElement("script"); 
  114. s.type = "text/javascript"; 
  115. s.src = this + ( this.toString().indexOf( '?' ) ? '&' : '?' ) + 'js_only=1'; 
  116. var x = document.getElementsByTagName("script")[0]; 
  117. x.parentNode.insertBefore(s, x); 
  118. }); 
  119. }); 
  120. </script> 
  121. <?php endif;