Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Message_Templates_Placeholders | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
| get_all | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| fetch_and_cache | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| fetch_from_wpcom | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| wpcom_get_placeholders | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Publicize Message Templates Placeholders class. |
| 4 | * |
| 5 | * @package automattic/jetpack-publicize |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Publicize; |
| 9 | |
| 10 | use Automattic\Jetpack\Publicize\REST_API\Proxy_Requests; |
| 11 | use WP_REST_Request; |
| 12 | |
| 13 | /** |
| 14 | * Fetches and caches the canonical message-template placeholder catalogue from WPCOM. |
| 15 | * |
| 16 | * The value transient holds the catalogue and is only overwritten on a |
| 17 | * successful refetch — we never want to drop back to an empty list. |
| 18 | * The validity transient acts as a soft TTL: its expiry triggers the next refetch. |
| 19 | */ |
| 20 | class Message_Templates_Placeholders { |
| 21 | |
| 22 | const VALUE_TRANSIENT = 'jetpack_social_message_template_placeholders'; |
| 23 | const VALIDITY_TRANSIENT = 'jetpack_social_message_template_placeholders_validity'; |
| 24 | |
| 25 | const VALIDITY_TTL = 3 * DAY_IN_SECONDS; |
| 26 | const FAILURE_RETRY_TTL = HOUR_IN_SECONDS; |
| 27 | |
| 28 | /** |
| 29 | * Get the placeholder catalogue. |
| 30 | * |
| 31 | * @return array<int, array{id: string, label: string}> |
| 32 | */ |
| 33 | public static function get_all() { |
| 34 | if ( Publicize_Utils::is_wpcom() ) { |
| 35 | return self::wpcom_get_placeholders(); |
| 36 | } |
| 37 | |
| 38 | $cached = get_transient( self::VALUE_TRANSIENT ); |
| 39 | $is_valid = false !== get_transient( self::VALIDITY_TRANSIENT ); |
| 40 | |
| 41 | if ( $is_valid ) { |
| 42 | return is_array( $cached ) ? $cached : array(); |
| 43 | } |
| 44 | |
| 45 | return self::fetch_and_cache( $cached ); |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Fetch from WPCOM and update the cache. |
| 50 | * |
| 51 | * @param array|false $existing Existing cached value to fall back to on failure. |
| 52 | * @return array<int, array{id: string, label: string}> |
| 53 | */ |
| 54 | public static function fetch_and_cache( $existing = false ) { |
| 55 | $placeholders = self::fetch_from_wpcom(); |
| 56 | |
| 57 | if ( is_wp_error( $placeholders ) || empty( $placeholders ) ) { |
| 58 | // Throttle retries so we don't hammer WPCOM while it's unreachable. |
| 59 | set_transient( self::VALIDITY_TRANSIENT, 1, self::FAILURE_RETRY_TTL ); |
| 60 | |
| 61 | return is_array( $existing ) ? $existing : array(); |
| 62 | } |
| 63 | |
| 64 | set_transient( self::VALUE_TRANSIENT, $placeholders, YEAR_IN_SECONDS ); |
| 65 | set_transient( self::VALIDITY_TRANSIENT, 1, self::VALIDITY_TTL ); |
| 66 | |
| 67 | return $placeholders; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Proxy the request to WPCOM as blog. |
| 72 | * |
| 73 | * @return array|\WP_Error |
| 74 | */ |
| 75 | public static function fetch_from_wpcom() { |
| 76 | $proxy = new Proxy_Requests( 'publicize/message-templates/placeholders' ); |
| 77 | $request = new WP_REST_Request( 'GET' ); |
| 78 | |
| 79 | return $proxy->proxy_request_to_wpcom_as_blog( $request ); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Read and reshape the catalogue directly from WPCOM's helper. WPCOM Simple only. |
| 84 | * |
| 85 | * @return array<int, array{id: string, label: string}> |
| 86 | */ |
| 87 | public static function wpcom_get_placeholders() { |
| 88 | Publicize_Utils::assert_is_wpcom( __METHOD__ ); |
| 89 | |
| 90 | require_lib( 'publicize/util/message-templates' ); |
| 91 | |
| 92 | $placeholders = array(); |
| 93 | foreach ( \Publicize\get_supported_placeholders() as $id => $entry ) { |
| 94 | $placeholders[] = array( |
| 95 | 'id' => $id, |
| 96 | 'label' => $entry['title'], |
| 97 | ); |
| 98 | } |
| 99 | |
| 100 | return $placeholders; |
| 101 | } |
| 102 | } |