PluginProbe
Media Cloud Sync / 1.4.2
Media Cloud Sync v1.4.2
1.4.2 1.4.1 1.4.0 1.3.12 1.3.11 1.3.10 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.1.0 1.1.1 1.2.0 1.2.10 1.2.11 1.2.12 1.2.13 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 All 36 releases
media-cloud-sync / includes / base / counter.php

counter.php in Media Cloud Sync 1.4.2, at includes/base/counter.php

209 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Self-heal a legacy/corrupted non-array row instead of crashing on it.
34 $data = is_array($data) ? $data : [];
35 $data[$property] = isset($data[$property]) ? (int)$data[$property] + 1 : 1;
36 return Utils::update_option( $source_type, $data, $settings_key );
37 }
38
39 /**
40 *
41 * Remove Count
42 * @since 1.0.0
43 *
44 */
45 public static function remove( $property = 'uploaded', $source_type = 'media_library' ){
46 $settings_key = Schema::getConstant('COUNTER_KEY');
47 $data = Utils::get_option( $source_type, [], $settings_key );
48 // Self-heal a legacy/corrupted non-array row instead of crashing on it.
49 $data = is_array($data) ? $data : [];
50 $data[$property] = isset($data[$property]) ? (int)$data[$property] - 1 : -1;
51
52 return Utils::update_option( $source_type, $data, $settings_key );
53 }
54
55 /**
56 * Get Count
57 * @since 1.0.0
58 */
59 public static function get( $property = 'uploaded', $source_type = 'media_library' ) {
60 // Get the count by source type
61 $data = Utils::get_option( $source_type, [], Schema::getConstant('COUNTER_KEY') );
62 return isset($data[$property]) ? (int)$data[$property] : 0;
63 }
64
65 /**
66 * Update Count
67 * @since 1.0.0
68 */
69 public static function update( $property, $value, $source_type = 'media_library' ) {
70 $key = Schema::getConstant('COUNTER_KEY');
71 // Update the count by source type
72 $data = Utils::get_option( $source_type, [], $key );
73 // Self-heal a legacy/corrupted non-array row instead of crashing on it.
74 $data = is_array($data) ? $data : [];
75 $data[$property] = $value;
76 return Utils::update_option( $source_type, $data, $key );
77 }
78
79 /**
80 * Get total count from all source types.
81 *
82 * @param string $property The property to get ('uploaded', 'total', or 'all').
83 * @param bool $combined Whether to combine counts from all source types.
84 * @return mixed Combined total count (int) or an associative array grouped by source types.
85 */
86 public static function get_count($property = 'all', $combined = true) {
87 $settings_key = Schema::getConstant('COUNTER_KEY');
88 $source_types = Integration::$source_types;
89
90 // Determine properties to process
91 $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property];
92 $result = $combined ? array_fill_keys($properties, 0) : [];
93
94 if (!empty($source_types)) {
95 foreach ($source_types as $source_type => $args) {
96 $data = Utils::get_option($source_type, [], $settings_key);
97
98 foreach ($properties as $prop) {
99 $count = isset($data[$prop]) ? (int)$data[$prop] : 0;
100
101 if ($combined) {
102 // Add to combined total
103 $result[$prop] += $count;
104 } else {
105 // Group by source type
106 if (!isset($result[$source_type])) {
107 $result[$source_type] = array_fill_keys($properties, 0);
108 }
109 $result[$source_type][$prop] += $count;
110 }
111 }
112 }
113 }
114
115 return $combined ? ($property === 'all' ? $result : array_sum($result)) : $result;
116 }
117
118 /**
119 * Fetch and update media counts for uploaded or total items.
120 *
121 * @param string $source_type The type of source to fetch (default: 'all').
122 * @param string $property The property to update ('uploaded', 'total', or 'all').
123 */
124 public static function fetch_and_update($property = 'all', $source_type = 'all') {
125 global $wpdb;
126
127 $credentials = Utils::get_credentials();
128 $config = isset($credentials['config']) && !empty($credentials['config'])
129 ? $credentials['config']
130 : [];
131 $bucketConfig = isset($credentials['bucketConfig']) && !empty($credentials['bucketConfig'])
132 ? $credentials['bucketConfig']
133 : [];
134 $service = isset($credentials['service']) && !empty($credentials['service'])
135 ? $credentials['service']
136 : '';
137 $bucket_name = isset($bucketConfig['bucket_name']) ? $bucketConfig['bucket_name'] : '';
138 $region = isset($config['region']) ? $config['region'] : '';
139
140 $source_types = Integration::$source_types;
141 $fetch_all = ($source_type === 'all');
142 $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property];
143
144 foreach ($properties as $prop) {
145 if ($prop === 'uploaded') {
146 // Reset all
147 if(!empty($source_types)) {
148 foreach($source_types as $source_t => $source_type_data) {
149 self::update('uploaded', 0, $source_t);
150 }
151 }
152 // Fetch uploaded counts grouped by source type
153 $table_name = Db::get_table_name();
154 $query = "SELECT source_type, COUNT(*) AS count
155 FROM {$table_name}
156 WHERE provider=%s
157 AND storage=%s";
158
159 if(!empty(($region))) {
160 $query .= " AND region=%s GROUP BY source_type";
161 $query = $wpdb->prepare($query, $service, $bucket_name, $region);
162 } else {
163 $query .= " GROUP BY source_type";
164 $query = $wpdb->prepare($query, $service, $bucket_name);
165 }
166
167 $results = $wpdb->get_results($query, ARRAY_A);
168
169 foreach ($results as $row) {
170 $source_key = $row['source_type'];
171 $count = (int) $row['count'];
172
173 if ($fetch_all || $source_key === $source_type) {
174 $data = $source_types[$source_key] ?? false;
175 if ($data) {
176 self::update('uploaded', $count, $source_key);
177 }
178 }
179 }
180 }
181
182 if ($prop === 'total') {
183 // Reset all
184 if(!empty($source_types)) {
185 foreach($source_types as $source_t => $source_type_data) {
186 self::update('total', 0, $source_t);
187 }
188 }
189 // Fetch total counts for all or specific source types
190 foreach ($source_types as $key => $data) {
191 if ($fetch_all || $key === $source_type) {
192 if ($data && $class = Integration::get_handler_class($key)) {
193 $total = $class::get_total_media($key);
194 self::update('total', $total, $key);
195 }
196 }
197 }
198 }
199 }
200 }
201
202
203 public static function instance(){
204 if (is_null(self::$instance)) {
205 self::$instance = new self();
206 }
207 return self::$instance;
208 }
209 }