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

179 lines 6.4 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::get_option( $source_type, [], $settings_key );
46 $data[$property] = isset($data[$property]) ? (int)$data[$property] - 1 : -1;
47 return Utils::update_option( $source_type, $data, $settings_key );
48 }
49
50 /**
51 * Get Count
52 * @since 1.0.0
53 */
54 public static function get( $property = 'uploaded', $source_type = 'media_library' ) {
55 // Get the count by source type
56 $data = Utils::get_option( $source_type, [], Schema::getConstant('COUNTER_KEY') );
57 return isset($data[$property]) ? (int)$data[$property] : 0;
58 }
59
60 /**
61 * Update Count
62 * @since 1.0.0
63 */
64 public static function update( $property, $value, $source_type = 'media_library' ) {
65 $key = Schema::getConstant('COUNTER_KEY');
66 // Update the count by source type
67 $data = Utils::get_option( $source_type, [], $key );
68 $data[$property] = $value;
69 return Utils::update_option( $source_type, $data, $key );
70 }
71
72 /**
73 * Get total count from all source types.
74 *
75 * @param string $property The property to get ('uploaded', 'total', or 'all').
76 * @param bool $combined Whether to combine counts from all source types.
77 * @return mixed Combined total count (int) or an associative array grouped by source types.
78 */
79 public static function get_count($property = 'all', $combined = true) {
80 $settings_key = Schema::getConstant('COUNTER_KEY');
81 $source_types = Integration::$source_types;
82
83 // Determine properties to process
84 $properties = ($property === 'all') ? ['uploaded', 'total', 'failed'] : [$property];
85 $result = $combined ? array_fill_keys($properties, 0) : [];
86
87 if (!empty($source_types)) {
88 foreach ($source_types as $source_type => $args) {
89 $data = Utils::get_option($source_type, [], $settings_key);
90
91 foreach ($properties as $prop) {
92 $count = isset($data[$prop]) ? (int)$data[$prop] : 0;
93
94 if ($combined) {
95 // Add to combined total
96 $result[$prop] += $count;
97 } else {
98 // Group by source type
99 if (!isset($result[$source_type])) {
100 $result[$source_type] = array_fill_keys($properties, 0);
101 }
102 $result[$source_type][$prop] += $count;
103 }
104 }
105 }
106 }
107
108 return $combined ? ($property === 'all' ? $result : array_sum($result)) : $result;
109 }
110
111 /**
112 * Fetch and update media counts for uploaded or total items.
113 *
114 * @param string $source_type The type of source to fetch (default: 'all').
115 * @param string $property The property to update ('uploaded', 'total', or 'all').
116 */
117 public static function fetch_and_update($property = 'all', $source_type = 'all') {
118 global $wpdb;
119
120 $source_types = Integration::$source_types;
121 $fetch_all = ($source_type === 'all');
122 $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property];
123
124 foreach ($properties as $prop) {
125 if ($prop === 'uploaded') {
126 // Reset all
127 if(!empty($source_types)) {
128 foreach($source_types as $source_type => $source_type_data) {
129 self::update('uploaded', 0, $source_type);
130 }
131 }
132 // Fetch uploaded counts grouped by source type
133 $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 );
138
139 foreach ($results as $row) {
140 $source_key = $row['source_type'];
141 $count = (int) $row['count'];
142
143 if ($fetch_all || $source_key === $source_type) {
144 $data = $source_types[$source_key] ?? false;
145 if ($data && class_exists($class = __NAMESPACE__ . '\\' . $data['class'])) {
146 self::update('uploaded', $count, $source_key);
147 }
148 }
149 }
150 }
151
152 if ($prop === 'total') {
153 // Reset all
154 if(!empty($source_types)) {
155 foreach($source_types as $source_type => $source_type_data) {
156 self::update('total', 0, $source_type);
157 }
158 }
159 // Fetch total counts for all or specific source types
160 foreach ($source_types as $key => $data) {
161 if ($fetch_all || $key === $source_type) {
162 if ($data && class_exists($class = __NAMESPACE__ . '\\' . $data['class'])) {
163 $total = $class::get_total_media($key);
164 self::update('total', $total, $key);
165 }
166 }
167 }
168 }
169 }
170 }
171
172
173 public static function instance(){
174 if (is_null(self::$instance)) {
175 self::$instance = new self();
176 }
177 return self::$instance;
178 }
179 }