| 1 |
<?php |
| 2 |
/** |
| 3 |
* The per-post Likes switch in the REST API. |
| 4 |
* |
| 5 |
* @package automattic/jetpack-sharing-likes |
| 6 |
*/ |
| 7 |
|
| 8 |
declare( strict_types = 1 ); |
| 9 |
|
| 10 |
namespace Automattic\Jetpack\Sharing_Likes; |
| 11 |
|
| 12 |
use Automattic\Jetpack\Sharing_Likes\Settings\Likes_Options; |
| 13 |
|
| 14 |
/** |
| 15 |
* Whether Like buttons show on a given post, as a REST field on every public post type. |
| 16 |
* |
| 17 |
* The post meta only records a choice that contradicts the sitewide default, so a |
| 18 |
* post without it follows whatever that default is at the time. |
| 19 |
*/ |
| 20 |
final class Post_Likes_Switch { |
| 21 |
|
| 22 |
/** |
| 23 |
* REST field the block editor reads and writes. |
| 24 |
*/ |
| 25 |
public const FIELD = 'jetpack_likes_enabled'; |
| 26 |
|
| 27 |
/** |
| 28 |
* Post meta overriding the sitewide default: '1' forces Likes on, '0' forces them off. |
| 29 |
*/ |
| 30 |
public const META_KEY = 'switch_like_status'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Post type support the block editor checks before offering the switch. |
| 34 |
*/ |
| 35 |
public const POST_TYPE_SUPPORT = 'jetpack-post-likes'; |
| 36 |
|
| 37 |
/** |
| 38 |
* Register the field whenever the REST API starts. Safe to call more than once. |
| 39 |
*/ |
| 40 |
public static function init(): void { |
| 41 |
add_action( 'rest_api_init', array( __CLASS__, 'register_rest_field' ) ); |
| 42 |
// Theme-dependent CPTs (portfolios, testimonials) register earlier on this same hook. |
| 43 |
add_action( 'restapi_theme_init', array( __CLASS__, 'register_rest_field' ), 20 ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add the field and the post type support to every public post type. |
| 48 |
*/ |
| 49 |
public static function register_rest_field(): void { |
| 50 |
foreach ( get_post_types( array( 'public' => true ) ) as $post_type ) { |
| 51 |
register_rest_field( |
| 52 |
$post_type, |
| 53 |
self::FIELD, |
| 54 |
array( |
| 55 |
'get_callback' => array( __CLASS__, 'get_value' ), |
| 56 |
'update_callback' => array( __CLASS__, 'update_value' ), |
| 57 |
'schema' => array( |
| 58 |
'description' => __( 'Are Likes enabled?', 'jetpack-sharing-likes' ), |
| 59 |
'type' => 'boolean', |
| 60 |
), |
| 61 |
) |
| 62 |
); |
| 63 |
|
| 64 |
add_post_type_support( $post_type, self::POST_TYPE_SUPPORT ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Whether Likes show on the post. Null for a meta value that is neither '0' nor '1'. |
| 70 |
* |
| 71 |
* @param array $post Post data as the REST API prepared it. |
| 72 |
*/ |
| 73 |
public static function get_value( array $post ): ?bool { |
| 74 |
if ( ! isset( $post['id'] ) ) { |
| 75 |
return false; |
| 76 |
} |
| 77 |
|
| 78 |
$switched = get_post_meta( $post['id'], self::META_KEY, true ); |
| 79 |
|
| 80 |
if ( '' === $switched ) { |
| 81 |
return Likes_Options::likes_enabled_sitewide(); |
| 82 |
} |
| 83 |
|
| 84 |
if ( '0' === $switched ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
if ( '1' === $switched ) { |
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
return null; |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* Store the choice for the post, or clear it where it matches the sitewide default. |
| 97 |
* |
| 98 |
* @param bool $enable Whether Likes should show on the post. |
| 99 |
* @param \WP_Post $post_object The post being updated. |
| 100 |
* |
| 101 |
* @return int|bool What `update_post_meta()` or `delete_post_meta()` returned. |
| 102 |
*/ |
| 103 |
public static function update_value( $enable, $post_object ) { |
| 104 |
if ( $enable !== Likes_Options::likes_enabled_sitewide() ) { |
| 105 |
return update_post_meta( $post_object->ID, self::META_KEY, $enable ? 1 : 0 ); |
| 106 |
} |
| 107 |
|
| 108 |
return delete_post_meta( $post_object->ID, self::META_KEY ); |
| 109 |
} |
| 110 |
} |
| 111 |
|