Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add constant for MICROSOFT_AZURE_OVERRIDE_CONTAINER_PATH #240

Merged
merged 6 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 "/"
rickalee marked this conversation as resolved.
Show resolved Hide resolved
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
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
Loading