Share_Custom

The Jetpack by WordPress.com Share Custom class.

Defined (1)

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

/modules/sharedaddy/sharing-sources.php  
  1. class Share_Custom extends Sharing_Advanced_Source { 
  2. private $name; 
  3. private $icon; 
  4. private $url; 
  5. public $smart = true; 
  6. public $shortname; 
  7.  
  8. public function get_class() { 
  9. return 'custom share-custom-' . sanitize_html_class( strtolower( $this->name ) ); 
  10.  
  11. public function __construct( $id, array $settings ) { 
  12. parent::__construct( $id, $settings ); 
  13.  
  14. $opts = $this->get_options(); 
  15.  
  16. if ( isset( $settings['name'] ) ) { 
  17. $this->name = $settings['name']; 
  18. $this->shortname = preg_replace( '/[^a-z0-9]*/', '', $settings['name'] ); 
  19.  
  20. if ( isset( $settings['icon'] ) ) { 
  21. $this->icon = $settings['icon']; 
  22.  
  23. $new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) ); 
  24. $i = 0; 
  25. while ( $new_icon != $this->icon ) { 
  26. if ( $i > 5 ) { 
  27. $this->icon = false; 
  28. break; 
  29. } else { 
  30. $this->icon = $new_icon; 
  31. $new_icon = esc_url_raw( wp_specialchars_decode( $this->icon, ENT_QUOTES ) ); 
  32. $i++; 
  33.  
  34. if ( isset( $settings['url'] ) ) 
  35. $this->url = $settings['url']; 
  36.  
  37. public function get_name() { 
  38. return $this->name; 
  39.  
  40. public function get_display( $post ) { 
  41. $str = $this->get_link( $this->get_process_request_url( $post->ID ), esc_html( $this->name ), sprintf( __( 'Click to share on %s', 'jetpack' ), esc_attr( $this->name ) ), 'share='.$this->id ); 
  42. return str_replace( '<span>', '<span style="' . esc_attr( 'background-image:url("' . addcslashes( esc_url_raw( $this->icon ), '"' ) . '");' ) . '">', $str ); 
  43.  
  44. public function process_request( $post, array $post_data ) { 
  45. $url = str_replace( '&', '&', $this->url ); 
  46. $url = str_replace( '%post_url%', rawurlencode( $this->get_share_url( $post->ID ) ), $url ); 
  47. $url = str_replace( '%post_full_url%', rawurlencode( get_permalink( $post->ID ) ), $url ); 
  48. $url = str_replace( '%post_title%', rawurlencode( $this->get_share_title( $post->ID ) ), $url ); 
  49.  
  50. if ( strpos( $url, '%post_tags%' ) !== false ) { 
  51. $tags = get_the_tags( $post->ID ); 
  52. $tagged = ''; 
  53.  
  54. if ( $tags ) { 
  55. foreach ( $tags AS $tag ) { 
  56. $tagged[] = rawurlencode( $tag->name ); 
  57.  
  58. $tagged = implode( ', ', $tagged ); 
  59.  
  60. $url = str_replace( '%post_tags%', $tagged, $url ); 
  61.  
  62. if ( strpos( $url, '%post_excerpt%' ) !== false ) { 
  63. $url_excerpt = $post->post_excerpt; 
  64. if ( empty( $url_excerpt ) ) 
  65. $url_excerpt = $post->post_content; 
  66.  
  67. $url_excerpt = strip_tags( strip_shortcodes( $url_excerpt ) ); 
  68. $url_excerpt = wp_html_excerpt( $url_excerpt, 100 ); 
  69. $url_excerpt = rtrim( preg_replace( '/[^ .]*$/', '', $url_excerpt ) ); 
  70. $url = str_replace( '%post_excerpt%', rawurlencode( $url_excerpt ), $url ); 
  71.  
  72. // Record stats 
  73. parent::process_request( $post, $post_data ); 
  74.  
  75. // Redirect 
  76. wp_redirect( $url ); 
  77. die(); 
  78.  
  79. public function display_options() { 
  80. ?> 
  81. <div class="input"> 
  82. <table class="form-table"> 
  83. <tbody> 
  84. <tr> 
  85. <th scope="row"><?php _e( 'Label', 'jetpack' ); ?></th> 
  86. <td><input type="text" name="name" value="<?php echo esc_attr( $this->name ); ?>" /></td> 
  87. </tr> 
  88.  
  89. <tr> 
  90. <th scope="row"><?php _e( 'URL', 'jetpack' ); ?></th> 
  91. <td><input type="text" name="url" value="<?php echo esc_attr( $this->url ); ?>" /></td> 
  92. </tr> 
  93.  
  94. <tr> 
  95. <th scope="row"><?php _e( 'Icon', 'jetpack' ); ?></th> 
  96. <td><input type="text" name="icon" value="<?php echo esc_attr( $this->icon ); ?>" /></td> 
  97. </tr> 
  98.  
  99. <tr> 
  100. <th scope="row"></th> 
  101. <td> 
  102. <input class="button-secondary" type="submit" value="<?php esc_attr_e( 'Save', 'jetpack' ); ?>" /> 
  103. <a href="#" class="remove"><small><?php _e( 'Remove Service', 'jetpack' ); ?></small></a> 
  104. </td> 
  105. </tr> 
  106. </tbody> 
  107. </table> 
  108. </div> 
  109. <?php 
  110.  
  111. public function update_options( array $data ) { 
  112. $name = trim( wp_html_excerpt( wp_kses( stripslashes( $data['name'] ), array() ), 30 ) ); 
  113. $url = trim( esc_url_raw( $data['url'] ) ); 
  114. $icon = trim( esc_url_raw( $data['icon'] ) ); 
  115.  
  116. if ( $name ) 
  117. $this->name = $name; 
  118.  
  119. if ( $url ) 
  120. $this->url = $url; 
  121.  
  122. if ( $icon ) 
  123. $this->icon = $icon; 
  124.  
  125. public function get_options() { 
  126. return array( 
  127. 'name' => $this->name,  
  128. 'icon' => $this->icon,  
  129. 'url' => $this->url,  
  130. ); 
  131.  
  132. public function display_preview( $echo = true, $force_smart = false, $button_style = null ) { 
  133. $opts = $this->get_options(); 
  134.  
  135. $text = ' '; 
  136. if ( !$this->smart ) 
  137. if ( $this->button_style != 'icon' ) 
  138. $text = $this->get_name(); 
  139.  
  140. $klasses = array( 'share-'.$this->shortname ); 
  141.  
  142. if ( $this->button_style == 'icon' || $this->button_style == 'icon-text' ) 
  143. $klasses[] = 'share-icon'; 
  144.  
  145. if ( $this->button_style == 'icon' ) { 
  146. $text = ''; 
  147. $klasses[] = 'no-text'; 
  148.  
  149. if ( $this->button_style == 'text' ) 
  150. $klasses[] = 'no-icon'; 
  151.  
  152. $link = sprintf( 
  153. '<a rel="nofollow" class="%s" href="javascript:void(0)" title="%s"><span style="background-image:url("%s") !important;background-position:left center;background-repeat:no-repeat;">%s</span></a>',  
  154. implode( ' ', $klasses ),  
  155. $this->get_name(),  
  156. addcslashes( esc_url_raw( $opts['icon'] ), '"' ),  
  157. $text 
  158. ); 
  159. ?> 
  160. <div class="option option-smart-off"> 
  161. <?php echo $link ; ?> 
  162. </div><?php