PluginProbe
UpdraftCentral Dashboard / 0.7.2
UpdraftCentral Dashboard v0.7.2
0.8.33 0.7.2 0.7.3 0.7.4 0.8.0 0.8.1 0.8.10 0.8.11 0.8.12 0.8.13 0.8.14 0.8.15 0.8.16 0.8.17 0.8.18 0.8.19 0.8.2 0.8.20 0.8.21 0.8.22 0.8.23 0.8.24 0.8.25 0.8.26 0.8.27 All 51 releases
updraftcentral / classes / user-cron.php

user-cron.php in UpdraftCentral Dashboard 0.7.2, at classes/user-cron.php

100 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('UD_CENTRAL_DIR')) die('Security check');
4
5 /**
6 * Handles DB request intended for the "updraftcentral_user_cron" table
7 */
8 class UpdraftCentral_User_Cron {
9
10 /**
11 * The name of the table that we are working on that
12 * is stored in the Database. Includes the actual name with prefixes.
13 *
14 * @internal
15 * @var string $table
16 */
17 private $table;
18
19
20 /**
21 * The class constructor. Initializes the $table member variable.
22 */
23 public function __construct() {
24 global $wpdb;
25
26 $table_prefix = UpdraftCentral()->table_prefix;
27 $this->table = $wpdb->base_prefix.$table_prefix.'user_cron';
28 }
29
30 /**
31 * Retrieves a list of user IDs where the user's pending cron process
32 * had passed a certain process interval (1 hour at least)
33 *
34 * @param int|boolean $limit Optional. The number of record(s) to return. Defaults to return every records it can find.
35 * @return array - The list of user IDs found.
36 */
37 public function get_process_queue($limit = false) {
38 global $wpdb;
39
40 // At least the minimum time interval the last process was executed. Default to 1 hour (3600).
41 $process_interval = apply_filters('updraftcentral_process_interval', 3600);
42
43 $records_to_return = "";
44 if (false !== $limit && is_int($limit)) {
45 $records_to_return = " LIMIT ".$limit;
46 }
47
48 $result = $wpdb->get_col($wpdb->prepare("SELECT user_id FROM {$this->table} WHERE (UNIX_TIMESTAMP()-last_run) > %d ORDER BY last_run DESC".$records_to_return, $process_interval));
49
50 return $result;
51 }
52
53 /**
54 * Updates the last_run field for the given user.
55 *
56 * @param int $user_id The ID of the user
57 * @return boolean - True, when the update process succeeded. False, otherwise.
58 */
59 public function update_last_run($user_id) {
60 global $wpdb;
61
62 $data = array('last_run' => time());
63 $where = compact('user_id');
64
65 // Run the update query, all fields in $data are %s, $where is a %d.
66 $result = $wpdb->update($this->table, $data, $where, '%d', '%d');
67 if (false !== $result) {
68 return true;
69 }
70
71 return false;
72 }
73
74 /**
75 * Insert a new entry in the "updraftcentral_user_cron" table for the current user
76 * if it doesn't exists yet
77 *
78 * @param int $user_id The ID of the user
79 * @return boolean - True, when the insert process succeeded. False, otherwise.
80 */
81 public function maybe_insert_entry($user_id) {
82 global $wpdb;
83
84 $result = $wpdb->get_col($wpdb->prepare("SELECT id FROM {$this->table} WHERE user_id = %d", $user_id));
85 if (empty($result) && !empty($user_id)) {
86 // We make sure that this gets picked up by the "get_process_queue" api by deducting 24 hours
87 // from the current time. This applies to new entries only, since no process has been check yet for these new entries.
88 $last_run = time() - (24 * 60 * 60);
89
90 $data = compact('user_id', 'last_run');
91 $result = $wpdb->insert($this->table, $data, '%d');
92 if (false !== $result) {
93 return true;
94 }
95 }
96
97 return false;
98 }
99 }
100