PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-beta
Jetpack – WP Security, Backup, Speed, & Growth v16.3-beta
16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 All 507 releases
jetpack / jetpack_vendor / automattic / jetpack-publicize / src / class-message-templates-placeholders.php

class-message-templates-placeholders.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-beta, at jetpack_vendor/automattic/jetpack-publicize/src/class-message-templates-placeholders.php

103 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }
103