PluginProbe
Media Cloud Sync / 1.3.0
Media Cloud Sync v1.3.0
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.0, at includes/base/counter.php

182 lines 6.5 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 $source_types = Integration::$source_types;
124 $fetch_all = ($source_type === 'all');
125 $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property];
126
127 foreach ($properties as $prop) {
128 if ($prop === 'uploaded') {
129 // Reset all
130 if(!empty($source_types)) {
131 foreach($source_types as $source_t => $source_type_data) {
132 self::update('uploaded', 0, $source_t);
133 }
134 }
135 // Fetch uploaded counts grouped by source type
136 $table_name = Db::get_table_name();
137 $results = $wpdb->get_results(
138 "SELECT source_type, COUNT(*) AS count FROM " . $table_name . " GROUP BY source_type",
139 ARRAY_A
140 );
141
142 foreach ($results as $row) {
143 $source_key = $row['source_type'];
144 $count = (int) $row['count'];
145
146 if ($fetch_all || $source_key === $source_type) {
147 $data = $source_types[$source_key] ?? false;
148 if ($data) {
149 self::update('uploaded', $count, $source_key);
150 }
151 }
152 }
153 }
154
155 if ($prop === 'total') {
156 // Reset all
157 if(!empty($source_types)) {
158 foreach($source_types as $source_t => $source_type_data) {
159 self::update('total', 0, $source_t);
160 }
161 }
162 // Fetch total counts for all or specific source types
163 foreach ($source_types as $key => $data) {
164 if ($fetch_all || $key === $source_type) {
165 if ($data && $class = Integration::get_handler_class($key)) {
166 $total = $class::get_total_media($key);
167 self::update('total', $total, $key);
168 }
169 }
170 }
171 }
172 }
173 }
174
175
176 public static function instance(){
177 if (is_null(self::$instance)) {
178 self::$instance = new self();
179 }
180 return self::$instance;
181 }
182 }