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:
9
* add_theme_support( 'social-links', array(
10
* 'facebook', 'twitter', 'linkedin', 'tumblr', 'google_plus',
14
function jetpack_theme_supports_social_links() {
15
if ( current_theme_supports( 'social-links' ) && function_exists( 'publicize_init' ) ) {
19
add_action( 'init', 'jetpack_theme_supports_social_links', 30 );
21
if ( ! class_exists( 'Social_Links' ) ) {
26
* The links the user set for each service.
40
* An array with all services that are supported by both Publicize and the
41
* currently active theme.
45
private $services = array();
48
* An array of the services the theme supports
52
private $theme_supported_services = array();
57
public function __construct() {
58
$theme_support = get_theme_support( 'social-links' );
60
/* An array of named arguments must be passed as the second parameter
61
* of add_theme_support().
63
if ( empty( $theme_support[0] ) )
66
$this->theme_supported_services = $theme_support[0];
67
$this->links = Jetpack_Options::get_option( 'social_links', array() );
71
add_filter( 'jetpack_has_social_links', array( $this, 'has_social_links' ) );
72
add_filter( 'jetpack_get_social_links', array( $this, 'get_social_links' ) );
74
foreach ( $theme_support[0] as $service ) {
75
add_filter( "pre_option_jetpack-$service", array( $this, 'get_social_link_filter' ) ); // get_option( 'jetpack-service' );
76
add_filter( "theme_mod_jetpack-$service", array( $this, 'get_social_link_filter' ) ); // get_theme_mod( 'jetpack-service' );
80
public function admin_setup() {
81
if ( ! current_user_can( 'manage_options' ) ) {
85
if ( ! is_admin() && ! $this->is_customize_preview() ) {
89
$this->publicize = publicize_init();
90
$publicize_services = $this->publicize->get_services( 'connected' );
91
$this->services = array_intersect( array_keys( $publicize_services ), $this->theme_supported_services );
93
add_action( 'publicize_connected', array( $this, 'check_links' ), 20 );
94
add_action( 'publicize_disconnected', array( $this, 'check_links' ), 20 );
95
add_action( 'customize_register', array( $this, 'customize_register' ) );
96
add_filter( 'sanitize_option_jetpack_options', array( $this, 'sanitize_link' ) );
100
* Compares the currently saved links with the connected services and removes
101
* links from services that are no longer connected.
105
public function check_links() {
106
$active_links = array_intersect_key( $this->links, array_flip( $this->services ) );
108
if ( $active_links !== $this->links ) {
109
$this->links = $active_links;
110
Jetpack_Options::update_option( 'social_links', $active_links );
115
* Add social link dropdown to the Customizer.
117
* @param WP_Customize_Manager $wp_customize Theme Customizer object.
119
public function customize_register( $wp_customize ) {
120
$wp_customize->add_section( 'jetpack_social_links', array(
121
'title' => __( 'Connect', 'jetpack' ),
125
foreach ( array_keys( $this->publicize->get_services( 'all' ) ) as $service ) {
126
$choices = $this->get_customize_select( $service );
128
if ( empty( $choices ) ) {
132
$wp_customize->add_setting( "jetpack_options[social_links][$service]", array(
137
$wp_customize->add_control( "jetpack-$service", array(
138
'label' => $this->publicize->get_service_label( $service ),
139
'section' => 'jetpack_social_links',
140
'settings' => "jetpack_options[social_links][$service]",
142
'choices' => $choices,
148
* Sanitizes social links.
150
* @param array $option The incoming values to be sanitized.
153
public function sanitize_link( $option ) {
154
foreach ( $this->services as $service ) {
155
if ( ! empty( $option['social_links'][ $service ] ) )
156
$option['social_links'][ $service ] = esc_url_raw( $option['social_links'][ $service ] );
158
unset( $option['social_links'][ $service ] );
165
* Returns whether there are any social links set.
169
public function has_social_links() {
170
return ! empty( $this->links );
174
* Return available social links.
178
public function get_social_links() {
183
* Short-circuits get_option and get_theme_mod calls.
185
* @param string $link The incoming value to be replaced.
186
* @returns string $link The social link that we've got.
188
public function get_social_link_filter( $link ) {
189
if ( preg_match( '/_jetpack-(.+)$/i', current_filter(), $matches ) && ! empty( $this->links[ $matches[1] ] ) )
190
return $this->links[ $matches[1] ];
196
* Puts together an array of choices for a specific service.
198
* @param string $service The social service.
199
* @return array An associative array with profile links and display names.
201
private function get_customize_select( $service ) {
203
'' => __( '— Select —', 'jetpack' )
206
if ( isset( $this->links[ $service ] ) ) {
207
$choices[ $this->links[ $service ] ] = $this->links[ $service ];
210
$connected_services = $this->publicize->get_services( 'connected' );
211
if ( isset( $connected_services[ $service ] ) ) {
212
foreach ( $connected_services[ $service ] as $c ) {
213
$profile_link = $this->publicize->get_profile_link( $service, $c );
215
if ( false === $profile_link ) {
219
$choices[ $profile_link ] = $this->publicize->get_display_name( $service, $c );
223
if ( 1 === count( $choices ) ) {
231
* Back-compat function for versions prior to 4.0.
233
private function is_customize_preview() {
234
global $wp_customize;
235
return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();
239
} // end if ( ! class_exists( 'Social_Links' )