Make WordPress Core

Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#51729 closed defect (bug) (invalid)

"Uncaught TypeError: is_file(): Argument #1 ($filename) must be of type string, array given" when using the 'mce_external_languages' filter

Reported by: backups's profile BackuPs Owned by:
Milestone: Priority: normal
Severity: normal Version:
Component: Editor Keywords: php8
Focuses: Cc:

Description (last modified by SergeyBiryukov)

Hi

Is wordpress latest beta php 8.0 compatible as i am getting a fatal error what used to work perfect in php 7.4 or before

https://share.getcloudapp.com/P8uyPYw4?utm_source=show

Fatal error: Uncaught TypeError: is_file(): Argument #1 ($filename) must be of type string, array given in /var/docs/wpdemo.tst/public/wp-includes/class-wp-editor.php on line 489
( ! ) TypeError: is_file(): Argument #1 ($filename) must be of type string, array given in /var/docs/wpdemo.tst/public/wp-includes/class-wp-editor.php on line 489

Change History (9)

#1 @BackuPs
4 years ago

i am using this code.

function add_tinymce_languages($langs){
	$langs[] = array('shortcodeGenerator' => THEME_ADMIN.'/shortcodes/langs/langs.php');
	return $langs;
}
Last edited 4 years ago by SergeyBiryukov (previous) (diff)

#2 @ayeshrajans
4 years ago

  • Keywords php8 added

#3 @ayeshrajans
4 years ago

Hi @BackuPs - Looking at the backtrace, it looks like a problematic theme filter/hook is the cause of this. This is a fatal error in PHP 8.0 because internal functions throw TypeError exceptions in PHP 8.0, but the same code should emit a warning; I presume that warning was silenced from error handler or PHP settings.

#4 @SergeyBiryukov
4 years ago

  • Component changed from General to Editor
  • Description modified (diff)
  • Summary changed from PHP 8.0 issue to "Uncaught TypeError: is_file(): Argument #1 ($filename) must be of type string, array given" when using the 'mce_external_languages' filter

#5 @BackuPs
4 years ago

it happens when adding

add_filter('mce_external_languages', array(&$this, 'add_tinymce_languages'));

But the hook requires a array of languages..

https://developer.wordpress.org/reference/hooks/mce_external_languages/

#6 @SergeyBiryukov
4 years ago

  • Milestone changed from Awaiting Review to 5.6

#7 @jrf
4 years ago

  • Milestone 5.6 deleted
  • Resolution set to invalid
  • Status changed from new to closed
  • Version trunk deleted

@BackuPs Thanks for reporting this, but this is not a WP core bug, but a bug in your own code.

As per the documentation for the mce_external_languages filter:

The filter takes an associative array ('plugin_name' => 'path') where 'path' is the include path to the file.

That is, however, not what your code is doing. You are returning a non-associative array with an associative array as the value of path.

<?php
function add_tinymce_languages($langs){
        $langs[] = array('shortcodeGenerator' => THEME_ADMIN.'/shortcodes/langs/langs.php');
        return $langs;
}

While this code would not throw an error on PHP < 8, it should also not work.

Try replacing your code with this:

<?php
function add_tinymce_languages($langs){
        $langs['shortcodeGenerator'] = THEME_ADMIN.'/shortcodes/langs/langs.php';
        return $langs;
}

That should fix your bug and prevent the error on PHP 8.

And while you are at it, you may also want to replace the hook-in to not pass $this by reference. Objects are always passed by reference in PHP 5+, so the reference is unnecessary.

<?php
add_filter('mce_external_languages', array($this, 'add_tinymce_languages'));

but the same code should emit a warning; I presume that warning was silenced from error handler or PHP settings.

@ayeshrajans The relevant line in WP Core handling the output of the filter uses error silencing, so the bug would have been silently ignored prior to PHP 8.

File wp-includes/class-wp-editor.php:489:

<?php
if ( @is_file( $path ) && @is_readable( $path ) ) {

#8 @BackuPs
4 years ago

@jrf Thanks that did the trick.

#9 @ayeshrajans
4 years ago

Thanks a lot for the detailed explanation @jrf. I didn't realize the @ suppression operator there.

Note: See TracTickets for help on using tickets.