| 1 |
<?php |
| 2 |
|
| 3 |
include_once dirname( __FILE__ ) . '/sharing-sources.php'; |
| 4 |
|
| 5 |
define( 'WP_SHARING_PLUGIN_VERSION', JETPACK__VERSION ); |
| 6 |
|
| 7 |
class Sharing_Service { |
| 8 |
private $global = false; |
| 9 |
public $default_sharing_label = ''; |
| 10 |
|
| 11 |
public function __construct() { |
| 12 |
$this->default_sharing_label = __( 'Share this:', 'jetpack' ); |
| 13 |
} |
| 14 |
|
| 15 |
/** |
| 16 |
* Gets a generic list of all services, without any config |
| 17 |
*/ |
| 18 |
public function get_all_services_blog() { |
| 19 |
$options = get_option( 'sharing-options' ); |
| 20 |
|
| 21 |
$all = $this->get_all_services(); |
| 22 |
$services = array(); |
| 23 |
|
| 24 |
foreach ( $all as $id => $name ) { |
| 25 |
if ( isset( $all[ $id ] ) ) { |
| 26 |
$config = array(); |
| 27 |
|
| 28 |
// Pre-load custom modules otherwise they won't know who they are |
| 29 |
if ( substr( $id, 0, 7 ) == 'custom-' && is_array( $options[ $id ] ) ) { |
| 30 |
$config = $options[ $id ]; |
| 31 |
} |
| 32 |
|
| 33 |
$services[ $id ] = new $all[ $id ]( $id, $config ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
return $services; |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Gets a list of all available service names and classes |
| 42 |
*/ |
| 43 |
public function get_all_services( $include_custom = true ) { |
| 44 |
// Default services |
| 45 |
// if you update this list, please update the REST API tests |
| 46 |
// in bin/tests/api/suites/SharingTest.php |
| 47 |
$services = array( |
| 48 |
'print' => 'Share_Print', |
| 49 |
'facebook' => 'Share_Facebook', |
| 50 |
'linkedin' => 'Share_LinkedIn', |
| 51 |
'reddit' => 'Share_Reddit', |
| 52 |
'twitter' => 'Share_Twitter', |
| 53 |
'tumblr' => 'Share_Tumblr', |
| 54 |
'pinterest' => 'Share_Pinterest', |
| 55 |
'pocket' => 'Share_Pocket', |
| 56 |
'telegram' => 'Share_Telegram', |
| 57 |
'jetpack-whatsapp' => 'Jetpack_Share_WhatsApp', |
| 58 |
'skype' => 'Share_Skype', |
| 59 |
); |
| 60 |
|
| 61 |
/** |
| 62 |
* Filters if Email Sharing is enabled. |
| 63 |
* |
| 64 |
* E-Mail sharing is often problematic due to spam concerns, so this filter enables it to be quickly and simply toggled. |
| 65 |
* @module sharedaddy |
| 66 |
* |
| 67 |
* @since 5.1.0 |
| 68 |
* |
| 69 |
* @param bool $email Is e-mail sharing enabled? Default false if Akismet is not active or true if Akismet is active. |
| 70 |
*/ |
| 71 |
if ( apply_filters( 'sharing_services_email', Jetpack::is_akismet_active() ) ) { |
| 72 |
$services['email'] = 'Share_Email'; |
| 73 |
} |
| 74 |
|
| 75 |
if ( is_multisite() && is_plugin_active( 'press-this/press-this-plugin.php' ) ) { |
| 76 |
$services['press-this'] = 'Share_PressThis'; |
| 77 |
} |
| 78 |
|
| 79 |
if ( $include_custom ) { |
| 80 |
// Add any custom services in |
| 81 |
$options = $this->get_global_options(); |
| 82 |
foreach ( (array) $options['custom'] as $custom_id ) { |
| 83 |
$services[ $custom_id ] = 'Share_Custom'; |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Filters the list of available Sharing Services. |
| 89 |
* |
| 90 |
* @module sharedaddy |
| 91 |
* |
| 92 |
* @since 1.1.0 |
| 93 |
* |
| 94 |
* @param array $services Array of all available Sharing Services. |
| 95 |
*/ |
| 96 |
return apply_filters( 'sharing_services', $services ); |
| 97 |
} |
| 98 |
|
| 99 |
public function new_service( $label, $url, $icon ) { |
| 100 |
// Validate |
| 101 |
$label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) ); |
| 102 |
$url = trim( esc_url_raw( $url ) ); |
| 103 |
$icon = trim( esc_url_raw( $icon ) ); |
| 104 |
|
| 105 |
if ( $label && $url && $icon ) { |
| 106 |
$options = get_option( 'sharing-options' ); |
| 107 |
if ( ! is_array( $options ) ) { |
| 108 |
$options = array(); |
| 109 |
} |
| 110 |
|
| 111 |
$service_id = 'custom-' . time(); |
| 112 |
|
| 113 |
// Add a new custom service |
| 114 |
$options['global']['custom'][] = $service_id; |
| 115 |
if ( false !== $this->global ) { |
| 116 |
$this->global['custom'][] = $service_id; |
| 117 |
} |
| 118 |
|
| 119 |
update_option( 'sharing-options', $options ); |
| 120 |
|
| 121 |
// Create a custom service and set the options for it |
| 122 |
$service = new Share_Custom( |
| 123 |
$service_id, array( |
| 124 |
'name' => $label, |
| 125 |
'url' => $url, |
| 126 |
'icon' => $icon, |
| 127 |
) |
| 128 |
); |
| 129 |
$this->set_service( $service_id, $service ); |
| 130 |
|
| 131 |
// Return the service |
| 132 |
return $service; |
| 133 |
} |
| 134 |
|
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
public function delete_service( $service_id ) { |
| 139 |
$options = get_option( 'sharing-options' ); |
| 140 |
if ( isset( $options[ $service_id ] ) ) { |
| 141 |
unset( $options[ $service_id ] ); |
| 142 |
} |
| 143 |
|
| 144 |
$key = array_search( $service_id, $options['global']['custom'] ); |
| 145 |
if ( $key !== false ) { |
| 146 |
unset( $options['global']['custom'][ $key ] ); |
| 147 |
} |
| 148 |
|
| 149 |
update_option( 'sharing-options', $options ); |
| 150 |
return true; |
| 151 |
} |
| 152 |
|
| 153 |
public function set_blog_services( array $visible, array $hidden ) { |
| 154 |
$services = $this->get_all_services(); |
| 155 |
// Validate the services |
| 156 |
$available = array_keys( $services ); |
| 157 |
|
| 158 |
// Only allow services that we have defined |
| 159 |
$hidden = array_intersect( $hidden, $available ); |
| 160 |
$visible = array_intersect( $visible, $available ); |
| 161 |
|
| 162 |
// Ensure we don't have the same ones in hidden and visible |
| 163 |
$hidden = array_diff( $hidden, $visible ); |
| 164 |
|
| 165 |
/** |
| 166 |
* Control the state of the list of sharing services. |
| 167 |
* |
| 168 |
* @module sharedaddy |
| 169 |
* |
| 170 |
* @since 1.1.0 |
| 171 |
* |
| 172 |
* @param array $args { |
| 173 |
* Array of options describing the state of the sharing services. |
| 174 |
* |
| 175 |
* @type array $services List of all available service names and classes. |
| 176 |
* @type array $available Validated list of all available service names and classes. |
| 177 |
* @type array $hidden List of services hidden behind a "More" button. |
| 178 |
* @type array $visible List of visible services. |
| 179 |
* @type array $this->get_blog_services() Array of Sharing Services currently enabled. |
| 180 |
* } |
| 181 |
*/ |
| 182 |
do_action( |
| 183 |
'sharing_get_services_state', array( |
| 184 |
'services' => $services, |
| 185 |
'available' => $available, |
| 186 |
'hidden' => $hidden, |
| 187 |
'visible' => $visible, |
| 188 |
'currently_enabled' => $this->get_blog_services(), |
| 189 |
) |
| 190 |
); |
| 191 |
|
| 192 |
return update_option( |
| 193 |
'sharing-services', array( |
| 194 |
'visible' => $visible, |
| 195 |
'hidden' => $hidden, |
| 196 |
) |
| 197 |
); |
| 198 |
} |
| 199 |
|
| 200 |
public function get_blog_services() { |
| 201 |
$options = get_option( 'sharing-options' ); |
| 202 |
$enabled = get_option( 'sharing-services' ); |
| 203 |
$services = $this->get_all_services(); |
| 204 |
|
| 205 |
/** |
| 206 |
* Check if options exist and are well formatted. |
| 207 |
* This avoids issues on sites with corrupted options. |
| 208 |
* @see https://github.com/Automattic/jetpack/issues/6121 |
| 209 |
*/ |
| 210 |
if ( ! is_array( $options ) || ! isset( $options['button_style'], $options['global'] ) ) { |
| 211 |
$global_options = array( 'global' => $this->get_global_options() ); |
| 212 |
$options = is_array( $options ) |
| 213 |
? array_merge( $options, $global_options ) |
| 214 |
: $global_options; |
| 215 |
} |
| 216 |
|
| 217 |
$global = $options['global']; |
| 218 |
|
| 219 |
// Default services |
| 220 |
if ( ! is_array( $enabled ) ) { |
| 221 |
$enabled = array( |
| 222 |
'visible' => array(), |
| 223 |
'hidden' => array(), |
| 224 |
); |
| 225 |
|
| 226 |
/** |
| 227 |
* Filters the list of default Sharing Services. |
| 228 |
* |
| 229 |
* @module sharedaddy |
| 230 |
* |
| 231 |
* @since 1.1.0 |
| 232 |
* |
| 233 |
* @param array $enabled Array of default Sharing Services. |
| 234 |
*/ |
| 235 |
$enabled = apply_filters( 'sharing_default_services', $enabled ); |
| 236 |
} |
| 237 |
|
| 238 |
// Cleanup after any filters that may have produced duplicate services |
| 239 |
if ( is_array( $enabled['visible'] ) ) { |
| 240 |
$enabled['visible'] = array_unique( $enabled['visible'] ); |
| 241 |
} else { |
| 242 |
$enabled['visible'] = array(); |
| 243 |
} |
| 244 |
|
| 245 |
if ( is_array( $enabled['hidden'] ) ) { |
| 246 |
$enabled['hidden'] = array_unique( $enabled['hidden'] ); |
| 247 |
} else { |
| 248 |
$enabled['hidden'] = array(); |
| 249 |
} |
| 250 |
|
| 251 |
// Form the enabled services |
| 252 |
$blog = array( |
| 253 |
'visible' => array(), |
| 254 |
'hidden' => array(), |
| 255 |
); |
| 256 |
|
| 257 |
foreach ( $blog as $area => $stuff ) { |
| 258 |
foreach ( (array) $enabled[ $area ] as $service ) { |
| 259 |
if ( isset( $services[ $service ] ) ) { |
| 260 |
if ( ! isset( $options[ $service ] ) || ! is_array( $options[ $service ] ) ) { |
| 261 |
$options[ $service ] = array(); |
| 262 |
} |
| 263 |
$blog[ $area ][ $service ] = new $services[ $service ]( $service, array_merge( $global, $options[ $service ] ) ); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Filters the list of enabled Sharing Services. |
| 270 |
* |
| 271 |
* @module sharedaddy |
| 272 |
* |
| 273 |
* @since 1.1.0 |
| 274 |
* |
| 275 |
* @param array $blog Array of enabled Sharing Services. |
| 276 |
*/ |
| 277 |
$blog = apply_filters( 'sharing_services_enabled', $blog ); |
| 278 |
|
| 279 |
// Add CSS for NASCAR |
| 280 |
if ( count( $blog['visible'] ) || count( $blog['hidden'] ) ) { |
| 281 |
add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' ); |
| 282 |
} |
| 283 |
|
| 284 |
// Convenience for checking if a service is present |
| 285 |
$blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) ); |
| 286 |
return $blog; |
| 287 |
} |
| 288 |
|
| 289 |
public function get_service( $service_name ) { |
| 290 |
$services = $this->get_blog_services(); |
| 291 |
|
| 292 |
if ( isset( $services['visible'][ $service_name ] ) ) { |
| 293 |
return $services['visible'][ $service_name ]; |
| 294 |
} |
| 295 |
|
| 296 |
if ( isset( $services['hidden'][ $service_name ] ) ) { |
| 297 |
return $services['hidden'][ $service_name ]; |
| 298 |
} |
| 299 |
|
| 300 |
return false; |
| 301 |
} |
| 302 |
|
| 303 |
public function set_global_options( $data ) { |
| 304 |
$options = get_option( 'sharing-options' ); |
| 305 |
|
| 306 |
// No options yet |
| 307 |
if ( ! is_array( $options ) ) { |
| 308 |
$options = array(); |
| 309 |
} |
| 310 |
|
| 311 |
// Defaults |
| 312 |
$options['global'] = array( |
| 313 |
'button_style' => 'icon-text', |
| 314 |
'sharing_label' => $this->default_sharing_label, |
| 315 |
'open_links' => 'same', |
| 316 |
'show' => array(), |
| 317 |
'custom' => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array(), |
| 318 |
); |
| 319 |
|
| 320 |
/** |
| 321 |
* Filters global sharing settings. |
| 322 |
* |
| 323 |
* @module sharedaddy |
| 324 |
* |
| 325 |
* @since 1.1.0 |
| 326 |
* |
| 327 |
* @param array $options['global'] Array of global sharing settings. |
| 328 |
*/ |
| 329 |
$options['global'] = apply_filters( 'sharing_default_global', $options['global'] ); |
| 330 |
|
| 331 |
// Validate options and set from our data |
| 332 |
if ( isset( $data['button_style'] ) && in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ) ) ) { |
| 333 |
$options['global']['button_style'] = $data['button_style']; |
| 334 |
} |
| 335 |
|
| 336 |
if ( isset( $data['sharing_label'] ) ) { |
| 337 |
if ( $this->default_sharing_label === $data['sharing_label'] ) { |
| 338 |
$options['global']['sharing_label'] = false; |
| 339 |
} else { |
| 340 |
$options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) ); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
if ( isset( $data['open_links'] ) && in_array( $data['open_links'], array( 'new', 'same' ) ) ) { |
| 345 |
$options['global']['open_links'] = $data['open_links']; |
| 346 |
} |
| 347 |
|
| 348 |
$shows = array_values( get_post_types( array( 'public' => true ) ) ); |
| 349 |
$shows[] = 'index'; |
| 350 |
if ( isset( $data['show'] ) ) { |
| 351 |
if ( is_scalar( $data['show'] ) ) { |
| 352 |
switch ( $data['show'] ) { |
| 353 |
case 'posts': |
| 354 |
$data['show'] = array( 'post', 'page' ); |
| 355 |
break; |
| 356 |
case 'index': |
| 357 |
$data['show'] = array( 'index' ); |
| 358 |
break; |
| 359 |
case 'posts-index': |
| 360 |
$data['show'] = array( 'post', 'page', 'index' ); |
| 361 |
break; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if ( $data['show'] = array_intersect( $data['show'], $shows ) ) { |
| 366 |
$options['global']['show'] = $data['show']; |
| 367 |
} |
| 368 |
} |
| 369 |
|
| 370 |
update_option( 'sharing-options', $options ); |
| 371 |
return $options['global']; |
| 372 |
} |
| 373 |
|
| 374 |
public function get_global_options() { |
| 375 |
if ( $this->global === false ) { |
| 376 |
$options = get_option( 'sharing-options' ); |
| 377 |
|
| 378 |
if ( is_array( $options ) && isset( $options['global'] ) && is_array( $options['global'] ) ) { |
| 379 |
$this->global = $options['global']; |
| 380 |
} else { |
| 381 |
$this->global = $this->set_global_options( $options['global'] ); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
if ( ! isset( $this->global['show'] ) ) { |
| 386 |
$this->global['show'] = array( 'post', 'page' ); |
| 387 |
} elseif ( is_scalar( $this->global['show'] ) ) { |
| 388 |
switch ( $this->global['show'] ) { |
| 389 |
case 'posts': |
| 390 |
$this->global['show'] = array( 'post', 'page' ); |
| 391 |
break; |
| 392 |
case 'index': |
| 393 |
$this->global['show'] = array( 'index' ); |
| 394 |
break; |
| 395 |
case 'posts-index': |
| 396 |
$this->global['show'] = array( 'post', 'page', 'index' ); |
| 397 |
break; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
if ( false === $this->global['sharing_label'] ) { |
| 402 |
$this->global['sharing_label'] = $this->default_sharing_label; |
| 403 |
} |
| 404 |
|
| 405 |
return $this->global; |
| 406 |
} |
| 407 |
|
| 408 |
public function set_service( $id, Sharing_Source $service ) { |
| 409 |
// Update the options for this service |
| 410 |
$options = get_option( 'sharing-options' ); |
| 411 |
|
| 412 |
// No options yet |
| 413 |
if ( ! is_array( $options ) ) { |
| 414 |
$options = array(); |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Get the state of a sharing button. |
| 419 |
* |
| 420 |
* @module sharedaddy |
| 421 |
* |
| 422 |
* @since 1.1.0 |
| 423 |
* |
| 424 |
* @param array $args { |
| 425 |
* State of a sharing button. |
| 426 |
* |
| 427 |
* @type string $id Service ID. |
| 428 |
* @type array $options Array of all sharing options. |
| 429 |
* @type array $service Details about a service. |
| 430 |
* } |
| 431 |
*/ |
| 432 |
do_action( |
| 433 |
'sharing_get_button_state', array( |
| 434 |
'id' => $id, |
| 435 |
'options' => $options, |
| 436 |
'service' => $service, |
| 437 |
) |
| 438 |
); |
| 439 |
|
| 440 |
$options[ $id ] = $service->get_options(); |
| 441 |
|
| 442 |
update_option( 'sharing-options', array_filter( $options ) ); |
| 443 |
} |
| 444 |
|
| 445 |
// Soon to come to a .org plugin near you! |
| 446 |
public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) { |
| 447 |
global $wpdb, $blog_id; |
| 448 |
if ( ! $_blog_id ) { |
| 449 |
$_blog_id = $blog_id; |
| 450 |
} |
| 451 |
if ( $service_name == false ) { |
| 452 |
if ( $post_id > 0 ) { |
| 453 |
// total number of shares for this post |
| 454 |
return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d', $_blog_id, $post_id ) ); |
| 455 |
} else { |
| 456 |
// total number of shares for this blog |
| 457 |
return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d', $_blog_id ) ); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
if ( $post_id > 0 ) { |
| 462 |
return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d AND share_service = %s', $_blog_id, $post_id, $service_name ) ); |
| 463 |
} else { |
| 464 |
return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s', $_blog_id, $service_name ) ); |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
public function get_services_total( $post_id = false ) { |
| 469 |
$totals = array(); |
| 470 |
$services = $this->get_blog_services(); |
| 471 |
|
| 472 |
if ( ! empty( $services ) && isset( $services['all'] ) ) { |
| 473 |
foreach ( $services['all'] as $key => $value ) { |
| 474 |
$totals[ $key ] = new Sharing_Service_Total( $key, $this->get_total( $key, $post_id ) ); |
| 475 |
} |
| 476 |
} |
| 477 |
usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) ); |
| 478 |
|
| 479 |
return $totals; |
| 480 |
} |
| 481 |
|
| 482 |
public function get_posts_total() { |
| 483 |
$totals = array(); |
| 484 |
global $wpdb, $blog_id; |
| 485 |
|
| 486 |
$my_data = $wpdb->get_results( $wpdb->prepare( 'SELECT post_id as id, SUM( count ) as total FROM sharing_stats WHERE blog_id = %d GROUP BY post_id ORDER BY count DESC ', $blog_id ) ); |
| 487 |
|
| 488 |
if ( ! empty( $my_data ) ) { |
| 489 |
foreach ( $my_data as $row ) { |
| 490 |
$totals[] = new Sharing_Post_Total( $row->id, $row->total ); |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) ); |
| 495 |
|
| 496 |
return $totals; |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
class Sharing_Service_Total { |
| 501 |
public $id = ''; |
| 502 |
public $name = ''; |
| 503 |
public $service = ''; |
| 504 |
public $total = 0; |
| 505 |
|
| 506 |
public function __construct( $id, $total ) { |
| 507 |
$services = new Sharing_Service(); |
| 508 |
$this->id = esc_html( $id ); |
| 509 |
$this->service = $services->get_service( $id ); |
| 510 |
$this->total = (int) $total; |
| 511 |
|
| 512 |
$this->name = $this->service->get_name(); |
| 513 |
} |
| 514 |
|
| 515 |
static function cmp( $a, $b ) { |
| 516 |
if ( $a->total == $b->total ) { |
| 517 |
return $a->name < $b->name; |
| 518 |
} |
| 519 |
return $a->total < $b->total; |
| 520 |
} |
| 521 |
} |
| 522 |
|
| 523 |
class Sharing_Post_Total { |
| 524 |
public $id = 0; |
| 525 |
public $total = 0; |
| 526 |
public $title = ''; |
| 527 |
public $url = ''; |
| 528 |
|
| 529 |
public function __construct( $id, $total ) { |
| 530 |
$this->id = (int) $id; |
| 531 |
$this->total = (int) $total; |
| 532 |
$this->title = get_the_title( $this->id ); |
| 533 |
$this->url = get_permalink( $this->id ); |
| 534 |
} |
| 535 |
|
| 536 |
static function cmp( $a, $b ) { |
| 537 |
if ( $a->total == $b->total ) { |
| 538 |
return $a->id < $b->id; |
| 539 |
} |
| 540 |
return $a->total < $b->total; |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
function sharing_register_post_for_share_counts( $post_id ) { |
| 545 |
global $jetpack_sharing_counts; |
| 546 |
|
| 547 |
if ( ! isset( $jetpack_sharing_counts ) || ! is_array( $jetpack_sharing_counts ) ) { |
| 548 |
$jetpack_sharing_counts = array(); |
| 549 |
} |
| 550 |
|
| 551 |
$jetpack_sharing_counts[ (int) $post_id ] = get_permalink( $post_id ); |
| 552 |
} |
| 553 |
|
| 554 |
function sharing_maybe_enqueue_scripts() { |
| 555 |
$sharer = new Sharing_Service(); |
| 556 |
$global_options = $sharer->get_global_options(); |
| 557 |
|
| 558 |
$enqueue = false; |
| 559 |
if ( is_singular() && in_array( get_post_type(), $global_options['show'] ) ) { |
| 560 |
$enqueue = true; |
| 561 |
} elseif ( in_array( 'index', $global_options['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global_options['show'] ) ) ) { |
| 562 |
$enqueue = true; |
| 563 |
} |
| 564 |
|
| 565 |
/** |
| 566 |
* Filter to decide when sharing scripts should be enqueued. |
| 567 |
* |
| 568 |
* @module sharedaddy |
| 569 |
* |
| 570 |
* @since 3.2.0 |
| 571 |
* |
| 572 |
* @param bool $enqueue Decide if the sharing scripts should be enqueued. |
| 573 |
*/ |
| 574 |
return (bool) apply_filters( 'sharing_enqueue_scripts', $enqueue ); |
| 575 |
} |
| 576 |
|
| 577 |
function sharing_add_footer() { |
| 578 |
if ( |
| 579 |
class_exists( 'Jetpack_AMP_Support' ) |
| 580 |
&& Jetpack_AMP_Support::is_amp_request() |
| 581 |
) { |
| 582 |
return; |
| 583 |
} |
| 584 |
|
| 585 |
global $jetpack_sharing_counts; |
| 586 |
|
| 587 |
/** |
| 588 |
* Filter all JavaScript output by the sharing module. |
| 589 |
* |
| 590 |
* @module sharedaddy |
| 591 |
* |
| 592 |
* @since 1.1.0 |
| 593 |
* |
| 594 |
* @param bool true Control whether the sharing module should add any JavaScript to the site. Default to true. |
| 595 |
*/ |
| 596 |
if ( apply_filters( 'sharing_js', true ) && sharing_maybe_enqueue_scripts() ) { |
| 597 |
|
| 598 |
/** |
| 599 |
* Filter the display of sharing counts next to the sharing buttons. |
| 600 |
* |
| 601 |
* @module sharedaddy |
| 602 |
* |
| 603 |
* @since 3.2.0 |
| 604 |
* |
| 605 |
* @param bool true Control the display of counters next to the sharing buttons. Default to true. |
| 606 |
*/ |
| 607 |
if ( apply_filters( 'jetpack_sharing_counts', true ) && is_array( $jetpack_sharing_counts ) && count( $jetpack_sharing_counts ) ) : |
| 608 |
$sharing_post_urls = array_filter( $jetpack_sharing_counts ); |
| 609 |
if ( $sharing_post_urls ) : |
| 610 |
?> |
| 611 |
|
| 612 |
<script type="text/javascript"> |
| 613 |
window.WPCOM_sharing_counts = <?php echo json_encode( array_flip( $sharing_post_urls ) ); ?>; |
| 614 |
</script> |
| 615 |
<?php |
| 616 |
endif; |
| 617 |
endif; |
| 618 |
|
| 619 |
wp_enqueue_script( 'sharing-js' ); |
| 620 |
$sharing_js_options = array( |
| 621 |
'lang' => get_base_recaptcha_lang_code(), |
| 622 |
/** This filter is documented in modules/sharedaddy/sharing-service.php */ |
| 623 |
'counts' => apply_filters( 'jetpack_sharing_counts', true ), |
| 624 |
'is_stats_active' => Jetpack::is_module_active( 'stats' ), |
| 625 |
); |
| 626 |
wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options ); |
| 627 |
} |
| 628 |
$sharer = new Sharing_Service(); |
| 629 |
$enabled = $sharer->get_blog_services(); |
| 630 |
foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) as $service ) { |
| 631 |
$service->display_footer(); |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
function sharing_add_header() { |
| 636 |
$sharer = new Sharing_Service(); |
| 637 |
$enabled = $sharer->get_blog_services(); |
| 638 |
|
| 639 |
foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) as $service ) { |
| 640 |
$service->display_header(); |
| 641 |
} |
| 642 |
|
| 643 |
if ( count( $enabled['all'] ) > 0 && sharing_maybe_enqueue_scripts() ) { |
| 644 |
wp_enqueue_style( 'sharedaddy', plugin_dir_url( __FILE__ ) . 'sharing.css', array(), JETPACK__VERSION ); |
| 645 |
wp_enqueue_style( 'social-logos' ); |
| 646 |
} |
| 647 |
|
| 648 |
} |
| 649 |
add_action( 'wp_head', 'sharing_add_header', 1 ); |
| 650 |
|
| 651 |
function sharing_process_requests() { |
| 652 |
global $post; |
| 653 |
|
| 654 |
// Only process if: single post and share=X defined |
| 655 |
if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) ) { |
| 656 |
$sharer = new Sharing_Service(); |
| 657 |
|
| 658 |
$service = $sharer->get_service( $_GET['share'] ); |
| 659 |
if ( $service ) { |
| 660 |
$service->process_request( $post, $_POST ); |
| 661 |
} |
| 662 |
} |
| 663 |
} |
| 664 |
add_action( 'template_redirect', 'sharing_process_requests', 9 ); |
| 665 |
|
| 666 |
function sharing_display( $text = '', $echo = false ) { |
| 667 |
global $post, $wp_current_filter; |
| 668 |
|
| 669 |
require_once JETPACK__PLUGIN_DIR . '/sync/class.jetpack-sync-settings.php'; |
| 670 |
if ( Jetpack_Sync_Settings::is_syncing() ) { |
| 671 |
return $text; |
| 672 |
} |
| 673 |
|
| 674 |
if ( empty( $post ) ) { |
| 675 |
return $text; |
| 676 |
} |
| 677 |
|
| 678 |
if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 679 |
return $text; |
| 680 |
} |
| 681 |
|
| 682 |
// Don't output flair on excerpts |
| 683 |
if ( in_array( 'get_the_excerpt', (array) $wp_current_filter ) ) { |
| 684 |
return $text; |
| 685 |
} |
| 686 |
|
| 687 |
// Don't allow flair to be added to the_content more than once (prevent infinite loops) |
| 688 |
$done = false; |
| 689 |
foreach ( $wp_current_filter as $filter ) { |
| 690 |
if ( 'the_content' == $filter ) { |
| 691 |
if ( $done ) { |
| 692 |
return $text; |
| 693 |
} else { |
| 694 |
$done = true; |
| 695 |
} |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
// check whether we are viewing the front page and whether the front page option is checked |
| 700 |
$options = get_option( 'sharing-options' ); |
| 701 |
$display_options = $options['global']['show']; |
| 702 |
|
| 703 |
if ( is_front_page() && ( is_array( $display_options ) && ! in_array( 'index', $display_options ) ) ) { |
| 704 |
return $text; |
| 705 |
} |
| 706 |
|
| 707 |
if ( is_attachment() && in_array( 'the_excerpt', (array) $wp_current_filter ) ) { |
| 708 |
// Many themes run the_excerpt() conditionally on an attachment page, then run the_content(). |
| 709 |
// We only want to output the sharing buttons once. Let's stick with the_content(). |
| 710 |
return $text; |
| 711 |
} |
| 712 |
|
| 713 |
$sharer = new Sharing_Service(); |
| 714 |
$global = $sharer->get_global_options(); |
| 715 |
|
| 716 |
$show = false; |
| 717 |
if ( ! is_feed() ) { |
| 718 |
if ( is_singular() && in_array( get_post_type(), $global['show'] ) ) { |
| 719 |
$show = true; |
| 720 |
} elseif ( in_array( 'index', $global['show'] ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global['show'] ) ) ) { |
| 721 |
$show = true; |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
/** |
| 726 |
* Filter to decide if sharing buttons should be displayed. |
| 727 |
* |
| 728 |
* @module sharedaddy |
| 729 |
* |
| 730 |
* @since 1.1.0 |
| 731 |
* |
| 732 |
* @param bool $show Should the sharing buttons be displayed. |
| 733 |
* @param WP_Post $post The post to share. |
| 734 |
*/ |
| 735 |
$show = apply_filters( 'sharing_show', $show, $post ); |
| 736 |
|
| 737 |
// Disabled for this post? |
| 738 |
$switched_status = get_post_meta( $post->ID, 'sharing_disabled', false ); |
| 739 |
|
| 740 |
if ( ! empty( $switched_status ) ) { |
| 741 |
$show = false; |
| 742 |
} |
| 743 |
|
| 744 |
// Private post? |
| 745 |
$post_status = get_post_status( $post->ID ); |
| 746 |
|
| 747 |
if ( 'private' === $post_status ) { |
| 748 |
$show = false; |
| 749 |
} |
| 750 |
|
| 751 |
/** |
| 752 |
* Filter the Sharing buttons' Ajax action name Jetpack checks for. |
| 753 |
* This allows the use of the buttons with your own Ajax implementation. |
| 754 |
* |
| 755 |
* @module sharedaddy |
| 756 |
* |
| 757 |
* @since 7.3.0 |
| 758 |
* |
| 759 |
* @param string $sharing_ajax_action_name Name of the Sharing buttons' Ajax action. |
| 760 |
*/ |
| 761 |
$ajax_action = apply_filters( 'sharing_ajax_action', 'get_latest_posts' ); |
| 762 |
|
| 763 |
// Allow to be used in ajax requests for latest posts. |
| 764 |
if ( |
| 765 |
defined( 'DOING_AJAX' ) |
| 766 |
&& DOING_AJAX |
| 767 |
&& isset( $_REQUEST['action'] ) |
| 768 |
&& $ajax_action === $_REQUEST['action'] |
| 769 |
) { |
| 770 |
$show = true; |
| 771 |
} |
| 772 |
|
| 773 |
$sharing_content = ''; |
| 774 |
$enabled = false; |
| 775 |
|
| 776 |
if ( $show ) { |
| 777 |
/** |
| 778 |
* Filters the list of enabled Sharing Services. |
| 779 |
* |
| 780 |
* @module sharedaddy |
| 781 |
* |
| 782 |
* @since 2.2.3 |
| 783 |
* |
| 784 |
* @param array $sharer->get_blog_services() Array of Sharing Services currently enabled. |
| 785 |
*/ |
| 786 |
$enabled = apply_filters( 'sharing_enabled', $sharer->get_blog_services() ); |
| 787 |
|
| 788 |
if ( count( $enabled['all'] ) > 0 ) { |
| 789 |
global $post; |
| 790 |
|
| 791 |
$dir = get_option( 'text_direction' ); |
| 792 |
|
| 793 |
// Wrapper |
| 794 |
$sharing_content .= '<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-' . $global['button_style'] . ' sd-sharing">'; |
| 795 |
if ( $global['sharing_label'] != '' ) { |
| 796 |
$sharing_content .= sprintf( |
| 797 |
/** |
| 798 |
* Filter the sharing buttons' headline structure. |
| 799 |
* |
| 800 |
* @module sharedaddy |
| 801 |
* |
| 802 |
* @since 4.4.0 |
| 803 |
* |
| 804 |
* @param string $sharing_headline Sharing headline structure. |
| 805 |
* @param string $global['sharing_label'] Sharing title. |
| 806 |
* @param string $sharing Module name. |
| 807 |
*/ |
| 808 |
apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', $global['sharing_label'], 'sharing' ), |
| 809 |
esc_html( $global['sharing_label'] ) |
| 810 |
); |
| 811 |
} |
| 812 |
$sharing_content .= '<div class="sd-content"><ul>'; |
| 813 |
|
| 814 |
// Visible items |
| 815 |
$visible = ''; |
| 816 |
foreach ( $enabled['visible'] as $id => $service ) { |
| 817 |
$klasses = array( 'share-' . $service->get_class() ); |
| 818 |
if ( $service->is_deprecated() ) { |
| 819 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 820 |
continue; |
| 821 |
} |
| 822 |
$klasses[] = 'share-deprecated'; |
| 823 |
} |
| 824 |
// Individual HTML for sharing service |
| 825 |
$visible .= '<li class="' . implode( ' ', $klasses ) . '">' . $service->get_display( $post ) . '</li>'; |
| 826 |
} |
| 827 |
|
| 828 |
$parts = array(); |
| 829 |
$parts[] = $visible; |
| 830 |
if ( count( $enabled['hidden'] ) > 0 ) { |
| 831 |
if ( count( $enabled['visible'] ) > 0 ) { |
| 832 |
$expand = __( 'More', 'jetpack' ); |
| 833 |
} else { |
| 834 |
$expand = __( 'Share', 'jetpack' ); |
| 835 |
} |
| 836 |
$parts[] = '<li><a href="#" class="sharing-anchor sd-button share-more"><span>' . $expand . '</span></a></li>'; |
| 837 |
} |
| 838 |
|
| 839 |
if ( $dir == 'rtl' ) { |
| 840 |
$parts = array_reverse( $parts ); |
| 841 |
} |
| 842 |
|
| 843 |
$sharing_content .= implode( '', $parts ); |
| 844 |
$sharing_content .= '<li class="share-end"></li></ul>'; |
| 845 |
|
| 846 |
if ( count( $enabled['hidden'] ) > 0 ) { |
| 847 |
$sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;'; |
| 848 |
|
| 849 |
if ( count( $enabled['hidden'] ) == 1 ) { |
| 850 |
$sharing_content .= 'width:150px;'; |
| 851 |
} |
| 852 |
|
| 853 |
$sharing_content .= '">'; |
| 854 |
|
| 855 |
if ( count( $enabled['hidden'] ) == 1 ) { |
| 856 |
$sharing_content .= '<ul style="background-image:none;">'; |
| 857 |
} else { |
| 858 |
$sharing_content .= '<ul>'; |
| 859 |
} |
| 860 |
|
| 861 |
$count = 1; |
| 862 |
foreach ( $enabled['hidden'] as $id => $service ) { |
| 863 |
// Individual HTML for sharing service |
| 864 |
$klasses = array( 'share-' . $service->get_class() ); |
| 865 |
if ( $service->is_deprecated() ) { |
| 866 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 867 |
continue; |
| 868 |
} |
| 869 |
$klasses[] = 'share-deprecated'; |
| 870 |
} |
| 871 |
$sharing_content .= '<li class="' . implode( ' ', $klasses ) . '">'; |
| 872 |
$sharing_content .= $service->get_display( $post ); |
| 873 |
$sharing_content .= '</li>'; |
| 874 |
|
| 875 |
if ( ( $count % 2 ) == 0 ) { |
| 876 |
$sharing_content .= '<li class="share-end"></li>'; |
| 877 |
} |
| 878 |
|
| 879 |
$count ++; |
| 880 |
} |
| 881 |
|
| 882 |
// End of wrapper |
| 883 |
$sharing_content .= '<li class="share-end"></li></ul></div></div>'; |
| 884 |
} |
| 885 |
|
| 886 |
$sharing_content .= '</div></div></div>'; |
| 887 |
|
| 888 |
// Register our JS |
| 889 |
if ( defined( 'JETPACK__VERSION' ) ) { |
| 890 |
$ver = JETPACK__VERSION; |
| 891 |
} else { |
| 892 |
$ver = '20141212'; |
| 893 |
} |
| 894 |
wp_register_script( |
| 895 |
'sharing-js', |
| 896 |
Jetpack::get_file_url_for_environment( |
| 897 |
'_inc/build/sharedaddy/sharing.min.js', |
| 898 |
'modules/sharedaddy/sharing.js' |
| 899 |
), |
| 900 |
array( 'jquery' ), |
| 901 |
$ver |
| 902 |
); |
| 903 |
|
| 904 |
// Enqueue scripts for the footer |
| 905 |
add_action( 'wp_footer', 'sharing_add_footer' ); |
| 906 |
} |
| 907 |
} |
| 908 |
|
| 909 |
/** |
| 910 |
* Filters the content markup of the Jetpack sharing links |
| 911 |
* |
| 912 |
* @module sharedaddy |
| 913 |
* |
| 914 |
* @since 3.8.0 |
| 915 |
* @since 6.2.0 Started sending $enabled as a second parameter. |
| 916 |
* |
| 917 |
* @param string $sharing_content Content markup of the Jetpack sharing links |
| 918 |
* @param array $enabled Array of Sharing Services currently enabled. |
| 919 |
*/ |
| 920 |
$sharing_markup = apply_filters( 'jetpack_sharing_display_markup', $sharing_content, $enabled ); |
| 921 |
|
| 922 |
if ( $echo ) { |
| 923 |
echo $text . $sharing_markup; |
| 924 |
} else { |
| 925 |
return $text . $sharing_markup; |
| 926 |
} |
| 927 |
} |
| 928 |
|
| 929 |
add_filter( 'the_content', 'sharing_display', 19 ); |
| 930 |
add_filter( 'the_excerpt', 'sharing_display', 19 ); |
| 931 |
function get_base_recaptcha_lang_code() { |
| 932 |
|
| 933 |
$base_recaptcha_lang_code_mapping = array( |
| 934 |
'en' => 'en', |
| 935 |
'nl' => 'nl', |
| 936 |
'fr' => 'fr', |
| 937 |
'fr-be' => 'fr', |
| 938 |
'fr-ca' => 'fr', |
| 939 |
'fr-ch' => 'fr', |
| 940 |
'de' => 'de', |
| 941 |
'pt' => 'pt', |
| 942 |
'pt-br' => 'pt', |
| 943 |
'ru' => 'ru', |
| 944 |
'es' => 'es', |
| 945 |
'tr' => 'tr', |
| 946 |
); |
| 947 |
|
| 948 |
$blog_lang_code = function_exists( 'get_blog_lang_code' ) ? get_blog_lang_code() : get_bloginfo( 'language' ); |
| 949 |
if ( isset( $base_recaptcha_lang_code_mapping[ $blog_lang_code ] ) ) { |
| 950 |
return $base_recaptcha_lang_code_mapping[ $blog_lang_code ]; |
| 951 |
} |
| 952 |
|
| 953 |
// if no base mapping is found return default 'en' |
| 954 |
return 'en'; |
| 955 |
} |
| 956 |
|