['name' => 'Freemium', 'limit' => 10, 'price_monthly' => 0, 'is_paid' => false], 'image_standard' => ['name' => 'Image Standard', 'limit' => 200, 'price_monthly' => 69, 'is_paid' => true], 'image_gold' => ['name' => 'Image Gold', 'limit' => 600, 'price_monthly' => 179, 'is_paid' => true], 'image_platinum' => ['name' => 'Image Platinum', 'limit' => 2000, 'price_monthly' => 479, 'is_paid' => true], ]; public static $ADACOGU_VIDEO_PLANS = [ // No freemium for video 'video_standard' => ['name' => 'Video Standard', 'limit_minutes' => 20, 'price_monthly' => 98, 'is_paid' => true], 'video_gold' => ['name' => 'Video Gold', 'limit_minutes' => 100, 'price_monthly' => 179, 'is_paid' => true], 'video_platinum' => ['name' => 'Video Platinum', 'limit_minutes' => 500, 'price_monthly' => 699, 'is_paid' => true], ]; // Option keys for usage tracking private static $OPTION_USAGE_IMAGE_PREFIX = 'adacogu_img_usage_'; private static $OPTION_USAGE_VIDEO_PREFIX = 'adacogu_video_usage_'; private static $OPTION_USAGE_FREEMIUM_IMAGE = 'adacogu_freemium_img_usage'; /** * Determines the user's plan based on the main plugin API key (Gemini API Key). * THIS IS A SIMULATION. Replace with your actual licensing/plan logic. * @param string|null $main_plugin_api_key The main plugin API key (Gemini key) to check. * @return array Plan information. */ public function get_user_plan_info($main_plugin_api_key = null) { // If OPTION_API_KEY is not passed, try to get it. This is the key that determines the plan. if ($main_plugin_api_key === null) { $main_plugin_api_key = get_option(self::OPTION_API_KEY); } if (empty($main_plugin_api_key)) { return [ 'image_plan' => self::ADACOGU_IMAGE_PLANS['freemium'], 'video_plan' => null, 'api_key_status' => 'freemium_no_key_present' ]; } // SIMULATED: Different keys for different plans. if (strpos($main_plugin_api_key, 'STANDARD_PLAN_KEY') !== false) { return ['image_plan' => self::ADACOGU_IMAGE_PLANS['image_standard'], 'video_plan' => self::ADACOGU_VIDEO_PLANS['video_standard'], 'api_key_status' => 'valid_standard']; } elseif (strpos($main_plugin_api_key, 'GOLD_PLAN_KEY') !== false) { return ['image_plan' => self::ADACOGU_IMAGE_PLANS['image_gold'], 'video_plan' => self::ADACOGU_VIDEO_PLANS['video_gold'], 'api_key_status' => 'valid_gold']; } elseif (strpos($main_plugin_api_key, 'PLATINUM_PLAN_KEY') !== false) { return ['image_plan' => self::ADACOGU_IMAGE_PLANS['image_platinum'], 'video_plan' => self::ADACOGU_VIDEO_PLANS['video_platinum'], 'api_key_status' => 'valid_platinum']; } elseif (strpos($main_plugin_api_key, 'AIza') === 0) { return ['image_plan' => self::ADACOGU_IMAGE_PLANS['image_standard'], 'video_plan' => self::ADACOGU_VIDEO_PLANS['video_standard'], 'api_key_status' => 'valid_assumed_standard']; } return [ 'image_plan' => self::ADACOGU_IMAGE_PLANS['freemium'], 'video_plan' => null, 'api_key_status' => 'unrecognized_api_key_for_plan' ]; } private function get_current_period_marker_for_usage() { return '_' . gmdate('Y') . '_' . gmdate('m'); } public function get_freemium_image_usage_data() { $usage_string = get_option(self::OPTION_USAGE_FREEMIUM_IMAGE, '0' . $this->get_current_period_marker_for_usage()); $parts = explode('_', $usage_string); $count = isset($parts[0]) ? (int)$parts[0] : 0; $year_stored = isset($parts[1]) ? $parts[1] : ''; $month_stored = isset($parts[2]) ? $parts[2] : ''; $current_period_marker_suffix = $this->get_current_period_marker_for_usage(); if ('_' . $year_stored . '_' . $month_stored !== $current_period_marker_suffix) { return ['used' => 0, 'limit' => self::ADACOGU_IMAGE_PLANS['freemium']['limit'], 'period_marker' => $current_period_marker_suffix]; } return ['used' => $count, 'limit' => self::ADACOGU_IMAGE_PLANS['freemium']['limit'], 'period_marker' => '_' . $year_stored . '_' . $month_stored]; } public function increment_freemium_image_usage_count($increment_by = 1) { $usage = $this->get_freemium_image_usage_data(); $new_count = $usage['used'] + $increment_by; update_option(self::OPTION_USAGE_FREEMIUM_IMAGE, $new_count . $usage['period_marker']); } public function get_api_image_usage_data($api_key) { if (empty($api_key)) return ['used' => 0, 'limit' => 0, 'period_marker' => $this->get_current_period_marker_for_usage()]; $current_period_marker = $this->get_current_period_marker_for_usage(); $option_key_for_period = self::OPTION_USAGE_IMAGE_PREFIX . md5($api_key) . $current_period_marker; $current_count = (int)get_option($option_key_for_period, 0); $plan_info = $this->get_user_plan_info($api_key); $limit = ($plan_info && isset($plan_info['image_plan']['limit'])) ? $plan_info['image_plan']['limit'] : 0; return ['used' => $current_count, 'limit' => $limit, 'period_marker' => $current_period_marker]; } public function increment_api_image_usage_count($api_key, $increment_by = 1) { if (empty($api_key)) return false; $usage_data = $this->get_api_image_usage_data($api_key); $new_count = $usage_data['used'] + $increment_by; $option_key = self::OPTION_USAGE_IMAGE_PREFIX . md5($api_key) . $usage_data['period_marker']; update_option($option_key, $new_count); } public function get_api_video_usage_data($api_key) { if (empty($api_key)) return ['used_minutes' => 0, 'limit_minutes' => 0, 'period_marker' => $this->get_current_period_marker_for_usage()]; $current_period_marker = $this->get_current_period_marker_for_usage(); $option_key_for_period = self::OPTION_USAGE_VIDEO_PREFIX . md5($api_key) . $current_period_marker; $current_minutes = (int)get_option($option_key_for_period, 0); $plan_info = $this->get_user_plan_info($api_key); $limit_minutes = ($plan_info && isset($plan_info['video_plan']['limit_minutes'])) ? $plan_info['video_plan']['limit_minutes'] : 0; return ['used_minutes' => $current_minutes, 'limit_minutes' => $limit_minutes, 'period_marker' => $current_period_marker]; } public function increment_api_video_usage_minutes($api_key, $minutes_used) { if (empty($api_key) || $minutes_used <= 0) return false; $usage_data = $this->get_api_video_usage_data($api_key); $new_minutes = $usage_data['used_minutes'] + $minutes_used; $option_key = self::OPTION_USAGE_VIDEO_PREFIX . md5($api_key) . $usage_data['period_marker']; update_option($option_key, $new_minutes); } }