array( 'type' => 'string', 'minLength' => 10, 'maxLength' => 4000, ), 'purpose' => array( 'type' => 'string', 'maxLength' => 300, ), ), array( 'sql', 'purpose' ) ); } /** * Runs the data function. * * @param array $args Validated arguments. * @return array */ public function run( array $args ): array { global $wpdb; $sql = SqlGuard::prepare( (string) $args['sql'] ); $result = SqlGuard::run( $sql ); $notes = array( 'A custom query written for this question. Built-in lookups follow WooCommerce Analytics exactly; check this one’s filters before relying on it.' ); $lower = strtolower( (string) $args['sql'] ); if ( str_contains( $lower, 'wc_order_stats' ) && ! str_contains( $lower, 'status' ) ) { $notes[] = sprintf( 'This query does not filter order status. WooCommerce Analytics leaves out orders with these statuses: %s.', implode( ', ', array_map( static fn( string $s ): string => 'wc-' . $s, Analytics::excluded_statuses() ) ) ); } if ( $result['truncated'] ) { $notes[] = sprintf( 'Only the first %d rows are shown.', SqlGuard::MAX_ROWS ); } return array( // Shown to the owner in "How I got this", without the table prefix. 'sql' => str_replace( '`' . $wpdb->prefix, '`', $sql ), 'columns' => $result['columns'], 'rows' => $result['rows'], 'row_count' => count( $result['rows'] ), 'truncated' => $result['truncated'], 'rows_read' => count( $result['rows'] ), 'notes' => $notes, ); } }