flexible-shipping
/
src
/
WPDesk
/
FS
/
TableRate
/
ShippingMethod
/
Timestamps
/
MethodTimestamps.php
MethodTimestamps.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPDesk\FS\TableRate\ShippingMethod\Timestamps; |
| 4 | |
| 5 | use FSVendor\WPDesk\PluginBuilder\Plugin\Hookable; |
| 6 | use WPDesk\FS\TableRate\ShippingMethodSingle; |
| 7 | |
| 8 | class MethodTimestamps implements Hookable { |
| 9 | |
| 10 | public const CREATION_TIME_WITH_FREE = 'creation_time_with_free'; |
| 11 | public const METHOD_RULES_UPDATE_TIME_WITH_FREE = 'method_rules_update_time_with_free'; |
| 12 | |
| 13 | public function hooks(): void { |
| 14 | add_action( 'woocommerce_shipping_zone_method_added', [ $this, 'save_creation_time_on_shipping_method' ], 10, 3 ); |
| 15 | add_filter( 'woocommerce_shipping_flexible_shipping_single_instance_settings_values', [ $this, 'save_rules_table_update_time_on_shipping_method' ], 10, 2 ); |
| 16 | } |
| 17 | |
| 18 | /** |
| 19 | * @param int $instance_id |
| 20 | * @param string $type |
| 21 | * @param int $zone_id |
| 22 | * |
| 23 | * @return void |
| 24 | */ |
| 25 | public function save_creation_time_on_shipping_method( $instance_id, $type, $zone_id ): void { |
| 26 | if ( $this->is_pro_plugin_active() ) { |
| 27 | return; |
| 28 | } |
| 29 | if ( $type !== 'flexible_shipping_single' ) { |
| 30 | return; |
| 31 | } |
| 32 | $shipping_method = new ShippingMethodSingle( $instance_id ); |
| 33 | $shipping_method->update_instance_option( self::CREATION_TIME_WITH_FREE, current_time( 'timestamp' ) ); // phpcs:ignore |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param array $settings |
| 38 | * @param \WC_Shipping_Method $shipping_method |
| 39 | * |
| 40 | * @return array |
| 41 | */ |
| 42 | public function save_rules_table_update_time_on_shipping_method( $settings, $shipping_method ) { |
| 43 | if ( $this->is_pro_plugin_active() ) { |
| 44 | return $settings; |
| 45 | } |
| 46 | $current_settings = get_option( $shipping_method->get_instance_option_key(), [] ); |
| 47 | if ( is_array( $settings ) && is_array( $current_settings ) && ( $settings['method_rules'] ?? '' ) !== ( $current_settings['method_rules'] ?? '' ) ) { |
| 48 | $settings[ self::METHOD_RULES_UPDATE_TIME_WITH_FREE ] = current_time( 'timestamp' ); // phpcs:ignore |
| 49 | } |
| 50 | |
| 51 | return $settings; |
| 52 | } |
| 53 | |
| 54 | private function is_pro_plugin_active(): bool { |
| 55 | return defined( 'FLEXIBLE_SHIPPING_PRO_VERSION' ); |
| 56 | } |
| 57 | } |
| 58 |