PluginProbe
Media Cloud Sync / 1.3.10
Media Cloud Sync v1.3.10
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 1.2.9 1.3.0 All 34 releases
media-cloud-sync / includes / base / counter.php

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

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