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