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4 11.2 11.2.1 11.2.2 11.3 11.3.1 11.3.2 11.3.3 11.3.4 11.4 11.4.1 11.4.2 11.5 11.5.1 11.5.2 11.5.3 11.6 11.6.1 11.6.2 11.7 11.7.1 11.7.2 11.7.3 11.8 11.8.3 11.8.4 11.8.5 11.8.6 11.9 11.9.1 11.9.2 11.9.3 12.0 12.0.1 12.0.2 12.1 12.1.1 12.1.2 12.2 12.2.1 12.2.2 12.3 12.3.1 12.4 12.4.1 12.5 12.5.1 12.6 12.6.1 12.6.2 12.6.3 12.7 12.7.1 12.7.2 12.8 12.8.1 12.8.2 12.9 12.9.1 12.9.2 12.9.3 12.9.4 13.0 13.0.1 13.1 13.1.1 13.1.2 13.1.3 13.1.4 13.2 13.2.1 13.2.2 13.2.3 13.3 13.3.1 13.3.2 13.4 13.4.1 13.4.2 13.4.3 13.4.4 13.5 13.5.1 13.6 13.6.1 13.7 13.7.1 13.8 13.8.1 13.8.2 13.9 13.9.1 14.0 14.1 14.2 14.2.1 14.3 14.4 14.4.1 14.5 14.6 14.7 14.8 14.9 14.9.1 15.0 15.0.1 15.0.2 15.1 15.1.1 15.2 15.3 15.3.1 15.4 15.5 15.6 15.7 15.7-a.1 15.7-a.3 15.7-a.5 15.7-a.7 15.7-beta
jetpack / jetpack_vendor / automattic / jetpack-videopress / src / class-initial-state.php
jetpack / jetpack_vendor / automattic / jetpack-videopress / src Last commit date
js 2 months ago tus 1 month ago videopress-divi 2 months ago videopress-divi-5 2 months ago class-access-control.php 5 days ago class-admin-ui.php 2 weeks ago class-ajax.php 1 month ago class-attachment-handler.php 1 month ago class-block-editor-content.php 1 month ago class-block-editor-extensions.php 2 weeks ago class-block-replacement.php 9 months ago class-caption-tracks.php 1 month ago class-data.php 1 month ago class-divi.php 2 months ago class-initial-state.php 2 weeks ago class-initializer.php 5 days ago class-jwt-token-bridge.php 2 years ago class-module-control.php 3 years ago class-options.php 2 years ago class-package-version.php 5 days ago class-plan.php 2 years ago class-rest-controller.php 1 month ago class-site.php 3 years ago class-stats.php 1 year ago class-status.php 9 months ago class-upload-exception.php 3 years ago class-uploader-rest-endpoints.php 2 weeks ago class-uploader.php 1 month ago class-utils.php 3 months ago class-video-block-email-renderer.php 2 months ago class-videopress-rest-api-v1-features.php 6 months ago class-videopress-rest-api-v1-settings.php 1 month ago class-videopress-rest-api-v1-site.php 2 weeks ago class-videopress-rest-api-v1-stats.php 3 years ago class-videopresstoken.php 1 month ago class-wpcom-rest-api-v2-attachment-field-videopress.php 9 months ago class-wpcom-rest-api-v2-attachment-videopress-data.php 1 month ago class-wpcom-rest-api-v2-endpoint-videopress-caption-tracks.php 1 month ago class-wpcom-rest-api-v2-endpoint-videopress.php 2 weeks ago class-xmlrpc.php 1 month ago utility-functions.php 4 weeks ago
class-initial-state.php
238 lines
1 <?php
2 /**
3 * The modernized VideoPress dashboard React initial state.
4 *
5 * @package automattic/jetpack-videopress
6 */
7
8 namespace Automattic\Jetpack\VideoPress;
9
10 use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
11 use Automattic\Jetpack\Connection\Manager as Connection_Manager;
12 use Automattic\Jetpack\My_Jetpack\Product;
13 use Automattic\Jetpack\My_Jetpack\Products as My_Jetpack_Products;
14 use Automattic\Jetpack\Status;
15 use Automattic\Jetpack\Status\Host;
16 use Jetpack_Options;
17 use function admin_url;
18 use function esc_url_raw;
19 use function get_bloginfo;
20 use function get_locale;
21 use function get_option;
22 use function get_site_url;
23 use function plugins_url;
24 use function rest_url;
25 use function wp_add_inline_script;
26 use function wp_create_nonce;
27 use function wp_json_encode;
28 use function wp_parse_url;
29 use function wp_script_is;
30
31 /**
32 * The modernized VideoPress dashboard React initial state.
33 *
34 * Mirrors the Activity Log dashboard pattern: a small structured payload
35 * inlined as `var JPVIDEOPRESS_INITIAL_STATE=...` before the wp-build boot
36 * script runs. Phases 6 and 8 will consume the payload via per-file
37 * `declare const` ambient types.
38 */
39 class Initial_State {
40
41 const SCRIPT_HANDLE = 'jetpack-videopress-dashboard-wp-admin-prerequisites';
42
43 /**
44 * WPCOM product slug purchased by the dashboard's upgrade CTA.
45 *
46 * Mirrors the product `Plan::get_product()` resolves to
47 * (`$products->jetpack_videopress`). Kept as a constant rather than read
48 * from `Plan::get_product()` because that helper issues a synchronous
49 * WPCOM request, which we don't want to incur on every page render.
50 */
51 const VIDEOPRESS_PRODUCT_SLUG = 'jetpack_videopress';
52
53 /**
54 * Register the inline-state enqueue hook.
55 *
56 * Hooks `admin_enqueue_scripts` at priority 11 so the wp-build page
57 * (`build/pages/jetpack-videopress-dashboard/page-wp-admin.php`) has
58 * already registered the prerequisites handle at its default priority 10.
59 * The `wp_script_is( ..., 'registered' )` check inside the callback
60 * doubles as the page guard.
61 *
62 * @return void
63 */
64 public static function init() {
65 add_action( 'admin_enqueue_scripts', array( __CLASS__, 'enqueue' ), 11 );
66 }
67
68 /**
69 * Inline the initial-state payload before the wp-build boot script.
70 *
71 * @return void
72 */
73 public static function enqueue() {
74 if ( ! Admin_UI::is_modernized() ) {
75 return;
76 }
77
78 if ( ! wp_script_is( self::SCRIPT_HANDLE, 'registered' ) ) {
79 return;
80 }
81
82 wp_add_inline_script( self::SCRIPT_HANDLE, ( new self() )->render(), 'before' );
83
84 // Hydrate the JP connection store (`window.JP_CONNECTION_INITIAL_STATE`)
85 // so shared connection-aware hooks — notably
86 // `useProductCheckoutWorkflow`, which powers the upgrade CTA — work on
87 // the modernized dashboard exactly as they do on the legacy one. This
88 // also sets `window.jpTracksContext.blog_id` for `@automattic/jetpack-analytics`.
89 Connection_Initial_State::render_script( self::SCRIPT_HANDLE );
90 }
91
92 /**
93 * Get the initial state data.
94 *
95 * @return array
96 */
97 private function get_data() {
98 $gmt_offset = get_option( 'gmt_offset' );
99 $timezone_string = get_option( 'timezone_string' );
100 $home_host = wp_parse_url( get_site_url(), PHP_URL_HOST );
101
102 return array(
103 'API' => array(
104 'WP_API_root' => esc_url_raw( rest_url() ),
105 'WP_API_nonce' => wp_create_nonce( 'wp_rest' ),
106 'contentNonce' => wp_create_nonce( 'videopress-content-nonce' ),
107 ),
108 'jetpackStatus' => array(
109 'calypsoSlug' => ( new Status() )->get_site_suffix(),
110 ),
111 'product' => array(
112 // Fed to `useProductCheckoutWorkflow` as the product to purchase.
113 'slug' => self::VIDEOPRESS_PRODUCT_SLUG,
114 ),
115 'siteData' => array(
116 'id' => Jetpack_Options::get_option( 'id' ),
117 'title' => get_bloginfo( 'name' ) ? get_bloginfo( 'name' ) : get_site_url(),
118 'adminUrl' => esc_url_raw( admin_url() ),
119 'slug' => is_string( $home_host ) ? $home_host : '',
120 'gmtOffset' => is_numeric( $gmt_offset ) ? (float) $gmt_offset : 0.0,
121 'timezoneString' => is_string( $timezone_string ) ? $timezone_string : '',
122 'locale' => str_replace( '_', '-', (string) get_locale() ),
123 // Paid-tier capability check. Drives the free-tier UX (callout /
124 // DataViews configuration) once designer input lands. Backed by
125 // `Product::get_site_features_from_wpcom()`, which caches the WPCOM
126 // `/sites/%d/features` response in a 15-second site transient. On a
127 // cache miss this issues a synchronous WPCOM request that blocks
128 // page rendering until it returns.
129 'hasVideoPressAccess' => self::has_videopress_access(),
130 'isVideoPress1TB' => self::has_videopress_feature( 'videopress-1tb-storage' ),
131 'isVideoPressUnlimited' => self::has_videopress_feature( 'videopress-unlimited-storage' ),
132 ),
133 'assets' => array(
134 'buildUrl' => plugins_url( '../build/', __FILE__ ),
135 ),
136 // Feature gates mirrored from the PHP-side filters so the client can
137 // hide gated UI without a round trip.
138 'features' => array(
139 'chaptersEditor' => Admin_UI::is_chapters_editor_enabled(),
140 ),
141 // Authoritative map of accepted upload types (extension => mimetype),
142 // so the dashboard's drag-and-drop filter accepts exactly what the
143 // VideoPress backend supports rather than guessing client-side.
144 'allowedVideoExtensions' => Admin_UI::get_allowed_video_extensions(),
145 // Product/pricing payload for the pre-connection upsell. Only
146 // populated when the site isn't connected — the only time the
147 // connection gate renders the upsell — so connected dashboards never
148 // incur the synchronous WPCOM pricing request `get_pricing_data()`
149 // makes. Skipped on WordPress.com Simple, where the connection gate is
150 // bypassed (Simple sites are inherently wpcom-connected).
151 'pricing' => ( ( new Host() )->is_wpcom_simple() || ( new Connection_Manager() )->is_connected() ) ? null : $this->get_pricing_data(),
152 );
153 }
154
155 /**
156 * Product description, feature list, and yearly price for the
157 * pre-connection upsell (a port of the legacy dashboard's pricing table).
158 *
159 * Backed by `Plan::get_product_price()`, which issues a synchronous WPCOM
160 * request, so this only runs for disconnected sites. Returns null when the
161 * product or price data isn't available (e.g. the WPCOM request fails), in
162 * which case the gate falls back to the plain connect screen.
163 *
164 * @return array|null The upsell payload, or null when it can't be built.
165 */
166 private function get_pricing_data() {
167 $site_product = My_Jetpack_Products::get_product( 'videopress' );
168 $product_price = Plan::get_product_price();
169
170 // Plan::get_product_price() returns a WP_Error when the WPCOM products
171 // request doesn't come back 200, and `isset()` on a non-ArrayAccess
172 // object is a fatal in PHP 8 — so the array check has to come first.
173 if ( ! is_array( $site_product ) || ! is_array( $product_price ) || ! isset( $product_price['yearly'] ) ) {
174 return null;
175 }
176
177 return array(
178 'title' => isset( $site_product['description'] ) ? (string) $site_product['description'] : '',
179 'features' => isset( $site_product['features'] ) ? array_values( (array) $site_product['features'] ) : array(),
180 'yearly' => $product_price['yearly'],
181 );
182 }
183
184 /**
185 * Whether the site has any paid VideoPress feature flag active.
186 *
187 * Matches the active-features check used by the existing
188 * `videopress/v1/features` REST endpoint: any of the storage tiers
189 * counts as paid access. On WPCOM the legacy `videopress` slug also
190 * grants access.
191 *
192 * @return bool
193 */
194 public static function has_videopress_access() {
195 // Any paid storage tier grants access; on the WPCOM platform (Simple or
196 // Atomic) the legacy `videopress` slug does too. No Simple special-case is
197 // needed here: each has_videopress_feature() call already routes through the
198 // Simple-local wpcom_site_has_feature() check under IS_WPCOM, and
199 // is_wpcom_platform() is true on Simple, so the third term reduces to
200 // wpcom_site_has_feature( 'videopress' ) there — the check the old early
201 // return performed, minus the redundancy.
202 return self::has_videopress_feature( 'videopress-1tb-storage' )
203 || self::has_videopress_feature( 'videopress-unlimited-storage' )
204 || ( ( new Host() )->is_wpcom_platform() && self::has_videopress_feature( 'videopress' ) );
205 }
206
207 /**
208 * Whether the named feature appears in the WPCOM active-features list.
209 *
210 * @param string $feature_slug Feature slug as returned by WPCOM (e.g. `videopress-1tb-storage`).
211 * @return bool
212 */
213 private static function has_videopress_feature( $feature_slug ) {
214 if ( ( new Host() )->is_wpcom_simple() ) {
215 return function_exists( 'wpcom_site_has_feature' ) && (bool) wpcom_site_has_feature( $feature_slug );
216 }
217
218 $features = Product::get_site_features_from_wpcom();
219
220 if ( is_wp_error( $features ) ) {
221 return false;
222 }
223
224 $active = $features['active'] ?? array();
225
226 return in_array( $feature_slug, $active, true );
227 }
228
229 /**
230 * Render the initial state into a JavaScript variable.
231 *
232 * @return string
233 */
234 public function render() {
235 return 'var JPVIDEOPRESS_INITIAL_STATE=' . wp_json_encode( $this->get_data(), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';';
236 }
237 }
238