Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_Metadata
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 2
110
0.00% covered (danger)
0.00%
0 / 1
 is_public
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 is_internal_only
0.00% covered (danger)
0.00%
0 / 14
0.00% covered (danger)
0.00%
0 / 1
30
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
8if ( ! defined( 'ABSPATH' ) ) {
9    exit( 0 );
10}
11
12/**
13 * Base class for WPCOM_JSON_API_Metadata
14 */
15class WPCOM_JSON_API_Metadata {
16    /**
17     * Checks to see if a meta key is in the array of allowed public (and whitelisted) meta data.
18     *
19     * Additionally, if the key begins with 'geo_' or '_wpas_', true will also be returned.
20     *
21     * @param string $key A post metadata key value to check.
22     * @return bool True or false depending on whether the key meets the defined criteria.
23     **/
24    public static function is_public( $key ) {
25        if ( empty( $key ) ) {
26            return false;
27        }
28
29        // Default whitelisted meta keys.
30        $whitelisted_meta = array( '_thumbnail_id' );
31
32        // whitelist of metadata that can be accessed.
33        /** This filter is documented in json-endpoints/class.wpcom-json-api-post-endpoint.php */
34        if ( in_array( $key, apply_filters( 'rest_api_allowed_public_metadata', $whitelisted_meta ), true ) ) {
35            return true;
36        }
37
38        if ( str_starts_with( $key, 'geo_' ) ) {
39            return true;
40        }
41
42        if ( str_starts_with( $key, '_wpas_' ) ) {
43            return true;
44        }
45
46        return false;
47    }
48
49    /**
50     * Checks to see if a meta key should be used internally only.
51     *
52     * @param string $key A post metadata key value to check.
53     * @return bool True or false depending on whether the key meets the defined criteria.
54     **/
55    public static function is_internal_only( $key ) {
56        // We want to always return the `_jetpack_blogging_prompt_key` key in post responses if it is available.
57        if ( $key === '_jetpack_blogging_prompt_key' ) {
58            return false;
59        }
60
61        // We want to always return the `_jetpack_newsletter_access` key to
62        // display the correct newsletter access in Calypso.
63        $whitelist = array(
64            '_jetpack_newsletter_access',
65            '_jetpack_newsletter_tier_id',
66            '_jetpack_dont_email_post_to_subs',
67        );
68
69        if ( in_array( $key, $whitelist, true ) ) {
70            return false;
71        }
72
73        if ( str_starts_with( $key, '_jetpack_' ) ) {
74            return true;
75        }
76
77        if ( str_starts_with( $key, '_elasticsearch_' ) ) {
78            return true;
79        }
80
81        return false;
82    }
83}