xwp / stream

🗄️ Stream plugin for WordPress
https://wordpress.org/plugins/stream/
GNU General Public License v2.0
407 stars 116 forks source link

WP-CLI stream query fatal error: array_key_exists() must be of type array #1437

Closed dougaxe1 closed 2 months ago

dougaxe1 commented 1 year ago

Bug Report

The issue is inside \WP_Stream\CLI->query():

        $records = wp_stream_get_instance()->db->query( $query_args );

        // Make structure Formatter compatible.
        foreach ( (array) $records as $key => $record ) {
            $formatted_records[ $key ] = array();

            // Catch any fields missing in records.
            foreach ( $fields as $field ) {
                if ( ! array_key_exists( $field, $record ) ) {
                    $record->$field = null;
                }
            }

wp_stream_get_instance()->db->query() calls \WP_Stream\Query->query():

        /**
         * QUERY THE DATABASE FOR RESULTS
         */
        $result = array(
            'items' => $wpdb->get_results( $query ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
            'count' => absint( $wpdb->get_var( $count_query ) ), // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
        );

Results from $wpdb->get_results( $query ) default to $output of OBJECT. Line 137 of class-cli.php then calls array_key_exists( $field, $record ) where $record is an object instead of an array.

Possible Fixes

  1. Return the results as an associative array $wpdb->get_results( $query, ARRAY_A ) in \WP_Stream\Query->query()
  2. Use property_exists( $record, $field ) instead of array_key_exists in \WP_Stream\CLI->query()

Expected Behavior

Run the query WP-CLI stream command:

$ wp stream query
+---------------------+-----------+---------+---------------+-------------------------------------------------+
| created             | ip        | user_id | user_role     | summary                                         |
+---------------------+-----------+---------+---------------+-------------------------------------------------+
...
+---------------------+-----------+---------+---------------+-------------------------------------------------+

Actual Behavior

Fatal error: Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, stdClass given .../wp-content/plugins/stream/classes/class-cli.php(137): array_key_exists()

Steps to Reproduce the Problem

  1. Run wp stream query via WP-CLI
  2. View debug/error

System Information

jjpmann commented 1 year ago

Seems to be a php 8+ bug

7.4 warnings:

PHP Deprecated:  array_key_exists(): Using array_key_exists() on objects is deprecated. Use isset() or property_exists() instead in /Users/jerryprice/Sites/bn/wp-content/plugins/stream/classes/class-cli.php on line 137
delawski commented 2 months ago

This should be fixed by #1437 in the 4.0.1 release.