Skip to content

Commit 74bfc5d

Browse files
committed
Cloudflare account specific subdomain now fetched and offered for use with video embeds.
1 parent 6f44014 commit 74bfc5d

3 files changed

Lines changed: 54 additions & 12 deletions

File tree

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Changes from Official
2828
* If updating from version older than 1.0.6, you'll need to enter your Cloudflare API Token and Cloudflare Account ID in the configuration page.
2929
* Added admin setting for signed URL/token duration (default is otherwise 1 hour).
3030
* Added admin toggle for whether or not to use signed URLs/tokens.
31+
* Can select Cloudflare media domain, including new account specific sub-domain.
3132

3233

3334
To-Do
@@ -70,6 +71,9 @@ When this is checked [x], videos are accessed using a temporary time-limited tok
7071
### Signed URL Expiration ###
7172
When **Use Signed URLs** is checked [x], this setting controls how long any particular token / signed ULR is valid for **in minutes**. The Cloudflare default, is 60 minutes. Generally, you'd want to make sure this is larger than your longest video.
7273

74+
### Preferred Media Domain ###
75+
This option allows you to select from a small list of different Cloudflare media domains. This domain is used when delivering content to your users. The 3rd option is a unique subdomain specific to your Cloudflare account. This option will only be presented if you have at least one video already uploaded to your Cloudflare Stream account.
76+
7377

7478
Securing Video Access
7579
------------

src/inc/class-cloudflare-stream-api.php

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,16 +250,21 @@ public function update_video_details( $uid, $args = array(), $return_headers = f
250250
public function get_video_embed( $uid, $args = array(), $return_headers = false ) {
251251
$media_domain = get_option( Cloudflare_Stream_Settings::OPTION_MEDIA_DOMAIN );
252252
$signed_urls = get_option( Cloudflare_Stream_Settings::OPTION_SIGNED_URLS );
253-
if ( $signed_urls ) {
254-
$uid = $this->get_signed_video_token($uid)->result->token;
255-
}
253+
$uid = ( $signed_urls ) ? $this->get_signed_video_token($uid)->result->token : $uid;
254+
255+
$standard_uri = ' src="https://iframe.' . $media_domain . '/' . $uid . '?';
256+
$account_subdomain_uri = ' src="https://' . $media_domain . '/' . $uid . '/iframe?';
257+
258+
$src_uri = ( $media_domain == "cloudflarestream.com" || $media_domain == "videodelivery.net" ) ? $standard_uri : $account_subdomain_uri;
259+
256260
$video_embed = '<div style="position: relative; padding-top: 56.25%"><iframe'
257-
. ' src="https://iframe.' . $media_domain . '/' . $uid . '?'
258-
. 'muted=' . $args['muted'] . '&'
259-
. 'preload=' . $args['preload'] . '&'
260-
. 'loop=' . $args['loop'] . '&'
261-
. 'autoplay=' . $args['autoplay'] . '&'
262-
. 'controls=' . $args['controls'] . '" '
261+
. $src_uri
262+
. 'muted=' . $args['muted'] . '&'
263+
. 'preload=' . $args['preload'] . '&'
264+
. 'loop=' . $args['loop'] . '&'
265+
. 'autoplay=' . $args['autoplay'] . '&'
266+
. 'controls=' . $args['controls'] . '&'
267+
. 'poster=https://' . $media_domain . '/' . $uid . '/thumbnails/thumbnail.jpg" '
263268
. 'style="border: none; position: absolute; top: 0; height: 100%; width: 100%" '
264269
. 'allow="accelerometer; gyroscope; autoplay; encrypted-media; picture-in-picture;" '
265270
. 'allowfullscreen="true" '
@@ -332,14 +337,30 @@ public function delete_video( $uid, $args = array(), $return_headers = false ) {
332337
return json_decode( $response_text );
333338
}
334339

340+
/**
341+
* Retrieve unique Cloudflare account subdomain.
342+
*
343+
* @param array $args Additional API arguments.
344+
* @param bool $return_headers Return the response headers intead of the response body.
345+
* @since 1.0.9
346+
*/
347+
public function get_account_subdomain( $args = array(), $return_headers = false ) {
348+
$response_text = json_decode( $this->request( 'stream/', $args, $return_headers ) );
349+
if ( count($response_text->result) > 0 ) {
350+
$text_array = explode( "/", $response_text->result[0]->thumbnail );
351+
return $text_array[2];
352+
}
353+
return false;
354+
}
355+
335356
/**
336357
* Retrieve Cloudflare Account ID using the Zones API.
337358
*
338359
* @param bool $save If true, saves retrieved Account ID to database, but only if the option does not already exist.
339360
* @since 1.0.9
340361
*/
341362
public function get_account_id( $save = false ) {
342-
$response_text = json_decode( $this->zone_request( '', array(), false ) );
363+
$response_text = json_decode( $this->request( '', array(), false ) );
343364
if ( $response_text->success ) {
344365
$api_id = $response_text->result->account->id;
345366
if ( strlen( $api_id ) == 32 ) {

src/inc/class-cloudflare-stream-settings.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,8 +177,15 @@ public function api_signed_urls_duration_cb() {
177177
public function media_domain_cb() {
178178
$media_domain = get_option( self::OPTION_MEDIA_DOMAIN );
179179
echo '<label for="cloudflare_stream_media_domain_0"><input type="radio" class="radio-option" name="cloudflare_stream_media_domain" id="cloudflare_stream_media_domain_0" value="cloudflarestream.com" ' . checked( "cloudflarestream.com", $media_domain, false ) . ' >cloudflarestream.com (default)</label>'
180-
. '<label for="cloudflare_stream_media_domain_"><input type="radio" class="radio-option" name="cloudflare_stream_media_domain" id="cloudflare_stream_media_domain_1" value="videodelivery.net" ' . checked( "videodelivery.net", $media_domain, false ) . ' >videodelivery.net</label>'
181-
. '<small class="form-text text-muted">' . esc_html__( 'Set which Cloudflare domain is used by your users, to access video content. Changing this may require an update to your sites Content Security Policy.', 'cloudflare-stream-wordpress' ) . '</small>';
180+
. '<label for="cloudflare_stream_media_domain_"><input type="radio" class="radio-option" name="cloudflare_stream_media_domain" id="cloudflare_stream_media_domain_1" value="videodelivery.net" ' . checked( "videodelivery.net", $media_domain, false ) . ' >videodelivery.net</label>';
181+
182+
// The account subdomain option is only presented if it was able to be retrieved from the API.
183+
$account_subdomain = self::get_account_subdomain();
184+
if ( $account_subdomain ) {
185+
echo '<label for="cloudflare_stream_media_domain_"><input type="radio" class="radio-option" name="cloudflare_stream_media_domain" id="cloudflare_stream_media_domain_2" value="' . $account_subdomain . '" ' . checked( $account_subdomain, $media_domain, false ) . ' >' . $account_subdomain . ' (<a href="https://community.cloudflare.com/t/upcoming-domain-change-to-ensure-delivery-of-your-video-content/405842" target="_blank">more information</a>)</label>';
186+
}
187+
188+
echo '<small class="form-text text-muted">' . esc_html__( 'Set which Cloudflare domain is used by your users, to access video content. Changing this may require an update to your sites Content Security Policy.', 'cloudflare-stream-wordpress' ) . '</small>';
182189
}
183190

184191
/**
@@ -263,6 +270,16 @@ public function onboarding_admin_notices() {
263270
}
264271
}
265272

273+
/**
274+
* Try to fetch and save the Cloudflare Account ID using Zone ID.
275+
*
276+
* @since 1.0.9
277+
*/
278+
public function get_account_subdomain() {
279+
$api = Cloudflare_Stream_API::instance();
280+
return $api->get_account_subdomain();
281+
}
282+
266283
/**
267284
* Try to fetch and save the Cloudflare Account ID using Zone ID.
268285
*

0 commit comments

Comments
 (0)