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
← All changes | includes/base/counter.php +151 -15 1.2.7 → 1.4.2 View file →
@@ -26,26 +26,31 @@
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 + // 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 );
35 37 }
36 38
37 39 /**
38 40 *
39 - * Add Count
41 + * Remove Count
40 42 * @since 1.0.0
41 43 *
42 44 */
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') );
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 );
48 53 }
49 54
50 55 /**
51 56 * Get Count
@@ -50,10 +55,12 @@
50 55 /**
51 56 * Get Count
52 57 * @since 1.0.0
53 58 */
54 - public static function get( $property = 'uploaded' ) {
55 - return Utils::get_option( $property, 0, Schema::getConstant('COUNTER_KEY') );
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;
56 63 }
57 64
58 65 /**
59 66 * Update Count
@@ -58,10 +65,139 @@
58 65 /**
59 66 * Update Count
60 67 * @since 1.0.0
61 68 */
62 - public static function update( $property, $value ) {
63 - return Utils::update_option( $property, $value, Schema::getConstant('COUNTER_KEY') );
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 + }
64 200 }
65 201
66 202
67 203 public static function instance(){