Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/tutor-pro/gift-course/GiftScheduler.php

Keine Baseline-Datei – Diff nur gegen leer.
Zur Liste
1 -
1 + <?php
2 + /**
3 + * Handle Gift Schedule
4 + *
5 + * @package TutorPro\GiftCourse
6 + * @author Themeum <support@themeum.com>
7 + * @link https://themeum.com
8 + * @since 3.8.0
9 + */
10 +
11 + namespace TutorPro\GiftCourse;
12 +
13 + use AllowDynamicProperties;
14 + use Tutor\Helpers\QueryHelper;
15 +
16 + defined( 'ABSPATH' ) || exit;
17 +
18 + /**
19 + * Manage gift scheduling
20 + */
21 + #[AllowDynamicProperties]
22 + class GiftScheduler {
23 +
24 + /**
25 + * Schedule type
26 + *
27 + * @since 3.8.0
28 + *
29 + * @var string
30 + */
31 + const TYPE = 'gift_course';
32 +
33 + const STATUS_SCHEDULED = 'scheduled';
34 + const STATUS_PROCESSED = 'processed';
35 + const STATUS_COMPLETED = 'completed';
36 +
37 + const CRON_ACTION = 'tutor_gift_course';
38 +
39 + /**
40 + * Database instance
41 + *
42 + * @var \wpdb
43 + */
44 + private $db;
45 +
46 + /**
47 + * Resolve props
48 + */
49 + public function __construct() {
50 + global $wpdb;
51 + $this->db = $wpdb;
52 + $this->table = $this->db->prefix . 'tutor_scheduler';
53 +
54 + add_filter( 'cron_schedules', array( $this, 'filter_schedules' ) );
55 + $this->handle_cron();
56 + }
57 +
58 + /**
59 + * Schedule a gift
60 + *
61 + * @since 3.8.0
62 + *
63 + * @throws \Throwable If database error occur.
64 + *
65 + * @param object $payload Gift payload.
66 + * @param string $status Schedule status.
67 + *
68 + * @return int
69 + */
70 + public function schedule( object $payload, string $status = self::STATUS_SCHEDULED ) {
71 + $recipient_email = $payload->recipient_email;
72 + $user = get_user_by( 'email', $recipient_email );
73 +
74 + try {
75 + $insert = QueryHelper::insert(
76 + $this->table,
77 + array(
78 + 'type' => self::TYPE,
79 + 'payload' => wp_json_encode( $payload, JSON_UNESCAPED_UNICODE ),
80 + 'reference_id' => $payload->reference_id,
81 + 'status' => $status,
82 + 'scheduled_at_gmt' => $payload->scheduled_at_gmt,
83 + 'scheduled_by' => get_current_user_id(),
84 + 'scheduled_for' => $user ? $user->ID : 0,
85 + )
86 + );
87 +
88 + return $insert;
89 + } catch ( \Throwable $th ) {
90 + throw $th;
91 + }
92 + }
93 +
94 + /**
95 + * Get schedule data
96 + *
97 + * @since 3.8.0
98 + *
99 + * @param mixed $where Reference id or array of where condition.
100 + *
101 + * @return mixed
102 + */
103 + public function get( $where ) {
104 + if ( ! is_array( $where ) ) {
105 + $where = array(
106 + 'reference_id' => $where,
107 + );
108 + }
109 +
110 + return QueryHelper::get_row(
111 + $this->table,
112 + $where,
113 + 'id'
114 + );
115 + }
116 +
117 + /**
118 + * Get all schedule data based on where condition
119 + *
120 + * @since 3.8.0
121 + *
122 + * @param array $where Reference id or array of where condition.
123 + * @param int $limit Limit of fetching data.
124 + *
125 + * @return mixed
126 + */
127 + public function get_all( array $where, $limit = 399 ) {
128 + return QueryHelper::get_all(
129 + $this->table,
130 + $where,
131 + 'id',
132 + $limit
133 + );
134 + }
135 +
136 + /**
137 + * Update schedule data
138 + *
139 + * @since 3.8.0
140 + *
141 + * @param array $data Data to update.
142 + * @param array $where Where condition.
143 + *
144 + * @return bool
145 + */
146 + public function update( array $data, array $where ) {
147 + $update = QueryHelper::update(
148 + $this->table,
149 + $data,
150 + $where
151 + );
152 + return $update;
153 + }
154 +
155 + /**
156 + * Delete schedule data
157 + *
158 + * @since 3.8.0
159 + *
160 + * @param array $where Where condition.
161 + *
162 + * @return bool
163 + */
164 + public function delete( array $where ) {
165 + return QueryHelper::delete(
166 + $this->table,
167 + $where
168 + );
169 + }
170 +
171 + /**
172 + * Handle cron job
173 + *
174 + * Register cron job if not exist
175 + *
176 + * @since 3.8.0
177 + *
178 + * @return void
179 + */
180 + public function handle_cron() {
181 + add_action( self::CRON_ACTION, array( $this, 'process_scheduled' ) );
182 +
183 + if ( ! wp_next_scheduled( self::CRON_ACTION ) ) {
184 + wp_schedule_event( time(), '10min', self::CRON_ACTION );
185 + }
186 + }
187 +
188 + /**
189 + * Process scheduled gifts
190 + *
191 + * @since 3.8.0
192 + *
193 + * @return void
194 + */
195 + public function process_scheduled() {
196 + $this->delete_completed_schedules();
197 +
198 + $scheduled_gifts = $this->get_all( array( 'status' => self::STATUS_SCHEDULED ) );
199 + if ( tutor_utils()->count( $scheduled_gifts ) ) {
200 + foreach ( $scheduled_gifts as $gift ) {
201 + $payload = json_decode( $gift->payload );
202 + if ( $payload && time() >= strtotime( $payload->scheduled_at_gmt ) ) {
203 + $this->update(
204 + array(
205 + 'status' => self::STATUS_PROCESSED,
206 + ),
207 + array(
208 + 'id' => $gift->id,
209 + )
210 + );
211 +
212 + // Notify the receiver.
213 + ( new GiftMailer( $payload ) )->send_notification_email_to_receiver();
214 + }
215 + }
216 + }
217 + }
218 +
219 + /**
220 + * Add new interval to the cron schedule
221 + *
222 + * @since 3.8.0
223 + *
224 + * @param array $schedules Array of schedules.
225 + *
226 + * @return array
227 + */
228 + public function filter_schedules( array $schedules ): array {
229 + $schedules['10min'] = array(
230 + 'interval' => 60 * 10, // 10 mins in seconds.
231 + 'display' => __( '10 Minutes', 'tutor-pro' ),
232 + );
233 +
234 + return $schedules;
235 + }
236 +
237 + /**
238 + * Delete completed schedules
239 + *
240 + * @since 3.8.0
241 + *
242 + * @return void
243 + */
244 + public function delete_completed_schedules() {
245 + $where = array(
246 + 'type' => self::TYPE,
247 + 'status' => self::STATUS_COMPLETED,
248 + );
249 +
250 + $this->delete( $where );
251 + }
252 + }
253 +