images
6 years ago
services
3 years ago
admin-sharing-rtl.css
3 years ago
admin-sharing-rtl.min.css
3 years ago
admin-sharing.css
3 years ago
admin-sharing.js
3 years ago
admin-sharing.min.css
3 years ago
amp-sharing.css
4 years ago
recaptcha.php
4 years ago
sharedaddy.php
3 years ago
sharing-service.php
3 years ago
sharing-sources.php
3 years ago
sharing.css
3 years ago
sharing.js
3 years ago
sharing.php
3 years ago
sharing-service.php
1263 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * Utilities to register and interact with a sharing service. |
| 4 | * |
| 5 | * Sharing_Service gets info about a service. |
| 6 | * Sharing_Service_Total and Sharing_Post_Total get stats data. |
| 7 | * |
| 8 | * @package automattic/jetpack |
| 9 | * |
| 10 | * phpcs:disable Generic.Files.OneObjectStructurePerFile.MultipleFound |
| 11 | */ |
| 12 | |
| 13 | // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. |
| 14 | |
| 15 | use Automattic\Jetpack\Assets; |
| 16 | use Automattic\Jetpack\Redirect; |
| 17 | use Automattic\Jetpack\Status; |
| 18 | use Automattic\Jetpack\Sync\Settings; |
| 19 | |
| 20 | require_once __DIR__ . '/sharing-sources.php'; |
| 21 | |
| 22 | define( 'WP_SHARING_PLUGIN_VERSION', JETPACK__VERSION ); |
| 23 | |
| 24 | /** |
| 25 | * Interact with a sharing service. |
| 26 | */ |
| 27 | class Sharing_Service { |
| 28 | /** |
| 29 | * Should the service be available globally? |
| 30 | * |
| 31 | * @var bool |
| 32 | */ |
| 33 | private $global = false; |
| 34 | |
| 35 | /** |
| 36 | * Default sharing label. |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $default_sharing_label = ''; |
| 41 | |
| 42 | /** |
| 43 | * Initialize the sharing service. |
| 44 | * Only run this method once upon module loading. |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public static function init() { |
| 49 | add_filter( 'the_content', 'sharing_display', 19 ); |
| 50 | add_filter( 'the_excerpt', 'sharing_display', 19 ); |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Constructor. |
| 55 | */ |
| 56 | public function __construct() { |
| 57 | $this->default_sharing_label = __( 'Share this:', 'jetpack' ); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Gets a generic list of all services, without any config |
| 62 | * |
| 63 | * @return array |
| 64 | */ |
| 65 | public function get_all_services_blog() { |
| 66 | $options = get_option( 'sharing-options' ); |
| 67 | $all = $this->get_all_services(); |
| 68 | $services = array(); |
| 69 | |
| 70 | foreach ( $all as $id => $name ) { |
| 71 | if ( isset( $all[ $id ] ) ) { |
| 72 | $config = array(); |
| 73 | |
| 74 | // Pre-load custom modules otherwise they won't know who they are |
| 75 | if ( substr( $id, 0, 7 ) === 'custom-' && is_array( $options[ $id ] ) ) { |
| 76 | $config = $options[ $id ]; |
| 77 | } |
| 78 | |
| 79 | $services[ $id ] = new $all[ $id ]( $id, $config ); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | return $services; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Gets a list of all available service names and classes |
| 88 | * |
| 89 | * @param bool $include_custom Include custom sharing services. |
| 90 | * |
| 91 | * @return array |
| 92 | */ |
| 93 | public function get_all_services( $include_custom = true ) { |
| 94 | // Default services |
| 95 | // if you update this list, please update the REST API tests |
| 96 | // in bin/tests/api/suites/SharingTest.php |
| 97 | $services = array( |
| 98 | 'print' => 'Share_Print', |
| 99 | 'email' => 'Share_Email', |
| 100 | 'facebook' => 'Share_Facebook', |
| 101 | 'linkedin' => 'Share_LinkedIn', |
| 102 | 'reddit' => 'Share_Reddit', |
| 103 | 'twitter' => 'Share_Twitter', |
| 104 | 'tumblr' => 'Share_Tumblr', |
| 105 | 'pinterest' => 'Share_Pinterest', |
| 106 | 'pocket' => 'Share_Pocket', |
| 107 | 'telegram' => 'Share_Telegram', |
| 108 | 'jetpack-whatsapp' => 'Jetpack_Share_WhatsApp', |
| 109 | 'skype' => 'Share_Skype', |
| 110 | 'mastodon' => 'Share_Mastodon', |
| 111 | ); |
| 112 | |
| 113 | if ( is_multisite() && is_plugin_active( 'press-this/press-this-plugin.php' ) ) { |
| 114 | $services['press-this'] = 'Share_PressThis'; |
| 115 | } |
| 116 | |
| 117 | if ( $include_custom ) { |
| 118 | // Add any custom services in |
| 119 | $options = $this->get_global_options(); |
| 120 | foreach ( (array) $options['custom'] as $custom_id ) { |
| 121 | $services[ $custom_id ] = 'Share_Custom'; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /** |
| 126 | * Filters the list of available Sharing Services. |
| 127 | * |
| 128 | * @module sharedaddy |
| 129 | * |
| 130 | * @since 1.1.0 |
| 131 | * |
| 132 | * @param array $services Array of all available Sharing Services. |
| 133 | */ |
| 134 | return apply_filters( 'sharing_services', $services ); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Save a new custom sharing service. |
| 139 | * |
| 140 | * @param string $label Service name. |
| 141 | * @param string $url Service sharing URL. |
| 142 | * @param string $icon Service icon. |
| 143 | * |
| 144 | * @return bool|Share_Custom |
| 145 | */ |
| 146 | public function new_service( $label, $url, $icon ) { |
| 147 | // Validate. |
| 148 | $label = trim( wp_html_excerpt( wp_kses( $label, array() ), 30 ) ); |
| 149 | $url = trim( esc_url_raw( $url ) ); |
| 150 | $icon = trim( esc_url_raw( $icon ) ); |
| 151 | |
| 152 | if ( $label && $url && $icon ) { |
| 153 | $options = get_option( 'sharing-options' ); |
| 154 | if ( ! is_array( $options ) ) { |
| 155 | $options = array(); |
| 156 | } |
| 157 | |
| 158 | $service_id = 'custom-' . time(); |
| 159 | |
| 160 | // Add a new custom service |
| 161 | $options['global']['custom'][] = $service_id; |
| 162 | if ( false !== $this->global ) { |
| 163 | $this->global['custom'][] = $service_id; |
| 164 | } |
| 165 | |
| 166 | update_option( 'sharing-options', $options ); |
| 167 | |
| 168 | // Create a custom service and set the options for it |
| 169 | $service = new Share_Custom( |
| 170 | $service_id, |
| 171 | array( |
| 172 | 'name' => $label, |
| 173 | 'url' => $url, |
| 174 | 'icon' => $icon, |
| 175 | ) |
| 176 | ); |
| 177 | $this->set_service( $service_id, $service ); |
| 178 | |
| 179 | // Return the service |
| 180 | return $service; |
| 181 | } |
| 182 | |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Delete a sharing service. |
| 188 | * |
| 189 | * @param string $service_id Service ID. |
| 190 | * |
| 191 | * @return bool |
| 192 | */ |
| 193 | public function delete_service( $service_id ) { |
| 194 | $options = get_option( 'sharing-options' ); |
| 195 | if ( isset( $options[ $service_id ] ) ) { |
| 196 | unset( $options[ $service_id ] ); |
| 197 | } |
| 198 | |
| 199 | $key = array_search( $service_id, $options['global']['custom'], true ); |
| 200 | if ( $key !== false ) { |
| 201 | unset( $options['global']['custom'][ $key ] ); |
| 202 | } |
| 203 | |
| 204 | update_option( 'sharing-options', $options ); |
| 205 | return true; |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Save enabled sharing services. |
| 210 | * |
| 211 | * @param array $visible Visible sharing services. |
| 212 | * @param array $hidden Hidden sharing services (available under a dropdown). |
| 213 | * |
| 214 | * @return bool |
| 215 | */ |
| 216 | public function set_blog_services( array $visible, array $hidden ) { |
| 217 | $services = $this->get_all_services(); |
| 218 | // Validate the services |
| 219 | $available = array_keys( $services ); |
| 220 | |
| 221 | // Only allow services that we have defined |
| 222 | $hidden = array_intersect( $hidden, $available ); |
| 223 | $visible = array_intersect( $visible, $available ); |
| 224 | |
| 225 | // Ensure we don't have the same ones in hidden and visible |
| 226 | $hidden = array_diff( $hidden, $visible ); |
| 227 | |
| 228 | /** |
| 229 | * Control the state of the list of sharing services. |
| 230 | * |
| 231 | * @module sharedaddy |
| 232 | * |
| 233 | * @since 1.1.0 |
| 234 | * |
| 235 | * @param array $args { |
| 236 | * Array of options describing the state of the sharing services. |
| 237 | * |
| 238 | * @type array $services List of all available service names and classes. |
| 239 | * @type array $available Validated list of all available service names and classes. |
| 240 | * @type array $hidden List of services hidden behind a "More" button. |
| 241 | * @type array $visible List of visible services. |
| 242 | * @type array $this->get_blog_services() Array of Sharing Services currently enabled. |
| 243 | * } |
| 244 | */ |
| 245 | do_action( |
| 246 | 'sharing_get_services_state', |
| 247 | array( |
| 248 | 'services' => $services, |
| 249 | 'available' => $available, |
| 250 | 'hidden' => $hidden, |
| 251 | 'visible' => $visible, |
| 252 | 'currently_enabled' => $this->get_blog_services(), |
| 253 | ) |
| 254 | ); |
| 255 | |
| 256 | return update_option( |
| 257 | 'sharing-services', |
| 258 | array( |
| 259 | 'visible' => $visible, |
| 260 | 'hidden' => $hidden, |
| 261 | ) |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Get information about enabled sharing services on the site. |
| 267 | * |
| 268 | * @return array |
| 269 | */ |
| 270 | public function get_blog_services() { |
| 271 | $options = get_option( 'sharing-options' ); |
| 272 | $enabled = get_option( 'sharing-services' ); |
| 273 | $services = $this->get_all_services(); |
| 274 | |
| 275 | /** |
| 276 | * Check if options exist and are well formatted. |
| 277 | * This avoids issues on sites with corrupted options. |
| 278 | * |
| 279 | * @see https://github.com/Automattic/jetpack/issues/6121 |
| 280 | */ |
| 281 | if ( ! is_array( $options ) || ! isset( $options['button_style'], $options['global'] ) ) { |
| 282 | $global_options = array( 'global' => $this->get_global_options() ); |
| 283 | $options = is_array( $options ) |
| 284 | ? array_merge( $options, $global_options ) |
| 285 | : $global_options; |
| 286 | } |
| 287 | |
| 288 | $global = $options['global']; |
| 289 | |
| 290 | // Default services |
| 291 | if ( ! is_array( $enabled ) ) { |
| 292 | $enabled = array( |
| 293 | 'visible' => array( |
| 294 | 'twitter', |
| 295 | 'facebook', |
| 296 | ), |
| 297 | 'hidden' => array(), |
| 298 | ); |
| 299 | |
| 300 | /** |
| 301 | * Filters the list of default Sharing Services. |
| 302 | * |
| 303 | * @module sharedaddy |
| 304 | * |
| 305 | * @since 1.1.0 |
| 306 | * |
| 307 | * @param array $enabled Array of default Sharing Services. |
| 308 | */ |
| 309 | $enabled = apply_filters( 'sharing_default_services', $enabled ); |
| 310 | } |
| 311 | |
| 312 | // Cleanup after any filters that may have produced duplicate services |
| 313 | if ( is_array( $enabled['visible'] ) ) { |
| 314 | $enabled['visible'] = array_unique( $enabled['visible'] ); |
| 315 | } else { |
| 316 | $enabled['visible'] = array(); |
| 317 | } |
| 318 | |
| 319 | if ( is_array( $enabled['hidden'] ) ) { |
| 320 | $enabled['hidden'] = array_unique( $enabled['hidden'] ); |
| 321 | } else { |
| 322 | $enabled['hidden'] = array(); |
| 323 | } |
| 324 | |
| 325 | // Form the enabled services |
| 326 | $blog = array( |
| 327 | 'visible' => array(), |
| 328 | 'hidden' => array(), |
| 329 | ); |
| 330 | |
| 331 | foreach ( $blog as $area => $stuff ) { |
| 332 | foreach ( (array) $enabled[ $area ] as $service ) { |
| 333 | if ( isset( $services[ $service ] ) ) { |
| 334 | if ( ! isset( $options[ $service ] ) || ! is_array( $options[ $service ] ) ) { |
| 335 | $options[ $service ] = array(); |
| 336 | } |
| 337 | $blog[ $area ][ $service ] = new $services[ $service ]( $service, array_merge( $global, $options[ $service ] ) ); |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * Filters the list of enabled Sharing Services. |
| 344 | * |
| 345 | * @module sharedaddy |
| 346 | * |
| 347 | * @since 1.1.0 |
| 348 | * |
| 349 | * @param array $blog Array of enabled Sharing Services. |
| 350 | */ |
| 351 | $blog = apply_filters( 'sharing_services_enabled', $blog ); |
| 352 | |
| 353 | // Add CSS for NASCAR |
| 354 | if ( count( $blog['visible'] ) || count( $blog['hidden'] ) ) { |
| 355 | add_filter( 'post_flair_block_css', 'post_flair_service_enabled_sharing' ); |
| 356 | } |
| 357 | |
| 358 | // Convenience for checking if a service is present |
| 359 | $blog['all'] = array_flip( array_merge( array_keys( $blog['visible'] ), array_keys( $blog['hidden'] ) ) ); |
| 360 | return $blog; |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Get information about a specific enabled sharing service. |
| 365 | * |
| 366 | * @param string $service_name Service name. |
| 367 | * |
| 368 | * @return bool|Sharing_Source |
| 369 | */ |
| 370 | public function get_service( $service_name ) { |
| 371 | $services = $this->get_blog_services(); |
| 372 | |
| 373 | if ( isset( $services['visible'][ $service_name ] ) ) { |
| 374 | return $services['visible'][ $service_name ]; |
| 375 | } |
| 376 | |
| 377 | if ( isset( $services['hidden'][ $service_name ] ) ) { |
| 378 | return $services['hidden'][ $service_name ]; |
| 379 | } |
| 380 | |
| 381 | return false; |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Update global sharing options. |
| 386 | * |
| 387 | * @param array $data Array of new sharing options to save. |
| 388 | */ |
| 389 | public function set_global_options( $data ) { |
| 390 | $options = get_option( 'sharing-options' ); |
| 391 | |
| 392 | // No options yet. |
| 393 | if ( ! is_array( $options ) ) { |
| 394 | $options = array(); |
| 395 | } |
| 396 | |
| 397 | // Defaults. |
| 398 | $options['global'] = array( |
| 399 | 'button_style' => 'icon-text', |
| 400 | 'sharing_label' => $this->default_sharing_label, |
| 401 | 'open_links' => 'same', |
| 402 | 'show' => ! isset( $options['global'] ) ? array( 'post', 'page' ) : array(), |
| 403 | 'custom' => isset( $options['global']['custom'] ) ? $options['global']['custom'] : array(), |
| 404 | ); |
| 405 | |
| 406 | /** |
| 407 | * Filters global sharing settings. |
| 408 | * |
| 409 | * @module sharedaddy |
| 410 | * |
| 411 | * @since 1.1.0 |
| 412 | * |
| 413 | * @param array $options['global'] Array of global sharing settings. |
| 414 | */ |
| 415 | $options['global'] = apply_filters( 'sharing_default_global', $options['global'] ); |
| 416 | |
| 417 | // Validate options and set from our data |
| 418 | if ( |
| 419 | isset( $data['button_style'] ) |
| 420 | && in_array( $data['button_style'], array( 'icon-text', 'icon', 'text', 'official' ), true ) |
| 421 | ) { |
| 422 | $options['global']['button_style'] = $data['button_style']; |
| 423 | } |
| 424 | |
| 425 | if ( isset( $data['sharing_label'] ) ) { |
| 426 | if ( $this->default_sharing_label === $data['sharing_label'] ) { |
| 427 | $options['global']['sharing_label'] = false; |
| 428 | } else { |
| 429 | $options['global']['sharing_label'] = trim( wp_kses( stripslashes( $data['sharing_label'] ), array() ) ); |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | if ( |
| 434 | isset( $data['open_links'] ) |
| 435 | && in_array( $data['open_links'], array( 'new', 'same' ), true ) |
| 436 | ) { |
| 437 | $options['global']['open_links'] = $data['open_links']; |
| 438 | } |
| 439 | |
| 440 | $shows = array_values( get_post_types( array( 'public' => true ) ) ); |
| 441 | $shows[] = 'index'; |
| 442 | if ( isset( $data['show'] ) ) { |
| 443 | if ( is_scalar( $data['show'] ) ) { |
| 444 | switch ( $data['show'] ) { |
| 445 | case 'posts': |
| 446 | $data['show'] = array( 'post', 'page' ); |
| 447 | break; |
| 448 | case 'index': |
| 449 | $data['show'] = array( 'index' ); |
| 450 | break; |
| 451 | case 'posts-index': |
| 452 | $data['show'] = array( 'post', 'page', 'index' ); |
| 453 | break; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | $data['show'] = array_intersect( $data['show'], $shows ); |
| 458 | if ( $data['show'] ) { |
| 459 | $options['global']['show'] = $data['show']; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | update_option( 'sharing-options', $options ); |
| 464 | return $options['global']; |
| 465 | } |
| 466 | |
| 467 | /** |
| 468 | * Get global sharing options for the site. |
| 469 | * |
| 470 | * @return array |
| 471 | */ |
| 472 | public function get_global_options() { |
| 473 | if ( $this->global === false ) { |
| 474 | $options = get_option( 'sharing-options' ); |
| 475 | |
| 476 | if ( is_array( $options ) && isset( $options['global'] ) && is_array( $options['global'] ) ) { |
| 477 | $this->global = $options['global']; |
| 478 | } else { |
| 479 | $this->global = $this->set_global_options( $options ); |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if ( ! isset( $this->global['show'] ) ) { |
| 484 | $this->global['show'] = array( 'post', 'page' ); |
| 485 | } elseif ( is_scalar( $this->global['show'] ) ) { |
| 486 | switch ( $this->global['show'] ) { |
| 487 | case 'posts': |
| 488 | $this->global['show'] = array( 'post', 'page' ); |
| 489 | break; |
| 490 | case 'index': |
| 491 | $this->global['show'] = array( 'index' ); |
| 492 | break; |
| 493 | case 'posts-index': |
| 494 | $this->global['show'] = array( 'post', 'page', 'index' ); |
| 495 | break; |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | if ( false === $this->global['sharing_label'] ) { |
| 500 | $this->global['sharing_label'] = $this->default_sharing_label; |
| 501 | } |
| 502 | |
| 503 | return $this->global; |
| 504 | } |
| 505 | |
| 506 | /** |
| 507 | * Save a sharing service for use. |
| 508 | * |
| 509 | * @param int $id Sharing unique ID. |
| 510 | * @param Sharing_Source $service Sharing service. |
| 511 | * |
| 512 | * @return void |
| 513 | */ |
| 514 | public function set_service( $id, Sharing_Source $service ) { |
| 515 | // Update the options for this service |
| 516 | $options = get_option( 'sharing-options' ); |
| 517 | |
| 518 | // No options yet |
| 519 | if ( ! is_array( $options ) ) { |
| 520 | $options = array(); |
| 521 | } |
| 522 | |
| 523 | /** |
| 524 | * Get the state of a sharing button. |
| 525 | * |
| 526 | * @module sharedaddy |
| 527 | * |
| 528 | * @since 1.1.0 |
| 529 | * |
| 530 | * @param array $args { |
| 531 | * State of a sharing button. |
| 532 | * |
| 533 | * @type string $id Service ID. |
| 534 | * @type array $options Array of all sharing options. |
| 535 | * @type array $service Details about a service. |
| 536 | * } |
| 537 | */ |
| 538 | do_action( |
| 539 | 'sharing_get_button_state', |
| 540 | array( |
| 541 | 'id' => $id, |
| 542 | 'options' => $options, |
| 543 | 'service' => $service, |
| 544 | ) |
| 545 | ); |
| 546 | |
| 547 | $options[ $id ] = $service->get_options(); |
| 548 | |
| 549 | update_option( 'sharing-options', array_filter( $options ) ); |
| 550 | } |
| 551 | |
| 552 | /** |
| 553 | * Get stats for a site, a post, or a sharing service. |
| 554 | * Soon to come to a .org plugin near you! |
| 555 | * |
| 556 | * @param string|bool $service_name Service name. |
| 557 | * @param int|bool $post_id Post ID. |
| 558 | * @param int|bool $_blog_id Blog ID. |
| 559 | * |
| 560 | * @return int |
| 561 | */ |
| 562 | public function get_total( $service_name = false, $post_id = false, $_blog_id = false ) { |
| 563 | global $wpdb, $blog_id; |
| 564 | if ( ! $_blog_id ) { |
| 565 | $_blog_id = $blog_id; |
| 566 | } |
| 567 | if ( $service_name === false ) { |
| 568 | if ( $post_id > 0 ) { |
| 569 | // total number of shares for this post |
| 570 | $sql = $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND post_id = %d', $_blog_id, $post_id ); |
| 571 | $cache_key = "sharing_service_get_total_b{$_blog_id}_p{$post_id}"; |
| 572 | } else { |
| 573 | // total number of shares for this blog |
| 574 | $sql = $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d', $_blog_id ); |
| 575 | $cache_key = "sharing_service_get_total_b{$_blog_id}"; |
| 576 | } |
| 577 | } elseif ( $post_id > 0 ) { |
| 578 | $sql = $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 ); |
| 579 | $cache_key = "sharing_service_get_total_b{$_blog_id}_p{$post_id}_s{$service_name}"; |
| 580 | } else { |
| 581 | $sql = $wpdb->prepare( 'SELECT SUM( count ) FROM sharing_stats WHERE blog_id = %d AND share_service = %s', $_blog_id, $service_name ); |
| 582 | $cache_key = "sharing_service_get_total_b{$_blog_id}_s{$service_name}"; |
| 583 | } |
| 584 | |
| 585 | $ret = wp_cache_get( $cache_key, 'sharing' ); |
| 586 | if ( $ret === false ) { |
| 587 | // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery -- Prepared above. |
| 588 | $ret = (int) $wpdb->get_var( $sql ); |
| 589 | wp_cache_set( $cache_key, $ret, 'sharing', 5 * MINUTE_IN_SECONDS ); |
| 590 | } |
| 591 | return $ret; |
| 592 | } |
| 593 | |
| 594 | /** |
| 595 | * Get total stats for a site, for all sharing services. |
| 596 | * |
| 597 | * @param int|bool $post_id Post ID. |
| 598 | * |
| 599 | * @return array |
| 600 | */ |
| 601 | public function get_services_total( $post_id = false ) { |
| 602 | $totals = array(); |
| 603 | $services = $this->get_blog_services(); |
| 604 | |
| 605 | if ( ! empty( $services ) && isset( $services['all'] ) ) { |
| 606 | foreach ( $services['all'] as $key => $value ) { |
| 607 | $totals[ $key ] = new Sharing_Service_Total( $key, $this->get_total( $key, $post_id ) ); |
| 608 | } |
| 609 | } |
| 610 | usort( $totals, array( 'Sharing_Service_Total', 'cmp' ) ); |
| 611 | |
| 612 | return $totals; |
| 613 | } |
| 614 | |
| 615 | /** |
| 616 | * Get sharing stats for all posts on the site. |
| 617 | * |
| 618 | * @return array |
| 619 | */ |
| 620 | public function get_posts_total() { |
| 621 | $totals = array(); |
| 622 | global $wpdb, $blog_id; |
| 623 | |
| 624 | $cache_key = "sharing_service_get_posts_total_{$blog_id}"; |
| 625 | $my_data = wp_cache_get( $cache_key, 'sharing' ); |
| 626 | if ( $my_data === false ) { |
| 627 | $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 ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery |
| 628 | wp_cache_set( $cache_key, $my_data, 'sharing', 5 * MINUTE_IN_SECONDS ); |
| 629 | } |
| 630 | |
| 631 | if ( ! empty( $my_data ) ) { |
| 632 | foreach ( $my_data as $row ) { |
| 633 | $totals[] = new Sharing_Post_Total( $row->id, $row->total ); |
| 634 | } |
| 635 | } |
| 636 | |
| 637 | usort( $totals, array( 'Sharing_Post_Total', 'cmp' ) ); |
| 638 | |
| 639 | return $totals; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | /** |
| 644 | * Get stats for a specific sharing service. |
| 645 | */ |
| 646 | class Sharing_Service_Total { |
| 647 | /** |
| 648 | * Sharing service ID. |
| 649 | * |
| 650 | * @var int |
| 651 | */ |
| 652 | public $id = ''; |
| 653 | |
| 654 | /** |
| 655 | * Service name. |
| 656 | * |
| 657 | * @var string |
| 658 | */ |
| 659 | public $name = ''; |
| 660 | |
| 661 | /** |
| 662 | * Sharing service name. |
| 663 | * |
| 664 | * @var string |
| 665 | */ |
| 666 | public $service = ''; |
| 667 | |
| 668 | /** |
| 669 | * Total number of shares for this service. |
| 670 | * |
| 671 | * @var string |
| 672 | */ |
| 673 | public $total = 0; |
| 674 | |
| 675 | /** |
| 676 | * Constructor. |
| 677 | * |
| 678 | * @param int $id Service ID. |
| 679 | * @param int $total Total shares. |
| 680 | */ |
| 681 | public function __construct( $id, $total ) { |
| 682 | $services = new Sharing_Service(); |
| 683 | $this->id = esc_html( $id ); |
| 684 | $this->service = $services->get_service( $id ); |
| 685 | $this->total = (int) $total; |
| 686 | |
| 687 | $this->name = $this->service->get_name(); |
| 688 | } |
| 689 | |
| 690 | /** |
| 691 | * Compare total shares between 2 posts. |
| 692 | * |
| 693 | * @param object $a Sharing_Service_Total object. |
| 694 | * @param object $b Sharing_Service_Total object. |
| 695 | * |
| 696 | * @return bool |
| 697 | */ |
| 698 | public static function cmp( $a, $b ) { |
| 699 | if ( $a->total === $b->total ) { |
| 700 | return $a->name < $b->name; |
| 701 | } |
| 702 | return $a->total < $b->total; |
| 703 | } |
| 704 | } |
| 705 | |
| 706 | /** |
| 707 | * Get sharing stats for a specific post. |
| 708 | */ |
| 709 | class Sharing_Post_Total { |
| 710 | /** |
| 711 | * Sharing service ID. |
| 712 | * |
| 713 | * @var int |
| 714 | */ |
| 715 | public $id = 0; |
| 716 | |
| 717 | /** |
| 718 | * Total shares. |
| 719 | * |
| 720 | * @var int |
| 721 | */ |
| 722 | public $total = 0; |
| 723 | |
| 724 | /** |
| 725 | * Post title. |
| 726 | * |
| 727 | * @var string |
| 728 | */ |
| 729 | public $title = ''; |
| 730 | |
| 731 | /** |
| 732 | * Post permalink. |
| 733 | * |
| 734 | * @var string |
| 735 | */ |
| 736 | public $url = ''; |
| 737 | |
| 738 | /** |
| 739 | * Constructor. |
| 740 | * |
| 741 | * @param int $id Service ID. |
| 742 | * @param int $total Total shares. |
| 743 | */ |
| 744 | public function __construct( $id, $total ) { |
| 745 | $this->id = (int) $id; |
| 746 | $this->total = (int) $total; |
| 747 | $this->title = get_the_title( $this->id ); |
| 748 | $this->url = get_permalink( $this->id ); |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * Compare total shares between 2 posts. |
| 753 | * |
| 754 | * @param object $a Sharing_Post_Total object. |
| 755 | * @param object $b Sharing_Post_Total object. |
| 756 | * |
| 757 | * @return bool |
| 758 | */ |
| 759 | public static function cmp( $a, $b ) { |
| 760 | if ( $a->total === $b->total ) { |
| 761 | return $a->id < $b->id; |
| 762 | } |
| 763 | return $a->total < $b->total; |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | /** |
| 768 | * Populate sharing counts global with a post we want to count shares for. |
| 769 | * |
| 770 | * @param int $post_id Post ID. |
| 771 | * |
| 772 | * @return void |
| 773 | */ |
| 774 | function sharing_register_post_for_share_counts( $post_id ) { |
| 775 | global $jetpack_sharing_counts; |
| 776 | |
| 777 | if ( ! isset( $jetpack_sharing_counts ) || ! is_array( $jetpack_sharing_counts ) ) { |
| 778 | $jetpack_sharing_counts = array(); |
| 779 | } |
| 780 | |
| 781 | $jetpack_sharing_counts[ (int) $post_id ] = get_permalink( $post_id ); |
| 782 | } |
| 783 | |
| 784 | /** |
| 785 | * Determine whether we should load sharing scripts or not. |
| 786 | * |
| 787 | * @return bool |
| 788 | */ |
| 789 | function sharing_maybe_enqueue_scripts() { |
| 790 | $sharer = new Sharing_Service(); |
| 791 | $global_options = $sharer->get_global_options(); |
| 792 | |
| 793 | $enqueue = false; |
| 794 | if ( is_singular() && in_array( get_post_type(), $global_options['show'], true ) ) { |
| 795 | $enqueue = true; |
| 796 | } elseif ( |
| 797 | in_array( 'index', $global_options['show'], true ) |
| 798 | && ( |
| 799 | is_home() |
| 800 | || is_front_page() |
| 801 | || is_archive() |
| 802 | || is_search() |
| 803 | || in_array( get_post_type(), $global_options['show'], true ) |
| 804 | ) |
| 805 | ) { |
| 806 | $enqueue = true; |
| 807 | } |
| 808 | |
| 809 | /** |
| 810 | * Filter to decide when sharing scripts should be enqueued. |
| 811 | * |
| 812 | * @module sharedaddy |
| 813 | * |
| 814 | * @since 3.2.0 |
| 815 | * |
| 816 | * @param bool $enqueue Decide if the sharing scripts should be enqueued. |
| 817 | */ |
| 818 | return (bool) apply_filters( 'sharing_enqueue_scripts', $enqueue ); |
| 819 | } |
| 820 | |
| 821 | /** |
| 822 | * Add sharing JavaScript to the footer of a page. |
| 823 | * |
| 824 | * @return void |
| 825 | */ |
| 826 | function sharing_add_footer() { |
| 827 | if ( |
| 828 | class_exists( 'Jetpack_AMP_Support' ) |
| 829 | && Jetpack_AMP_Support::is_amp_request() |
| 830 | ) { |
| 831 | return; |
| 832 | } |
| 833 | |
| 834 | global $jetpack_sharing_counts; |
| 835 | |
| 836 | if ( |
| 837 | /** |
| 838 | * Filter all JavaScript output by the sharing module. |
| 839 | * |
| 840 | * @module sharedaddy |
| 841 | * |
| 842 | * @since 1.1.0 |
| 843 | * |
| 844 | * @param bool true Control whether the sharing module should add any JavaScript to the site. Default to true. |
| 845 | */ |
| 846 | apply_filters( 'sharing_js', true ) |
| 847 | && sharing_maybe_enqueue_scripts() |
| 848 | ) { |
| 849 | if ( |
| 850 | /** |
| 851 | * Filter the display of sharing counts next to the sharing buttons. |
| 852 | * |
| 853 | * @module sharedaddy |
| 854 | * |
| 855 | * @since 3.2.0 |
| 856 | * |
| 857 | * @param bool true Control the display of counters next to the sharing buttons. Default to true. |
| 858 | */ |
| 859 | apply_filters( 'jetpack_sharing_counts', true ) |
| 860 | && is_array( $jetpack_sharing_counts ) |
| 861 | && count( $jetpack_sharing_counts ) |
| 862 | ) : |
| 863 | $sharing_post_urls = array_filter( $jetpack_sharing_counts ); |
| 864 | if ( $sharing_post_urls ) : |
| 865 | ?> |
| 866 | |
| 867 | <script type="text/javascript"> |
| 868 | window.WPCOM_sharing_counts = <?php echo wp_json_encode( array_flip( $sharing_post_urls ) ); ?>; |
| 869 | </script> |
| 870 | <?php |
| 871 | endif; |
| 872 | endif; |
| 873 | |
| 874 | wp_enqueue_script( 'sharing-js' ); |
| 875 | $sharing_js_options = array( |
| 876 | 'lang' => get_base_recaptcha_lang_code(), |
| 877 | /** This filter is documented in modules/sharedaddy/sharing-service.php */ |
| 878 | 'counts' => apply_filters( 'jetpack_sharing_counts', true ), |
| 879 | 'is_stats_active' => Jetpack::is_module_active( 'stats' ), |
| 880 | ); |
| 881 | wp_localize_script( 'sharing-js', 'sharing_js_options', $sharing_js_options ); |
| 882 | } |
| 883 | $sharer = new Sharing_Service(); |
| 884 | $enabled = $sharer->get_blog_services(); |
| 885 | foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) as $service ) { |
| 886 | $service->display_footer(); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | /** |
| 891 | * Enqueue sharing CSS in head. |
| 892 | * |
| 893 | * @return void |
| 894 | */ |
| 895 | function sharing_add_header() { |
| 896 | $sharer = new Sharing_Service(); |
| 897 | $enabled = $sharer->get_blog_services(); |
| 898 | |
| 899 | foreach ( array_merge( $enabled['visible'], $enabled['hidden'] ) as $service ) { |
| 900 | $service->display_header(); |
| 901 | } |
| 902 | |
| 903 | if ( count( $enabled['all'] ) > 0 && sharing_maybe_enqueue_scripts() ) { |
| 904 | wp_enqueue_style( 'sharedaddy', plugin_dir_url( __FILE__ ) . 'sharing.css', array(), JETPACK__VERSION ); |
| 905 | wp_enqueue_style( 'social-logos' ); |
| 906 | } |
| 907 | } |
| 908 | add_action( 'wp_head', 'sharing_add_header', 1 ); |
| 909 | |
| 910 | /** |
| 911 | * Launch sharing requests on page load when a specific query string is used. |
| 912 | * |
| 913 | * @return void |
| 914 | */ |
| 915 | function sharing_process_requests() { |
| 916 | global $post; |
| 917 | |
| 918 | // Only process if: single post and share=X defined |
| 919 | if ( ( is_page() || is_single() ) && isset( $_GET['share'] ) && is_string( $_GET['share'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 920 | $sharer = new Sharing_Service(); |
| 921 | |
| 922 | $service = $sharer->get_service( sanitize_text_field( wp_unslash( $_GET['share'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 923 | if ( $service ) { |
| 924 | $service->process_request( $post, $_POST ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 925 | } |
| 926 | } |
| 927 | } |
| 928 | add_action( 'template_redirect', 'sharing_process_requests', 9 ); |
| 929 | |
| 930 | /** |
| 931 | * Gets the url to customise the sharing buttons in Calypso. |
| 932 | * |
| 933 | * @return string the customisation URL or null if it couldn't be determinde. |
| 934 | */ |
| 935 | function get_sharing_buttons_customisation_url() { |
| 936 | return Redirect::get_url( 'calypso-marketing-sharing-buttons', array( 'site' => ( new Status() )->get_site_suffix() ) ); |
| 937 | } |
| 938 | |
| 939 | /** |
| 940 | * Append sharing links to text. |
| 941 | * |
| 942 | * @param string $text The original text to append sharing links onto. |
| 943 | * @param bool $echo Where to echo the text or return. |
| 944 | * |
| 945 | * @return string The original $text with, if conditions are met, the sharing links. |
| 946 | */ |
| 947 | function sharing_display( $text = '', $echo = false ) { |
| 948 | global $post, $wp_current_filter; |
| 949 | |
| 950 | if ( Settings::is_syncing() ) { |
| 951 | return $text; |
| 952 | } |
| 953 | |
| 954 | if ( empty( $post ) ) { |
| 955 | return $text; |
| 956 | } |
| 957 | |
| 958 | if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) { |
| 959 | return $text; |
| 960 | } |
| 961 | |
| 962 | // Prevent from rendering sharing buttons in block which is fetched from REST endpoint by editor |
| 963 | if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { |
| 964 | return $text; |
| 965 | } |
| 966 | |
| 967 | // Don't output flair on excerpts. |
| 968 | if ( in_array( 'get_the_excerpt', (array) $wp_current_filter, true ) ) { |
| 969 | return $text; |
| 970 | } |
| 971 | |
| 972 | // Ensure we don't display sharing buttons on post excerpts that are hooked inside the post content |
| 973 | if ( in_array( 'the_excerpt', (array) $wp_current_filter, true ) && |
| 974 | in_array( 'the_content', (array) $wp_current_filter, true ) ) { |
| 975 | return $text; |
| 976 | } |
| 977 | |
| 978 | // Don't allow flair to be added to the_content more than once (prevent infinite loops). |
| 979 | $done = false; |
| 980 | foreach ( $wp_current_filter as $filter ) { |
| 981 | if ( 'the_content' === $filter ) { |
| 982 | if ( $done ) { |
| 983 | return $text; |
| 984 | } else { |
| 985 | $done = true; |
| 986 | } |
| 987 | } |
| 988 | } |
| 989 | |
| 990 | // check whether we are viewing the front page and whether the front page option is checked. |
| 991 | $options = get_option( 'sharing-options' ); |
| 992 | $display_options = null; |
| 993 | |
| 994 | if ( is_array( $options ) ) { |
| 995 | $display_options = $options['global']['show']; |
| 996 | } |
| 997 | |
| 998 | if ( is_front_page() && ( is_array( $display_options ) && ! in_array( 'index', $display_options, true ) ) ) { |
| 999 | return $text; |
| 1000 | } |
| 1001 | |
| 1002 | if ( is_attachment() && in_array( 'the_excerpt', (array) $wp_current_filter, true ) ) { |
| 1003 | // Many themes run the_excerpt() conditionally on an attachment page, then run the_content(). |
| 1004 | // We only want to output the sharing buttons once. Let's stick with the_content(). |
| 1005 | return $text; |
| 1006 | } |
| 1007 | |
| 1008 | $sharer = new Sharing_Service(); |
| 1009 | $global = $sharer->get_global_options(); |
| 1010 | |
| 1011 | $show = false; |
| 1012 | if ( ! is_feed() ) { |
| 1013 | if ( is_singular() && in_array( get_post_type(), $global['show'], true ) ) { |
| 1014 | $show = true; |
| 1015 | } elseif ( in_array( 'index', $global['show'], true ) && ( is_home() || is_front_page() || is_archive() || is_search() || in_array( get_post_type(), $global['show'], true ) ) ) { |
| 1016 | $show = true; |
| 1017 | } |
| 1018 | } |
| 1019 | |
| 1020 | /** |
| 1021 | * Filter to decide if sharing buttons should be displayed. |
| 1022 | * |
| 1023 | * @module sharedaddy |
| 1024 | * |
| 1025 | * @since 1.1.0 |
| 1026 | * |
| 1027 | * @param bool $show Should the sharing buttons be displayed. |
| 1028 | * @param WP_Post $post The post to share. |
| 1029 | */ |
| 1030 | $show = apply_filters( 'sharing_show', $show, $post ); |
| 1031 | |
| 1032 | // Disabled for this post? |
| 1033 | $switched_status = get_post_meta( $post->ID, 'sharing_disabled', false ); |
| 1034 | |
| 1035 | if ( ! empty( $switched_status ) ) { |
| 1036 | $show = false; |
| 1037 | } |
| 1038 | |
| 1039 | // Is the post private? |
| 1040 | $post_status = get_post_status( $post->ID ); |
| 1041 | |
| 1042 | if ( 'private' === $post_status ) { |
| 1043 | $show = false; |
| 1044 | } |
| 1045 | |
| 1046 | // Hide on password protected posts unless password is provided. |
| 1047 | if ( post_password_required( $post->ID ) ) { |
| 1048 | $show = false; |
| 1049 | } |
| 1050 | |
| 1051 | /** |
| 1052 | * Filter the Sharing buttons' Ajax action name Jetpack checks for. |
| 1053 | * This allows the use of the buttons with your own Ajax implementation. |
| 1054 | * |
| 1055 | * @module sharedaddy |
| 1056 | * |
| 1057 | * @since 7.3.0 |
| 1058 | * |
| 1059 | * @param string $sharing_ajax_action_name Name of the Sharing buttons' Ajax action. |
| 1060 | */ |
| 1061 | $ajax_action = apply_filters( 'sharing_ajax_action', 'get_latest_posts' ); |
| 1062 | |
| 1063 | // Allow to be used in ajax requests for latest posts. |
| 1064 | if ( |
| 1065 | defined( 'DOING_AJAX' ) |
| 1066 | && DOING_AJAX |
| 1067 | && isset( $_REQUEST['action'] ) // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce handling happens within each custom implementation. |
| 1068 | && $ajax_action === $_REQUEST['action'] // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Nonce handling happens within each custom implementation. |
| 1069 | ) { |
| 1070 | $show = true; |
| 1071 | } |
| 1072 | |
| 1073 | $sharing_content = ''; |
| 1074 | $enabled = false; |
| 1075 | |
| 1076 | if ( $show ) { |
| 1077 | /** |
| 1078 | * Filters the list of enabled Sharing Services. |
| 1079 | * |
| 1080 | * @module sharedaddy |
| 1081 | * |
| 1082 | * @since 2.2.3 |
| 1083 | * |
| 1084 | * @param array $sharer->get_blog_services() Array of Sharing Services currently enabled. |
| 1085 | */ |
| 1086 | $enabled = apply_filters( 'sharing_enabled', $sharer->get_blog_services() ); |
| 1087 | |
| 1088 | if ( count( $enabled['all'] ) > 0 ) { |
| 1089 | $dir = get_option( 'text_direction' ); |
| 1090 | |
| 1091 | // Wrapper. |
| 1092 | $sharing_content .= '<div class="sharedaddy sd-sharing-enabled"><div class="robots-nocontent sd-block sd-social sd-social-' . $global['button_style'] . ' sd-sharing">'; |
| 1093 | if ( '' !== $global['sharing_label'] ) { |
| 1094 | $sharing_content .= sprintf( |
| 1095 | /** |
| 1096 | * Filter the sharing buttons' headline structure. |
| 1097 | * |
| 1098 | * @module sharedaddy |
| 1099 | * |
| 1100 | * @since 4.4.0 |
| 1101 | * |
| 1102 | * @param string $sharing_headline Sharing headline structure. |
| 1103 | * @param string $global['sharing_label'] Sharing title. |
| 1104 | * @param string $sharing Module name. |
| 1105 | */ |
| 1106 | apply_filters( 'jetpack_sharing_headline_html', '<h3 class="sd-title">%s</h3>', $global['sharing_label'], 'sharing' ), |
| 1107 | esc_html( $global['sharing_label'] ) |
| 1108 | ); |
| 1109 | } |
| 1110 | $sharing_content .= '<div class="sd-content"><ul>'; |
| 1111 | |
| 1112 | // Visible items. |
| 1113 | $visible = ''; |
| 1114 | foreach ( $enabled['visible'] as $service ) { |
| 1115 | $klasses = array( 'share-' . $service->get_class() ); |
| 1116 | if ( $service->is_deprecated() ) { |
| 1117 | if ( ! current_user_can( 'manage_options' ) ) { |
| 1118 | continue; |
| 1119 | } |
| 1120 | $klasses[] = 'share-deprecated'; |
| 1121 | } |
| 1122 | // Individual HTML for sharing service. |
| 1123 | $visible .= '<li class="' . implode( ' ', $klasses ) . '">' . $service->get_display( $post ) . '</li>'; |
| 1124 | } |
| 1125 | |
| 1126 | $parts = array(); |
| 1127 | $parts[] = $visible; |
| 1128 | if ( count( $enabled['hidden'] ) > 0 ) { |
| 1129 | if ( count( $enabled['visible'] ) > 0 ) { |
| 1130 | $expand = __( 'More', 'jetpack' ); |
| 1131 | } else { |
| 1132 | $expand = __( 'Share', 'jetpack' ); |
| 1133 | } |
| 1134 | $parts[] = '<li><a href="#" class="sharing-anchor sd-button share-more"><span>' . $expand . '</span></a></li>'; |
| 1135 | } |
| 1136 | |
| 1137 | if ( 'rtl' === $dir ) { |
| 1138 | $parts = array_reverse( $parts ); |
| 1139 | } |
| 1140 | |
| 1141 | $sharing_content .= implode( '', $parts ); |
| 1142 | $sharing_content .= '<li class="share-end"></li></ul>'; |
| 1143 | |
| 1144 | // Link to customization options if user can manage them. |
| 1145 | if ( current_user_can( 'manage_options' ) ) { |
| 1146 | $link_url = get_sharing_buttons_customisation_url(); |
| 1147 | if ( ! empty( $link_url ) ) { |
| 1148 | $link_text = __( 'Customize buttons', 'jetpack' ); |
| 1149 | $sharing_content .= '<p class="share-customize-link"><a href="' . esc_url( $link_url ) . '" target="_blank" rel="noopener noreferrer">' . esc_html( $link_text ) . '</a></p>'; |
| 1150 | } |
| 1151 | } |
| 1152 | |
| 1153 | if ( count( $enabled['hidden'] ) > 0 ) { |
| 1154 | $sharing_content .= '<div class="sharing-hidden"><div class="inner" style="display: none;'; |
| 1155 | |
| 1156 | if ( count( $enabled['hidden'] ) === 1 ) { |
| 1157 | $sharing_content .= 'width:150px;'; |
| 1158 | } |
| 1159 | |
| 1160 | $sharing_content .= '">'; |
| 1161 | |
| 1162 | if ( count( $enabled['hidden'] ) === 1 ) { |
| 1163 | $sharing_content .= '<ul style="background-image:none;">'; |
| 1164 | } else { |
| 1165 | $sharing_content .= '<ul>'; |
| 1166 | } |
| 1167 | |
| 1168 | foreach ( $enabled['hidden'] as $service ) { |
| 1169 | // Individual HTML for sharing service. |
| 1170 | $klasses = array( 'share-' . $service->get_class() ); |
| 1171 | if ( $service->is_deprecated() ) { |
| 1172 | if ( ! current_user_can( 'manage_options' ) ) { |
| 1173 | continue; |
| 1174 | } |
| 1175 | $klasses[] = 'share-deprecated'; |
| 1176 | } |
| 1177 | $sharing_content .= '<li class="' . implode( ' ', $klasses ) . '">'; |
| 1178 | $sharing_content .= $service->get_display( $post ); |
| 1179 | $sharing_content .= '</li>'; |
| 1180 | } |
| 1181 | |
| 1182 | // End of wrapper. |
| 1183 | $sharing_content .= '<li class="share-end"></li></ul></div></div>'; |
| 1184 | } |
| 1185 | |
| 1186 | $sharing_content .= '</div></div></div>'; |
| 1187 | |
| 1188 | // Register our JS. |
| 1189 | if ( defined( 'JETPACK__VERSION' ) ) { |
| 1190 | $ver = JETPACK__VERSION; |
| 1191 | } else { |
| 1192 | $ver = '20211226'; |
| 1193 | } |
| 1194 | |
| 1195 | // @todo: Investigate if we can load this JS in the footer instead. |
| 1196 | wp_register_script( |
| 1197 | 'sharing-js', |
| 1198 | Assets::get_file_url_for_environment( |
| 1199 | '_inc/build/sharedaddy/sharing.min.js', |
| 1200 | 'modules/sharedaddy/sharing.js' |
| 1201 | ), |
| 1202 | array(), |
| 1203 | $ver, |
| 1204 | false |
| 1205 | ); |
| 1206 | |
| 1207 | // Enqueue scripts for the footer. |
| 1208 | add_action( 'wp_footer', 'sharing_add_footer' ); |
| 1209 | } |
| 1210 | } |
| 1211 | |
| 1212 | /** |
| 1213 | * Filters the content markup of the Jetpack sharing links |
| 1214 | * |
| 1215 | * @module sharedaddy |
| 1216 | * |
| 1217 | * @since 3.8.0 |
| 1218 | * @since 6.2.0 Started sending $enabled as a second parameter. |
| 1219 | * |
| 1220 | * @param string $sharing_content Content markup of the Jetpack sharing links |
| 1221 | * @param array $enabled Array of Sharing Services currently enabled. |
| 1222 | */ |
| 1223 | $sharing_markup = apply_filters( 'jetpack_sharing_display_markup', $sharing_content, $enabled ); |
| 1224 | |
| 1225 | if ( $echo ) { |
| 1226 | echo $text . $sharing_markup; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 1227 | } else { |
| 1228 | return $text . $sharing_markup; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | /** |
| 1233 | * Get reCAPTCHA language code based off the language code of the site. |
| 1234 | * |
| 1235 | * @return string |
| 1236 | */ |
| 1237 | function get_base_recaptcha_lang_code() { |
| 1238 | $base_recaptcha_lang_code_mapping = array( |
| 1239 | 'en' => 'en', |
| 1240 | 'nl' => 'nl', |
| 1241 | 'fr' => 'fr', |
| 1242 | 'fr-be' => 'fr', |
| 1243 | 'fr-ca' => 'fr', |
| 1244 | 'fr-ch' => 'fr', |
| 1245 | 'de' => 'de', |
| 1246 | 'pt' => 'pt', |
| 1247 | 'pt-br' => 'pt', |
| 1248 | 'ru' => 'ru', |
| 1249 | 'es' => 'es', |
| 1250 | 'tr' => 'tr', |
| 1251 | ); |
| 1252 | |
| 1253 | $blog_lang_code = get_bloginfo( 'language' ); |
| 1254 | if ( isset( $base_recaptcha_lang_code_mapping[ $blog_lang_code ] ) ) { |
| 1255 | return $base_recaptcha_lang_code_mapping[ $blog_lang_code ]; |
| 1256 | } |
| 1257 | |
| 1258 | // if no base mapping is found return default 'en' |
| 1259 | return 'en'; |
| 1260 | } |
| 1261 | |
| 1262 | Sharing_Service::init(); |
| 1263 |