PluginProbe
Media Cloud Sync / trunk
Media Cloud Sync vtrunk
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
← All changes | includes/base/counter.php +35 -11 1.2.12trunk View file →
@@ -43,8 +43,9 @@
43 43 public static function remove( $property = 'uploaded', $source_type = 'media_library' ){
44 44 $settings_key = Schema::getConstant('COUNTER_KEY');
45 45 $data = Utils::get_option( $source_type, [], $settings_key );
46 46 $data[$property] = isset($data[$property]) ? (int)$data[$property] - 1 : -1;
47 +
47 48 return Utils::update_option( $source_type, $data, $settings_key );
48 49 }
49 50
50 51 /**
@@ -80,9 +81,9 @@
80 81 $settings_key = Schema::getConstant('COUNTER_KEY');
81 82 $source_types = Integration::$source_types;
82 83
83 84 // Determine properties to process
84 - $properties = ($property === 'all') ? ['uploaded', 'total', 'failed'] : [$property];
85 + $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property];
85 86 $result = $combined ? array_fill_keys($properties, 0) : [];
86 87
87 88 if (!empty($source_types)) {
88 89 foreach ($source_types as $source_type => $args) {
@@ -116,8 +117,21 @@
116 117 */
117 118 public static function fetch_and_update($property = 'all', $source_type = 'all') {
118 119 global $wpdb;
119 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 +
120 134 $source_types = Integration::$source_types;
121 135 $fetch_all = ($source_type === 'all');
122 136 $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property];
123 137
@@ -124,19 +138,29 @@
124 138 foreach ($properties as $prop) {
125 139 if ($prop === 'uploaded') {
126 140 // Reset all
127 141 if(!empty($source_types)) {
128 - foreach($source_types as $source_type => $source_type_data) {
129 - self::update('uploaded', 0, $source_type);
142 + foreach($source_types as $source_t => $source_type_data) {
143 + self::update('uploaded', 0, $source_t);
130 144 }
131 145 }
132 146 // Fetch uploaded counts grouped by source type
133 147 $table_name = Db::get_table_name();
134 - $results = $wpdb->get_results(
135 - "SELECT source_type, COUNT(*) AS count FROM " . $table_name . " GROUP BY source_type",
136 - ARRAY_A
137 - );
148 + $query = "SELECT source_type, COUNT(*) AS count
149 + FROM {$table_name}
150 + WHERE provider=%s
151 + AND storage=%s";
138 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 +
139 163 foreach ($results as $row) {
140 164 $source_key = $row['source_type'];
141 165 $count = (int) $row['count'];
142 166
@@ -141,9 +165,9 @@
141 165 $count = (int) $row['count'];
142 166
143 167 if ($fetch_all || $source_key === $source_type) {
144 168 $data = $source_types[$source_key] ?? false;
145 - if ($data && class_exists($class = __NAMESPACE__ . '\\' . $data['class'])) {
169 + if ($data) {
146 170 self::update('uploaded', $count, $source_key);
147 171 }
148 172 }
149 173 }
@@ -151,16 +175,16 @@
151 175
152 176 if ($prop === 'total') {
153 177 // Reset all
154 178 if(!empty($source_types)) {
155 - foreach($source_types as $source_type => $source_type_data) {
156 - self::update('total', 0, $source_type);
179 + foreach($source_types as $source_t => $source_type_data) {
180 + self::update('total', 0, $source_t);
157 181 }
158 182 }
159 183 // Fetch total counts for all or specific source types
160 184 foreach ($source_types as $key => $data) {
161 185 if ($fetch_all || $key === $source_type) {
162 - if ($data && class_exists($class = __NAMESPACE__ . '\\' . $data['class'])) {
186 + if ($data && $class = Integration::get_handler_class($key)) {
163 187 $total = $class::get_total_media($key);
164 188 self::update('total', $total, $key);
165 189 }
166 190 }