Replace call_user_func() with variable functions (#2763)

Fixes #2714
This commit is contained in:
྅༻ Ǭɀħ ༄༆ཉ 2020-10-11 19:57:35 +02:00 committed by GitHub
parent 347971f25d
commit fb1415027c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 3 deletions

View File

@ -149,7 +149,7 @@ function yourls_apply_filter( $hook, $value = '' ) {
reset( $yourls_filters[ $hook ] );
do {
foreach ( (array)current( $yourls_filters[ $hook ] ) as $the_ ) {
if ( !is_null( $the_[ 'function' ] ) ) {
if ( !is_null($the_[ 'function' ]) ) {
$args[ 1 ] = $value;
$count = $the_[ 'accepted_args' ];
if ( is_null( $count ) ) {
@ -628,13 +628,19 @@ function yourls_plugin_admin_page( $plugin_page ) {
yourls_die( yourls__( 'This page does not exist. Maybe a plugin you thought was activated is inactive?' ), yourls__( 'Invalid link' ) );
}
// Check the plugin page function is actually callable
$page_function = $pages[ $plugin_page ][ 'function' ];
if (!is_callable($page_function)) {
yourls_die( yourls__( 'This page cannot be displayed because the displaying function is not callable.' ), yourls__( 'Invalid code' ) );
}
// Draw the page itself
yourls_do_action( 'load-'.$plugin_page );
yourls_html_head( 'plugin_page_'.$plugin_page, $pages[ $plugin_page ][ 'title' ] );
yourls_html_logo();
yourls_html_menu();
call_user_func( $pages[ $plugin_page ][ 'function' ] );
$page_function( );
yourls_html_footer();
}

View File

@ -28,7 +28,10 @@ class API_Func_Tests extends PHPUnit_Framework_TestCase {
public function test_api_actions( $action, $alias ) {
$action = $alias ? $alias : $action;
$this->assertTrue( is_array( call_user_func( 'yourls_api_action_' . $action ) ) );
$function = 'yourls_api_action_' . $action;
$this->assertTrue( is_callable( $function ) );
$this->assertTrue( is_array( $function() ) );
}
}

View File

@ -165,4 +165,26 @@ class Plugin_Misc_Tests extends PHPUnit_Framework_TestCase {
$this->assertSame( 1, yourls_did_action( $action ) );
}
/**
* Simulate a valid plugin admin page but with a drawing function that is not callable
*
* @expectedException Exception
* @since 0.1
*/
public function test_plugin_admin_page_not_callable() {
$plugin = rand_str();
$title = rand_str();
$action = rand_str();
$func = rand_str();
yourls_register_plugin_page( $plugin, $title, $func );
$this->assertSame( 0, yourls_did_action( 'load-' . $plugin ) );
// intercept yourls_die() before it actually dies
yourls_add_action( 'pre_yourls_die', function() { throw new Exception( 'I have died' ); } );
// This should trigger yourls_die()
yourls_plugin_admin_page( $plugin );
}
}