Site-kit-wp: call_user_func warnings raised when using wp-cli

Created on 4 Mar 2020  Â·  10Comments  Â·  Source: google/site-kit-wp

Bug Description

$ wp plugin list
Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in wp-includes/rest-api/class-wp-rest-server.php on line 1165
Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in wp-includes/rest-api/class-wp-rest-server.php on line 1165
$ wp plugin list --skip-plugins=google-site-kit
no warnings

Steps to reproduce

  1. Run wp plugin list

    • see warnings above plugin list table

  2. Run wp plugin list --skip-plugins=google-site-kit

    • no warnings above plugin list table

Additional Context

This seems to be environment specific and I can't duplicate on a server running PHP 7.4 (shrug)

  • PHP CLI Version: PHP 7.2.24-0ubuntu0.18.04.3 (cli)
  • PHP FPM Version: v7.2.24-0ubuntu0.18.04.3
  • Server: DISTRIB_DESCRIPTION="Ubuntu 18.04.4 LTS"
  • WP Cli Version: 2.4.0
  • WP Core version: 5.3.2
  • Google Site Kit: 1.4.0 (also reproduced on 1.3.1)

I DO have opcache enabled for fpm & cli on the affected server but when I turned it off I still got the warnings.


_Do not alter or remove anything below. The following sections will be managed by moderators only._

Acceptance criteria

  • The REST API should not trigger a PHP warning due to Site Kit.
  • Site Kit should provide REST API response schemas as expected by WordPress core.

Implementation Brief

  • Update \Google\Site_Kit\Core\Modules\Modules::get_module_schema method to return a closure that returns the schema itself.

QA Brief

  • Install and activate woocommerce
  • With Site Kit active, run wp plugin list
  • See no warnings above table output

Changelog entry

  • Fix PHP warning that could occur when retrieving REST API information via help endpoint.
Escalation P0 Bug

All 10 comments

Hi.
I have the same problem, only together WooCommerce.
I see this "warning" when add a "new REST_Route()" with "schema". Example "... 'schema' => $this->get_module_schema()..." in line 583 of file google-site-kit/includes/Core/Modules/Modules.php

Hi,
I have the same problem. Error with WooCommerce for each wp-cli request.
Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in www/wp-includes/rest-api/class-wp-rest-server.php on line 1244 (x3)

Passing this to level 2 as I don't have a host with CLI setup to try this one

receiving a similar error

root@5273c015a93e:# wp-cli --allow-root --path=/var/www/vhosts/html/ cache flush                                                 
Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /var/www/vhosts/html/wp-includes/rest-api/class-wp-rest-server.php on line 1244
Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /var/www/vhosts/html/wp-includes/rest-api/class-wp-rest-server.php on line 1244
Warning: call_user_func() expects parameter 1 to be a valid callback, array must have exactly two members in /var/www/vhosts/html/wp-includes/rest-api/class-wp-rest-server.php on line 1244
Success: The cache was flushed.

@felixarntz Looks like the bug still exists in the latest vesion of the plugin. The issue happens here:

https://github.com/google/site-kit-wp/blob/25642bc9aff64bc38e4d18739a92f15eec69ad6c/includes/Core/Modules/Modules.php#L496
https://github.com/google/site-kit-wp/blob/25642bc9aff64bc38e4d18739a92f15eec69ad6c/includes/Core/Modules/Modules.php#L556
https://github.com/google/site-kit-wp/blob/25642bc9aff64bc38e4d18739a92f15eec69ad6c/includes/Core/Modules/Modules.php#L584

The $this->get_module_schema() function returns schema array, but the rest server class expects it to be a function that returns the schema. To fix it we need to wrap get_module_schema calls with anonymous functions, something like this:

new REST_Route(
    'core/modules/data/list',
    array(
        array(
            'methods' => WP_REST_Server::READABLE,
            'callback'  => function( WP_REST_Request $request ) {
                $modules = array_map(
                    array( $this, 'prepare_module_data_for_response' ),
                    $this->get_available_modules()
                );
                return new WP_REST_Response( array_values( $modules ) );
            },
            'permission_callback' => $can_authenticate,
        ),
    ),
    array(
        'schema' => function() {
            return $this->get_module_schema();
        },
    )
),

Or we can make get_modules_schema to be public and then we cau make it to be callable:

array(
    'schema' => array( $this, 'get_module_schema' ),
)

Thanks @eugene-manuilov, let's fix this for the next release.

@felixarntz could you please check my IB? Is there anything you want me to add/update?

@eugene-manuilov IB looks good, except let's keep the method private and pass a closure to schema instead, which calls the method (tentative IB ✅ ).

If that's okay with you, feel free to just make that adjustment and move directly to execution.

Ok, updated. Moving to the execution.

Tested

Built latest develop, installed SK and Woocommerce, activated both.

Checked wp plugin list

image

No errors to report.

Passed QA ✅

Was this page helpful?
0 / 5 - 0 ratings

Related issues

felixarntz picture felixarntz  Â·  4Comments

Loganson picture Loganson  Â·  5Comments

aaemnnosttv picture aaemnnosttv  Â·  4Comments

jsmshay picture jsmshay  Â·  3Comments

aaemnnosttv picture aaemnnosttv  Â·  5Comments