PluginProbe
ووسلام – همگام سازی ووکامرس و باسلام / trunk
ووسلام – همگام سازی ووکامرس و باسلام vtrunk
1.10.17 1.10.15 1.10.14 1.10.13 1.10.12 1.10.10 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.2 1.9.1 1.9.0 1.8.8 1.8.5 1.8.6 1.8.7 1.8.4 1.7.6 All 50 releases
sync-basalam / includes / Jobs / AbstractJobType.php

AbstractJobType.php in ووسلام – همگام سازی ووکامرس و باسلام trunk, at includes/Jobs/AbstractJobType.php

80 lines 2.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SyncBasalam\Jobs;
4
5 use SyncBasalam\Jobs\Exceptions\RetryableException;
6 use SyncBasalam\Jobs\Exceptions\NonRetryableException;
7
8 defined('ABSPATH') || exit;
9
10 abstract class AbstractJobType implements JobType
11 {
12 protected $jobManager;
13
14 public function __construct($jobManager)
15 {
16 $this->jobManager = $jobManager;
17 }
18
19 abstract public function getType(): string;
20
21 abstract public function getPriority(): int;
22
23 abstract public function execute(array $payload);
24
25 public function canRun(): bool
26 {
27 return true;
28 }
29
30 protected function success(array $data = []): JobResult
31 {
32 return JobResult::success($data);
33 }
34
35 protected function retryable(string $message, int $code = 0, array $data = []): JobResult
36 {
37 return JobResult::failed(new RetryableException($message, $code), $data);
38 }
39
40 protected function nonRetryable(string $message, int $code = 0, array $data = []): JobResult
41 {
42 return JobResult::failed(new NonRetryableException($message, $code), $data);
43 }
44
45 protected function hasProductJobInProgress(int $productId, string $jobType): bool
46 {
47 global $wpdb;
48
49 $tableName = $wpdb->prefix . 'sync_basalam_job_manager';
50
51 // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, PluginCheck.Security.DirectDB.UnescapedDBParameter -- Custom plugin table; identifier from $wpdb->prefix, not user input.
52 $jobs = $wpdb->get_results($wpdb->prepare(
53 "SELECT payload FROM {$tableName}
54 WHERE job_type = %s
55 AND (status = %s OR status = %s)",
56 $jobType,
57 'pending',
58 'processing'
59 ));
60
61 if (empty($jobs)) return false;
62
63 foreach ($jobs as $job) {
64 $payload = json_decode($job->payload, true);
65 $jobProductId = $payload['product_id'] ?? $payload;
66
67 if (intval($jobProductId) === intval($productId)) return true;
68 }
69
70 return false;
71 }
72
73 protected function areAllSingleJobsCompleted(string $jobType): bool
74 {
75 $pendingJob = $this->jobManager->getJob(['job_type' => $jobType, 'status' => 'pending']);
76
77 return $pendingJob === null;
78 }
79 }
80