Skip to content

Commit

Permalink
Merge pull request #240 from 10up/hotfix/add_storage_container_overri…
Browse files Browse the repository at this point in the history
…de_path

Add constant for MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH
  • Loading branch information
jeffpaul authored Jul 11, 2024
2 parents faeedf0 + 62ce58b commit d4825ed
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions includes/class-windows-azure-helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
35 changes: 35 additions & 0 deletions windows-azure-storage-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
}
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -331,6 +345,27 @@ function windows_azure_storage_setting_storage_container() {
echo '</p>';
}

/**
* 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 '<input type="text" class="regular-text" value="', esc_attr( $azure_storage_override_container_path ), '" readonly disabled>';
} else {
echo '<input type="text" name="azure_storage_override_container_path" class="regular-text" value="', esc_attr( $azure_storage_override_container_path ), '">';
}

echo '<p>';
_e( 'Use this option if you do not like to display container name in the image URLs like <code>http://mydomain.com/uploads</code> instead of <code>http://mydomain.com/[container_name]/</code>. 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 <code>MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH</code> constant to override it.', 'windows-azure-storage' );
echo '</p>';
}

/**
* CNAME setting callback function.
*
Expand Down
6 changes: 3 additions & 3 deletions windows-azure-storage-util.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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();
Expand Down

0 comments on commit d4825ed

Please sign in to comment.