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
GetSyncStatus.php
104 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Get sync status 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-sync-status ability. |
| 19 | * |
| 20 | * Reference ability for the Square for WooCommerce Abilities API rollout — |
| 21 | * zero-arg overview answering "is Square product/inventory sync healthy?". |
| 22 | * Returns a small associative array composed from six accessor methods on |
| 23 | * the Sync handler: whether a sync is currently in progress, the id of the |
| 24 | * running job (if any), the last product- and inventory-sync timestamps, |
| 25 | * the next scheduled sync timestamp, and whether product sync is enabled. |
| 26 | * |
| 27 | * Today this state is only exposed via the `wc_square_get_sync_with_square_status` |
| 28 | * wp_ajax_* handler; this ability fills the agent-facing gap without adding |
| 29 | * a new REST endpoint. |
| 30 | * |
| 31 | * @internal Only loaded when WooCommerce 10.9+ is active. The |
| 32 | * Abilities_Registrar short-circuits before referencing this |
| 33 | * class on earlier WC versions; PHP's lazy autoload means the |
| 34 | * unresolved AbilityDefinition interface FQN never reaches the |
| 35 | * parser there. |
| 36 | */ |
| 37 | class GetSyncStatus extends AbstractSquareAbility implements AbilityDefinition { |
| 38 | |
| 39 | public static function get_name(): string { |
| 40 | return 'woocommerce-square/get-sync-status'; |
| 41 | } |
| 42 | |
| 43 | public static function get_registration_args(): array { |
| 44 | return array( |
| 45 | 'label' => __( 'Get Square sync status', 'woocommerce-square' ), |
| 46 | 'description' => __( 'Return the current Square sync state — whether a sync is in progress, the last product- and inventory-sync timestamps, the next scheduled sync, and whether product sync is enabled.', 'woocommerce-square' ), |
| 47 | 'category' => self::CATEGORY_SLUG, |
| 48 | 'input_schema' => array( |
| 49 | 'type' => 'object', |
| 50 | 'default' => (object) array(), |
| 51 | 'properties' => array(), |
| 52 | 'additionalProperties' => false, |
| 53 | ), |
| 54 | 'execute_callback' => array( self::class, 'execute' ), |
| 55 | 'permission_callback' => array( Abilities_Registrar::class, 'can_manage_woocommerce_square' ), |
| 56 | // output_schema deliberately omitted — see first-ability-template §"output_schema omission rule". |
| 57 | 'meta' => array( |
| 58 | 'annotations' => array( |
| 59 | 'readonly' => true, |
| 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 | * Composes a small associative array from the Sync handler's read methods. |
| 75 | * The job object returned by `get_job_in_progress()` is coerced to a |
| 76 | * stable string id (or null) — the internal class shape is not part of |
| 77 | * the ability contract. |
| 78 | * |
| 79 | * @param mixed $input Ignored; the ability accepts no input. |
| 80 | * @return array|\WP_Error Associative array of sync state, or WP_Error |
| 81 | * when the plugin has not finished initializing. |
| 82 | */ |
| 83 | public static function execute( $input = null ) { |
| 84 | unset( $input ); |
| 85 | |
| 86 | $sync_handler = self::get_sync_handler_or_error(); |
| 87 | if ( is_wp_error( $sync_handler ) ) { |
| 88 | return $sync_handler; |
| 89 | } |
| 90 | |
| 91 | $job = $sync_handler->get_job_in_progress(); |
| 92 | $current_job_id = ( is_object( $job ) && isset( $job->id ) ) ? (string) $job->id : null; |
| 93 | |
| 94 | return array( |
| 95 | 'is_in_progress' => (bool) $sync_handler->is_sync_in_progress(), |
| 96 | 'is_sync_enabled' => (bool) $sync_handler->is_sync_enabled(), |
| 97 | 'current_job_id' => $current_job_id, |
| 98 | 'last_synced_at' => $sync_handler->get_last_synced_at(), |
| 99 | 'inventory_last_synced_at' => $sync_handler->get_inventory_last_synced_at(), |
| 100 | 'next_sync_at' => $sync_handler->get_next_sync_at(), |
| 101 | ); |
| 102 | } |
| 103 | } |
| 104 |