| 1 |
<?php |
| 2 |
namespace Dudlewebs\WPMCS; |
| 3 |
|
| 4 |
defined('ABSPATH') || exit; |
| 5 |
|
| 6 |
class Counter { |
| 7 |
private static $instance = null; |
| 8 |
private $assets_url; |
| 9 |
private $version; |
| 10 |
private $token; |
| 11 |
|
| 12 |
/** |
| 13 |
* Admin constructor. |
| 14 |
* @since 1.0.0 |
| 15 |
*/ |
| 16 |
public function __construct() { |
| 17 |
$this->assets_url = WPMCS_ASSETS_URL; |
| 18 |
$this->version = WPMCS_VERSION; |
| 19 |
$this->token = WPMCS_TOKEN; |
| 20 |
|
| 21 |
// Initialize setup |
| 22 |
// $this->init(); |
| 23 |
} |
| 24 |
/** |
| 25 |
* |
| 26 |
* Add Count |
| 27 |
* @since 1.0.0 |
| 28 |
* |
| 29 |
*/ |
| 30 |
public static function add( $property = 'uploaded', $source_type = 'media_library' ){ |
| 31 |
$settings_key = Schema::getConstant('COUNTER_KEY'); |
| 32 |
$data = Utils::get_option( $source_type, [], $settings_key ); |
| 33 |
$data[$property] = isset($data[$property]) ? (int)$data[$property] + 1 : 1; |
| 34 |
return Utils::update_option( $source_type, $data, $settings_key ); |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* |
| 39 |
* Remove Count |
| 40 |
* @since 1.0.0 |
| 41 |
* |
| 42 |
*/ |
| 43 |
public static function remove( $property = 'uploaded', $source_type = 'media_library' ){ |
| 44 |
$settings_key = Schema::getConstant('COUNTER_KEY'); |
| 45 |
$data = Utils::get_option( $source_type, [], $settings_key ); |
| 46 |
$data[$property] = isset($data[$property]) ? (int)$data[$property] - 1 : -1; |
| 47 |
|
| 48 |
return Utils::update_option( $source_type, $data, $settings_key ); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get Count |
| 53 |
* @since 1.0.0 |
| 54 |
*/ |
| 55 |
public static function get( $property = 'uploaded', $source_type = 'media_library' ) { |
| 56 |
// Get the count by source type |
| 57 |
$data = Utils::get_option( $source_type, [], Schema::getConstant('COUNTER_KEY') ); |
| 58 |
return isset($data[$property]) ? (int)$data[$property] : 0; |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Update Count |
| 63 |
* @since 1.0.0 |
| 64 |
*/ |
| 65 |
public static function update( $property, $value, $source_type = 'media_library' ) { |
| 66 |
$key = Schema::getConstant('COUNTER_KEY'); |
| 67 |
// Update the count by source type |
| 68 |
$data = Utils::get_option( $source_type, [], $key ); |
| 69 |
$data[$property] = $value; |
| 70 |
return Utils::update_option( $source_type, $data, $key ); |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Get total count from all source types. |
| 75 |
* |
| 76 |
* @param string $property The property to get ('uploaded', 'total', or 'all'). |
| 77 |
* @param bool $combined Whether to combine counts from all source types. |
| 78 |
* @return mixed Combined total count (int) or an associative array grouped by source types. |
| 79 |
*/ |
| 80 |
public static function get_count($property = 'all', $combined = true) { |
| 81 |
$settings_key = Schema::getConstant('COUNTER_KEY'); |
| 82 |
$source_types = Integration::$source_types; |
| 83 |
|
| 84 |
// Determine properties to process |
| 85 |
$properties = ($property === 'all') ? ['uploaded', 'total'] : [$property]; |
| 86 |
$result = $combined ? array_fill_keys($properties, 0) : []; |
| 87 |
|
| 88 |
if (!empty($source_types)) { |
| 89 |
foreach ($source_types as $source_type => $args) { |
| 90 |
$data = Utils::get_option($source_type, [], $settings_key); |
| 91 |
|
| 92 |
foreach ($properties as $prop) { |
| 93 |
$count = isset($data[$prop]) ? (int)$data[$prop] : 0; |
| 94 |
|
| 95 |
if ($combined) { |
| 96 |
// Add to combined total |
| 97 |
$result[$prop] += $count; |
| 98 |
} else { |
| 99 |
// Group by source type |
| 100 |
if (!isset($result[$source_type])) { |
| 101 |
$result[$source_type] = array_fill_keys($properties, 0); |
| 102 |
} |
| 103 |
$result[$source_type][$prop] += $count; |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
return $combined ? ($property === 'all' ? $result : array_sum($result)) : $result; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Fetch and update media counts for uploaded or total items. |
| 114 |
* |
| 115 |
* @param string $source_type The type of source to fetch (default: 'all'). |
| 116 |
* @param string $property The property to update ('uploaded', 'total', or 'all'). |
| 117 |
*/ |
| 118 |
public static function fetch_and_update($property = 'all', $source_type = 'all') { |
| 119 |
global $wpdb; |
| 120 |
|
| 121 |
$credentials = Utils::get_credentials(); |
| 122 |
$config = isset($credentials['config']) && !empty($credentials['config']) |
| 123 |
? $credentials['config'] |
| 124 |
: []; |
| 125 |
$bucketConfig = isset($credentials['bucketConfig']) && !empty($credentials['bucketConfig']) |
| 126 |
? $credentials['bucketConfig'] |
| 127 |
: []; |
| 128 |
$service = isset($credentials['service']) && !empty($credentials['service']) |
| 129 |
? $credentials['service'] |
| 130 |
: ''; |
| 131 |
$bucket_name = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : ''; |
| 132 |
$region = isset($config['region']) ? $config['region'] : ''; |
| 133 |
|
| 134 |
$source_types = Integration::$source_types; |
| 135 |
$fetch_all = ($source_type === 'all'); |
| 136 |
$properties = ($property === 'all') ? ['uploaded', 'total'] : [$property]; |
| 137 |
|
| 138 |
foreach ($properties as $prop) { |
| 139 |
if ($prop === 'uploaded') { |
| 140 |
// Reset all |
| 141 |
if(!empty($source_types)) { |
| 142 |
foreach($source_types as $source_t => $source_type_data) { |
| 143 |
self::update('uploaded', 0, $source_t); |
| 144 |
} |
| 145 |
} |
| 146 |
// Fetch uploaded counts grouped by source type |
| 147 |
$table_name = Db::get_table_name(); |
| 148 |
$query = "SELECT source_type, COUNT(*) AS count |
| 149 |
FROM {$table_name} |
| 150 |
WHERE provider=%s |
| 151 |
AND storage=%s"; |
| 152 |
|
| 153 |
if(!empty(($region))) { |
| 154 |
$query .= " AND region=%s GROUP BY source_type"; |
| 155 |
$query = $wpdb->prepare($query, $service, $bucket_name, $region); |
| 156 |
} else { |
| 157 |
$query .= " GROUP BY source_type"; |
| 158 |
$query = $wpdb->prepare($query, $service, $bucket_name); |
| 159 |
} |
| 160 |
|
| 161 |
$results = $wpdb->get_results($query, ARRAY_A); |
| 162 |
|
| 163 |
foreach ($results as $row) { |
| 164 |
$source_key = $row['source_type']; |
| 165 |
$count = (int) $row['count']; |
| 166 |
|
| 167 |
if ($fetch_all || $source_key === $source_type) { |
| 168 |
$data = $source_types[$source_key] ?? false; |
| 169 |
if ($data) { |
| 170 |
self::update('uploaded', $count, $source_key); |
| 171 |
} |
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|
| 176 |
if ($prop === 'total') { |
| 177 |
// Reset all |
| 178 |
if(!empty($source_types)) { |
| 179 |
foreach($source_types as $source_t => $source_type_data) { |
| 180 |
self::update('total', 0, $source_t); |
| 181 |
} |
| 182 |
} |
| 183 |
// Fetch total counts for all or specific source types |
| 184 |
foreach ($source_types as $key => $data) { |
| 185 |
if ($fetch_all || $key === $source_type) { |
| 186 |
if ($data && $class = Integration::get_handler_class($key)) { |
| 187 |
$total = $class::get_total_media($key); |
| 188 |
self::update('total', $total, $key); |
| 189 |
} |
| 190 |
} |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
public static function instance(){ |
| 198 |
if (is_null(self::$instance)) { |
| 199 |
self::$instance = new self(); |
| 200 |
} |
| 201 |
return self::$instance; |
| 202 |
} |
| 203 |
} |