Edit File by line
/home/barbar84/www/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
* @codeCoverageIgnore It only wraps a WordPress function.
[102] Fix | Delete
*
[103] Fix | Delete
* @param string $url The URL to get the path from.
[104] Fix | Delete
*
[105] Fix | Delete
* @return string The path of the URL. Returns an empty string if URL parsing fails.
[106] Fix | Delete
*/
[107] Fix | Delete
public function get_url_path( $url ) {
[108] Fix | Delete
return (string) \wp_parse_url( $url, \PHP_URL_PATH );
[109] Fix | Delete
}
[110] Fix | Delete
[111] Fix | Delete
/**
[112] Fix | Delete
* Determines the file extension of the given url.
[113] Fix | Delete
*
[114] Fix | Delete
* @param string $url The URL.
[115] Fix | Delete
*
[116] Fix | Delete
* @return string The extension.
[117] Fix | Delete
*/
[118] Fix | Delete
public function get_extension_from_url( $url ) {
[119] Fix | Delete
$path = $this->get_url_path( $url );
[120] Fix | Delete
[121] Fix | Delete
if ( $path === '' ) {
[122] Fix | Delete
return '';
[123] Fix | Delete
}
[124] Fix | Delete
[125] Fix | Delete
$parts = \explode( '.', $path );
[126] Fix | Delete
if ( empty( $parts ) || \count( $parts ) === 1 ) {
[127] Fix | Delete
return '';
[128] Fix | Delete
}
[129] Fix | Delete
[130] Fix | Delete
return \end( $parts );
[131] Fix | Delete
}
[132] Fix | Delete
[133] Fix | Delete
/**
[134] Fix | Delete
* Ensures that the given url is an absolute url.
[135] Fix | Delete
*
[136] Fix | Delete
* @param string $url The url that needs to be absolute.
[137] Fix | Delete
*
[138] Fix | Delete
* @return string The absolute url.
[139] Fix | Delete
*/
[140] Fix | Delete
public function ensure_absolute_url( $url ) {
[141] Fix | Delete
if ( ! \is_string( $url ) || $url === '' ) {
[142] Fix | Delete
return $url;
[143] Fix | Delete
}
[144] Fix | Delete
[145] Fix | Delete
if ( $this->is_relative( $url ) === true ) {
[146] Fix | Delete
return $this->build_absolute_url( $url );
[147] Fix | Delete
}
[148] Fix | Delete
[149] Fix | Delete
return $url;
[150] Fix | Delete
}
[151] Fix | Delete
[152] Fix | Delete
/**
[153] Fix | Delete
* Parse the home URL setting to find the base URL for relative URLs.
[154] Fix | Delete
*
[155] Fix | Delete
* @param string|null $path Optional path string.
[156] Fix | Delete
*
[157] Fix | Delete
* @return string
[158] Fix | Delete
*/
[159] Fix | Delete
public function build_absolute_url( $path = null ) {
[160] Fix | Delete
$path = \wp_parse_url( $path, \PHP_URL_PATH );
[161] Fix | Delete
$url_parts = \wp_parse_url( \home_url() );
[162] Fix | Delete
[163] Fix | Delete
$base_url = \trailingslashit( $url_parts['scheme'] . '://' . $url_parts['host'] );
[164] Fix | Delete
[165] Fix | Delete
if ( ! \is_null( $path ) ) {
[166] Fix | Delete
$base_url .= \ltrim( $path, '/' );
[167] Fix | Delete
}
[168] Fix | Delete
[169] Fix | Delete
return $base_url;
[170] Fix | Delete
}
[171] Fix | Delete
[172] Fix | Delete
/**
[173] Fix | Delete
* Returns the link type.
[174] Fix | Delete
*
[175] Fix | Delete
* @param array $url The URL, as parsed by wp_parse_url.
[176] Fix | Delete
* @param array|null $home_url Optional. The home URL, as parsed by wp_parse_url. Used to avoid reparsing the home_url.
[177] Fix | Delete
* @param bool $is_image Whether or not the link is an image.
[178] Fix | Delete
*
[179] Fix | Delete
* @return string The link type.
[180] Fix | Delete
*/
[181] Fix | Delete
public function get_link_type( $url, $home_url = null, $is_image = false ) {
[182] Fix | Delete
// If there is no scheme and no host the link is always internal.
[183] Fix | Delete
// Beware, checking just the scheme isn't enough as a link can be //yoast.com for instance.
[184] Fix | Delete
if ( empty( $url['scheme'] ) && empty( $url['host'] ) ) {
[185] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
[186] Fix | Delete
}
[187] Fix | Delete
[188] Fix | Delete
// If there is a scheme but it's not http(s) then the link is always external.
[189] Fix | Delete
if ( \array_key_exists( 'scheme', $url ) && ! \in_array( $url['scheme'], [ 'http', 'https' ], true ) ) {
[190] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
[191] Fix | Delete
}
[192] Fix | Delete
[193] Fix | Delete
if ( \is_null( $home_url ) ) {
[194] Fix | Delete
$home_url = \wp_parse_url( \home_url() );
[195] Fix | Delete
}
[196] Fix | Delete
[197] Fix | Delete
// When the base host is equal to the host.
[198] Fix | Delete
if ( isset( $url['host'] ) && $url['host'] !== $home_url['host'] ) {
[199] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
[200] Fix | Delete
}
[201] Fix | Delete
[202] Fix | Delete
// There is no base path and thus all URLs of the same domain are internal.
[203] Fix | Delete
if ( empty( $home_url['path'] ) ) {
[204] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
[205] Fix | Delete
}
[206] Fix | Delete
[207] Fix | Delete
// When there is a path and it matches the start of the url.
[208] Fix | Delete
if ( isset( $url['path'] ) && \strpos( $url['path'], $home_url['path'] ) === 0 ) {
[209] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_INTERNAL_IMAGE : SEO_Links::TYPE_INTERNAL;
[210] Fix | Delete
}
[211] Fix | Delete
[212] Fix | Delete
return ( $is_image ) ? SEO_Links::TYPE_EXTERNAL_IMAGE : SEO_Links::TYPE_EXTERNAL;
[213] Fix | Delete
}
[214] Fix | Delete
}
[215] Fix | Delete
[216] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function