PluginProbe ʕ •ᴥ•ʔ
WooCommerce Square / 5.4.2
WooCommerce Square v5.4.2
5.4.2 5.4.1 5.4.0 trunk 1.0.25 1.0.26 1.0.27 1.0.28 1.0.29 1.0.30 1.0.31 1.0.32 1.0.33 1.0.34 1.0.35 1.0.36 1.0.37 1.0.38 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.7.0 2.8.0 2.9.0 2.9.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0 3.2.0 3.3.0 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.7.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 4.0.0 4.1.0 4.2.0 4.2.1 4.2.2 4.2.3 4.3.0 4.3.1 4.3.2 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.2 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.9.0 4.9.1 4.9.2 4.9.3 4.9.4 4.9.5 4.9.6 4.9.7 4.9.8 4.9.9 5.0.0 5.0.1 5.1.0 5.1.1 5.1.2 5.2.0 5.3.0 5.3.1 5.3.2 5.3.3
woocommerce-square / includes / Internal / Abilities / Domain / GetLocations.php
woocommerce-square / includes / Internal / Abilities / Domain Last commit date
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