| @@ -29,8 +29,10 @@ | ||
| 29 | 29 | */ |
| 30 | 30 | public static function add( $property = 'uploaded', $source_type = 'media_library' ){ |
| 31 | 31 | $settings_key = Schema::getConstant('COUNTER_KEY'); |
| 32 | 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 : []; | |
| 33 | 35 | $data[$property] = isset($data[$property]) ? (int)$data[$property] + 1 : 1; |
| 34 | 36 | return Utils::update_option( $source_type, $data, $settings_key ); |
| 35 | 37 | } |
| 36 | 38 | |
| @@ -42,9 +44,12 @@ | ||
| 42 | 44 | */ |
| 43 | 45 | public static function remove( $property = 'uploaded', $source_type = 'media_library' ){ |
| 44 | 46 | $settings_key = Schema::getConstant('COUNTER_KEY'); |
| 45 | 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 : []; | |
| 46 | 50 | $data[$property] = isset($data[$property]) ? (int)$data[$property] - 1 : -1; |
| 51 | + | |
| 47 | 52 | return Utils::update_option( $source_type, $data, $settings_key ); |
| 48 | 53 | } |
| 49 | 54 | |
| 50 | 55 | /** |
| @@ -64,8 +69,10 @@ | ||
| 64 | 69 | public static function update( $property, $value, $source_type = 'media_library' ) { |
| 65 | 70 | $key = Schema::getConstant('COUNTER_KEY'); |
| 66 | 71 | // Update the count by source type |
| 67 | 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 : []; | |
| 68 | 75 | $data[$property] = $value; |
| 69 | 76 | return Utils::update_option( $source_type, $data, $key ); |
| 70 | 77 | } |
| 71 | 78 | |
| @@ -80,9 +87,9 @@ | ||
| 80 | 87 | $settings_key = Schema::getConstant('COUNTER_KEY'); |
| 81 | 88 | $source_types = Integration::$source_types; |
| 82 | 89 | |
| 83 | 90 | // Determine properties to process |
| 84 | - $properties = ($property === 'all') ? ['uploaded', 'total', 'failed'] : [$property]; | |
| 91 | + $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property]; | |
| 85 | 92 | $result = $combined ? array_fill_keys($properties, 0) : []; |
| 86 | 93 | |
| 87 | 94 | if (!empty($source_types)) { |
| 88 | 95 | foreach ($source_types as $source_type => $args) { |
| @@ -116,8 +123,21 @@ | ||
| 116 | 123 | */ |
| 117 | 124 | public static function fetch_and_update($property = 'all', $source_type = 'all') { |
| 118 | 125 | global $wpdb; |
| 119 | 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 | + | |
| 120 | 140 | $source_types = Integration::$source_types; |
| 121 | 141 | $fetch_all = ($source_type === 'all'); |
| 122 | 142 | $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property]; |
| 123 | 143 | |
| @@ -124,19 +144,29 @@ | ||
| 124 | 144 | foreach ($properties as $prop) { |
| 125 | 145 | if ($prop === 'uploaded') { |
| 126 | 146 | // Reset all |
| 127 | 147 | if(!empty($source_types)) { |
| 128 | - foreach($source_types as $source_type => $source_type_data) { | |
| 129 | - self::update('uploaded', 0, $source_type); | |
| 148 | + foreach($source_types as $source_t => $source_type_data) { | |
| 149 | + self::update('uploaded', 0, $source_t); | |
| 130 | 150 | } |
| 131 | 151 | } |
| 132 | 152 | // Fetch uploaded counts grouped by source type |
| 133 | 153 | $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 | - ); | |
| 154 | + $query = "SELECT source_type, COUNT(*) AS count | |
| 155 | + FROM {$table_name} | |
| 156 | + WHERE provider=%s | |
| 157 | + AND storage=%s"; | |
| 138 | 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 | + | |
| 139 | 169 | foreach ($results as $row) { |
| 140 | 170 | $source_key = $row['source_type']; |
| 141 | 171 | $count = (int) $row['count']; |
| 142 | 172 | |
| @@ -141,9 +171,9 @@ | ||
| 141 | 171 | $count = (int) $row['count']; |
| 142 | 172 | |
| 143 | 173 | if ($fetch_all || $source_key === $source_type) { |
| 144 | 174 | $data = $source_types[$source_key] ?? false; |
| 145 | - if ($data && class_exists($class = __NAMESPACE__ . '\\' . $data['class'])) { | |
| 175 | + if ($data) { | |
| 146 | 176 | self::update('uploaded', $count, $source_key); |
| 147 | 177 | } |
| 148 | 178 | } |
| 149 | 179 | } |
| @@ -151,16 +181,16 @@ | ||
| 151 | 181 | |
| 152 | 182 | if ($prop === 'total') { |
| 153 | 183 | // Reset all |
| 154 | 184 | if(!empty($source_types)) { |
| 155 | - foreach($source_types as $source_type => $source_type_data) { | |
| 156 | - self::update('total', 0, $source_type); | |
| 185 | + foreach($source_types as $source_t => $source_type_data) { | |
| 186 | + self::update('total', 0, $source_t); | |
| 157 | 187 | } |
| 158 | 188 | } |
| 159 | 189 | // Fetch total counts for all or specific source types |
| 160 | 190 | foreach ($source_types as $key => $data) { |
| 161 | 191 | if ($fetch_all || $key === $source_type) { |
| 162 | - if ($data && class_exists($class = __NAMESPACE__ . '\\' . $data['class'])) { | |
| 192 | + if ($data && $class = Integration::get_handler_class($key)) { | |
| 163 | 193 | $total = $class::get_total_media($key); |
| 164 | 194 | self::update('total', $total, $key); |
| 165 | 195 | } |
| 166 | 196 | } |