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