Social_Links

The Jetpack by WordPress.com Social Links class.

Defined (1)

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

/modules/theme-tools/social-links.php  
  1. class Social_Links { 
  2.  
  3. /** 
  4. * The links the user set for each service. 
  5. * @var array 
  6. */ 
  7. private $links; 
  8.  
  9. /** 
  10. * A Publicize object. 
  11. * @var Publicize 
  12. */ 
  13. private $publicize; 
  14.  
  15. /** 
  16. * An array with all services that are supported by both Publicize and the 
  17. * currently active theme. 
  18. * @var array 
  19. */ 
  20. private $services = array(); 
  21.  
  22. /** 
  23. * An array of the services the theme supports 
  24. * @var array 
  25. */ 
  26. private $theme_supported_services = array(); 
  27.  
  28. /** 
  29. * Constructor. 
  30. */ 
  31. public function __construct() { 
  32. $theme_support = get_theme_support( 'social-links' ); 
  33.  
  34. /** An array of named arguments must be passed as the second parameter 
  35. * of add_theme_support(). 
  36. */ 
  37. if ( empty( $theme_support[0] ) ) 
  38. return; 
  39.  
  40. $this->theme_supported_services = $theme_support[0]; 
  41. $this->links = Jetpack_Options::get_option( 'social_links', array() ); 
  42.  
  43. $this->admin_setup(); 
  44.  
  45. add_filter( 'jetpack_has_social_links', array( $this, 'has_social_links' ) ); 
  46. add_filter( 'jetpack_get_social_links', array( $this, 'get_social_links' ) ); 
  47.  
  48. foreach ( $theme_support[0] as $service ) { 
  49. add_filter( "pre_option_jetpack-$service", array( $this, 'get_social_link_filter' ) ); // get_option( 'jetpack-service' ); 
  50. add_filter( "theme_mod_jetpack-$service", array( $this, 'get_social_link_filter' ) ); // get_theme_mod( 'jetpack-service' ); 
  51.  
  52. public function admin_setup() { 
  53. if ( ! current_user_can( 'manage_options' ) ) { 
  54. return; 
  55.  
  56. if ( ! is_admin() && ! $this->is_customize_preview() ) { 
  57. return; 
  58.  
  59. $this->publicize = publicize_init(); 
  60. $publicize_services = $this->publicize->get_services( 'connected' ); 
  61. $this->services = array_intersect( array_keys( $publicize_services ), $this->theme_supported_services ); 
  62.  
  63. add_action( 'publicize_connected', array( $this, 'check_links' ), 20 ); 
  64. add_action( 'publicize_disconnected', array( $this, 'check_links' ), 20 ); 
  65. add_action( 'customize_register', array( $this, 'customize_register' ) ); 
  66. add_filter( 'sanitize_option_jetpack_options', array( $this, 'sanitize_link' ) ); 
  67.  
  68. /** 
  69. * Compares the currently saved links with the connected services and removes 
  70. * links from services that are no longer connected. 
  71. * @return void 
  72. */ 
  73. public function check_links() { 
  74. $active_links = array_intersect_key( $this->links, array_flip( $this->services ) ); 
  75.  
  76. if ( $active_links !== $this->links ) { 
  77. $this->links = $active_links; 
  78. Jetpack_Options::update_option( 'social_links', $active_links ); 
  79.  
  80. /** 
  81. * Add social link dropdown to the Customizer. 
  82. * @param WP_Customize_Manager $wp_customize Theme Customizer object. 
  83. */ 
  84. public function customize_register( $wp_customize ) { 
  85. $wp_customize->add_section( 'jetpack_social_links', array( 
  86. 'title' => __( 'Connect', 'jetpack' ),  
  87. 'priority' => 35,  
  88. ) ); 
  89.  
  90. foreach ( $this->services as $service ) { 
  91. $choices = $this->get_customize_select( $service ); 
  92.  
  93. if ( empty( $choices ) ) { 
  94. continue; 
  95.  
  96. $wp_customize->add_setting( "jetpack_options[social_links][$service]", array( 
  97. 'type' => 'option',  
  98. 'default' => '',  
  99. ) ); 
  100.  
  101. $wp_customize->add_control( "jetpack-$service", array( 
  102. 'label' => $this->publicize->get_service_label( $service ),  
  103. 'section' => 'jetpack_social_links',  
  104. 'settings' => "jetpack_options[social_links][$service]",  
  105. 'type' => 'select',  
  106. 'choices' => $choices,  
  107. ) ); 
  108.  
  109. /** 
  110. * Sanitizes social links. 
  111. * @param array $option The incoming values to be sanitized. 
  112. * @returns array 
  113. */ 
  114. public function sanitize_link( $option ) { 
  115. foreach ( $this->services as $service ) { 
  116. if ( ! empty( $option['social_links'][ $service ] ) ) 
  117. $option['social_links'][ $service ] = esc_url_raw( $option['social_links'][ $service ] ); 
  118. else 
  119. unset( $option['social_links'][ $service ] ); 
  120.  
  121. return $option; 
  122.  
  123. /** 
  124. * Returns whether there are any social links set. 
  125. * @returns bool 
  126. */ 
  127. public function has_social_links() { 
  128. return ! empty( $this->links ); 
  129.  
  130. /** 
  131. * Return available social links. 
  132. * @returns array 
  133. */ 
  134. public function get_social_links() { 
  135. return $this->links; 
  136.  
  137. /** 
  138. * Short-circuits get_option and get_theme_mod calls. 
  139. * @param string $link The incoming value to be replaced. 
  140. * @returns string $link The social link that we've got. 
  141. */ 
  142. public function get_social_link_filter( $link ) { 
  143. if ( preg_match( '/_jetpack-(.+)$/i', current_filter(), $matches ) && ! empty( $this->links[ $matches[1] ] ) ) 
  144. return $this->links[ $matches[1] ]; 
  145.  
  146. return $link; 
  147.  
  148. /** 
  149. * Puts together an array of choices for a specific service. 
  150. * @param string $service The social service. 
  151. * @return array An associative array with profile links and display names. 
  152. */ 
  153. private function get_customize_select( $service ) { 
  154. $choices = array( 
  155. '' => __( '— Select —', 'jetpack' ) 
  156. ); 
  157.  
  158. $connected_services = $this->publicize->get_services( 'connected' ); 
  159. if ( isset( $connected_services[ $service ] ) ) { 
  160. foreach ( $connected_services[ $service ] as $c ) { 
  161. $profile_link = $this->publicize->get_profile_link( $service, $c ); 
  162.  
  163. if ( false === $profile_link ) { 
  164. continue; 
  165.  
  166. $choices[ $profile_link ] = $this->publicize->get_display_name( $service, $c ); 
  167.  
  168. if ( 1 === count( $choices ) ) { 
  169. return array(); 
  170.  
  171. return $choices; 
  172.  
  173. /** 
  174. * Back-compat function for versions prior to 4.0. 
  175. */ 
  176. private function is_customize_preview() { 
  177. global $wp_customize; 
  178. return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();