Bez popisu

social-links.php 6.6KB

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