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
EditorChangeTracker.php
77 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WCML\EditorScopedSync; |
| 4 | |
| 5 | class EditorChangeTracker implements \IWPML_Backend_Action { |
| 6 | |
| 7 | const PRIORITY_BEFORE_WC_SAVES = 0; |
| 8 | |
| 9 | private static $editedVariationIds = []; |
| 10 | |
| 11 | private static $deletedVariationIds = []; |
| 12 | |
| 13 | private static $productChanges = []; |
| 14 | |
| 15 | public function add_hooks() { |
| 16 | add_action( 'woocommerce_save_product_variation', [ __CLASS__, 'recordVariationSave' ], self::PRIORITY_BEFORE_WC_SAVES, 2 ); |
| 17 | add_action( 'before_delete_post', [ __CLASS__, 'recordVariationDelete' ], self::PRIORITY_BEFORE_WC_SAVES, 2 ); |
| 18 | add_action( 'woocommerce_admin_process_product_object', [ __CLASS__, 'recordProductChanges' ], self::PRIORITY_BEFORE_WC_SAVES, 1 ); |
| 19 | } |
| 20 | |
| 21 | public static function recordVariationSave( $variationId, $i ) { |
| 22 | $variationId = (int) $variationId; |
| 23 | $parent = (int) wp_get_post_parent_id( $variationId ); |
| 24 | if ( $parent <= 0 ) { |
| 25 | return; |
| 26 | } |
| 27 | self::$editedVariationIds[ $parent ][ $variationId ] = true; |
| 28 | } |
| 29 | |
| 30 | public static function recordVariationDelete( $postId, $post = null ) { |
| 31 | if ( ! $post || 'product_variation' !== $post->post_type ) { |
| 32 | return; |
| 33 | } |
| 34 | $parent = (int) $post->post_parent; |
| 35 | if ( $parent <= 0 ) { |
| 36 | return; |
| 37 | } |
| 38 | self::$deletedVariationIds[ $parent ][ (int) $postId ] = true; |
| 39 | } |
| 40 | |
| 41 | public static function recordProductChanges( $product ) { |
| 42 | if ( ! is_object( $product ) || ! method_exists( $product, 'get_changes' ) || ! method_exists( $product, 'get_id' ) ) { |
| 43 | return; |
| 44 | } |
| 45 | self::$productChanges[ (int) $product->get_id() ] = $product->get_changes(); |
| 46 | } |
| 47 | |
| 48 | public static function editedVariationIdsFor( $productId ) { |
| 49 | $productId = (int) $productId; |
| 50 | $ids = isset( self::$editedVariationIds[ $productId ] ) ? self::$editedVariationIds[ $productId ] : []; |
| 51 | return array_keys( $ids ); |
| 52 | } |
| 53 | |
| 54 | public static function deletedVariationIdsFor( $productId ) { |
| 55 | $productId = (int) $productId; |
| 56 | $ids = isset( self::$deletedVariationIds[ $productId ] ) ? self::$deletedVariationIds[ $productId ] : []; |
| 57 | return array_keys( $ids ); |
| 58 | } |
| 59 | |
| 60 | public static function productChangesFor( $productId ) { |
| 61 | $productId = (int) $productId; |
| 62 | return isset( self::$productChanges[ $productId ] ) ? self::$productChanges[ $productId ] : []; |
| 63 | } |
| 64 | |
| 65 | public static function hasAnyVariationActivity( $productId ) { |
| 66 | $productId = (int) $productId; |
| 67 | return ! empty( self::$editedVariationIds[ $productId ] ) |
| 68 | || ! empty( self::$deletedVariationIds[ $productId ] ); |
| 69 | } |
| 70 | |
| 71 | public static function reset() { |
| 72 | self::$editedVariationIds = []; |
| 73 | self::$deletedVariationIds = []; |
| 74 | self::$productChanges = []; |
| 75 | } |
| 76 | } |
| 77 |