| 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 |
|