diff --git a/README.md b/README.md index 5490ef0d..a34c2bc7 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ For multisites or to enforce Azure Blob Storage settings, you can define the fol * MICROSOFT_AZURE_CONTAINER - Azure Blob Container * MICROSOFT_AZURE_CNAME - Domain: must start with http(s):// * MICROSOFT_AZURE_USE_FOR_DEFAULT_UPLOAD - boolean (default false) - +* MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH - Override Container name in the Image URL , can be just "/" See Settings->Microsoft Azure for more information. ## Upgrade Notice diff --git a/includes/class-windows-azure-helper.php b/includes/class-windows-azure-helper.php index a22fd9bc..e5b136f2 100644 --- a/includes/class-windows-azure-helper.php +++ b/includes/class-windows-azure-helper.php @@ -105,6 +105,19 @@ static public function get_account_key() { : get_option( 'azure_storage_account_primary_access_key' ); } + /** + * Return override container name. + * + * @since 4.5.0 + * + * @return mixed|void Account key. + */ + static public function get_azure_storage_override_container_path() { + return defined( 'MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH' ) + ? MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH + : get_option( 'azure_storage_override_container_path' ); + } + /** * Return CNAME url. * diff --git a/readme.txt b/readme.txt index 62caca16..64ef0e4e 100644 --- a/readme.txt +++ b/readme.txt @@ -28,6 +28,7 @@ For multisites or to enforce Azure Blob Storage settings, you can define the fol * MICROSOFT_AZURE_CONTAINER - Azure Blob Container * MICROSOFT_AZURE_CNAME - Domain: must start with http(s):// * MICROSOFT_AZURE_USE_FOR_DEFAULT_UPLOAD - boolean (default false) +* MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH - Override Container name in the Image URL , can be just "/" See Settings->Microsoft Azure for more information. diff --git a/windows-azure-storage-settings.php b/windows-azure-storage-settings.php index 57440e6e..65bfe873 100644 --- a/windows-azure-storage-settings.php +++ b/windows-azure-storage-settings.php @@ -117,6 +117,10 @@ function windows_azure_storage_plugin_register_settings() { register_setting( 'windows-azure-storage-settings-group', 'cname', 'esc_url_raw' ); } + if ( ! defined( 'MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH' ) ) { + register_setting( 'windows-azure-storage-settings-group', 'azure_storage_override_container_path', 'sanitize_text_field' ); + } + if ( ! defined( 'MICROSOFT_AZURE_USE_FOR_DEFAULT_UPLOAD' ) ) { register_setting( 'windows-azure-storage-settings-group', 'azure_storage_use_for_default_upload', 'wp_validate_boolean' ); } @@ -164,6 +168,16 @@ function windows_azure_storage_plugin_register_settings() { 'windows-azure-storage-plugin-options', 'windows-azure-storage-settings' ); + /** + * @since 4.5.0 + */ + add_settings_field( + 'azure_storage_override_container_path', + __( 'Override Container Path', 'windows-azure-storage' ), + 'windows_azure_storage_override_container_path', + 'windows-azure-storage-plugin-options', + 'windows-azure-storage-settings' + ); /** * @since 4.0.0 */ @@ -331,6 +345,27 @@ function windows_azure_storage_setting_storage_container() { echo '

'; } +/** + * Container ovveride path setting callback function. + * + * @since 4.5.0 + * + * @return void + */ +function windows_azure_storage_override_container_path() { + $azure_storage_override_container_path = Windows_Azure_Helper::get_azure_storage_override_container_path(); + + if ( defined( 'MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH' ) ) { + echo ''; + } else { + echo ''; + } + + echo '

'; + _e( 'Use this option if you do not like to display container name in the image URLs like http://mydomain.com/uploads instead of http://mydomain.com/[container_name]/. As sometime container name can be wired and log and also container names can change during migration resulting in URL change for the images. Using this option image urls will remain same. You can use MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH constant to override it.', 'windows-azure-storage' ); + echo '

'; +} + /** * CNAME setting callback function. * diff --git a/windows-azure-storage-util.php b/windows-azure-storage-util.php index 31ea1a38..3f9feee7 100644 --- a/windows-azure-storage-util.php +++ b/windows-azure-storage-util.php @@ -315,7 +315,7 @@ protected static function _maybe_rewrite_cname( $cname ) { public static function get_storage_url_base( $append_container = true ) { $azure_storage_account_name = \Windows_Azure_Helper::get_account_name(); $default_azure_storage_account_container_name = \Windows_Azure_Helper::get_default_container(); - + $azure_storage_override_container_path = \Windows_Azure_Helper::get_azure_storage_override_container_path(); /** * Filter the blob URL protocol to force a specific one. * @@ -338,10 +338,10 @@ public static function get_storage_url_base( $append_container = true ) { // Get CNAME if defined. $cname = \Windows_Azure_Helper::get_cname(); - if ( ! ( empty( $cname ) ) ) { + if ( ! empty( $cname ) ) { $url = sprintf( '%1$s/%2$s', $cname, - $append_container ? $default_azure_storage_account_container_name : '' + $append_container = ! empty( $azure_storage_override_container_path ) ? $azure_storage_override_container_path : $default_azure_storage_account_container_name ); } else { $blob_storage_host_name = \Windows_Azure_Helper::get_hostname();