Skip to content

Commit ba6ae4f

Browse files
author
Md. Alimuzzaman Alim
authored
Merge pull request #282 from bbiHQ/v2.1
Added 'Force SSL' option for Custom CDN Domains that is served behind services like CloudFlare
2 parents 40d2745 + ea2f78d commit ba6ae4f

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

lib/classes/class-bootstrap.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ public function get_gs_host($sm = array()) {
284284
$sm = $sm?$sm: $this->get( 'sm');
285285
$image_host = 'https://storage.googleapis.com/';
286286
$custom_domain = $sm['custom_domain'];
287-
$is_ssl = strpos($custom_domain, 'https://');
287+
$is_ssl = $sm['force_ssl'] === "1";
288288
$custom_domain = str_replace('https://', '', $custom_domain);
289289
$custom_domain = str_replace('http://', '', $custom_domain);
290290
if ( $sm['bucket'] && $custom_domain == $sm['bucket']) {
291-
$image_host = $is_ssl === 0 ? 'https://' : 'http://'; // bucketname will be host
291+
$image_host = $is_ssl ? 'https://' : 'http://'; // bucketname will be host
292292
}
293293
return apply_filters( 'get_gs_host', $image_host . $sm['bucket'], $image_host, $sm['bucket'], $is_ssl );
294294
}
@@ -303,7 +303,8 @@ public function get_gs_host($sm = array()) {
303303
public function wp_stateless_bucket_link($fileLink) {
304304
$bucketname = $this->get( 'sm.bucket' );
305305
if ( strpos($fileLink, $bucketname) > 8) {
306-
$fileLink = 'http://' . substr($fileLink, strpos($fileLink, $bucketname));
306+
$sm = $sm?$sm: $this->get( 'sm');
307+
$fileLink = ($sm['force_ssl'] === '1' ? 'https://': 'http://') . substr($fileLink, strpos($fileLink, $bucketname));
307308
}
308309
return $fileLink;
309310
}
@@ -345,6 +346,7 @@ public function get_settings($settings) {
345346
'cache_control',
346347
'delete_remote',
347348
'custom_domain',
349+
'force_ssl',
348350
'organize_media',
349351
'hashify_file_name'
350352
);

lib/classes/class-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ final class Settings extends \UsabilityDynamics\Settings {
3030
'cache_control' => array('WP_STATELESS_MEDIA_CACHE_CONTROL', ''),
3131
'delete_remote' => array('WP_STATELESS_MEDIA_DELETE_REMOTE', 'true'),
3232
'custom_domain' => array('WP_STATELESS_MEDIA_CUSTOM_DOMAIN', ''),
33+
'force_ssl' => array('', 'false'),
3334
'organize_media' => array('', 'true'),
3435
'hashify_file_name' => array('WP_STATELESS_MEDIA_HASH_FILENAME', 'true'),
3536
);

static/scripts/wp-stateless.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,12 +641,13 @@ var wpStatelessApp = angular.module('wpStatelessApp', [])
641641

642642
$scope.sm.generatePreviewUrl = function() {
643643
var host = 'https://storage.googleapis.com/';
644-
var is_ssl = $scope.sm.custom_domain.indexOf('https://');
644+
var is_ssl = $scope.sm.force_ssl === "1";
645645
var custom_domain = $scope.sm.custom_domain;
646646
custom_domain = custom_domain.replace('https://', '');
647647
custom_domain = custom_domain.replace('http://', '');
648+
$scope.sm.custom_domain = custom_domain;
648649
if ( $scope.sm.bucket && custom_domain == $scope.sm.bucket) {
649-
host = is_ssl === 0 ? 'https://' : 'http://'; // bucketname will be host
650+
host = is_ssl ? 'https://' : 'http://'; // bucketname will be host
650651
}
651652
host += $scope.sm.bucket ? $scope.sm.bucket : '{bucket-name}';
652653
var rootdir = $scope.sm.root_dir ? $scope.sm.root_dir + '/' : '';

static/views/settings_interface.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,24 @@
179179
</p>
180180
<p class="description">
181181
<strong ng-bind="sm.showNotice('custom_domain')" ></strong> <br>
182-
<?php printf(__( 'Replace the default GCS domain with your own custom domain. This will require you to <a href="%s" target="_blank">configure a CNAME</a>. Be advised that the bucket name and domain name must match exactly and HTTPS is not supported with a custom domain.', ud_get_stateless_media()->domain ), 'https://cloud.google.com/storage/docs/xml-api/reference-uris#cname'); ?>
182+
<?php printf(__( 'Replace the default GCS domain with your own custom domain. This will require you to <a href="%s" target="_blank">configure a CNAME</a>. Be advised that the bucket name and domain name must match exactly and HTTPS is not supported with a custom domain unless it is routed via other CDN services like <a href="https://cloudflare.com" target="_blank">CloudFlare</a>.', ud_get_stateless_media()->domain ), 'https://cloud.google.com/storage/docs/xml-api/reference-uris#cname'); ?>
183183
</p>
184+
185+
<h4><?php _e( 'Force SSL', ud_get_stateless_media()->domain ); ?></h4>
186+
<p>
187+
<select id="org_url_grp" name="sm[force_ssl]" ng-model="sm.force_ssl" ng-change="sm.generatePreviewUrl()" ng-disabled="sm.readonly.organize_media">
188+
<?php if(is_network_admin()): ?>
189+
<option value=""><?php _e( 'Don\'t override', ud_get_stateless_media()->domain ); ?></option>
190+
<?php endif; ?>
191+
<option value="1"><?php _e( 'Enable', ud_get_stateless_media()->domain ); ?></option>
192+
<option value=""><?php _e( 'Disable', ud_get_stateless_media()->domain ); ?></option>
193+
</select>
194+
</p>
195+
<p class="description">
196+
<?php printf(__( 'This will force SSL (HTTPS) to be used on your custom CDN domain, which is not supported by GCS by default. You will need to routed the custom CDN domain via other services like CloudFlare to <a href="%s" target="_blank">inject the required SSL encryption layer</a> (available in their free plan). <br/><br/>Note: To enforce SSL on items uploaded previously, you might need to use the features in the Sync tab to perform a resync.', ud_get_stateless_media()->domain ), 'https://www.cloudflare.com/ssl/'); ?>
197+
</p>
198+
199+
184200
<hr>
185201

186202
<?php if(!is_network_admin()): ?>

0 commit comments

Comments
 (0)