Edit File by line
/home/zeestwma/richards.../wp-conte.../plugins/woocomme.../src/Utilitie...
File: StringUtil.php
<?php
[0] Fix | Delete
/**
[1] Fix | Delete
* A class of utilities for dealing with strings.
[2] Fix | Delete
*/
[3] Fix | Delete
[4] Fix | Delete
namespace Automattic\WooCommerce\Utilities;
[5] Fix | Delete
[6] Fix | Delete
/**
[7] Fix | Delete
* A class of utilities for dealing with strings.
[8] Fix | Delete
*/
[9] Fix | Delete
final class StringUtil {
[10] Fix | Delete
[11] Fix | Delete
/**
[12] Fix | Delete
* Checks to see whether or not a string starts with another.
[13] Fix | Delete
*
[14] Fix | Delete
* @param string $string The string we want to check.
[15] Fix | Delete
* @param string $starts_with The string we're looking for at the start of $string.
[16] Fix | Delete
* @param bool $case_sensitive Indicates whether the comparison should be case-sensitive.
[17] Fix | Delete
*
[18] Fix | Delete
* @return bool True if the $string starts with $starts_with, false otherwise.
[19] Fix | Delete
*/
[20] Fix | Delete
public static function starts_with( string $string, string $starts_with, bool $case_sensitive = true ): bool {
[21] Fix | Delete
$len = strlen( $starts_with );
[22] Fix | Delete
if ( $len > strlen( $string ) ) {
[23] Fix | Delete
return false;
[24] Fix | Delete
}
[25] Fix | Delete
[26] Fix | Delete
$string = substr( $string, 0, $len );
[27] Fix | Delete
[28] Fix | Delete
if ( $case_sensitive ) {
[29] Fix | Delete
return strcmp( $string, $starts_with ) === 0;
[30] Fix | Delete
}
[31] Fix | Delete
[32] Fix | Delete
return strcasecmp( $string, $starts_with ) === 0;
[33] Fix | Delete
}
[34] Fix | Delete
[35] Fix | Delete
/**
[36] Fix | Delete
* Checks to see whether or not a string ends with another.
[37] Fix | Delete
*
[38] Fix | Delete
* @param string $string The string we want to check.
[39] Fix | Delete
* @param string $ends_with The string we're looking for at the end of $string.
[40] Fix | Delete
* @param bool $case_sensitive Indicates whether the comparison should be case-sensitive.
[41] Fix | Delete
*
[42] Fix | Delete
* @return bool True if the $string ends with $ends_with, false otherwise.
[43] Fix | Delete
*/
[44] Fix | Delete
public static function ends_with( string $string, string $ends_with, bool $case_sensitive = true ): bool {
[45] Fix | Delete
$len = strlen( $ends_with );
[46] Fix | Delete
if ( $len > strlen( $string ) ) {
[47] Fix | Delete
return false;
[48] Fix | Delete
}
[49] Fix | Delete
[50] Fix | Delete
$string = substr( $string, -$len );
[51] Fix | Delete
[52] Fix | Delete
if ( $case_sensitive ) {
[53] Fix | Delete
return strcmp( $string, $ends_with ) === 0;
[54] Fix | Delete
}
[55] Fix | Delete
[56] Fix | Delete
return strcasecmp( $string, $ends_with ) === 0;
[57] Fix | Delete
}
[58] Fix | Delete
[59] Fix | Delete
/**
[60] Fix | Delete
* Checks if one string is contained into another at any position.
[61] Fix | Delete
*
[62] Fix | Delete
* @param string $string The string we want to check.
[63] Fix | Delete
* @param string $contained The string we're looking for inside $string.
[64] Fix | Delete
* @param bool $case_sensitive Indicates whether the comparison should be case-sensitive.
[65] Fix | Delete
* @return bool True if $contained is contained inside $string, false otherwise.
[66] Fix | Delete
*/
[67] Fix | Delete
public static function contains( string $string, string $contained, bool $case_sensitive = true ): bool {
[68] Fix | Delete
if ( $case_sensitive ) {
[69] Fix | Delete
return false !== strpos( $string, $contained );
[70] Fix | Delete
} else {
[71] Fix | Delete
return false !== stripos( $string, $contained );
[72] Fix | Delete
}
[73] Fix | Delete
}
[74] Fix | Delete
[75] Fix | Delete
/**
[76] Fix | Delete
* Get the name of a plugin in the form 'directory/file.php', as in the keys of the array returned by 'get_plugins'.
[77] Fix | Delete
*
[78] Fix | Delete
* @param string $plugin_file_path The path of the main plugin file (can be passed as __FILE__ from the plugin itself).
[79] Fix | Delete
* @return string The name of the plugin in the form 'directory/file.php'.
[80] Fix | Delete
*/
[81] Fix | Delete
public static function plugin_name_from_plugin_file( string $plugin_file_path ): string {
[82] Fix | Delete
return basename( dirname( $plugin_file_path ) ) . DIRECTORY_SEPARATOR . basename( $plugin_file_path );
[83] Fix | Delete
}
[84] Fix | Delete
[85] Fix | Delete
/**
[86] Fix | Delete
* Check if a string is null or is empty.
[87] Fix | Delete
*
[88] Fix | Delete
* @param string|null $value The string to check.
[89] Fix | Delete
* @return bool True if the string is null or is empty.
[90] Fix | Delete
*/
[91] Fix | Delete
public static function is_null_or_empty( ?string $value ) {
[92] Fix | Delete
return is_null( $value ) || '' === $value;
[93] Fix | Delete
}
[94] Fix | Delete
[95] Fix | Delete
/**
[96] Fix | Delete
* Check if a string is null, is empty, or has only whitespace characters
[97] Fix | Delete
* (space, tab, vertical tab, form feed, carriage return, new line)
[98] Fix | Delete
*
[99] Fix | Delete
* @param string|null $value The string to check.
[100] Fix | Delete
* @return bool True if the string is null, is empty, or contains only whitespace characters.
[101] Fix | Delete
*/
[102] Fix | Delete
public static function is_null_or_whitespace( ?string $value ) {
[103] Fix | Delete
return is_null( $value ) || '' === $value || ctype_space( $value );
[104] Fix | Delete
}
[105] Fix | Delete
[106] Fix | Delete
/**
[107] Fix | Delete
* Convert an array of values to a list suitable for a SQL "IN" statement
[108] Fix | Delete
* (so comma separated and delimited by parenthesis).
[109] Fix | Delete
* e.g.: [1,2,3] --> (1,2,3)
[110] Fix | Delete
*
[111] Fix | Delete
* @param array $values The values to convert.
[112] Fix | Delete
* @return string A parenthesized and comma-separated string generated from the values.
[113] Fix | Delete
* @throws \InvalidArgumentException Empty values array passed.
[114] Fix | Delete
*/
[115] Fix | Delete
public static function to_sql_list( array $values ) {
[116] Fix | Delete
if ( empty( $values ) ) {
[117] Fix | Delete
// phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
[118] Fix | Delete
throw new \InvalidArgumentException( self::class_name_without_namespace( __CLASS__ ) . '::' . __FUNCTION__ . ': the values array is empty' );
[119] Fix | Delete
}
[120] Fix | Delete
[121] Fix | Delete
return '(' . implode( ',', $values ) . ')';
[122] Fix | Delete
}
[123] Fix | Delete
[124] Fix | Delete
/**
[125] Fix | Delete
* Get the name of a class without the namespace.
[126] Fix | Delete
*
[127] Fix | Delete
* @param string $class_name The full class name.
[128] Fix | Delete
* @return string The class name without the namespace.
[129] Fix | Delete
*/
[130] Fix | Delete
public static function class_name_without_namespace( string $class_name ) {
[131] Fix | Delete
// A '?:' would convert this to a one-liner, but WP coding standards disallow these :shrug:.
[132] Fix | Delete
$result = substr( strrchr( $class_name, '\\' ), 1 );
[133] Fix | Delete
return $result ? $result : $class_name;
[134] Fix | Delete
}
[135] Fix | Delete
[136] Fix | Delete
/**
[137] Fix | Delete
* Normalize the slashes (/ and \) of a local filesystem path by converting them to DIRECTORY_SEPARATOR.
[138] Fix | Delete
*
[139] Fix | Delete
* @param string|null $path Path to normalize.
[140] Fix | Delete
* @return string|null Normalized path, or null if the input was null.
[141] Fix | Delete
*/
[142] Fix | Delete
public static function normalize_local_path_slashes( ?string $path ) {
[143] Fix | Delete
return is_null( $path ) ? null : str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $path );
[144] Fix | Delete
}
[145] Fix | Delete
}
[146] Fix | Delete
[147] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function