0 && ! in_array( $selected_id, array_map( 'intval', array_column( $locations, 'id' ) ), true ); if ( ! ModuleToggles::instance()->is_enabled( 'localpickup' ) ) { $selected_id = 0; $invalid = false; } elseif ( $selected_id <= 0 && isset( $locations[0] ) ) { $selected_id = (int) $locations[0]['id']; } // Keep invalid explicit selections in the session: final validation must require a new choice. return [ 'available' => [] !== $locations, 'selectedLocationId' => $invalid ? 0 : $selected_id, 'selectionInvalid' => $invalid, 'locations' => $locations, ]; } /** * @return array> */ public static function get_active_locations(): array { if ( ! ModuleToggles::instance()->is_enabled( 'localpickup' ) ) { return []; } $query = new \WP_Query( [ 'post_type' => CartDataManifest::LOCAL_PICKUP_POST_TYPE, 'post_status' => [ 'publish' ], 'posts_per_page' => -1, 'orderby' => 'date', 'order' => 'DESC', 'no_found_rows' => true, ] ); $locations = []; foreach ( $query->posts as $post ) { if ( ! $post instanceof WP_Post ) { continue; } $location = self::build_location_payload( $post ); if ( null === $location ) { continue; } $locations[] = $location; } $locations = apply_filters( 'ipscmi_order_cart_localpickup_available_locations', $locations ); if ( ! is_array( $locations ) ) { return []; } return array_values( array_filter( $locations, static function( $location ): bool { return is_array( $location ) && [] !== $location; } ) ); } /** * @return array|null */ public static function get_active_location( int $post_id ): ?array { if ( ! ModuleToggles::instance()->is_enabled( 'localpickup' ) ) { return null; } $post = get_post( $post_id ); if ( ! $post instanceof WP_Post || CartDataManifest::LOCAL_PICKUP_POST_TYPE !== $post->post_type || 'publish' !== get_post_status( $post ) ) { return null; } $location = self::build_location_payload( $post ); $location = apply_filters( 'ipscmi_order_cart_localpickup_active_location', $location, $post_id ); return is_array( $location ) && [] !== $location ? $location : null; } /** * @return array|null */ private static function build_location_payload( WP_Post $post ): ?array { $meta = get_post_meta( $post->ID, self::META_KEY, true ); if ( ! is_array( $meta ) ) { $meta = []; } $location_name = trim( (string) ( $meta['ips-mb-localpickup-mainlocationname'] ?? '' ) ); if ( '' === $location_name ) { $location_name = trim( (string) $post->post_title ); } if ( '' === $location_name ) { $location_name = __( 'Pickup location', 'ips-order-cart' ); } return [ 'id' => (int) $post->ID, 'title' => (string) $post->post_title, 'locationName' => $location_name, 'addressActive' => rest_sanitize_boolean( $meta['ips-mb-localpickup-addressselect'] ?? true ), 'addressText' => trim( (string) ( $meta['ips-mb-localpickup-addresstext'] ?? '' ) ), 'estimatePickupTimeActive' => rest_sanitize_boolean( $meta['ips-mb-localpickup-estimatetimeselect'] ?? false ), 'estimatePickupTimeText' => trim( (string) ( $meta['ips-mb-localpickup-estimatetimetext'] ?? '' ) ), 'instructionsActive' => rest_sanitize_boolean( $meta['ips-mb-localpickup-instructionsselect'] ?? true ), 'instructionText' => trim( (string) ( $meta['ips-mb-localpickup-instructiontext'] ?? '' ) ), 'showOnThankYouPage' => rest_sanitize_boolean( $meta['ips-mb-localpickup-showthankyou'] ?? true ), 'showInEmails' => rest_sanitize_boolean( $meta['ips-mb-localpickup-showemails'] ?? true ), ]; } }