| @@ -20,8 +20,9 @@ | ||
| 20 | 20 | |
| 21 | 21 | // phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. |
| 22 | 22 | |
| 23 | 23 | use Automattic\Jetpack\Assets; |
| 24 | +use Automattic\Jetpack\Sharing_Likes\Post_Likes_Switch; | |
| 24 | 25 | use Automattic\Jetpack\Status\Host; |
| 25 | 26 | |
| 26 | 27 | if ( ! defined( 'ABSPATH' ) ) { |
| 27 | 28 | exit( 0 ); |
| @@ -490,100 +491,43 @@ | ||
| 490 | 491 | |
| 491 | 492 | /** |
| 492 | 493 | * Callback to get the value for the jetpack_likes_enabled field. |
| 493 | 494 | * |
| 494 | - * Warning: this behavior is somewhat complicated! | |
| 495 | - * When the switch_like_status post_meta is unset, we follow the global setting in Sharing. | |
| 496 | - * When it is set to 0, we disable likes on the post, regardless of the global setting. | |
| 497 | - * When it is set to 1, we enable likes on the post, regardless of the global setting. | |
| 495 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Likes_Switch::get_value() instead. | |
| 498 | 496 | * |
| 499 | 497 | * @param array $post - post data we're checking. |
| 500 | 498 | * |
| 501 | - * @return bool | |
| 499 | + * @return bool|null | |
| 502 | 500 | */ |
| 503 | 501 | function jetpack_post_likes_get_value( array $post ) { |
| 504 | - if ( ! isset( $post['id'] ) ) { | |
| 505 | - return false; | |
| 506 | - } | |
| 507 | - | |
| 508 | - $post_likes_switched = get_post_meta( $post['id'], 'switch_like_status', true ); | |
| 509 | - | |
| 510 | - /** This filter is documented in modules/jetpack-likes-settings.php */ | |
| 511 | - $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ); | |
| 512 | - | |
| 513 | - // An empty string: post meta was not set, so go with the global setting. | |
| 514 | - if ( '' === $post_likes_switched ) { | |
| 515 | - return $sitewide_likes_enabled; | |
| 516 | - } elseif ( '0' === $post_likes_switched ) { // User overrode the global setting to disable likes. | |
| 517 | - return false; | |
| 518 | - } elseif ( '1' === $post_likes_switched ) { // User overrode the global setting to enable likes. | |
| 519 | - return true; | |
| 520 | - } | |
| 521 | - // No default fallback, let's stay explicit. | |
| 502 | + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Likes_Switch::get_value' ); | |
| 503 | + return Post_Likes_Switch::get_value( $post ); | |
| 522 | 504 | } |
| 523 | 505 | |
| 524 | 506 | /** |
| 525 | 507 | * Callback to set switch_like_status post_meta when jetpack_likes_enabled is updated. |
| 526 | 508 | * |
| 527 | - * Warning: this behavior is somewhat complicated! | |
| 528 | - * When the switch_like_status post_meta is unset, we follow the global setting in Sharing. | |
| 529 | - * When it is set to 0, we disable likes on the post, regardless of the global setting. | |
| 530 | - * When it is set to 1, we enable likes on the post, regardless of the global setting. | |
| 509 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Likes_Switch::update_value() instead. | |
| 531 | 510 | * |
| 532 | 511 | * @param bool $enable_post_likes - checks if post likes are enabled. |
| 533 | 512 | * @param object $post_object - object containing post data. |
| 513 | + * | |
| 514 | + * @return int|bool | |
| 534 | 515 | */ |
| 535 | 516 | function jetpack_post_likes_update_value( $enable_post_likes, $post_object ) { |
| 536 | - /** This filter is documented in modules/jetpack-likes-settings.php */ | |
| 537 | - $sitewide_likes_enabled = (bool) apply_filters( 'wpl_is_enabled_sitewide', ! get_option( 'disabled_likes' ) ); | |
| 538 | - | |
| 539 | - $should_switch_status = $enable_post_likes !== $sitewide_likes_enabled; | |
| 540 | - | |
| 541 | - if ( $should_switch_status ) { | |
| 542 | - // Set the meta to 0 if the user wants to disable likes, 1 if user wants to enable. | |
| 543 | - $switch_like_status = ( $enable_post_likes ? 1 : 0 ); | |
| 544 | - return update_post_meta( $post_object->ID, 'switch_like_status', $switch_like_status ); | |
| 545 | - } else { | |
| 546 | - // Unset the meta otherwise. | |
| 547 | - return delete_post_meta( $post_object->ID, 'switch_like_status' ); | |
| 548 | - } | |
| 517 | + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Likes_Switch::update_value' ); | |
| 518 | + return Post_Likes_Switch::update_value( $enable_post_likes, $post_object ); | |
| 549 | 519 | } |
| 550 | 520 | |
| 551 | 521 | /** |
| 552 | 522 | * Add Likes post_meta to the REST API Post response. |
| 553 | 523 | * |
| 554 | - * @action rest_api_init | |
| 555 | - * @uses register_rest_field | |
| 556 | - * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/ | |
| 524 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Likes_Switch::register_rest_field() instead. | |
| 557 | 525 | */ |
| 558 | 526 | function jetpack_post_likes_register_rest_field() { |
| 559 | - $post_types = get_post_types( array( 'public' => true ) ); | |
| 560 | - foreach ( $post_types as $post_type ) { | |
| 561 | - register_rest_field( | |
| 562 | - $post_type, | |
| 563 | - 'jetpack_likes_enabled', | |
| 564 | - array( | |
| 565 | - 'get_callback' => 'jetpack_post_likes_get_value', | |
| 566 | - 'update_callback' => 'jetpack_post_likes_update_value', | |
| 567 | - 'schema' => array( | |
| 568 | - 'description' => __( 'Are Likes enabled?', 'jetpack' ), | |
| 569 | - 'type' => 'boolean', | |
| 570 | - ), | |
| 571 | - ) | |
| 572 | - ); | |
| 573 | - | |
| 574 | - /** | |
| 575 | - * Ensures all public internal post-types support `likes` | |
| 576 | - * This feature support flag is used by the REST API and Gutenberg. | |
| 577 | - */ | |
| 578 | - add_post_type_support( $post_type, 'jetpack-post-likes' ); | |
| 579 | - } | |
| 527 | + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Likes_Switch::register_rest_field' ); | |
| 528 | + Post_Likes_Switch::register_rest_field(); | |
| 580 | 529 | } |
| 581 | 530 | |
| 582 | -// Add Likes post_meta to the REST API Post response. | |
| 583 | -add_action( 'rest_api_init', 'jetpack_post_likes_register_rest_field' ); | |
| 584 | - | |
| 585 | -// Some CPTs (e.g. Jetpack portfolios and testimonials) get registered with | |
| 586 | -// restapi_theme_init because they depend on theme support, so let's also hook to that. | |
| 587 | -add_action( 'restapi_theme_init', 'jetpack_post_likes_register_rest_field', 20 ); | |
| 531 | +Post_Likes_Switch::init(); | |
| 588 | 532 | |
| 589 | 533 | Jetpack_Likes::init(); |