PluginProbe
ShopBuilder – WooCommerce Builder For Elementor / 3.2.6
ShopBuilder – WooCommerce Builder For Elementor v3.2.6
3.4.2 3.4.1 3.4.0 2.0.1 2.0.2 2.0.3 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 All 63 releases
shopbuilder / app / Modules / AbandonedCartRecovery / CartRecoveryCron.php

CartRecoveryCron.php in ShopBuilder – WooCommerce Builder For Elementor 3.2.6, at app/Modules/AbandonedCartRecovery/CartRecoveryCron.php

150 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Sticky Abandoned Cart Recovery Module Class.
4 *
5 * @package RadiusTheme\SB
6 */
7
8 namespace RadiusTheme\SB\Modules\AbandonedCartRecovery;
9
10 use RadiusTheme\SB\Helpers\Fns;
11 use RadiusTheme\SB\Traits\SingletonTrait;
12
13
14 defined( 'ABSPATH' ) || exit();
15
16 /**
17 * Sticky add-to-cart Module Class.
18 */
19 class CartRecoveryCron {
20 /**
21 * Singleton Trait.
22 */
23 use SingletonTrait;
24
25 /**
26 * Create custom schedule.
27 *
28 * @param array $schedules schedules.
29 * @return mixed
30 */
31 public function abandoned_cart_cron( $schedules ) {
32 $time = CartRecoveryFns::get_options( 'abandoned_time', 20 );
33 $cron_time = apply_filters( 'rtsb/ca/abandoned/time/interval', $time );
34 $schedule_key = 'abandoned_time_in_minutes_' . $cron_time;
35 if ( ! isset( $schedules[ $schedule_key ] ) ) {
36 $schedules[ $schedule_key ] = [
37 'interval' => absint( $cron_time ) * MINUTE_IN_SECONDS,
38 'display' => __( 'Abandoned Time In Minutes', 'shopbuilder' ),
39 ];
40 }
41 // Register the existing cron event's schedule to prevent reschedule errors.
42 $existing_event = wp_get_scheduled_event( 'rtsb_abandoned_cart_recovery_action' );
43 if ( $existing_event && ! empty( $existing_event->schedule ) && ! isset( $schedules[ $existing_event->schedule ] ) ) {
44 $schedules[ $existing_event->schedule ] = [
45 'interval' => ! empty( $existing_event->interval ) ? $existing_event->interval : absint( $cron_time ) * MINUTE_IN_SECONDS,
46 'display' => __( 'Abandoned Time In Minutes', 'shopbuilder' ),
47 ];
48 }
49 if ( ! isset( $schedules['every_minute'] ) ) {
50 $schedules['every_minute'] = [
51 'interval' => 60,
52 'display' => __( 'Every Minute', 'shopbuilder' ),
53 ];
54 }
55 return $schedules;
56 }
57
58 /**
59 * Run Cron Function
60 *
61 * @return void
62 */
63 public function run_cron() {
64 $recovery_action = 'rtsb_abandoned_cart_recovery_action';
65 $time = CartRecoveryFns::get_options( 'abandoned_time', 20 );
66 $cron_time = apply_filters( 'rtsb/ca/abandoned/time/interval', $time );
67 $schedule_key = 'abandoned_time_in_minutes_' . $cron_time;
68 $existing_event = wp_get_scheduled_event( $recovery_action );
69
70 // Clear and reschedule if the schedule key has changed.
71 if ( $existing_event && $existing_event->schedule !== $schedule_key ) {
72 wp_clear_scheduled_hook( $recovery_action );
73 $existing_event = false;
74 }
75
76 if ( ! $existing_event ) {
77 wp_schedule_event( time(), $schedule_key, $recovery_action );
78 Fns::add_to_scheduled_hook_list( $recovery_action );
79 }
80
81 /**
82 * Ensure the cron event is scheduled.
83 */
84 $abandoned_cart_emails = 'rtsb_send_abandoned_cart_emails';
85 if ( ! wp_next_scheduled( $abandoned_cart_emails ) ) {
86 wp_schedule_event( time(), 'every_minute', $abandoned_cart_emails );
87 }
88 }
89 /**
90 * Detect Abandoned Cart Recovery
91 */
92 public function detect_abandoned_cart_recovery() {
93 CartRecoveryDB::update_to_abandoned();
94 CartRecoveryDB::lost_abandonment_detection();
95 self::delete_expired_coupon();
96 }
97 /**
98 * Detect Abandoned Cart Recovery
99 */
100 public function delete_expired_coupon() {
101 $delete_coupons = CartRecoveryFns::get_options( 'delete_coupons_automatically', '' );
102 if ( 'on' !== $delete_coupons ) {
103 return;
104 }
105 $args = [
106 'post_type' => 'shop_coupon',
107 'posts_per_page' => -1,
108 'post_status' => 'publish',
109 'fields' => 'ids',
110 'meta_query' => [ // phpcs:ignore WordPress.DB.SlowDBQuery
111 'relation' => 'AND',
112 [
113 'key' => 'coupon_created_by_ca_abandonment',
114 'value' => 'yes',
115 ],
116 [
117 'key' => 'date_expires',
118 'value' => Fns::currentTimestampUTC(),
119 'compare' => '<',
120 'type' => 'NUMERIC',
121 ],
122 ],
123 ];
124 $expired_coupons = get_posts( $args );
125 if ( ! empty( $expired_coupons ) ) {
126 foreach ( $expired_coupons as $coupon_id ) {
127 wp_delete_post( $coupon_id, true ); // true = force delete.
128 }
129 }
130 }
131 /**
132 * Detect Abandoned Cart Recovery
133 */
134 public function process_abandoned_cart_emails() {
135 $emails_history = CartRecoveryDB::get_scheduled_email_history();
136 if ( empty( $emails_history ) ) {
137 return;
138 }
139 $recovery_email = RecoveryEmail::instance();
140 foreach ( $emails_history as $history ) {
141 $recovery = $recovery_email->send_recovery_email( $history );
142 if ( $recovery ) {
143 CartRecoveryDB::update_scheduled_email_history( $history['id'], [ 'email_sent' => 1 ] );
144 } else {
145 CartRecoveryDB::update_scheduled_email_history( $history['id'], [ 'email_sent' => -1 ] );
146 }
147 }
148 }
149 }
150