Skip to content

Commit

Permalink
Use str_contains (#43382)
Browse files Browse the repository at this point in the history
  • Loading branch information
Soean committed Aug 18, 2022
1 parent 2b99bd3 commit 7f0a211
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 19 deletions.
4 changes: 2 additions & 2 deletions lib/block-supports/duotone.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* @return float Value in the range [0,1].
*/
function gutenberg_tinycolor_bound01( $n, $max ) {
if ( 'string' === gettype( $n ) && false !== strpos( $n, '.' ) && 1 === (float) $n ) {
if ( 'string' === gettype( $n ) && str_contains( $n, '.' ) && 1 === (float) $n ) {
$n = '100%';
}

$n = min( $max, max( 0, (float) $n ) );

// Automatically convert percentage into number.
if ( 'string' === gettype( $n ) && false !== strpos( $n, '%' ) ) {
if ( 'string' === gettype( $n ) && str_contains( $n, '%' ) ) {
$n = (int) ( $n * $max ) / 100;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/block-supports/elements.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function gutenberg_render_elements_support( $block_content, $block ) {
$first_element = $html_element_matches[0][0];
// If the first HTML element has a class attribute just add the new class
// as we do on layout and duotone.
if ( strpos( $first_element, 'class="' ) !== false ) {
if ( str_contains( $first_element, 'class="' ) ) {
$content = preg_replace(
'/' . preg_quote( 'class="', '/' ) . '/',
'class="' . $class_name . ' ',
Expand Down
4 changes: 2 additions & 2 deletions lib/block-supports/layout.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
}
if ( $gap_value && ! $should_skip_gap_serialization ) {
// Get spacing CSS variable from preset value if provided.
if ( is_string( $gap_value ) && strpos( $gap_value, 'var:preset|spacing|' ) !== false ) {
if ( is_string( $gap_value ) && str_contains( $gap_value, 'var:preset|spacing|' ) ) {
$index_to_splice = strrpos( $gap_value, '|' ) + 1;
$slug = _wp_to_kebab_case( substr( $gap_value, $index_to_splice ) );
$gap_value = "var(--wp--preset--spacing--$slug)";
Expand Down Expand Up @@ -164,7 +164,7 @@ function gutenberg_get_layout_style( $selector, $layout, $has_block_gap_support
foreach ( $gap_sides as $gap_side ) {
$process_value = is_string( $gap_value ) ? $gap_value : _wp_array_get( $gap_value, array( $gap_side ), $fallback_gap_value );
// Get spacing CSS variable from preset value if provided.
if ( is_string( $process_value ) && strpos( $process_value, 'var:preset|spacing|' ) !== false ) {
if ( is_string( $process_value ) && str_contains( $process_value, 'var:preset|spacing|' ) ) {
$index_to_splice = strrpos( $process_value, '|' ) + 1;
$slug = _wp_to_kebab_case( substr( $process_value, $index_to_splice ) );
$process_value = "var(--wp--preset--spacing--$slug)";
Expand Down
4 changes: 2 additions & 2 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
*/
function gutenberg_typography_get_preset_inline_style_value( $style_value, $css_property ) {
// If the style value is not a preset CSS variable go no further.
if ( empty( $style_value ) || strpos( $style_value, "var:preset|{$css_property}|" ) === false ) {
if ( empty( $style_value ) || ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
return $style_value;
}

Expand Down Expand Up @@ -209,7 +209,7 @@ function gutenberg_typography_get_css_variable_inline_style( $attributes, $featu
}

// If we don't have a preset CSS variable, we'll assume it's a regular CSS value.
if ( strpos( $style_value, "var:preset|{$css_property}|" ) === false ) {
if ( ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
return sprintf( '%s:%s;', $css_property, $style_value );
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function wp_add_global_styles_for_blocks() {
array_filter(
$metadata['path'],
function ( $item ) {
if ( strpos( $item, 'core/' ) !== false ) {
if ( str_contains( $item, 'core/' ) ) {
return true;
}
return false;
Expand Down
6 changes: 3 additions & 3 deletions lib/experimental/class-wp-webfonts-provider-local.php
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,9 @@ private function build_font_face_css( array $webfont ) {

// Wrap font-family in quotes if it contains spaces.
if (
false !== strpos( $webfont['font-family'], ' ' ) &&
false === strpos( $webfont['font-family'], '"' ) &&
false === strpos( $webfont['font-family'], "'" )
str_contains( $webfont['font-family'], ' ' ) &&
! str_contains( $webfont['font-family'], '"' ) &&
! str_contains( $webfont['font-family'], "'" )
) {
$webfont['font-family'] = '"' . $webfont['font-family'] . '"';
}
Expand Down
4 changes: 2 additions & 2 deletions packages/block-library/src/calendar/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ function render_block_core_calendar( $attributes ) {
if ( isset( $attributes['month'] ) && isset( $attributes['year'] ) ) {
$permalink_structure = get_option( 'permalink_structure' );
if (
strpos( $permalink_structure, '%monthnum%' ) !== false &&
strpos( $permalink_structure, '%year%' ) !== false
str_contains( $permalink_structure, '%monthnum%' ) &&
str_contains( $permalink_structure, '%year%' )
) {
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.OverrideProhibited
$monthnum = $attributes['month'];
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/image/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ function render_block_core_image( $attributes, $content ) {
// which now wraps Image Blocks within innerBlocks.
// The data-id attribute is added in a core/gallery `render_block_data` hook.
$data_id_attribute = 'data-id="' . esc_attr( $attributes['data-id'] ) . '"';
if ( false === strpos( $content, $data_id_attribute ) ) {
if ( ! str_contains( $content, $data_id_attribute ) ) {
$content = str_replace( '<img', '<img ' . $data_id_attribute . ' ', $content );
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/block-library/src/search/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ function apply_block_core_search_border_style( $attributes, $property, $side, &$
}

if ( 'color' === $property && $side ) {
$has_color_preset = strpos( $value, 'var:preset|color|' ) !== false;
$has_color_preset = str_contains( $value, 'var:preset|color|' );
if ( $has_color_preset ) {
$named_color_value = substr( $value, strrpos( $value, '|' ) + 1 );
$value = sprintf( 'var(--wp--preset--color--%s)', $named_color_value );
Expand Down
8 changes: 4 additions & 4 deletions packages/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ class WP_Style_Engine {
* @return string The slug, or empty string if not found.
*/
protected static function get_slug_from_preset_value( $style_value, $property_key ) {
if ( is_string( $style_value ) && strpos( $style_value, "var:preset|{$property_key}|" ) !== false ) {
if ( is_string( $style_value ) && str_contains( $style_value, "var:preset|{$property_key}|" ) ) {
$index_to_splice = strrpos( $style_value, '|' ) + 1;
return _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );
}
Expand Down Expand Up @@ -390,7 +390,7 @@ protected static function get_css_declarations( $style_value, $style_definition,

// Build CSS var values from var:? values, e.g, `var(--wp--css--rule-slug )`
// Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition.
if ( is_string( $style_value ) && strpos( $style_value, 'var:' ) !== false ) {
if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) {
if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
$css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] );
if ( static::is_valid_style_value( $css_var ) ) {
Expand All @@ -410,7 +410,7 @@ protected static function get_css_declarations( $style_value, $style_definition,
}

foreach ( $style_value as $key => $value ) {
if ( is_string( $value ) && strpos( $value, 'var:' ) !== false && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) {
$value = static::get_css_var_value( $value, $style_definition['css_vars'] );
}

Expand Down Expand Up @@ -464,7 +464,7 @@ protected static function get_individual_property_css_declarations( $style_value

if ( $style_definition && isset( $style_definition['property_keys']['individual'] ) ) {
// Set a CSS var if there is a valid preset value.
if ( is_string( $value ) && strpos( $value, 'var:' ) !== false && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) ) {
if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $individual_property_definition['css_vars'] ) ) {
$value = static::get_css_var_value( $value, $individual_property_definition['css_vars'] );
}
$individual_css_property = sprintf( $style_definition['property_keys']['individual'], $individual_property_key );
Expand Down

0 comments on commit 7f0a211

Please sign in to comment.