任何人都可以帮我理解这个变量的声明位置吗?

I am trying to learn some more advanced WordPress, I have used and adapted a code snippet but cannot understand some of it's origins. The snippet is for WooCommerce and hides other shipping methods when 'free shipping' is available.

It looks like a loop using a variable called $rates but I can't find anything in the Woo Dev Docs about this variable. I thought it may have been an instance of a class but again can't find any info that may help. Based on the snippet below, could someone tell me where $rates has originated from, what it is and where it would have been declared please?

add_filter( 'woocommerce_package_rates', 'bbloomer_unset_shipping_when_free_is_available_all_zones', 10, 2 );

function bbloomer_unset_shipping_when_free_is_available_all_zones( $rates, $package ) {

    $all_free_rates = array();

        foreach ( $rates as $rate_id => $rate ) {
        if ( 'free_shipping' === $rate->method_id ) {
            $all_free_rates[ $rate_id ] = $rate;
            break;
        }
    }

    if ( empty( $all_free_rates )) {
        return $rates;
        } else {
        return $all_free_rates;
        } 
}

The filter hook woocommerce_package_rates is part of WC_Shipping class. If you look at the source code where this hook is located, the $rates variable is in fact equal to $package['rates']:

$package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package );

The $package variable array Stores packages to ship and to get quotes for.

You can take a look to the list of questions/answers that are using this hook, in StackOverFlow, to better understand it's usage.

$rates is generated from function calculate_shipping_for_package() inside woocommerce/includes/class-wc-shipping.php. You can see the filter woocommerce_package_rates in below function

public function calculate_shipping_for_package( $package = array(), $package_key = 0 ) {
        // If shipping is disabled or the package is invalid, return false.
        if ( ! $this->enabled || empty( $package ) ) {
            return false;
        }

        $package['rates'] = array();

        // If the package is not shippable, e.g. trying to ship to an invalid country, do not calculate rates.
        if ( $this->is_package_shippable( $package ) ) {
            // Check if we need to recalculate shipping for this package.
            $package_to_hash = $package;

            // Remove data objects so hashes are consistent.
            foreach ( $package_to_hash['contents'] as $item_id => $item ) {
                unset( $package_to_hash['contents'][ $item_id ]['data'] );
            }

            $package_hash = 'wc_ship_' . md5( wp_json_encode( $package_to_hash ) . WC_Cache_Helper::get_transient_version( 'shipping' ) );
            $session_key  = 'shipping_for_package_' . $package_key;
            $stored_rates = WC()->session->get( $session_key );

            if ( ! is_array( $stored_rates ) || $package_hash !== $stored_rates['package_hash'] || 'yes' === get_option( 'woocommerce_shipping_debug_mode', 'no' ) ) {
                foreach ( $this->load_shipping_methods( $package ) as $shipping_method ) {
                    if ( ! $shipping_method->supports( 'shipping-zones' ) || $shipping_method->get_instance_id() ) {
                        $package['rates'] = $package['rates'] + $shipping_method->get_rates_for_package( $package ); // + instead of array_merge maintains numeric keys
                    }
                }

                // Filter the calculated rates.
                $package['rates'] = apply_filters( 'woocommerce_package_rates', $package['rates'], $package );

                // Store in session to avoid recalculation.
                WC()->session->set(
                    $session_key, array(
                        'package_hash' => $package_hash,
                        'rates'        => $package['rates'],
                    )
                );
            } else {
                $package['rates'] = $stored_rates['rates'];
            }
        }
        return $package;
    }