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 / GetProductSyncState.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
GetProductSyncState.php
137 lines
1 <?php
2 /**
3 * Get product sync state 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\Handlers\Product;
16 use WooCommerce\Square\Internal\Abilities\Abilities_Registrar;
17
18 /**
19 * Registers the woocommerce-square/get-product-sync-state ability.
20 *
21 * One-arg per-product read. Answers "is product X synced to Square,
22 * and if so what's its Square item ID?". Sync state is held by a
23 * taxonomy term (`wc_square_synced`, yes/no), NOT post-meta — the
24 * Square item ID is post-meta (`_square_item_id`). The ability rolls
25 * up the variation-parent lookup so callers don't need to know
26 * variations check their parent product's term.
27 *
28 * Passes `$generate_if_not_found = false` to `Product::get_square_item_id()`
29 * so the response distinguishes "synced, has a Square ID" from "synced,
30 * not yet pushed". The default `true` arg returns a synthesized
31 * `#item_<id>` placeholder which would mask the unpushed state.
32 *
33 * @internal Only loaded when WooCommerce 10.9+ is active.
34 */
35 class GetProductSyncState extends AbstractSquareAbility implements AbilityDefinition {
36
37 public static function get_name(): string {
38 return 'woocommerce-square/get-product-sync-state';
39 }
40
41 public static function get_registration_args(): array {
42 return array(
43 'label' => __( 'Get product sync state', 'woocommerce-square' ),
44 'description' => __( 'Return whether a specific WooCommerce product is set to sync with Square, plus the Square item ID (if one has been pushed) and the parent-product lookup for variations.', 'woocommerce-square' ),
45 'category' => self::CATEGORY_SLUG,
46 'input_schema' => array(
47 'type' => 'object',
48 'properties' => array(
49 'product_id' => array(
50 'type' => 'integer',
51 'minimum' => 1,
52 'description' => __( 'The WooCommerce product or variation ID.', 'woocommerce-square' ),
53 ),
54 ),
55 'required' => array( 'product_id' ),
56 'additionalProperties' => false,
57 ),
58 'execute_callback' => array( self::class, 'execute' ),
59 'permission_callback' => array( Abilities_Registrar::class, 'can_manage_woocommerce_square' ),
60 'meta' => array(
61 'annotations' => array(
62 'readonly' => true,
63 'destructive' => false,
64 'idempotent' => true,
65 ),
66 'show_in_rest' => true,
67 'mcp' => array(
68 'public' => true,
69 ),
70 ),
71 );
72 }
73
74 /**
75 * Execute callback.
76 *
77 * @param mixed $input Expected shape: array{product_id: int}.
78 * @return array|\WP_Error
79 */
80 public static function execute( $input = null ) {
81 if ( ! is_array( $input ) || ! array_key_exists( 'product_id', $input ) ) {
82 return new \WP_Error(
83 'woocommerce_square_missing_product_id',
84 __( 'A product_id is required.', 'woocommerce-square' )
85 );
86 }
87
88 $product_id = (int) $input['product_id'];
89 if ( $product_id < 1 ) {
90 return new \WP_Error(
91 'woocommerce_square_invalid_product_id',
92 __( 'The product_id must be a positive integer.', 'woocommerce-square' )
93 );
94 }
95
96 if ( ! function_exists( 'wc_get_product' ) ) {
97 return new \WP_Error(
98 'woocommerce_square_not_initialized',
99 __( 'WooCommerce is not initialized.', 'woocommerce-square' )
100 );
101 }
102
103 $product = wc_get_product( $product_id );
104 if ( ! $product instanceof \WC_Product ) {
105 return new \WP_Error(
106 'woocommerce_square_product_not_found',
107 sprintf(
108 /* translators: %d: requested product id. */
109 __( 'No WooCommerce product was found with ID %d.', 'woocommerce-square' ),
110 $product_id
111 ),
112 array( 'status' => 404 )
113 );
114 }
115
116 $is_variation = $product->is_type( 'variation' );
117 $parent_product_id = $is_variation ? (int) $product->get_parent_id() : null;
118
119 // Critical: pass false for $generate_if_not_found so the response is
120 // `null` (rather than a synthesized "#item_<id>" placeholder) when
121 // the product has not yet been pushed to Square. Sync state is held
122 // on the parent for variations; Product::get_square_item_id resolves
123 // the post-meta directly via the WC_Product ID passed in.
124 $id_lookup_target = $is_variation && $parent_product_id ? $parent_product_id : $product;
125 $square_item_id = Product::get_square_item_id( $id_lookup_target, false );
126 $is_synced = Product::is_synced_with_square( $product );
127
128 return array(
129 'product_id' => $product_id,
130 'is_synced' => (bool) $is_synced,
131 'square_item_id' => $square_item_id ? (string) $square_item_id : null,
132 'is_variation' => $is_variation,
133 'parent_product_id' => $parent_product_id,
134 );
135 }
136 }
137