AbstractSquareAbility.php
1 month ago
GetConnectionStatus.php
1 month ago
GetLocations.php
1 month ago
GetOrderPaymentStatus.php
1 month ago
GetPendingJobs.php
1 month ago
GetProductSyncState.php
1 month ago
GetSyncRecords.php
1 month ago
GetSyncStatus.php
1 month ago
GetLocations.php
141 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Get Square locations ability definition. |
| 4 | * |
| 5 | * @package WooCommerce\Square |
| 6 | */ |
| 7 | |
| 8 | // @phan-file-suppress PhanUndeclaredClassMethod, PhanUndeclaredFunction @phan-suppress-current-line UnusedSuppression -- Abilities API + AbilityDefinition added in WC 10.9; suppression covers older-WC compat runs where this class never loads. |
| 9 | |
| 10 | namespace WooCommerce\Square\Internal\Abilities\Domain; |
| 11 | |
| 12 | defined( 'ABSPATH' ) || exit; |
| 13 | |
| 14 | use Automattic\WooCommerce\Abilities\AbilityDefinition; |
| 15 | use WooCommerce\Square\Internal\Abilities\Abilities_Registrar; |
| 16 | |
| 17 | /** |
| 18 | * Registers the woocommerce-square/get-locations ability. |
| 19 | * |
| 20 | * Returns the Square locations the merchant's connected account exposes — |
| 21 | * id, name, status, currency, country (when available). Empty array when |
| 22 | * not connected. Coerces Square SDK Location objects (\Square\Models\Location) |
| 23 | * into plain associative arrays so the SDK class shape stays out of the |
| 24 | * ability contract. |
| 25 | * |
| 26 | * Caveat: `Settings::get_locations()` round-trips the Square API on cache |
| 27 | * miss (transient ttl is 1 hour). Treat the response as point-in-time. |
| 28 | * Agents that need fresh data after a manual disconnect/reconnect must |
| 29 | * accept a small staleness window. |
| 30 | * |
| 31 | * @internal Only loaded when WooCommerce 10.9+ is active. |
| 32 | */ |
| 33 | class GetLocations extends AbstractSquareAbility implements AbilityDefinition { |
| 34 | |
| 35 | public static function get_name(): string { |
| 36 | return 'woocommerce-square/get-locations'; |
| 37 | } |
| 38 | |
| 39 | public static function get_registration_args(): array { |
| 40 | return array( |
| 41 | 'label' => __( 'Get Square locations', 'woocommerce-square' ), |
| 42 | 'description' => __( 'Return the Square locations the merchant\'s connected account exposes (id, name, status, currency, country). Empty array when not connected. Response is cached for ~1 hour; treat as point-in-time. On cold cache the call hydrates a transient and may self-heal a stale stored location_id, so the underlying path is not strictly read-only — phase 2 will switch to a bypass.', 'woocommerce-square' ), |
| 43 | 'category' => self::CATEGORY_SLUG, |
| 44 | 'input_schema' => array( |
| 45 | 'type' => 'object', |
| 46 | 'default' => (object) array(), |
| 47 | 'properties' => array(), |
| 48 | 'additionalProperties' => false, |
| 49 | ), |
| 50 | 'execute_callback' => array( self::class, 'execute' ), |
| 51 | 'permission_callback' => array( Abilities_Registrar::class, 'can_manage_woocommerce_square' ), |
| 52 | 'meta' => array( |
| 53 | 'annotations' => array( |
| 54 | // Cold-cache path writes a transient and may call |
| 55 | // Settings::clear_location_id() (self-heal of a stale |
| 56 | // stored location_id). Reflected here so MCP/agent |
| 57 | // clients see the honest contract; phase 2 will |
| 58 | // bypass Settings::get_locations() and restore readonly. |
| 59 | 'readonly' => false, |
| 60 | 'destructive' => false, |
| 61 | 'idempotent' => true, |
| 62 | ), |
| 63 | 'show_in_rest' => true, |
| 64 | 'mcp' => array( |
| 65 | 'public' => true, |
| 66 | ), |
| 67 | ), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Execute callback. |
| 73 | * |
| 74 | * @param mixed $input Ignored. |
| 75 | * @return array|\WP_Error |
| 76 | */ |
| 77 | public static function execute( $input = null ) { |
| 78 | unset( $input ); |
| 79 | |
| 80 | $settings = self::get_settings_handler_or_error(); |
| 81 | if ( is_wp_error( $settings ) ) { |
| 82 | return $settings; |
| 83 | } |
| 84 | |
| 85 | // Settings::get_locations() has two cold-cache side-effects: |
| 86 | // (1) hydrates the wc_square_locations_<ver> transient (TTL 1 hour); |
| 87 | // (2) self-heals a stale stored location_id via clear_location_id(). |
| 88 | // These are reflected in the ability's `readonly: false` annotation. |
| 89 | // Phase 2 will bypass Settings::get_locations() (read transient + API |
| 90 | // client directly, skip the self-heal) and restore `readonly: true`. |
| 91 | $locations = $settings->get_locations( false ); |
| 92 | |
| 93 | if ( ! is_array( $locations ) ) { |
| 94 | return array(); |
| 95 | } |
| 96 | |
| 97 | $out = array(); |
| 98 | foreach ( $locations as $location ) { |
| 99 | $out[] = self::normalize_location( $location ); |
| 100 | } |
| 101 | return $out; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Coerce a Square SDK Location object (or array) into a plain array |
| 106 | * with the keys agents can rely on. |
| 107 | * |
| 108 | * @param mixed $location Square Location object or array-shaped value. |
| 109 | * @return array |
| 110 | */ |
| 111 | private static function normalize_location( $location ): array { |
| 112 | if ( is_array( $location ) ) { |
| 113 | return array( |
| 114 | 'id' => isset( $location['id'] ) ? (string) $location['id'] : '', |
| 115 | 'name' => isset( $location['name'] ) ? (string) $location['name'] : '', |
| 116 | 'status' => isset( $location['status'] ) ? (string) $location['status'] : '', |
| 117 | 'currency' => isset( $location['currency'] ) ? (string) $location['currency'] : '', |
| 118 | 'country' => isset( $location['country'] ) ? (string) $location['country'] : '', |
| 119 | ); |
| 120 | } |
| 121 | |
| 122 | if ( ! is_object( $location ) ) { |
| 123 | return array( |
| 124 | 'id' => '', |
| 125 | 'name' => '', |
| 126 | 'status' => '', |
| 127 | 'currency' => '', |
| 128 | 'country' => '', |
| 129 | ); |
| 130 | } |
| 131 | |
| 132 | return array( |
| 133 | 'id' => method_exists( $location, 'getId' ) ? (string) $location->getId() : '', |
| 134 | 'name' => method_exists( $location, 'getName' ) ? (string) $location->getName() : '', |
| 135 | 'status' => method_exists( $location, 'getStatus' ) ? (string) $location->getStatus() : '', |
| 136 | 'currency' => method_exists( $location, 'getCurrency' ) ? (string) $location->getCurrency() : '', |
| 137 | 'country' => method_exists( $location, 'getCountry' ) ? (string) $location->getCountry() : '', |
| 138 | ); |
| 139 | } |
| 140 | } |
| 141 |