HTML
1 year ago
views
5 months ago
Apply.php
6 months ago
Cron.php
1 year ago
CronJob.php
8 months ago
CronJobs.php
2 months ago
Crypt.php
2 months ago
DownloadStats.php
5 months ago
Email.php
1 week ago
EmailCron.php
1 year ago
FileSystem.php
1 year ago
Installer.php
4 days ago
Messages.php
1 year ago
Query.php
5 months ago
Session.php
4 days ago
Settings.php
5 years ago
SimpleMath.php
5 years ago
TempStorage.php
4 days ago
Template.php
5 months ago
UI.php
6 months ago
Updater.php
4 years ago
UserAgent.php
2 years ago
__.php
2 months ago
__MailUI.php
3 years ago
CronJob.php
91 lines
| 1 | <?php |
| 2 | /** |
| 3 | * User: shahjada |
| 4 | * Date: 2019-03-21 |
| 5 | * Time: 13:14 |
| 6 | */ |
| 7 | |
| 8 | namespace WPDM\__; |
| 9 | |
| 10 | |
| 11 | class CronJob |
| 12 | { |
| 13 | private static $instance; |
| 14 | |
| 15 | public static function getInstance() |
| 16 | { |
| 17 | if (self::$instance === null) { |
| 18 | self::$instance = new self; |
| 19 | } |
| 20 | return self::$instance; |
| 21 | } |
| 22 | |
| 23 | function cronKey() { |
| 24 | $key = get_option('__wpdm_cron_key'); |
| 25 | if(!$key) { |
| 26 | $key = wp_generate_password(32, false); |
| 27 | update_option('__wpdm_cron_key', $key); |
| 28 | } |
| 29 | return $key; |
| 30 | } |
| 31 | |
| 32 | static function create($type, $data, $execute_at, $repeat_execution = 1, $interval = 0) { |
| 33 | global $wpdb; |
| 34 | $code = md5($type.$execute_at.json_encode($data)); |
| 35 | $wpdb->insert("{$wpdb->prefix}ahm_cron_jobs", [ |
| 36 | 'code' => $code, |
| 37 | 'type' => $type, |
| 38 | 'data' => json_encode($data), |
| 39 | 'execute_at' => $execute_at, |
| 40 | 'repeat_execution' => max( (int)$repeat_execution, 1 ), |
| 41 | 'execution_count' => 0, |
| 42 | 'interval' => $interval, |
| 43 | 'created_at' => time(), |
| 44 | 'created_by' => get_current_user_id(), |
| 45 | |
| 46 | ]); |
| 47 | } |
| 48 | |
| 49 | static function delete($id) { |
| 50 | global $wpdb; |
| 51 | $wpdb->delete("{$wpdb->prefix}ahm_cron_jobs", ['ID' => (int)$id]); |
| 52 | } |
| 53 | |
| 54 | function executeAll() { |
| 55 | global $wpdb; |
| 56 | $time = time(); |
| 57 | $jobs = $wpdb->get_results("select * from {$wpdb->prefix}ahm_cron_jobs where execute_at < $time limit 0, 10"); |
| 58 | foreach($jobs as $job) { |
| 59 | $this->execute($job); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | function execute($job) { |
| 64 | global $wpdb; |
| 65 | if(is_int($job)) { |
| 66 | $job = $wpdb->get_row("select * from {$wpdb->prefix}ahm_cron_jobs where ID = '{$job}'"); |
| 67 | } |
| 68 | if($job->execution_count >= $job->repeat_execution) { |
| 69 | $wpdb->delete("{$wpdb->prefix}ahm_cron_jobs", ['ID' => $job->ID]); |
| 70 | } else { |
| 71 | $target = $job->type; |
| 72 | $payload = new $target(json_decode($job->data)); |
| 73 | $payload->dispatch(); |
| 74 | $execute_at = $job->execute_at + ($job->interval * 86400); |
| 75 | $wpdb->update( "{$wpdb->prefix}ahm_cron_jobs", [ |
| 76 | 'execution_count' => $job->execution_count + 1, |
| 77 | 'execute_at' => $execute_at, |
| 78 | ], [ 'ID' => $job->ID ] ); |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | static function getAll() { |
| 83 | global $wpdb; |
| 84 | // $time = time(); |
| 85 | // where execute_at < $time |
| 86 | $jobs = $wpdb->get_results("select * from {$wpdb->prefix}ahm_cron_jobs"); |
| 87 | return $jobs; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 |