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 |