assets_url = WPMCS_ASSETS_URL; $this->version = WPMCS_VERSION; $this->token = WPMCS_TOKEN; // Initialize setup // $this->init(); } /** * * Add Count * @since 1.0.0 * */ public static function add( $property = 'uploaded', $source_type = 'media_library' ){ $settings_key = Schema::getConstant('COUNTER_KEY'); $data = Utils::get_option( $source_type, [], $settings_key ); $data[$property] = isset($data[$property]) ? (int)$data[$property] + 1 : 1; return Utils::update_option( $source_type, $data, $settings_key ); } /** * * Remove Count * @since 1.0.0 * */ public static function remove( $property = 'uploaded', $source_type = 'media_library' ){ $settings_key = Schema::getConstant('COUNTER_KEY'); $data = Utils::get_option( $source_type, [], $settings_key ); $data[$property] = isset($data[$property]) ? (int)$data[$property] - 1 : -1; return Utils::update_option( $source_type, $data, $settings_key ); } /** * Get Count * @since 1.0.0 */ public static function get( $property = 'uploaded', $source_type = 'media_library' ) { // Get the count by source type $data = Utils::get_option( $source_type, [], Schema::getConstant('COUNTER_KEY') ); return isset($data[$property]) ? (int)$data[$property] : 0; } /** * Update Count * @since 1.0.0 */ public static function update( $property, $value, $source_type = 'media_library' ) { $key = Schema::getConstant('COUNTER_KEY'); // Update the count by source type $data = Utils::get_option( $source_type, [], $key ); $data[$property] = $value; return Utils::update_option( $source_type, $data, $key ); } /** * Get total count from all source types. * * @param string $property The property to get ('uploaded', 'total', or 'all'). * @param bool $combined Whether to combine counts from all source types. * @return mixed Combined total count (int) or an associative array grouped by source types. */ public static function get_count($property = 'all', $combined = true) { $settings_key = Schema::getConstant('COUNTER_KEY'); $source_types = Integration::$source_types; // Determine properties to process $properties = ($property === 'all') ? ['uploaded', 'total', 'failed'] : [$property]; $result = $combined ? array_fill_keys($properties, 0) : []; if (!empty($source_types)) { foreach ($source_types as $source_type => $args) { $data = Utils::get_option($source_type, [], $settings_key); foreach ($properties as $prop) { $count = isset($data[$prop]) ? (int)$data[$prop] : 0; if ($combined) { // Add to combined total $result[$prop] += $count; } else { // Group by source type if (!isset($result[$source_type])) { $result[$source_type] = array_fill_keys($properties, 0); } $result[$source_type][$prop] += $count; } } } } return $combined ? ($property === 'all' ? $result : array_sum($result)) : $result; } /** * Fetch and update media counts for uploaded or total items. * * @param string $source_type The type of source to fetch (default: 'all'). * @param string $property The property to update ('uploaded', 'total', or 'all'). */ public static function fetch_and_update($property = 'all', $source_type = 'all') { global $wpdb; $source_types = Integration::$source_types; $fetch_all = ($source_type === 'all'); $properties = ($property === 'all') ? ['uploaded', 'total'] : [$property]; foreach ($properties as $prop) { if ($prop === 'uploaded') { // Reset all if(!empty($source_types)) { foreach($source_types as $source_type => $source_type_data) { self::update('uploaded', 0, $source_type); } } // Fetch uploaded counts grouped by source type $table_name = Db::get_table_name(); $results = $wpdb->get_results( "SELECT source_type, COUNT(*) AS count FROM " . $table_name . " GROUP BY source_type", ARRAY_A ); foreach ($results as $row) { $source_key = $row['source_type']; $count = (int) $row['count']; if ($fetch_all || $source_key === $source_type) { $data = $source_types[$source_key] ?? false; if ($data && class_exists($class = __NAMESPACE__ . '\\' . $data['class'])) { self::update('uploaded', $count, $source_key); } } } } if ($prop === 'total') { // Reset all if(!empty($source_types)) { foreach($source_types as $source_type => $source_type_data) { self::update('total', 0, $source_type); } } // Fetch total counts for all or specific source types foreach ($source_types as $key => $data) { if ($fetch_all || $key === $source_type) { if ($data && class_exists($class = __NAMESPACE__ . '\\' . $data['class'])) { $total = $class::get_total_media($key); self::update('total', $total, $key); } } } } } } public static function instance(){ if (is_null(self::$instance)) { self::$instance = new self(); } return self::$instance; } }