From 19e872bb0565879eb075bbff8d087f4d99558981 Mon Sep 17 00:00:00 2001 From: Dan0sz <18595395+Dan0sz@users.noreply.github.com> Date: Mon, 28 Aug 2023 13:18:03 +0000 Subject: [PATCH] Improved: don't use relative URLs when storing the cache_url to the DB. --- src/Includes/Helpers.php | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Includes/Helpers.php b/src/Includes/Helpers.php index ac558e4b..7968f229 100644 --- a/src/Includes/Helpers.php +++ b/src/Includes/Helpers.php @@ -38,7 +38,8 @@ public static function get_domain() { * Get Analytics URL. * * @since 1.0.0 - * @access public + * + * @param bool $local Return the Local JS file IF proxy is enabled. * * @return string */ @@ -235,6 +236,13 @@ public static function get_proxy_resources() { $resources = get_option( 'plausible_analytics_proxy_resources', [] ); } + /** + * Force a refresh of our resources if the user recently switched to SSL and we still have non-SSL resources stored. + */ + if ( ! empty( $resources ) && is_ssl() && isset( $resources['cache_url'] ) && ( strpos( $resources['cache_url'], 'http:' ) !== false ) ) { + $resources = []; + } + if ( empty( $resources ) ) { $cache_dir = bin2hex( random_bytes( 5 ) ); $upload_dir = wp_get_upload_dir(); @@ -243,7 +251,7 @@ public static function get_proxy_resources() { 'base' => bin2hex( random_bytes( 2 ) ), 'endpoint' => bin2hex( random_bytes( 4 ) ), 'cache_dir' => trailingslashit( $upload_dir['basedir'] ) . trailingslashit( $cache_dir ), - 'cache_url' => str_replace( [ 'https:', 'http:' ], '', trailingslashit( $upload_dir['baseurl'] ) . trailingslashit( $cache_dir ) ), + 'cache_url' => trailingslashit( $upload_dir['baseurl'] ) . trailingslashit( $cache_dir ), 'file_alias' => bin2hex( random_bytes( 4 ) ), ];