| 1 |
<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 |
/** |
| 3 |
* WPCOM_JSON_API_Metadata class - Utility classes that don't necessarily have a home yet. |
| 4 |
* |
| 5 |
* @package automattic/jetpack |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Base class for WPCOM_JSON_API_Metadata |
| 10 |
*/ |
| 11 |
class WPCOM_JSON_API_Metadata { |
| 12 |
/** |
| 13 |
* Checks to see if a meta key is in the array of allowed public (and whitelisted) meta data. |
| 14 |
* |
| 15 |
* Additionally, if the key begins with 'geo_' or '_wpas_', true will also be returned. |
| 16 |
* |
| 17 |
* @param string $key A post metadata key value to check. |
| 18 |
* @return bool True or false depending on whether the key meets the defined criteria. |
| 19 |
**/ |
| 20 |
public static function is_public( $key ) { |
| 21 |
if ( empty( $key ) ) { |
| 22 |
return false; |
| 23 |
} |
| 24 |
|
| 25 |
// Default whitelisted meta keys. |
| 26 |
$whitelisted_meta = array( '_thumbnail_id' ); |
| 27 |
|
| 28 |
// whitelist of metadata that can be accessed. |
| 29 |
/** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */ |
| 30 |
if ( in_array( $key, apply_filters( 'rest_api_allowed_public_metadata', $whitelisted_meta ), true ) ) { |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
if ( str_starts_with( $key, 'geo_' ) ) { |
| 35 |
return true; |
| 36 |
} |
| 37 |
|
| 38 |
if ( str_starts_with( $key, '_wpas_' ) ) { |
| 39 |
return true; |
| 40 |
} |
| 41 |
|
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Checks to see if a meta key should be used internally only. |
| 47 |
* |
| 48 |
* @param string $key A post metadata key value to check. |
| 49 |
* @return bool True or false depending on whether the key meets the defined criteria. |
| 50 |
**/ |
| 51 |
public static function is_internal_only( $key ) { |
| 52 |
// We want to always return the `_jetpack_blogging_prompt_key` key in post responses if it is available. |
| 53 |
if ( $key === '_jetpack_blogging_prompt_key' ) { |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// We want to always return the `_jetpack_newsletter_access` key to |
| 58 |
// display the correct newsletter access in Calypso. |
| 59 |
$whitelist = array( |
| 60 |
'_jetpack_newsletter_access', |
| 61 |
'_jetpack_newsletter_tier_id', |
| 62 |
); |
| 63 |
|
| 64 |
if ( in_array( $key, $whitelist, true ) ) { |
| 65 |
return false; |
| 66 |
} |
| 67 |
|
| 68 |
if ( str_starts_with( $key, '_jetpack_' ) ) { |
| 69 |
return true; |
| 70 |
} |
| 71 |
|
| 72 |
if ( str_starts_with( $key, '_elasticsearch_' ) ) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
} |
| 79 |
|