*/ public static function defaults(): array { $urls = array(); $urls[] = array( 'url' => home_url( '/' ), 'label' => __( 'Home', 'bloatwright' ), 'admin' => false, ); $post = get_posts( array( 'numberposts' => 1, 'post_status' => 'publish', 'post_type' => 'post', ) ); if ( $post ) { $urls[] = array( 'url' => (string) get_permalink( $post[0] ), 'label' => (string) $post[0]->post_title, 'admin' => false, ); } $page = get_posts( array( 'numberposts' => 1, 'post_status' => 'publish', 'post_type' => 'page', 'orderby' => 'menu_order', 'order' => 'ASC', ) ); if ( $page ) { $urls[] = array( 'url' => (string) get_permalink( $page[0] ), 'label' => (string) $page[0]->post_title, 'admin' => false, ); } if ( function_exists( 'wc_get_page_id' ) ) { $shop = (int) wc_get_page_id( 'shop' ); if ( $shop > 0 ) { $urls[] = array( 'url' => (string) get_permalink( $shop ), 'label' => __( 'Shop', 'bloatwright' ), 'admin' => false, ); } $product = get_posts( array( 'numberposts' => 1, 'post_status' => 'publish', 'post_type' => 'product', ) ); if ( $product ) { $urls[] = array( 'url' => (string) get_permalink( $product[0] ), 'label' => (string) $product[0]->post_title, 'admin' => false, ); } } $urls[] = array( 'url' => self::admin( 'index.php' ), 'label' => __( 'Dashboard', 'bloatwright' ), 'admin' => true, ); $urls[] = array( 'url' => self::admin( 'edit.php' ), 'label' => __( 'Posts list', 'bloatwright' ), 'admin' => true, ); $configured = (array) Settings::get( 'urls', array() ); if ( $configured ) { $urls = array_map( array( __CLASS__, 'describe' ), $configured ); } return (array) apply_filters( 'bloatwright_urls_default', $urls ); } /** * Describe a raw URL. * * @param string $url URL. * @return array{url:string,label:string,admin:bool} */ public static function describe( string $url ): array { $is_admin = false !== strpos( $url, '/wp-admin/' ); $path = (string) wp_parse_url( $url, PHP_URL_PATH ); $label = '' === $path || '/' === $path ? __( 'Home', 'bloatwright' ) : trim( $path, '/' ); if ( $is_admin ) { $label = 'wp-admin/' . basename( $path ); } return array( 'url' => $url, 'label' => $label, 'admin' => $is_admin, ); } /** * Only allow URLs on this site. Front-end URLs must be on the site's host; admin URLs (requested * with login cookies) must have exactly the scheme, host and port of the site's admin. * * @param string[] $urls URLs. * @return string[] */ public static function sanitize( array $urls ): array { $host = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); $admin = self::admin_origin(); $out = array(); foreach ( $urls as $u ) { $u = esc_url_raw( trim( (string) $u ) ); if ( '' === $u ) { continue; } $relative = 0 === strpos( $u, '/' ); if ( $relative ) { $u = home_url( $u ); } if ( Loopback::is_admin_url( $u ) ) { if ( $relative && self::admin_is_https() ) { $u = set_url_scheme( $u, 'https' ); } if ( self::origin( $u ) !== $admin ) { continue; } } elseif ( strtolower( (string) wp_parse_url( $u, PHP_URL_HOST ) ) !== $host ) { continue; } $out[] = $u; } return array_values( array_unique( array_slice( $out, 0, 20 ) ) ); } /** * "scheme://host:port" of an http(s) URL, lower-case and with the default port filled in; '' for * anything else. * * @param string $url URL. */ public static function origin( string $url ): string { $parts = wp_parse_url( $url ); if ( ! is_array( $parts ) || empty( $parts['host'] ) ) { return ''; } $scheme = strtolower( (string) ( $parts['scheme'] ?? '' ) ); if ( ! in_array( $scheme, array( 'http', 'https' ), true ) ) { return ''; } $port = isset( $parts['port'] ) ? (int) $parts['port'] : ( 'https' === $scheme ? 443 : 80 ); return $scheme . '://' . strtolower( (string) $parts['host'] ) . ':' . $port; } /** * Is the site's admin served over https? True with FORCE_SSL_ADMIN, an https admin URL or an * https site address, even when the current request (WP-CLI, a plain-http request) is not https. */ public static function admin_is_https(): bool { return force_ssl_admin() || 'https' === strtolower( (string) wp_parse_url( admin_url(), PHP_URL_SCHEME ) ) || 'https' === strtolower( (string) wp_parse_url( (string) get_option( 'siteurl' ), PHP_URL_SCHEME ) ); } /** * An admin URL with the admin's real scheme (see admin_is_https()). * * @param string $path Path relative to wp-admin/. */ public static function admin( string $path = '' ): string { $url = admin_url( $path ); return self::admin_is_https() ? set_url_scheme( $url, 'https' ) : $url; } /** * Origin of the site's admin (scheme, host and port of admin_url(), https when the admin is). */ public static function admin_origin(): string { return self::origin( self::admin() ); } /** * Origins that may receive login cookies: the admin's, the site address' and the home URL's. * When the admin is https, no http origin is allowed. * * @return string[] */ public static function login_origins(): array { $https = self::admin_is_https(); $out = array(); foreach ( array( self::admin_origin(), self::origin( site_url() ), self::origin( home_url() ) ) as $origin ) { if ( '' === $origin || ( $https && 0 !== strpos( $origin, 'https://' ) ) ) { continue; } $out[] = $origin; } return array_values( array_unique( $out ) ); } }