[wp-trac] [WordPress Trac] #62370: Fatal error in convert_smilies() due to count() receiving non-countable value
WordPress Trac
noreply at wordpress.org
Mon Nov 11 06:30:25 UTC 2024
#62370: Fatal error in convert_smilies() due to count() receiving non-countable
value
-------------------------------------+--------------------------------
Reporter: mcqueen22 | Owner: (none)
Type: defect (bug) | Status: closed
Priority: normal | Milestone:
Component: Formatting | Version: 6.6.2
Severity: normal | Resolution: duplicate
Keywords: has-patch needs-testing | Focuses: php-compatibility
-------------------------------------+--------------------------------
Comment (by mcqueen22):
**Comment to Add to Ticket #51019 on WordPress Trac**
**Possible Solution for the Issue with `convert_smilies()`**
Hi, I have encountered the same error described in ticket #51019:
''**Fatal error: Uncaught Error: count(): Argument #1 ($value) must be of
type Countable|array, false given
**''
This happens in the `formatting.php` file, specifically in the
`convert_smilies()` function, where `preg_split()` returns `false` and
`count()` tries to handle it, resulting in a fatal error.
**Implemented Solution**
To prevent this issue, I modified the `convert_smilies()` function by
adding a verification to ensure that `count()` only processes valid
values.
Below is the complete modified version of the `convert_smilies()`
function:
{{{
#!php
function convert_smilies( $text ) {
global $wp_smiliessearch;
$output = '';
// Verify if smilies are enabled and if the smilies search pattern is
defined.
if ( get_option( 'use_smilies' ) && ! empty( $wp_smiliessearch ) ) {
// Split the text into elements using preg_split.
$textarr = preg_split( '/(<.*>)/U', $text, -1,
PREG_SPLIT_DELIM_CAPTURE ); // Capture the tags as well as in between.
// Check if $textarr is an array before continuing.
if ( is_array( $textarr ) ) {
$stop = count( $textarr ); // Count elements for the loop.
// Ignore processing of certain tags.
$tags_to_ignore = 'code|pre|style|script|textarea';
$ignore_block_element = '';
// Iterate over each part of the text.
for ( $i = 0; $i < $stop; $i++ ) {
$content = $textarr[ $i ];
// If we are in an ignore block, wait until we find its
closing tag.
if ( '' === $ignore_block_element && preg_match( '/^<(' .
$tags_to_ignore . ')[^>]*>/', $content, $matches ) ) {
$ignore_block_element = $matches[1];
}
// If it's not a tag and not in an ignore block.
if ( '' === $ignore_block_element && strlen( $content ) >
0 && '<' !== $content[0] ) {
$content = preg_replace_callback( $wp_smiliessearch,
'translate_smiley', $content );
}
// Did we exit the ignore block?
if ( '' !== $ignore_block_element && '</' .
$ignore_block_element . '>' === $content ) {
$ignore_block_element = '';
}
$output .= $content;
}
} else {
// If $textarr is not an array, return the original text
without changes.
$output = $text;
}
} else {
// Return the original text if smilies are not used.
$output = $text;
}
return $output;
}
}}}
**Description of the Change**:
- **Check `$textarr`**: Added a check to ensure `$textarr` is an `array`
before attempting to use `count()`:
{{{
#!php
if ( is_array( $textarr ) ) {
$stop = count( $textarr );
} else {
$stop = 0;
}
}}}
- This ensures that `count()` only receives countable values, preventing
the fatal error.
**Additional Context**
This issue was encountered while using the **Woodmart** theme in
combination with some WooCommerce features. The `the_content()` function
triggered `convert_smilies()`, which is where the error occurred.
I hope this solution helps improve the functionality of
`convert_smilies()` and prevents similar errors for other users. I am
available to test patches or provide any additional information if needed.
Regards
--
Ticket URL: <https://core.trac.wordpress.org/ticket/62370#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list