369 function is_iterable( $var ) { |
373 function is_iterable( $var ) { |
370 return ( is_array( $var ) || $var instanceof Traversable ); |
374 return ( is_array( $var ) || $var instanceof Traversable ); |
371 } |
375 } |
372 } |
376 } |
373 |
377 |
|
378 if ( ! function_exists( 'array_key_first' ) ) { |
|
379 /** |
|
380 * Polyfill for array_key_first() function added in PHP 7.3. |
|
381 * |
|
382 * Get the first key of the given array without affecting |
|
383 * the internal array pointer. |
|
384 * |
|
385 * @since 5.9.0 |
|
386 * |
|
387 * @param array $arr An array. |
|
388 * @return string|int|null The first key of array if the array |
|
389 * is not empty; `null` otherwise. |
|
390 */ |
|
391 function array_key_first( array $arr ) { |
|
392 foreach ( $arr as $key => $value ) { |
|
393 return $key; |
|
394 } |
|
395 } |
|
396 } |
|
397 |
|
398 if ( ! function_exists( 'array_key_last' ) ) { |
|
399 /** |
|
400 * Polyfill for `array_key_last()` function added in PHP 7.3. |
|
401 * |
|
402 * Get the last key of the given array without affecting the |
|
403 * internal array pointer. |
|
404 * |
|
405 * @since 5.9.0 |
|
406 * |
|
407 * @param array $arr An array. |
|
408 * @return string|int|null The last key of array if the array |
|
409 *. is not empty; `null` otherwise. |
|
410 */ |
|
411 function array_key_last( array $arr ) { |
|
412 if ( empty( $arr ) ) { |
|
413 return null; |
|
414 } |
|
415 end( $arr ); |
|
416 return key( $arr ); |
|
417 } |
|
418 } |
|
419 |
|
420 if ( ! function_exists( 'str_contains' ) ) { |
|
421 /** |
|
422 * Polyfill for `str_contains()` function added in PHP 8.0. |
|
423 * |
|
424 * Performs a case-sensitive check indicating if needle is |
|
425 * contained in haystack. |
|
426 * |
|
427 * @since 5.9.0 |
|
428 * |
|
429 * @param string $haystack The string to search in. |
|
430 * @param string $needle The substring to search for in the haystack. |
|
431 * @return bool True if `$needle` is in `$haystack`, otherwise false. |
|
432 */ |
|
433 function str_contains( $haystack, $needle ) { |
|
434 return ( '' === $needle || false !== strpos( $haystack, $needle ) ); |
|
435 } |
|
436 } |
|
437 |
|
438 if ( ! function_exists( 'str_starts_with' ) ) { |
|
439 /** |
|
440 * Polyfill for `str_starts_with()` function added in PHP 8.0. |
|
441 * |
|
442 * Performs a case-sensitive check indicating if |
|
443 * the haystack begins with needle. |
|
444 * |
|
445 * @since 5.9.0 |
|
446 * |
|
447 * @param string $haystack The string to search in. |
|
448 * @param string $needle The substring to search for in the `$haystack`. |
|
449 * @return bool True if `$haystack` starts with `$needle`, otherwise false. |
|
450 */ |
|
451 function str_starts_with( $haystack, $needle ) { |
|
452 if ( '' === $needle ) { |
|
453 return true; |
|
454 } |
|
455 return 0 === strpos( $haystack, $needle ); |
|
456 } |
|
457 } |
|
458 |
|
459 if ( ! function_exists( 'str_ends_with' ) ) { |
|
460 /** |
|
461 * Polyfill for `str_ends_with()` function added in PHP 8.0. |
|
462 * |
|
463 * Performs a case-sensitive check indicating if |
|
464 * the haystack ends with needle. |
|
465 * |
|
466 * @since 5.9.0 |
|
467 * |
|
468 * @param string $haystack The string to search in. |
|
469 * @param string $needle The substring to search for in the `$haystack`. |
|
470 * @return bool True if `$haystack` ends with `$needle`, otherwise false. |
|
471 */ |
|
472 function str_ends_with( $haystack, $needle ) { |
|
473 if ( '' === $haystack && '' !== $needle ) { |
|
474 return false; |
|
475 } |
|
476 $len = strlen( $needle ); |
|
477 return 0 === substr_compare( $haystack, $needle, -$len, $len ); |
|
478 } |
|
479 } |
|
480 |
374 // IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later. |
481 // IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later. |
375 if ( ! defined( 'IMAGETYPE_WEBP' ) ) { |
482 if ( ! defined( 'IMAGETYPE_WEBP' ) ) { |
376 define( 'IMAGETYPE_WEBP', 18 ); |
483 define( 'IMAGETYPE_WEBP', 18 ); |
377 } |
484 } |
378 |
485 |
379 // IMG_WEBP constant is only defined in PHP 7.0.10 or later. |
486 // IMG_WEBP constant is only defined in PHP 7.0.10 or later. |
380 if ( ! defined( 'IMG_WEBP' ) ) { |
487 if ( ! defined( 'IMG_WEBP' ) ) { |
381 define( 'IMG_WEBP', IMAGETYPE_WEBP ); // phpcs:ignore PHPCompatibility.Constants.NewConstants.imagetype_webpFound |
488 define( 'IMG_WEBP', IMAGETYPE_WEBP ); |
382 } |
489 } |