EditorChangeTracker.php
1 week ago
ForceUpdateEndpoint.php
1 week ago
HooksFactory.php
1 week ago
Mode.php
1 week ago
Notices.php
1 week ago
Settings.php
1 week ago
SyncGate.php
1 week ago
ForceUpdateEndpoint.php
63 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\EditorScopedSync; |
| 4 | |
| 5 | use WCML\Synchronization\Manager as SyncManager; |
| 6 | use function WPML\Container\make; |
| 7 | |
| 8 | class ForceUpdateEndpoint implements \IWPML_Backend_Action { |
| 9 | |
| 10 | const ACTION = 'wcml_force_full_variation_sync'; |
| 11 | const NONCE = 'wcml_force_full_variation_sync'; |
| 12 | |
| 13 | const PRIORITY_OVERRIDE_SYNC_GATE = 100; |
| 14 | |
| 15 | public function add_hooks() { |
| 16 | add_action( 'wp_ajax_' . self::ACTION, [ __CLASS__, 'handle' ] ); |
| 17 | } |
| 18 | |
| 19 | public static function handle() { |
| 20 | check_ajax_referer( self::NONCE, '_wpnonce' ); |
| 21 | if ( ! current_user_can( 'edit_products' ) ) { |
| 22 | wp_send_json_error( [ 'message' => 'forbidden' ], 403 ); |
| 23 | } |
| 24 | |
| 25 | $productId = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; |
| 26 | if ( ! $productId ) { |
| 27 | wp_send_json_error( [ 'message' => 'missing product_id' ], 400 ); |
| 28 | } |
| 29 | |
| 30 | $product = get_post( $productId ); |
| 31 | if ( ! $product || 'product' !== $product->post_type) { |
| 32 | wp_send_json_error( [ 'message' => 'invalid product' ], 404 ); |
| 33 | } |
| 34 | |
| 35 | add_filter( 'wcml_editor_scoped_variation_ids', '__return_null', self::PRIORITY_OVERRIDE_SYNC_GATE ); |
| 36 | |
| 37 | try { |
| 38 | do_action( 'wcml_force_full_product_sync_run', $productId, $product ); |
| 39 | make( SyncManager::class )->run( $product ); |
| 40 | $message = 'sync completed'; |
| 41 | $success = true; |
| 42 | } catch ( \Throwable $e ) { |
| 43 | $message = 'error: ' . $e->getMessage(); |
| 44 | $success = false; |
| 45 | } |
| 46 | |
| 47 | remove_filter( 'wcml_editor_scoped_variation_ids', '__return_null', self::PRIORITY_OVERRIDE_SYNC_GATE ); |
| 48 | |
| 49 | wp_send_json( |
| 50 | [ |
| 51 | 'product_id' => $productId, |
| 52 | 'message' => $message, |
| 53 | 'success' => $success, |
| 54 | ], |
| 55 | 200 |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | public static function nonce() { |
| 60 | return wp_create_nonce( self::NONCE ); |
| 61 | } |
| 62 | } |
| 63 |