| @@ -26,26 +26,27 @@ | ||
| 26 | 26 | * Add Count |
| 27 | 27 | * @since 1.0.0 |
| 28 | 28 | * |
| 29 | 29 | */ |
| 30 | - public static function add( $property = 'uploaded' ){ | |
| 31 | - $settings_key = Schema::getConstant('COUNTER_KEY'); | |
| 32 | - $current = Utils::get_option( $property, 0, Schema::getConstant('COUNTER_KEY') ); | |
| 33 | - $current++; | |
| 34 | - return Utils::update_option( $property, $current, Schema::getConstant('COUNTER_KEY') ); | |
| 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 | 35 | } |
| 36 | 36 | |
| 37 | 37 | /** |
| 38 | 38 | * |
| 39 | - * Add Count | |
| 39 | + * Remove Count | |
| 40 | 40 | * @since 1.0.0 |
| 41 | 41 | * |
| 42 | 42 | */ |
| 43 | - public static function remove( $property = 'uploaded' ){ | |
| 44 | - $settings_key = Schema::getConstant('COUNTER_KEY'); | |
| 45 | - $current = Utils::get_option( $property, 0, Schema::getConstant('COUNTER_KEY') ); | |
| 46 | - $current--; | |
| 47 | - return Utils::update_option( $property, $current, Schema::getConstant('COUNTER_KEY') ); | |
| 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 ); | |
| 48 | 49 | } |
| 49 | 50 | |
| 50 | 51 | /** |
| 51 | 52 | * Get Count |
| @@ -50,10 +51,12 @@ | ||
| 50 | 51 | /** |
| 51 | 52 | * Get Count |
| 52 | 53 | * @since 1.0.0 |
| 53 | 54 | */ |
| 54 | - public static function get( $property = 'uploaded' ) { | |
| 55 | - return Utils::get_option( $property, 0, Schema::getConstant('COUNTER_KEY') ); | |
| 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; | |
| 56 | 59 | } |
| 57 | 60 | |
| 58 | 61 | /** |
| 59 | 62 | * Update Count |
| @@ -58,10 +61,137 @@ | ||
| 58 | 61 | /** |
| 59 | 62 | * Update Count |
| 60 | 63 | * @since 1.0.0 |
| 61 | 64 | */ |
| 62 | - public static function update( $property, $value ) { | |
| 63 | - return Utils::update_option( $property, $value, Schema::getConstant('COUNTER_KEY') ); | |
| 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 | + } | |
| 64 | 194 | } |
| 65 | 195 | |
| 66 | 196 | |
| 67 | 197 | public static function instance(){ |