| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Handles automatic quota updates using WordPress cron |
| 5 |
* |
| 6 |
* @since 4.9 |
| 7 |
* @package WpStream |
| 8 |
*/ |
| 9 |
|
| 10 |
class WpStream_Quota_Manager { |
| 11 |
private $main; |
| 12 |
|
| 13 |
private $api_connection; |
| 14 |
|
| 15 |
/* |
| 16 |
* Transient key for quota data |
| 17 |
*/ |
| 18 |
const TRANSIENT_KEY = 'wpstream_request_pack_data_per_user_transient'; |
| 19 |
|
| 20 |
public function __construct( $main ) { |
| 21 |
$this->main = $main; |
| 22 |
$this->api_connection = $main->wpstream_live_connection; |
| 23 |
|
| 24 |
$this->init_hooks(); |
| 25 |
} |
| 26 |
|
| 27 |
private function init_hooks() { |
| 28 |
// Add admin-only hook |
| 29 |
add_action( 'admin_init', array( $this, 'maybe_update_quota' ) ); |
| 30 |
} |
| 31 |
|
| 32 |
public function maybe_update_quota() { |
| 33 |
if ( !current_user_can( 'manage_options' ) ) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$cached_data = get_transient( self::TRANSIENT_KEY ); |
| 38 |
if ( $cached_data === false ) { |
| 39 |
$this->update_quota_transient(); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
/* |
| 44 |
* Update quota data transient |
| 45 |
*/ |
| 46 |
public function update_quota_transient() { |
| 47 |
$quota_data = $this->api_connection->wpstream_request_pack_data_per_user('user_quota_cron_update'); |
| 48 |
|
| 49 |
if ( $this->is_valid_quota_data( $quota_data ) ) { |
| 50 |
set_transient( self::TRANSIENT_KEY, $quota_data, 60 ); |
| 51 |
|
| 52 |
$this->log_quota_update( 'success', 'User quota updated via cron' ); |
| 53 |
} else { |
| 54 |
$this->log_quota_update( 'error', 'Failed to update user quota via cron' ); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
public function is_valid_quota_data( $data ) { |
| 59 |
return $data && |
| 60 |
is_array( $data ) && |
| 61 |
isset( $data['success'] ) && |
| 62 |
$data['success'] === true; |
| 63 |
} |
| 64 |
|
| 65 |
public function log_quota_update( $status, $message ) { |
| 66 |
if ( class_exists( 'WpStream_Logger' ) ) { |
| 67 |
$logger = new Wpstream_Logger(); |
| 68 |
$log_entry = new WpStream_Log_Entry([ |
| 69 |
'type' => $status, |
| 70 |
'description' => $message, |
| 71 |
'timestamp' => current_time( 'timestamp' ) |
| 72 |
]); |
| 73 |
$logger->add( $log_entry ); |
| 74 |
} |
| 75 |
} |
| 76 |
|
| 77 |
/* |
| 78 |
* Force immediate quota update and return fresh data |
| 79 |
* needed for example when starting a channel |
| 80 |
*/ |
| 81 |
public function force_quota_update() { |
| 82 |
delete_transient( self::TRANSIENT_KEY ); |
| 83 |
$this->update_quota_transient(); |
| 84 |
return get_transient( self::TRANSIENT_KEY ); |
| 85 |
} |
| 86 |
|
| 87 |
public function get_live_quota_data( $context = 'user_quota_on_demand' ) { |
| 88 |
$cached_data = get_transient( self::TRANSIENT_KEY ); |
| 89 |
|
| 90 |
if ( $cached_data !== false ) { |
| 91 |
return $cached_data; |
| 92 |
} |
| 93 |
|
| 94 |
// If transient is empty, fetch fresh data |
| 95 |
return $this->api_connection->wpstream_request_pack_data_per_user( $context ); |
| 96 |
} |
| 97 |
} |