markjaquith / page-links-to

#WordPressPlugin: Lets you make a WordPress page (or other content type) link to an external URL of your choosing, instead of its WordPress URL.
GNU General Public License v2.0
110 stars 46 forks source link

Error in wp-admin when setting the URL to just a forward slash #147

Open daveherman71 opened 3 years ago

daveherman71 commented 3 years ago

I have some pages on my site that I need to redirect to the home page (due to legacy SEO).

However redirecting to "/" causes an error in wp-admin.

A PHP Error was encountered

Severity: Notice

Message: Uninitialized string offset: 1

Filename: classes/plugin.php

Line Number: 799

This is the function where the error occurs:

    public static function absolute_url( $url ) {
        // Convert server- and protocol-relative URLs to absolute URLs.
        if ( '/' === $url[0] ) {
            // Protocol-relative.
            if ( '/' === $url[1] ) {
                $url = set_url_scheme( 'http:' . $url );
            } else {
                // Host-relative.
                $url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $url );
            }
        }

        if ( 'mailto' !== parse_url( $url, PHP_URL_SCHEME ) ) {
            $url = str_replace( '@', '%40', $url );
        }

        return $url;
    }

My proposed fix is to first check the length of the string before checking if it is protocol-relative, as follows:

    public static function absolute_url( $url ) {
        // Convert server- and protocol-relative URLs to absolute URLs.
        if ( '/' === $url[0] ) {
            // Protocol-relative.
            if ( strlen($url) > 1 && '/' === $url[1] ) {
                $url = set_url_scheme( 'http:' . $url );
            } else {
                // Host-relative.
                $url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $url );
            }
        }

        if ( 'mailto' !== parse_url( $url, PHP_URL_SCHEME ) ) {
            $url = str_replace( '@', '%40', $url );
        }

        return $url;
    }