social-links.php 6.3 KB

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