Background of the issue:
In my theme I need to hook onto WP core "wp_delete_file"
This filter expects a string param and return type: hidden link
But you return null: wp-contentpluginssitepress-multilingual-cmsclassesmediaclass-wpml-attachment-action.php line 180
<code>
public function delete_file_filter( $file ) {
if ( $file ) {
$file_name = $this->get_file_name( $file );
$scaled_file_name = $this->get_file_name( $file, true );
$sql = "SELECT pm.meta_id, pm.post_id FROM {$this->wpdb->postmeta} AS pm WHERE pm.meta_value IN ( %s, %s ) AND pm.meta_key='_wp_attached_file'";
$attachment_prepared = $this->wpdb->prepare( $sql, $file_name, $scaled_file_name );
$attachment = $this->wpdb->get_row( $attachment_prepared );
if ( ! empty( $attachment ) ) {
$file = null;
}
}
return $file;
}
</code>
Which then leads to a PHP fatal error:
TypeError: is_file() expects parameter 1 to be a valid path, null given
The code in my functions.php
<code>
add_filter('wp_delete_file', function ( $file ) {
if ( ! is_file( $file ) ) {
return $file;
}
// some custom logic
return '';
}, 20);
</code>
I know I can validate $file for null in my code, however that's not the point. Please fix your code to return empty string isntead of null.
Symptoms:
Fatal error: wp_delete_file expects string return type you return null - TypeError: is_file() expects parameter 1 to be a valid path, null given. The code in WPML returns null instead of an empty string, leading to a PHP fatal error.
Questions:
Can you fix your code to return an empty string instead of null
Hello and thank you for your report.
I will bring this up to our developers, however could you please provide more information about the issue, as you have not submitted the extra debug information for this ticket and I have to guess and assume things about your current setup.
For example, is there a specific PHP version that is causing the problem?
At present I can only reproduce a deprecated warnings in my test setup and not a fatal error.
We look extremely seriously on fatal error messages.
If a plugin/theme uses that filter and uses strict types, it will result in that fatal.
e.g. if a theme or plugin has code like this:
declare(strict_types=1);
add_filter('wp_delete_file', function ( $file ) {
if ( ! is_file( $file ) ) {
return $file;
}
// some custom logic
return '';
}, 20);
you'll get a fatal error when deleting an attachment that is still used in translations/exists in translations, due to the code in wp-content/plugins/sitepress-multilingual-cms/classes/media/class-wpml-attachment-action.php line 180
Yes, I understand all too good about forcing string types.
This is rather rare set of circumstances - hooking on `wp_delete_file`, having strict_types and so on.
I understood your case and added all the details to the ticket in our internal system that are going to be handled by a developer.