| @@ -29,8 +29,17 @@ | ||
| 29 | 29 | * @author Grégory Viguier |
| 30 | 30 | */ |
| 31 | 31 | const PATTERN_DELIMITER = '@'; |
| 32 | 32 | |
| 33 | + /** | |
| 34 | + * Cached site root URLs, keyed by blog ID. | |
| 35 | + * | |
| 36 | + * @since 2.3.1 | |
| 37 | + * | |
| 38 | + * @var array<int,string> | |
| 39 | + */ | |
| 40 | + private $site_root_urls = []; | |
| 41 | + | |
| 33 | 42 | /** ----------------------------------------------------------------------------------------- */ |
| 34 | 43 | /** INSTANCIATION =========================================================================== */ |
| 35 | 44 | /** ----------------------------------------------------------------------------------------- */ |
| 36 | 45 | |
| @@ -825,8 +834,9 @@ | ||
| 825 | 834 | /** |
| 826 | 835 | * Get the URL of the site's root. It corresponds to the main site's home page URL. |
| 827 | 836 | * |
| 828 | 837 | * @since 1.8.1 |
| 838 | + * @since 2.3.1 Added the imagify_site_root_url filter. | |
| 829 | 839 | * @access public |
| 830 | 840 | * @author Grégory Viguier |
| 831 | 841 | * |
| 832 | 842 | * @return string |
| @@ -831,37 +841,56 @@ | ||
| 831 | 841 | * |
| 832 | 842 | * @return string |
| 833 | 843 | */ |
| 834 | 844 | public function get_site_root_url() { |
| 835 | - static $root_url; | |
| 845 | + $blog_id = get_current_blog_id(); | |
| 836 | 846 | |
| 837 | - if ( isset( $root_url ) ) { | |
| 838 | - return $root_url; | |
| 847 | + if ( isset( $this->site_root_urls[ $blog_id ] ) ) { | |
| 848 | + return $this->site_root_urls[ $blog_id ]; | |
| 839 | 849 | } |
| 840 | 850 | |
| 841 | - if ( ! is_multisite() || is_main_site() ) { | |
| 842 | - $root_url = home_url( '/' ); | |
| 843 | - return $root_url; | |
| 844 | - } | |
| 851 | + $root_url = home_url( '/' ); | |
| 845 | 852 | |
| 846 | - $current_network = false; | |
| 853 | + if ( is_multisite() && ! is_main_site() ) { | |
| 854 | + $current_network = false; | |
| 847 | 855 | |
| 848 | - if ( function_exists( 'get_network' ) ) { | |
| 849 | - $current_network = get_network(); | |
| 850 | - } elseif ( function_exists( 'get_current_site' ) ) { | |
| 851 | - $current_network = get_current_site(); | |
| 856 | + if ( function_exists( 'get_network' ) ) { | |
| 857 | + $current_network = get_network(); | |
| 858 | + } elseif ( function_exists( 'get_current_site' ) ) { | |
| 859 | + $current_network = get_current_site(); | |
| 860 | + } | |
| 861 | + | |
| 862 | + if ( $current_network ) { | |
| 863 | + $scheme = is_ssl() ? 'https' : 'http'; | |
| 864 | + $root_url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $scheme ); | |
| 865 | + $root_url = trailingslashit( $root_url ); | |
| 866 | + } | |
| 852 | 867 | } |
| 853 | 868 | |
| 854 | - if ( ! $current_network ) { | |
| 855 | - $root_url = home_url( '/' ); | |
| 856 | - return $root_url; | |
| 857 | - } | |
| 869 | + /** | |
| 870 | + * Filters the URL of the site's root. | |
| 871 | + * | |
| 872 | + * On a multisite, this URL is built from the current network's domain and path. Sites using | |
| 873 | + * domain mapping are served from a domain that does not match it, which prevents Imagify from | |
| 874 | + * recognizing their image URLs as internal. Use this filter to return the URL actually used to | |
| 875 | + * serve the current site. | |
| 876 | + * | |
| 877 | + * The returned value is cached per blog for the rest of the request: callbacks must be | |
| 878 | + * deterministic for a given blog ID. | |
| 879 | + * | |
| 880 | + * @since 2.3.1 | |
| 881 | + * | |
| 882 | + * @param string $root_url URL of the site's root, with a trailing slash. | |
| 883 | + * @param int $blog_id ID of the blog the URL was built for. | |
| 884 | + */ | |
| 885 | + $filtered_url = apply_filters( 'imagify_site_root_url', $root_url, $blog_id ); | |
| 858 | 886 | |
| 859 | - $root_url = is_ssl() ? 'https' : 'http'; | |
| 860 | - $root_url = set_url_scheme( 'http://' . $current_network->domain . $current_network->path, $root_url ); | |
| 861 | - $root_url = trailingslashit( $root_url ); | |
| 887 | + // Ignore anything a callback returns that is not a usable URL, rather than casting it blindly. | |
| 888 | + $this->site_root_urls[ $blog_id ] = is_string( $filtered_url ) && '' !== trim( $filtered_url ) | |
| 889 | + ? trailingslashit( trim( $filtered_url ) ) | |
| 890 | + : home_url( '/' ); | |
| 862 | 891 | |
| 863 | - return $root_url; | |
| 892 | + return $this->site_root_urls[ $blog_id ]; | |
| 864 | 893 | } |
| 865 | 894 | |
| 866 | 895 | /** |
| 867 | 896 | * Tell if a path is the site's root. |