/modules/theme-tools/social-links.php

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