api_base_url); } public static function get_instance() { if (null === self::$instance) { self::$instance = new self(); } return self::$instance; } private function __construct() { // Check if cooldown expired and reset word count add_action('init', [$this, 'check_cooldown_expiry']); // Admin notices for cooldown add_action('admin_notices', [$this, 'show_cooldown_notices']); // Admin notices for license expired add_action('admin_notices', [$this, 'show_license_expired_notice']); // Word limit usage notices (80% warning + review nudge / 100% blocking warning) add_action('admin_notices', [$this, 'show_word_limit_notices']); // AJAX handler for dismissing license expired notice add_action('wp_ajax_aitrfoel_dismiss_license_expired', [$this, 'dismiss_license_expired_notice']); // AJAX handlers for dismissing word limit notices add_action('wp_ajax_aitrfoel_dismiss_80_notice', [$this, 'dismiss_80_notice']); add_action('wp_ajax_aitrfoel_dismiss_100_notice', [$this, 'dismiss_100_notice']); // Freemius activation hooks (initialize user in remote DB) add_action('aitrfoel_fs_loaded', [$this, 'init_freemius_hooks']); // Auto-register Free/unlicensed site in remote DB (once per month) add_action('admin_init', [$this, 'maybe_auto_register_free_user']); // Auto-sync plan on admin pages (checks once per hour) add_action('admin_init', [$this, 'maybe_sync_plan']); // AJAX handler for manual plan sync add_action('wp_ajax_aitrfoel_sync_plan', [$this, 'ajax_sync_plan']); } /** * Initialize Freemius hooks after SDK is loaded */ public function init_freemius_hooks() { if (function_exists('eatp_fs') && is_object(eatp_fs()) && method_exists(eatp_fs(), 'add_action')) { // When user opts in (connects Freemius account) - Free plan eatp_fs()->add_action('after_account_connection', [$this, 'on_freemius_connected']); // When user activates a paid license eatp_fs()->add_action('after_license_activation', [$this, 'on_license_activated']); // When user changes plan (upgrade/downgrade) eatp_fs()->add_action('after_plan_change', [$this, 'on_plan_changed'], 10, 2); // When license or plan is synced (e.g. "Sync License" button, cron, or account sync) eatp_fs()->add_action('after_account_plan_sync', [$this, 'on_account_plan_synced']); eatp_fs()->add_action('after_license_change', [$this, 'on_license_changed'], 10, 2); eatp_fs()->add_action('after_plans_sync', [$this, 'clear_status_cache']); } } /** * Get plan name directly from local Freemius SDK if active */ public function get_freemius_plan_name() { if (!function_exists('eatp_fs') || !is_object(eatp_fs())) { return null; } try { if (method_exists(eatp_fs(), 'is_registered') && eatp_fs()->is_registered()) { // Method 1: Check active license plan_id if (method_exists(eatp_fs(), '_get_license')) { $license = eatp_fs()->_get_license(); if ($license && !empty($license->plan_id) && method_exists(eatp_fs(), '_get_plan_by_id')) { $plan = eatp_fs()->_get_plan_by_id($license->plan_id); if ($plan && !empty($plan->name)) { return $plan->name; } } } // Method 2: Check get_plan_name() if (method_exists(eatp_fs(), 'get_plan_name')) { $plan_name = eatp_fs()->get_plan_name(); if (!empty($plan_name) && strtolower($plan_name) !== 'free' && strtolower($plan_name) !== 'plan_name') { return $plan_name; } } // Method 3: Check get_plan() if (method_exists(eatp_fs(), 'get_plan')) { $plan = eatp_fs()->get_plan(); if ($plan && !empty($plan->name)) { return $plan->name; } } } } catch (Exception $e) { // Ignore } return null; } /** * Get license key from Freemius (with caching to avoid repeated SDK calls) */ public function get_license_key() { // Return cached value if available if ($this->cached_license_key !== null) { return $this->cached_license_key; } // During background translation AJAX, avoid calling Freemius SDK $is_translation_ajax = defined('DOING_AJAX') && DOING_AJAX && isset($_POST['action']) && in_array($_POST['action'], ['aitrfoel_translate_page', 'aitrfoel_process_bulk_batch']); if ($is_translation_ajax) { // Use cached value from WordPress options (set during admin page load) $cached_key = get_option('aitrfoel_cached_license_key', ''); $this->cached_license_key = $cached_key ?: null; return $this->cached_license_key; } // Normal flow: Get from Freemius (only in admin context) if (function_exists('eatp_fs') && is_object(eatp_fs()) && method_exists(eatp_fs(), 'is_registered') && eatp_fs()->is_registered()) { try { $license = eatp_fs()->_get_license(); if ($license && isset($license->secret_key)) { $this->cached_license_key = $license->secret_key; // Cache in WordPress options for AJAX use update_option('aitrfoel_cached_license_key', $this->cached_license_key, false); return $this->cached_license_key; } } catch (Exception $e) { if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('Could not get Freemius license: ' . $e->getMessage(), 'warning'); } } } $this->cached_license_key = null; return null; } /** * Clear cached license status so the next call to get_license_status() re-fetches from server. */ public function clear_status_cache() { $this->cached_license_status = null; $this->cached_license_key = null; delete_transient('aitrfoel_license_status_cache'); delete_transient('aitrfoel_last_plan_sync_check'); delete_option('aitrfoel_cached_license_key'); } /** * Get current license status from server */ public function get_license_status() { // Return cached result if already fetched this request if ($this->cached_license_status !== null) { return $this->cached_license_status; } // PERFORMANCE: Check if there is a transient cache $cached = get_transient('aitrfoel_license_status_cache'); if ($cached !== false && is_array($cached)) { // Check if Freemius locally upgraded to a paid tier while transient was cached as Free $fs_plan = $this->get_freemius_plan_name(); $cached_is_free = empty($cached['plan']) || strtolower($cached['plan']) === 'free'; $fs_is_paid = !empty($fs_plan) && strtolower($fs_plan) !== 'free'; if ($fs_is_paid && $cached_is_free) { // Cache is stale - bust it $this->clear_status_cache(); } else { $this->cached_license_status = $cached; return $this->cached_license_status; } } $plan_name = 'Free'; $word_limit = 7200; // Get license key (uses cache during AJAX) $license_key = $this->get_license_key(); // Get REAL usage AND plan from server (server is source of truth) $server_usage = $this->get_usage_from_server($license_key); // Use plan and limit from server if available if (isset($server_usage['plan'])) { $plan_name = $server_usage['plan']; } // AUTHORITY OVERRIDE: If Freemius locally is registered with a verified paid plan, // but the remote server returned 'Free' or empty (e.g. before webhook arrival or delay), // Freemius's verified paid plan takes precedence! $fs_plan = $this->get_freemius_plan_name(); if (!empty($fs_plan) && strtolower($fs_plan) !== 'free') { if (empty($plan_name) || strtolower($plan_name) === 'free' || (strtolower($fs_plan) === 'agency lifetime' && strtolower($plan_name) !== 'agency lifetime')) { $plan_name = $fs_plan; // Dispatch update to remote server so DB is synced immediately if (!empty($license_key)) { $this->update_plan_in_remote_db($plan_name, $license_key); } } } if (isset($server_usage['word_limit']) && strtolower($plan_name) === strtolower($server_usage['plan'] ?? '')) { $word_limit = $server_usage['word_limit']; } else { // Calculate from authoritative plan name $word_limit = $this->get_word_limit_for_plan($plan_name); } // Add addon words to the effective limit when the 50K addon is active. $addon_words = 0; if (!empty($server_usage['addon_active']) && isset($server_usage['addon_words'])) { $addon_words = intval($server_usage['addon_words']); if ($word_limit !== -1) { $word_limit += $addon_words; } } $words_used = $server_usage['words_used']; $words_left = $word_limit === -1 ? -1 : max(0, $word_limit - $words_used); $is_unlimited = ($word_limit === -1 || strtolower($plan_name) === 'agency lifetime'); $cooldown_end = (int) get_option('aitrfoel_cooldown_end_date', 0); $cooldown_active = false; if (!$is_unlimited && strtolower($plan_name) === 'free') { $cooldown_active = isset($server_usage['cooldown_active']) ? (bool) $server_usage['cooldown_active'] : ($cooldown_end > 0 && $cooldown_end > time() && $words_used >= $word_limit); } $this->cached_license_status = [ 'plan' => $plan_name, 'word_limit' => $word_limit, 'words_used' => $words_used, 'words_left' => $words_left, 'license_key' => $license_key, 'unlimited' => $is_unlimited, 'is_unlimited' => $is_unlimited, 'freemius_validated' => isset($server_usage['freemius_validated']) ? $server_usage['freemius_validated'] : false, 'license_expired' => isset($server_usage['license_expired']) ? $server_usage['license_expired'] : false, 'addon_active' => isset($server_usage['addon_active']) ? (bool) $server_usage['addon_active'] : false, 'addon_words' => $addon_words, 'cooldown_active' => $cooldown_active, 'social_bonus_claimed' => isset($server_usage['social_bonus_claimed']) ? (bool) $server_usage['social_bonus_claimed'] : (bool) get_option('aitrfoel_share_bonus_claimed', false), ]; // Cache for 10 minutes to prevent excessive server requests on general page loads set_transient('aitrfoel_license_status_cache', $this->cached_license_status, 10 * MINUTE_IN_SECONDS); return $this->cached_license_status; } /** * Get usage from translation server */ private function get_usage_from_server($license_key = null) { try { $request_data = [ 'license_key' => $license_key, 'site_url' => home_url() ]; // Include addon license key if the 50K Words Pack addon is active if (function_exists('e50wp_fs')) { $addon_fs = e50wp_fs(); if (method_exists($addon_fs, '_get_license')) { $addon_license = $addon_fs->_get_license(); if ($addon_license && !empty($addon_license->secret_key)) { $request_data['addon_license_key'] = $addon_license->secret_key; } } } if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('🔍 Requesting usage from server: ' . $this->api_base_url . '/get-usage', 'info'); AITRFOEL_Logger::log('📦 Request data: ' . json_encode($request_data), 'info'); } $response = wp_remote_post($this->api_base_url . '/get-usage', [ 'timeout' => 5, 'body' => json_encode($request_data), 'headers' => [ 'Content-Type' => 'application/json' ] ]); if (is_wp_error($response)) { $error_msg = $response->get_error_message(); error_log('EAT: ❌ API Error: ' . $error_msg); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('❌ Failed to get usage from server: ' . $error_msg, 'warning'); } // Fallback to local cache $fallback_words = get_option('aitrfoel_words_used_this_month', 0); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('📊 Using local fallback: ' . $fallback_words . ' words', 'info'); } return ['words_used' => $fallback_words]; } $body = wp_remote_retrieve_body($response); $status_code = wp_remote_retrieve_response_code($response); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('📡 Server response status: ' . $status_code, 'info'); AITRFOEL_Logger::log('📄 Server response body: ' . $body, 'info'); } $data = json_decode($body, true); if (isset($data['success']) && $data['success']) { $words_used = isset($data['words_used']) ? intval($data['words_used']) : 0; if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('✅ Server returned words_used: ' . $words_used, 'info'); } // Cache the result locally for performance update_option('aitrfoel_words_used_this_month', $words_used); // Return full data including plan and word_limit from server $result = ['words_used' => $words_used]; if (isset($data['plan'])) { $result['plan'] = $data['plan']; } if (isset($data['word_limit'])) { $result['word_limit'] = intval($data['word_limit']); } // NEW: Handle Freemius validation fields if (isset($data['freemius_validated'])) { $result['freemius_validated'] = (bool) $data['freemius_validated']; } if (isset($data['license_expired'])) { $result['license_expired'] = (bool) $data['license_expired']; // If license expired, show admin notice if ($data['license_expired']) { set_transient('aitrfoel_license_expired', true, HOUR_IN_SECONDS); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('⚠️ License expired - downgraded to Free plan', 'warning'); } } else { // License is valid, clear any expired notice delete_transient('aitrfoel_license_expired'); } } // Handle addon boost fields if (isset($data['addon_active'])) { $result['addon_active'] = (bool) $data['addon_active']; } if (isset($data['addon_words'])) { $result['addon_words'] = intval($data['addon_words']); } // Sync social share bonus status from server (auto-heals any prior failed local locks) if (isset($data['social_bonus_claimed'])) { $result['social_bonus_claimed'] = (bool) $data['social_bonus_claimed']; if ($data['social_bonus_claimed'] === true) { if (!get_option('aitrfoel_share_bonus_claimed', false)) { update_option('aitrfoel_share_bonus_claimed', time()); } } elseif ($data['social_bonus_claimed'] === false && get_option('aitrfoel_share_bonus_claimed', false)) { delete_option('aitrfoel_share_bonus_claimed'); } } // Handle cooldown status from server (syncs when admin resets usage) if (isset($data['cooldown_active'])) { $result['cooldown_active'] = (bool) $data['cooldown_active']; if ($data['cooldown_active'] === true && isset($data['cooldown_ends_at'])) { // Server says cooldown is active - sync to local $cooldown_end = strtotime($data['cooldown_ends_at']); if ($cooldown_end && $cooldown_end > time()) { update_option('aitrfoel_cooldown_end_date', $cooldown_end); } } elseif ($data['cooldown_active'] === false) { // Server says NO cooldown - clear local (admin reset usage) $local_cooldown = get_option('aitrfoel_cooldown_end_date', 0); if ($local_cooldown > 0) { delete_option('aitrfoel_cooldown_end_date'); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('✅ Cooldown cleared - server confirmed no active cooldown', 'info'); } } } } return $result; } // Fallback to local cache $fallback_words = get_option('aitrfoel_words_used_this_month', 0); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('⚠️ Server response not successful, using local fallback: ' . $fallback_words . ' words', 'warning'); } return ['words_used' => $fallback_words]; } catch (Exception $e) { error_log('EAT: ❌ Exception: ' . $e->getMessage()); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('❌ Error getting usage from server: ' . $e->getMessage(), 'error'); } return ['words_used' => get_option('aitrfoel_words_used_this_month', 0)]; } } /** * Get total usage for a license across all sites (for multi-site licenses) * * @param string $license_key The license key to check * @return array Array with total_words_used, word_limit, active_sites, plan */ public function get_total_usage_for_license($license_key) { if (empty($license_key)) { return [ 'total_words_used' => 0, 'word_limit' => 7200, 'active_sites' => 1, 'plan' => 'Free' ]; } try { $response = wp_remote_post($this->api_base_url . '/get-license-usage', [ 'body' => json_encode(['license_key' => $license_key]), 'headers' => [ 'Content-Type' => 'application/json', 'X-API-Key' => $this->api_key ], 'timeout' => 15 ]); if (is_wp_error($response)) { AITRFOEL_Logger::log('❌ Error getting license usage: ' . $response->get_error_message(), 'error'); return [ 'total_words_used' => 0, 'word_limit' => 7200, 'active_sites' => 1, 'plan' => 'Free' ]; } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if (isset($data['success']) && $data['success']) { return [ 'total_words_used' => $data['total_words_used'] ?? 0, 'word_limit' => $data['word_limit'] ?? 7200, 'active_sites' => $data['active_sites'] ?? 1, 'plan' => $data['plan'] ?? 'Free' ]; } return [ 'total_words_used' => 0, 'word_limit' => 7200, 'active_sites' => 1, 'plan' => 'Free' ]; } catch (Exception $e) { AITRFOEL_Logger::log('❌ Exception getting license usage: ' . $e->getMessage(), 'error'); return [ 'total_words_used' => 0, 'word_limit' => 7200, 'active_sites' => 1, 'plan' => 'Free' ]; } } /** * Translate text via your server */ public function translate_via_server($text, $source_language, $target_language) { // Get license info $license_status = $this->get_license_status(); // Prepare request data $request_data = [ 'text' => $text, 'source_language' => $source_language, 'target_language' => $target_language, 'site_url' => home_url(), 'plan' => $license_status['plan'], 'license_key' => $license_status['license_key'] ]; if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('Sending translation request to server', 'info', [ 'word_count' => AITRFOEL_Translation_Engine::count_words($text), 'plan' => $license_status['plan'], 'text_preview' => substr($text, 0, 100) . '...' ]); } // Make request to hosted API $response = wp_remote_post($this->api_base_url . '/translate', [ 'headers' => [ 'Content-Type' => 'application/json', 'User-Agent' => 'ai-translator-for-elementor-and-polylang/' . (defined('AITRFOEL_VERSION') ? AITRFOEL_VERSION : '1.0') ], 'body' => json_encode($request_data), 'timeout' => 60 ]); if (is_wp_error($response)) { $error_msg = __('Failed to connect to translation service: ', 'ai-translator-for-elementor-and-polylang') . $response->get_error_message(); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log($error_msg, 'error'); } throw new Exception(esc_html($error_msg)); } $response_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); // Log raw response for debugging if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('🔍 Translation API Response Status: ' . $response_code, 'info'); AITRFOEL_Logger::log('📄 Translation API Response Body: ' . $response_body, 'info'); } if ($response_code !== 200) { $error_data = json_decode($response_body, true); $error_message = isset($error_data['error']) ? $error_data['error'] : __('Translation service error', 'ai-translator-for-elementor-and-polylang'); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('Translation API Error', 'error', [ 'status_code' => $response_code, 'error' => $error_message, 'full_response' => $response_body ]); } throw new Exception(esc_html($error_message)); } $data = json_decode($response_body, true); if (!isset($data['success']) || !$data['success']) { $error_msg = isset($data['error']) ? $data['error'] : __('Invalid response from translation service', 'ai-translator-for-elementor-and-polylang'); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('Translation API Invalid Response', 'error', [ 'response_data' => $data, 'error' => $error_msg ]); } throw new Exception(esc_html($error_msg)); } if (!isset($data['translated_text'])) { $error_msg = __('No translated text in response', 'ai-translator-for-elementor-and-polylang'); if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('Translation API Missing Field', 'error', [ 'response_data' => $data ]); } throw new Exception(esc_html($error_msg)); } // Update local cache with server usage if (isset($data['total_words_used'])) { if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('💾 Updating local cache: total_words_used = ' . $data['total_words_used'], 'info'); } update_option('aitrfoel_words_used_this_month', $data['total_words_used']); // Clear status cache to force fresh data on next read $this->clear_status_cache(); } else { if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('⚠️ WARNING: translate response missing "total_words_used" field!', 'warning'); AITRFOEL_Logger::log('📋 Available fields in response: ' . implode(', ', array_keys($data)), 'info'); } } if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('Translation completed successfully', 'info', [ 'words_used' => $data['words_used'] ?? 0, 'total_words_used' => $data['total_words_used'] ?? 0, 'translated_preview' => substr($data['translated_text'], 0, 100) . '...' ]); } return $data['translated_text']; } /** * Get word limit for plan */ private function get_word_limit_for_plan($plan_name) { $normalized = strtolower(trim((string) $plan_name)); $limits = [ 'free' => 7200, // Free: 7,200 words/month (1 site) 'starter' => 18000, // Starter: 18,000 words/month (1 site) 'professional' => 60000, // Professional: 60,000 words/month 'agency' => 89000, // Agency: 89,000 words/month (up to 10 sites, shared) 'agency lifetime' => -1, // Agency Lifetime: unlimited words (up to 50 sites) 'enterprise' => 355000, // Enterprise: 355,000 words/month (up to 50 sites, shared) ]; return $limits[$normalized] ?? 7200; } /** * Check if monthly cooldown expired and reset word count for free users * Also clear cooldown for paid plan users (they shouldn't have cooldown) */ public function check_cooldown_expiry() { $cooldown_end = get_option('aitrfoel_cooldown_end_date', 0); // If cooldown exists, check if user is on paid plan if ($cooldown_end > 0) { $license_status = $this->get_license_status(); $is_free_plan = ($license_status['plan'] === 'Free'); // IMPORTANT: Paid plans should NEVER have cooldown if (!$is_free_plan) { // Clear cooldown immediately for paid users delete_option('aitrfoel_cooldown_end_date'); return; } // For free users, check if cooldown expired if ($cooldown_end <= time()) { // Cooldown expired - the server is the source of truth for usage (monthly quota); // local count is only cleared if the server confirms words_used === 0. delete_option('aitrfoel_cooldown_end_date'); // Notify server about the reset $this->notify_server_of_cooldown_reset(); } } } /** * Notify server that monthly cooldown reset occurred */ private function notify_server_of_cooldown_reset() { try { $response = wp_remote_post($this->api_base_url . '/reset-usage', [ 'timeout' => 5, 'body' => json_encode([ 'site_url' => home_url(), 'reset_date' => gmdate('Y-m-d H:i:s'), 'event' => 'cooldown_reset' ]), 'headers' => [ 'Content-Type' => 'application/json' ] ]); if (!is_wp_error($response)) { $status_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); // Handle server response - clear cooldown when server confirms reset if (isset($data['success']) && $data['success'] && isset($data['words_used']) && $data['words_used'] === 0) { delete_option('aitrfoel_cooldown_end_date'); update_option('aitrfoel_words_used_this_month', 0); } } else { error_log('EAT: Failed to notify server of reset: ' . $response->get_error_message()); } } catch (Exception $e) { error_log('EAT: Exception notifying server of reset: ' . $e->getMessage()); } } /** * Show cooldown admin notices */ public function show_cooldown_notices() { // If user has a paid/unlimited license, cooldown notices don't apply. // This handles the case where a site has both a free entry and a paid // license on the server — the free entry may trigger cooldown but the // paid license should take priority. $status = $this->get_license_status(); if ($status && ( (isset($status['word_limit']) && $status['word_limit'] === -1) || (isset($status['plan']) && $status['plan'] !== 'Free' && $status['plan'] !== 'Free User') )) { // Paid plan — clear any stale cooldown state and skip notice delete_transient('aitrfoel_cooldown_activated'); delete_option('aitrfoel_cooldown_end_date'); return; } // Check if cooldown was just activated if (get_transient('aitrfoel_cooldown_activated')) { delete_transient('aitrfoel_cooldown_activated'); $cooldown_end = get_option('aitrfoel_cooldown_end_date', 0); $days_left = ceil(($cooldown_end - time()) / DAY_IN_SECONDS); $upgrade_url = (function_exists('eatp_fs') && is_object(eatp_fs()) && method_exists(eatp_fs(), 'is_registered') && eatp_fs()->is_registered()) ? eatp_fs()->get_upgrade_url() : ((defined('AITRFOEL_VERSION_TYPE') && AITRFOEL_VERSION_TYPE === 'pro') ? 'https://meeptech.com/products/ai-translator-for-elementor-polylang/?utm_source=wp-plugin&utm_medium=free-plugin&utm_campaign=pro-upgrade#pricing' : 'https://meeptech.com/products/ai-translator-for-elementor-polylang/?utm_source=wp-plugin&utm_medium=cooldown-notice&utm_campaign=pro-upgrade#pricing'); echo '
'; echo '

' . esc_html__('Meep Translator: Monthly Limit Reached', 'ai-translator-for-elementor-and-polylang') . '

'; echo '

' . sprintf( /* translators: %d: Number of days until word count resets */ esc_html__('You have used all 7,200 words in your free plan this month. Your word count will reset in %d days, or you can ', 'ai-translator-for-elementor-and-polylang'), absint($days_left) ); echo '' . esc_html__('upgrade now', 'ai-translator-for-elementor-and-polylang') . ''; echo ' ' . esc_html__('for immediate access to more words.', 'ai-translator-for-elementor-and-polylang') . '

'; echo '
'; } // Note: Persistent cooldown notice has been moved to show_word_limit_notices() } /** * Show license expired admin notice */ public function show_license_expired_notice() { // Check if license is expired if (!get_transient('aitrfoel_license_expired')) { return; } // Check if user dismissed the notice recently (24 hours) if (get_transient('aitrfoel_license_expired_dismissed')) { return; } // Only show on plugin pages or dashboard $screen = get_current_screen(); if (!$screen) { return; } $show_on_screens = [ 'dashboard', 'toplevel_page_ai-translator-for-elementor-and-polylang', 'ai-translator_page_ai-translator-for-elementor-and-polylang-pricing', 'ai-translator_page_ai-translator-for-elementor-and-polylang-account' ]; if (!in_array($screen->id, $show_on_screens) && strpos($screen->id, 'ai-translator') === false) { return; } $renew_url = (function_exists('eatp_fs') && is_object(eatp_fs()) && method_exists(eatp_fs(), 'is_registered') && eatp_fs()->is_registered()) ? eatp_fs()->get_upgrade_url() : 'https://meeptech.com/products/ai-translator-for-elementor-polylang/?utm_source=wp-plugin&utm_medium=limit-notice&utm_campaign=pro-upgrade#pricing'; ?>


.

get_license_status(); if (!$status) { return; } $plan = isset($status['plan']) ? $status['plan'] : 'Free'; $limit = isset($status['word_limit']) ? intval($status['word_limit']) : 0; $used = isset($status['words_used']) ? intval($status['words_used']) : 0; // Skip if unlimited plan if ($limit === -1 || $limit <= 0) { return; } // Calculate usage percentage // Calculate usage percentage $pct = min(100, round(($used / $limit) * 100)); // CASE 0: Review Prompt after 3+ translated pages (or 2,500+ words) when not showing usage warnings if ($pct < 80) { $rev_dismissed = (bool) get_option('aitrfoel_review_dismissed', false); $rev_snoozed = (int) get_option('aitrfoel_review_snoozed_until', 0); $rev_count = (int) get_option('aitrfoel_successful_translations_count', 0); $rev_activated = strtotime((string) get_option('aitrfoel_license_activated_at', '')) ?: time(); if (!$rev_dismissed && time() > $rev_snoozed && ($rev_count >= 3 || $used >= 2500) && (time() - $rev_activated) >= 3 * DAY_IN_SECONDS) { ?>
★★★★★

= 3) { printf(esc_html__('Milestone unlocked: %d+ pages translated with Meep AI Translator!', 'ai-translator-for-elementor-and-polylang'), absint($rev_count)); } else { printf(esc_html__('Milestone unlocked: %s+ words translated with Meep AI Translator!', 'ai-translator-for-elementor-and-polylang'), number_format_i18n($used)); } ?>

★
time()); $upgrade_url = (function_exists('eatp_fs') && is_object(eatp_fs()) && method_exists(eatp_fs(), 'is_registered') && eatp_fs()->is_registered()) ? eatp_fs()->get_upgrade_url() : ((defined('AITRFOEL_VERSION_TYPE') && AITRFOEL_VERSION_TYPE === 'pro') ? 'https://meeptech.com/products/ai-translator-for-elementor-polylang/?utm_source=wp-plugin&utm_medium=free-plugin&utm_campaign=pro-upgrade#pricing' : 'https://meeptech.com/products/ai-translator-for-elementor-polylang/?utm_source=wp-plugin&utm_medium=limit-notice&utm_campaign=pro-upgrade#pricing'); // Check if we are on a plugin page $is_plugin_page = (strpos($screen->id, 'ai-translator') !== false); // CASE 1: 100% Usage or Cooldown Active (Blocking Warning) if ($used >= $limit || $is_cooldown) { // Check if dismissed on general (non-plugin) pages if (!$is_plugin_page && get_transient('aitrfoel_limit_100_dismissed')) { return; } // Determine if notice should be dismissible $is_dismissible = !$is_plugin_page; // Calculate days left for cooldown $days_left = 0; if ($is_cooldown) { $days_left = ceil(($cooldown_end - time()) / DAY_IN_SECONDS); } $tm_stats = class_exists('AITRFOEL_Translation_Memory') ? AITRFOEL_Translation_Memory::get_instance()->get_stats() : ['total_words_saved' => 0]; $words_saved_tm = isset($tm_stats['total_words_saved']) ? intval($tm_stats['total_words_saved']) : 0; $inventory = class_exists('AITRFOEL_Settings') ? AITRFOEL_Settings::get_site_content_inventory() : null; $untranslated_words = $inventory && isset($inventory['untranslated_words']) ? intval($inventory['untranslated_words']) : 0; $rec_plan = class_exists('AITRFOEL_Settings') ? AITRFOEL_Settings::get_recommended_plan_for_words($untranslated_words) : null; $rec_plan_name = $rec_plan ? $rec_plan['name'] : 'Starter'; $rec_plan_url = $rec_plan && !empty($rec_plan['upgrade_url']) ? $rec_plan['upgrade_url'] : $upgrade_url; ?>
0): ?>

/
0): ?>

0 && $rec_plan): ?> ' . esc_html($rec_plan_name) . '', number_format_i18n($rec_plan['limit'])); ?>

🎁
= 80) { // Check if user dismissed the nudge $dismissed = get_user_meta(get_current_user_id(), 'aitrfoel_dismiss_80_nudge', true); if ($dismissed) { return; } ?>

initialize_user_in_remote_db('Free', null); } /** * Auto-register free user on admin_init (once per calendar month) * Ensures every active Free installation is registered in the remote database */ public function maybe_auto_register_free_user() { $current_month = gmdate('Y-m'); if (get_option('aitrfoel_remote_user_initialized') && get_option('aitrfoel_remote_user_initialized_month') === $current_month) { return; } $license_key = $this->get_license_key(); if (!empty($license_key)) { return; } // Back off after a failed attempt so an unreachable API doesn't block every admin page load (30s timeout). if (get_transient('aitrfoel_auto_register_backoff')) { return; } if (class_exists('AITRFOEL_Logger')) { AITRFOEL_Logger::log('Auto-registering free site in remote database for month ' . $current_month, 'info'); } if (!$this->initialize_user_in_remote_db('Free', null)) { set_transient('aitrfoel_auto_register_backoff', 1, HOUR_IN_SECONDS); } } /** * Handle license activation (Paid plan) * Called when user activates a paid license key */ public function on_license_activated($license = null) { $this->clear_status_cache(); delete_option('aitrfoel_cooldown_end_date'); // Resolve license object from Freemius if not passed if ((!$license || !is_object($license)) && function_exists('eatp_fs') && is_object(eatp_fs()) && method_exists(eatp_fs(), '_get_license')) { try { $license = eatp_fs()->_get_license(); } catch (Exception $e) {} } $license_key = (is_object($license) && !empty($license->secret_key)) ? $license->secret_key : $this->get_license_key(); $plan = $this->get_freemius_plan_name() ?: 'Starter'; if (is_object($license) && !empty($license->plan_id) && function_exists('eatp_fs') && method_exists(eatp_fs(), '_get_plan_by_id')) { try { $plan_obj = eatp_fs()->_get_plan_by_id($license->plan_id); if ($plan_obj && !empty($plan_obj->name)) { $plan = $plan_obj->name; } } catch (Exception $e) {} } AITRFOEL_Logger::log('License activated - initializing paid user in remote DB: ' . $plan, 'info', [ 'license_key' => $license_key ? substr($license_key, 0, 10) . '...' : 'null', 'plan' => $plan, ]); $this->initialize_user_in_remote_db($plan, $license_key); $this->update_plan_in_remote_db($plan, $license_key); $this->clear_status_cache(); } /** * Handle plan change (Upgrade/Downgrade) * Called when user upgrades or downgrades their plan */ public function on_plan_changed($old_plan, $new_plan) { $this->clear_status_cache(); delete_option('aitrfoel_cooldown_end_date'); $old_plan_name = isset($old_plan->name) ? $old_plan->name : 'Unknown'; $new_plan_name = isset($new_plan->name) ? $new_plan->name : ($this->get_freemius_plan_name() ?: 'Unknown'); AITRFOEL_Logger::log(sprintf( 'Plan changed from %s to %s - updating remote database', $old_plan_name, $new_plan_name ), 'info'); $license_key = $this->get_license_key(); $this->update_plan_in_remote_db($new_plan_name, $license_key); $this->clear_status_cache(); } /** * Handle Freemius plan sync (Sync License button or periodic sync) */ public function on_account_plan_synced($plan_name = null) { $this->clear_status_cache(); delete_option('aitrfoel_cooldown_end_date'); $plan = !empty($plan_name) ? $plan_name : $this->get_freemius_plan_name(); $license_key = $this->get_license_key(); if (!empty($plan) && strtolower($plan) !== 'free') { AITRFOEL_Logger::log('Freemius account plan synced: ' . $plan . ' - updating remote DB', 'info'); $this->update_plan_in_remote_db($plan, $license_key); $this->clear_status_cache(); } } /** * Handle Freemius license change */ public function on_license_changed($plan_change = null, $plan = null) { $this->clear_status_cache(); delete_option('aitrfoel_cooldown_end_date'); $plan_name = (is_object($plan) && !empty($plan->name)) ? $plan->name : $this->get_freemius_plan_name(); $license_key = $this->get_license_key(); if (!empty($plan_name) && strtolower($plan_name) !== 'free') { AITRFOEL_Logger::log('Freemius license changed to ' . $plan_name . ' - updating remote DB', 'info'); $this->update_plan_in_remote_db($plan_name, $license_key); $this->clear_status_cache(); } } /** * Initialize user in remote database * Creates a new user record with initial word count */ public function initialize_user_in_remote_db($plan, $license_key) { $site_url = home_url(); $body = json_encode([ 'site_url' => $site_url, 'license_key' => $license_key, 'plan' => $plan, 'action' => 'initialize', 'month' => gmdate('Y-m') ]); AITRFOEL_Logger::log('Initializing user in remote DB: ' . $body, 'info'); $response = wp_remote_post( $this->api_base_url . '/initialize-user', [ 'body' => $body, 'headers' => ['Content-Type' => 'application/json'], 'timeout' => 30, 'sslverify' => true ] ); if (is_wp_error($response)) { AITRFOEL_Logger::log('Initialize user failed: ' . $response->get_error_message(), 'error'); return false; } $result = json_decode(wp_remote_retrieve_body($response), true); AITRFOEL_Logger::log('Initialize user response: ' . wp_remote_retrieve_body($response), 'info'); if (isset($result['success']) && $result['success']) { AITRFOEL_Logger::log('User initialized successfully in remote database', 'success'); // Cache locally update_option('aitrfoel_license_activated_at', current_time('mysql')); update_option('aitrfoel_remote_user_initialized', true); update_option('aitrfoel_remote_user_initialized_month', gmdate('Y-m')); update_option('aitrfoel_initial_plan', $plan); // Update local cache with initial values if (isset($result['data'])) { $this->update_local_cache($result['data']); } return true; } else { $error_msg = isset($result['error']) ? $result['error'] : 'Unknown error'; AITRFOEL_Logger::log('Failed to initialize user: ' . $error_msg, 'error'); return false; } } /** * Update plan in remote database * Updates existing user's plan and word limit */ private function update_plan_in_remote_db($plan, $license_key) { $site_url = home_url(); $body = json_encode([ 'site_url' => $site_url, 'license_key' => $license_key, 'plan' => $plan, 'action' => 'update_plan' ]); AITRFOEL_Logger::log('Updating plan in remote DB: ' . $body, 'info'); $response = wp_remote_post( $this->api_base_url . '/update-plan', [ 'body' => $body, 'headers' => ['Content-Type' => 'application/json'], 'timeout' => 30, 'sslverify' => true ] ); if (is_wp_error($response)) { AITRFOEL_Logger::log('Update plan failed: ' . $response->get_error_message(), 'error'); return false; } $result = json_decode(wp_remote_retrieve_body($response), true); AITRFOEL_Logger::log('Update plan response: ' . wp_remote_retrieve_body($response), 'info'); if (isset($result['success']) && $result['success']) { AITRFOEL_Logger::log('Plan updated successfully in remote database', 'success'); // Update local cache if (isset($result['data'])) { $this->update_local_cache($result['data']); } $this->clear_status_cache(); return true; } else { $error_msg = isset($result['error']) ? $result['error'] : 'Unknown error'; AITRFOEL_Logger::log('Failed to update plan: ' . $error_msg, 'error'); return false; } } /** * Update local cache with remote data */ private function update_local_cache($data) { if (isset($data['words_used'])) { update_option('aitrfoel_words_used_this_month', intval($data['words_used'])); } if (isset($data['word_limit'])) { update_option('aitrfoel_word_limit', intval($data['word_limit'])); } if (isset($data['plan'])) { update_option('aitrfoel_current_plan', sanitize_text_field($data['plan'])); } $this->clear_status_cache(); } /** * Sync the current Freemius plan to the remote server * This can be called manually or automatically to fix plan mismatches * * @return array Result with success status and message */ public function sync_plan_to_server() { if (!function_exists('eatp_fs') || !is_object(eatp_fs()) || !method_exists(eatp_fs(), 'is_registered') || !eatp_fs()->is_registered()) { AITRFOEL_Logger::log('Cannot sync plan - Freemius not registered', 'warning'); return [ 'success' => false, 'message' => 'Freemius not registered' ]; } $license_key = $this->get_license_key(); if (empty($license_key)) { AITRFOEL_Logger::log('Cannot sync plan - no license key found', 'warning'); return [ 'success' => false, 'message' => 'No license key found' ]; } // Get the current plan from Freemius using multiple methods $plan = 'Free'; // Method 1: Try to get plan from license's plan_id try { $license = method_exists(eatp_fs(), '_get_license') ? eatp_fs()->_get_license() : null; if ($license && isset($license->plan_id) && !empty($license->plan_id)) { $plan_obj = method_exists(eatp_fs(), '_get_plan_by_id') ? eatp_fs()->_get_plan_by_id($license->plan_id) : null; if ($plan_obj && isset($plan_obj->name)) { $plan = $plan_obj->name; AITRFOEL_Logger::log('sync_plan_to_server: Got plan from license plan_id: ' . $plan, 'info'); } } } catch (Exception $e) { AITRFOEL_Logger::log('sync_plan_to_server: Could not get plan from license: ' . $e->getMessage(), 'warning'); } // Method 2: Fallback to get_plan_name() if ($plan === 'Free') { try { $plan_name = method_exists(eatp_fs(), 'get_plan_name') ? eatp_fs()->get_plan_name() : null; if ($plan_name && $plan_name !== 'PLAN_NAME' && $plan_name !== 'free') { $plan = $plan_name; AITRFOEL_Logger::log('sync_plan_to_server: Got plan from get_plan_name(): ' . $plan, 'info'); } } catch (Exception $e) { AITRFOEL_Logger::log('sync_plan_to_server: Could not get plan name: ' . $e->getMessage(), 'warning'); } } // Method 3: Fallback to get_plan() if ($plan === 'Free') { try { $plan_obj = method_exists(eatp_fs(), 'get_plan') ? eatp_fs()->get_plan() : null; if ($plan_obj && isset($plan_obj->name)) { $plan = $plan_obj->name; AITRFOEL_Logger::log('sync_plan_to_server: Got plan from get_plan(): ' . $plan, 'info'); } } catch (Exception $e) { AITRFOEL_Logger::log('sync_plan_to_server: Could not get plan: ' . $e->getMessage(), 'warning'); } } AITRFOEL_Logger::log('sync_plan_to_server: Syncing plan "' . $plan . '" to server', 'info'); // Update the plan on the server $result = $this->update_plan_in_remote_db($plan, $license_key); if ($result) { return [ 'success' => true, 'message' => 'Plan synced successfully: ' . $plan, 'plan' => $plan ]; } else { return [ 'success' => false, 'message' => 'Failed to sync plan to server' ]; } } /** * Check if plan needs to be synced and sync if necessary * Called on admin page loads to auto-fix plan mismatches */ public function maybe_sync_plan() { // Only run in admin context, not during AJAX if (!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) { return; } // Only run if Freemius is available and registered with a paid license if (!function_exists('eatp_fs') || !eatp_fs()->is_registered()) { return; } $license_key = $this->get_license_key(); if (empty($license_key)) { return; } // If a sync was explicitly requested in URL (like fs_action=sync_license or activated=true), bypass 1-hour throttle $force_sync = !empty($_GET['activated']) || (!empty($_GET['fs_action']) && strpos((string)$_GET['fs_action'], 'sync') !== false); if (!$force_sync) { $last_sync_check = get_transient('aitrfoel_last_plan_sync_check'); if ($last_sync_check) { return; } set_transient('aitrfoel_last_plan_sync_check', time(), HOUR_IN_SECONDS); } // Get current plan from Freemius $freemius_plan = 'Free'; try { $license = eatp_fs()->_get_license(); if ($license && isset($license->plan_id) && !empty($license->plan_id)) { $plan_obj = eatp_fs()->_get_plan_by_id($license->plan_id); if ($plan_obj && isset($plan_obj->name)) { $freemius_plan = $plan_obj->name; } } if ($freemius_plan === 'Free') { $plan_name = eatp_fs()->get_plan_name(); if ($plan_name && $plan_name !== 'PLAN_NAME' && $plan_name !== 'free') { $freemius_plan = $plan_name; } } } catch (Exception $e) { return; // Don't try to sync if we can't get the plan } // Get current plan from server $server_usage = $this->get_usage_from_server($license_key); $server_plan = isset($server_usage['plan']) ? $server_usage['plan'] : 'Unknown'; // If plans don't match and Freemius has a valid paid plan, sync it if ($freemius_plan !== 'Free' && strcasecmp($freemius_plan, $server_plan) !== 0) { AITRFOEL_Logger::log(sprintf( 'Plan mismatch detected! Freemius: %s, Server: %s - Auto-syncing...', $freemius_plan, $server_plan ), 'warning'); $this->update_plan_in_remote_db($freemius_plan, $license_key); $this->clear_status_cache(); } } /** * AJAX handler for manual plan sync */ public function ajax_sync_plan() { // Security check if (!current_user_can('manage_options')) { wp_send_json_error(['message' => 'Unauthorized']); } check_ajax_referer('aitrfoel_nonce', 'nonce'); // Clear the sync check transient to force a fresh sync delete_transient('aitrfoel_last_plan_sync_check'); $result = $this->sync_plan_to_server(); if ($result['success']) { wp_send_json_success($result); } else { wp_send_json_error($result); } } }