query_vars['adscaleShipping'] = sanitize_key( wp_unslash( $_GET['adscaleShipping'] ) ); } // make sure endpoint request if ( ! isset( $wp->query_vars['adscaleShipping'] ) ) { return; } try { ob_start(); // Error handler self::setErrorHandler(); Helper::nocache_headers(); // Check Request (Guards) self::checkRequest(); ob_end_clean(); // Handle Request self::handleRequest(); // Default error Helper::send_response_formatted_error( self::makeErrorBody() ); } catch ( PluginApiException $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getApiErrorCode(), $ex->getMessage(), $ex->getApiErrorDetails() ) ); } catch ( \Exception $ex ) { Helper::send_response_formatted_error( self::makeErrorBody( $ex->getCode(), $ex->getMessage() ) ); } } /** * Handle API request * */ public static function handleRequest() { $zones_data = []; $zones = \WC_Shipping_Zones::get_zones(); if ( ! is_array( $zones ) ) { Helper::send_response_formatted_error( self::makeErrorBody('S000','Unknown error', 'Shipping zones could not be found' ) ); } foreach ( $zones as $zone ) { $zones_data[] = self::getShippingZoneData( $zone ); } Helper::send_response_formatted_success( [ 'shipping_zones' => $zones_data, ] ); } /** * Get Shipping Zone data * * @param array $sz_data * * @return array Resolved Shipping Zone data */ public static function getShippingZoneData( $sz_data ) { try { return [ 'id' => Helper::get_array_value( $sz_data, 'zone_id', null, 'intval' ), 'name' => Helper::get_array_value( $sz_data, 'zone_name', null ), 'locations' => self::getShippingZoneLocationsData( $sz_data ), 'shipping_methods' => self::getShippingZoneMethodsData( $sz_data ), ]; } catch ( \Exception $ex ) { Helper::send_response_formatted_error( self::makeErrorBody('S000','Unknown error', $ex->getMessage() ) ); } } /** * Get Shipping Zone Locations data * * @param array $sz_data * * @return array Resolved data */ public static function getShippingZoneLocationsData( $sz_data ) { $zone_locations = Helper::get_array_value( $sz_data, 'zone_locations', [] ); $locations = []; foreach ( $zone_locations as $zone_location ) { $type = Helper::get_obj_property( $zone_location, 'type' ); $code = Helper::get_obj_property( $zone_location, 'code' ); if ( 'continent' === $type ) { $name = Helper::get_full_continent_name( $code ); } elseif ( 'country' === $type ) { $name = Helper::get_full_country_name( $code ); } elseif ( 'state' === $type ) { $state_data = explode( ':', $code ); $state_country_code = Helper::get_array_value( $state_data, 0, null ); $state_code = Helper::get_array_value( $state_data, 1, null ); $state_str_p1 = Helper::get_full_country_name( $state_country_code ); $state_str_p2 = Helper::get_full_state_name( $state_country_code, $state_code ); $name = "$state_str_p1, $state_str_p2"; } elseif ( 'postcode' === $type ) { $name = ''; } else { $name = ''; } $locations[] = [ 'type' => $type, 'code' => $code, 'name' => $name, ]; } return $locations; } /** * Get Shipping Zone Methods data * * @param array $sz_data * * @return array Resolved data */ public static function getShippingZoneMethodsData( $sz_data ) { $shipping_methods = Helper::get_array_value( $sz_data, 'shipping_methods', [] ); $shipping_methods_data = []; foreach ( $shipping_methods as $shipping_method ) { if ( $shipping_method instanceof \WC_Shipping_Method && $shipping_method->is_enabled() ) { $shipping_methods_data[] = [ 'id' => $shipping_method->get_instance_id(), 'name' => $shipping_method->get_title(), 'method_name' => $shipping_method->get_method_title(), ]; } } return $shipping_methods_data; } }