Edit File by line

Deprecated: str_replace(): Passing null to parameter #2 ($replace) of type array|string is deprecated in /home/sportsfever/public_html/filemanger/function.php on line 93
/home/sportsfe.../httpdocs/wp-conte.../plugins/wordpres.../src/helpers
File: url-helper.php
<?php
[0] Fix | Delete
[1] Fix | Delete
namespace Yoast\WP\SEO\Helpers;
[2] Fix | Delete
[3] Fix | Delete
use Yoast\WP\SEO\Models\SEO_Links;
[4] Fix | Delete
[5] Fix | Delete
/**
[6] Fix | Delete
* A helper object for URLs.
[7] Fix | Delete
*/
[8] Fix | Delete
class Url_Helper {
[9] Fix | Delete
[10] Fix | Delete
/**
[11] Fix | Delete
* Retrieve home URL with proper trailing slash.
[12] Fix | Delete
*
[13] Fix | Delete
* @param string $path Path relative to home URL.
[14] Fix | Delete
* @param string|null $scheme Scheme to apply.
[15] Fix | Delete
*
[16] Fix | Delete
* @return string Home URL with optional path, appropriately slashed if not.
[17] Fix | Delete
*/
[18] Fix | Delete
public function home( $path = '', $scheme = null ) {
[19] Fix | Delete
$home_url = \home_url( $path, $scheme );
[20] Fix | Delete
[21] Fix | Delete
if ( ! empty( $path ) ) {
[22] Fix | Delete
return $home_url;
[23] Fix | Delete
}
[24] Fix | Delete
[25] Fix | Delete
$home_path = \wp_parse_url( $home_url, \PHP_URL_PATH );
[26] Fix | Delete
[27] Fix | Delete
if ( $home_path === '/' ) { // Home at site root, already slashed.
[28] Fix | Delete
return $home_url;
[29] Fix | Delete
}
[30] Fix | Delete
[31] Fix | Delete
if ( \is_null( $home_path ) ) { // Home at site root, always slash.
[32] Fix | Delete
return \trailingslashit( $home_url );
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
if ( \is_string( $home_path ) ) { // Home in subdirectory, slash if permalink structure has slash.
[36] Fix | Delete
return \user_trailingslashit( $home_url );
[37] Fix | Delete
}
[38] Fix | Delete
[39] Fix | Delete
return $home_url;
[40] Fix | Delete
}
[41] Fix | Delete
[42] Fix | Delete
/**
[43] Fix | Delete
* Determines whether the plugin is active for the entire network.
[44] Fix | Delete
*
[45] Fix | Delete
* @return bool Whether or not the plugin is network-active.
[46] Fix | Delete
*/
[47] Fix | Delete
public function is_plugin_network_active() {
[48] Fix | Delete
static $network_active = null;
[49] Fix | Delete
[50] Fix | Delete
if ( ! \is_multisite() ) {
[51] Fix | Delete
return false;
[52] Fix | Delete
}
[53] Fix | Delete
[54] Fix | Delete
// If a cached result is available, bail early.
[55] Fix | Delete
if ( $network_active !== null ) {
[56] Fix | Delete
return $network_active;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
$network_active_plugins = \wp_get_active_network_plugins();
[60] Fix | Delete
[61] Fix | Delete
// Consider MU plugins and network-activated plugins as network-active.
[62] Fix | Delete
$network_active = \strpos( \wp_normalize_path( \WPSEO_FILE ), \wp_normalize_path( \WPMU_PLUGIN_DIR ) ) === 0
[63] Fix | Delete
|| \in_array( \WP_PLUGIN_DIR . '/' . \WPSEO_BASENAME, $network_active_plugins, true );
[64] Fix | Delete
[65] Fix | Delete
return $network_active;
[66] Fix | Delete
}
[67] Fix | Delete
[68] Fix | Delete
/**
[69] Fix | Delete
* Retrieve network home URL if plugin is network-activated, or home url otherwise.
[70] Fix | Delete
*
[71] Fix | Delete
* @return string Home URL with optional path, appropriately slashed if not.
[72] Fix | Delete
*/
[73] Fix | Delete
public function network_safe_home_url() {
[74] Fix | Delete
/**
[75] Fix | Delete
* Action: 'wpseo_home_url' - Allows overriding of the home URL.
[76] Fix | Delete
*/
[77] Fix | Delete
\do_action( 'wpseo_home_url' );
[78] Fix | Delete
[79] Fix | Delete
// If the plugin is network-activated, use the network home URL.
[80] Fix | Delete
if ( self::is_plugin_network_active() ) {
[81] Fix | Delete
return \network_home_url();
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
return \home_url();
[85] Fix | Delete
}
[86] Fix | Delete
[87] Fix | Delete
/**
[88] Fix | Delete
* Check whether a url is relative.
[89] Fix | Delete
*
[90] Fix | Delete
* @param string $url URL string to check.
[91] Fix | Delete
*
[92] Fix | Delete
* @return bool True when url is relative.
[93] Fix | Delete
*/
[94] Fix | Delete
public function is_relative( $url ) {
[95] Fix | Delete
return ( \strpos( $url, 'http' ) !== 0 && \strpos( $url, '//' ) !== 0 );
[96] Fix | Delete
}
[97] Fix | Delete
[98] Fix | Delete
/**
[99] Fix | Delete
* Gets the path from the passed URL.
[100] Fix | Delete
*
[101] Fix | Delete
* @param string $url The URL to get the path from.
[102] Fix | Delete
*
[103] Fix | Delete
* @return string The path of the URL. Returns an empty string if URL parsing fails.
[104] Fix | Delete
*/
[105] Fix | Delete
public function get_url_path( $url ) {
[106] Fix | Delete
if ( \is_string( $url ) === false
[107] Fix | Delete
&& ( \is_object( $url ) === false || \method_exists( $url, '__toString' ) === false )
[108] Fix | Delete
) {
[109] Fix | Delete
return '';
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
return (string) \wp_parse_url( $url, \PHP_URL_PATH );
[113] Fix | Delete
}
[114] Fix | Delete
[115] Fix | Delete
/**
[116] Fix | Delete
* Gets the host from the passed URL.
[117] Fix | Delete
*
[118] Fix | Delete
* @param string $url The URL to get the host from.
[119] Fix | Delete
*
[120] Fix | Delete
* @return string The host of the URL. Returns an empty string if URL parsing fails.
[121] Fix | Delete
*/
[122] Fix | Delete
public function get_url_host( $url ) {
[123] Fix | Delete
if ( \is_string( $url ) === false
[124] Fix | Delete
&& ( \is_object( $url ) === false || \method_exists( $url, '__toString' ) === false )
[125] Fix | Delete
) {
[126] Fix | Delete
return '';
[127] Fix | Delete
}
[128] Fix | Delete
[129] Fix | Delete
return (string) \wp_parse_url( $url, \PHP_URL_HOST );
[130] Fix | Delete
}
[131] Fix | Delete
[132] Fix | Delete
/**
[133] Fix | Delete
* Determines the file extension of the given url.
[134] Fix | Delete
*
[135] Fix | Delete
* @param string $url The URL.
[136] Fix | Delete
*
[137] Fix | Delete
* @return string The extension.
[138] Fix | Delete
*/
[139] Fix | Delete
public function get_extension_from_url( $url ) {
[140] Fix | Delete
$path = $this->get_url_path( $url );
[141] Fix | Delete
[142] Fix | Delete
if ( $path === '' ) {
[143] Fix | Delete
return '';
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
$parts = \explode( '.', $path );
[147] Fix | Delete
if ( empty( $parts ) || \count( $parts ) === 1 ) {
[148] Fix | Delete
return '';
[149] Fix | Delete
}
[150] Fix | Delete
[151] Fix | Delete
return \end( $parts );
[152] Fix | Delete
}
[153] Fix | Delete
[154] Fix | Delete
/**
[155] Fix | Delete
* Ensures that the given url is an absolute url.
[156] Fix | Delete
*
[157] Fix | Delete
* @param string $url The url that needs to be absolute.
[158] Fix | Delete
*
[159] Fix | Delete
* @return string The absolute url.
[160] Fix | Delete
*/
[161] Fix | Delete
public function ensure_absolute_url( $url ) {
[162] Fix | Delete
if ( ! \is_string( $url ) || $url === '' ) {
[163] Fix | Delete
return $url;
[164] Fix | Delete
}
[165] Fix | Delete
[166] Fix | Delete
if ( $this->is_relative( $url ) === true ) {
[167] Fix | Delete
return $this->build_absolute_url( $url );
[168] Fix | Delete
}
[169] Fix | Delete
[170] Fix | Delete
return $url;
[171] Fix | Delete
}
[172] Fix | Delete
[173] Fix | Delete
/**
[174] Fix | Delete
* Parse the home URL setting to find the base URL for relative URLs.
[175] Fix | Delete
*
[176] Fix | Delete
* @param string|null $path Optional path string.
[177] Fix | Delete
*
[178] Fix | Delete
* @return string
[179] Fix | Delete
*/
[180] Fix | Delete
public function build_absolute_url( $path = null ) {
[181] Fix | Delete
$path = \wp_parse_url( $path, \PHP_URL_PATH );
[182] Fix | Delete
$url_parts = \wp_parse_url( \home_url() );
[183] Fix | Delete
[184] Fix | Delete
$base_url = \trailingslashit( $url_parts['scheme'] . '://' . $url_parts['host'] );
[185] Fix | Delete
[186] Fix | Delete
if ( ! \is_null( $path ) ) {
[187] Fix | Delete
$base_url .= \ltrim( $path, '/' );
[188] Fix | Delete
}
[189] Fix | Delete
[190] Fix | Delete
return $base_url;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
/**
[194] Fix | Delete
* Returns the link type.
[195] Fix | Delete
*
[196] Fix | Delete
* @param array $url The URL, as parsed by wp_parse_url.
[197] Fix | Delete
* @param array|null $home_url Optional. The home URL, as parsed by wp_parse_url. Used to avoid reparsing the home_url.
[198] Fix | Delete
* @param bool $is_image Whether or not the link is an image.
[199] Fix | Delete
*
[200] Fix | Delete
* @return string The link type.
[201] Fix | Delete
*/
[202] Fix | Delete
public function get_link_type( $url, $home_url = null, $is_image = false ) {
[203] Fix | Delete
// If there is no scheme and no host the link is always internal.
[204] Fix | Delete
// Beware, checking just the scheme isn't enough as a link can be //yoast.com for instance.
[205] Fix | Delete
if ( empty( $url['scheme'] ) && empty( $url['host'] ) ) {
[206] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
[207] Fix | Delete
}
[208] Fix | Delete
[209] Fix | Delete
// If there is a scheme but it's not http(s) then the link is always external.
[210] Fix | Delete
if ( \array_key_exists( 'scheme', $url ) && ! \in_array( $url['scheme'], [ 'http', 'https' ], true ) ) {
[211] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
[212] Fix | Delete
}
[213] Fix | Delete
[214] Fix | Delete
if ( \is_null( $home_url ) ) {
[215] Fix | Delete
$home_url = \wp_parse_url( \home_url() );
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
// When the base host is equal to the host.
[219] Fix | Delete
if ( isset( $url['host'] ) && $url['host'] !== $home_url['host'] ) {
[220] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
// There is no base path and thus all URLs of the same domain are internal.
[224] Fix | Delete
if ( empty( $home_url['path'] ) ) {
[225] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
// When there is a path and it matches the start of the url.
[229] Fix | Delete
if ( isset( $url['path'] ) && \strpos( $url['path'], $home_url['path'] ) === 0 ) {
[230] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
[234] Fix | Delete
}
[235] Fix | Delete
[236] Fix | Delete
/**
[237] Fix | Delete
* Recreate current URL.
[238] Fix | Delete
*
[239] Fix | Delete
* @param bool $with_request_uri Whether we want the REQUEST_URI appended.
[240] Fix | Delete
*
[241] Fix | Delete
* @return string
[242] Fix | Delete
*/
[243] Fix | Delete
public function recreate_current_url( $with_request_uri = true ) {
[244] Fix | Delete
$current_url = 'http';
[245] Fix | Delete
if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) {
[246] Fix | Delete
$current_url .= 's';
[247] Fix | Delete
}
[248] Fix | Delete
$current_url .= '://';
[249] Fix | Delete
[250] Fix | Delete
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- We know this is scary.
[251] Fix | Delete
$suffix = ( $with_request_uri && isset( $_SERVER['REQUEST_URI'] ) ) ? $_SERVER['REQUEST_URI'] : '';
[252] Fix | Delete
[253] Fix | Delete
if ( isset( $_SERVER['SERVER_NAME'] ) && ! empty( $_SERVER['SERVER_NAME'] ) ) {
[254] Fix | Delete
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- We know this is scary.
[255] Fix | Delete
$server_name = $_SERVER['SERVER_NAME'];
[256] Fix | Delete
}
[257] Fix | Delete
else {
[258] Fix | Delete
// Early return with just the path.
[259] Fix | Delete
return $suffix;
[260] Fix | Delete
}
[261] Fix | Delete
[262] Fix | Delete
$server_port = '';
[263] Fix | Delete
if ( isset( $_SERVER['SERVER_PORT'] ) && $_SERVER['SERVER_PORT'] !== '80' && $_SERVER['SERVER_PORT'] !== '443' ) {
[264] Fix | Delete
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- We know this is scary.
[265] Fix | Delete
$server_port = $_SERVER['SERVER_PORT'];
[266] Fix | Delete
}
[267] Fix | Delete
[268] Fix | Delete
if ( ! empty( $server_port ) ) {
[269] Fix | Delete
$current_url .= $server_name . ':' . $server_port . $suffix;
[270] Fix | Delete
}
[271] Fix | Delete
else {
[272] Fix | Delete
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- We know this is scary.
[273] Fix | Delete
$current_url .= $server_name . $suffix;
[274] Fix | Delete
}
[275] Fix | Delete
[276] Fix | Delete
return $current_url;
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
/**
[280] Fix | Delete
* Parses a URL and returns its components, this wrapper function was created to support unit tests.
[281] Fix | Delete
*
[282] Fix | Delete
* @param string $parsed_url The URL to parse.
[283] Fix | Delete
* @return array The parsed components of the URL.
[284] Fix | Delete
*/
[285] Fix | Delete
public function parse_str_params( $parsed_url ) {
[286] Fix | Delete
$array = [];
[287] Fix | Delete
[288] Fix | Delete
// @todo parse_str changes spaces in param names into `_`, we should find a better way to support them.
[289] Fix | Delete
\wp_parse_str( $parsed_url, $array );
[290] Fix | Delete
[291] Fix | Delete
return $array;
[292] Fix | Delete
}
[293] Fix | Delete
}
[294] Fix | Delete
[295] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function