0 && ( time() - $at ) < self::MAX_AGE ) { return; } if ( ! function_exists( 'as_enqueue_async_action' ) || ! function_exists( 'as_has_scheduled_action' ) ) { return; } if ( ! as_has_scheduled_action( self::REFRESH_HOOK, array(), 'aieo' ) ) { as_enqueue_async_action( self::REFRESH_HOOK, array(), 'aieo' ); } } public static function maybe_schedule() { if ( ! wp_next_scheduled( self::CRON_HOOK ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CRON_HOOK ); } } public static function unschedule() { $ts = wp_next_scheduled( self::CRON_HOOK ); if ( $ts ) { wp_unschedule_event( $ts, self::CRON_HOOK ); } } public static function is_enabled() { return '1' === (string) get_option( self::OPT_ENABLED, '0' ); } public static function email() { $e = trim( (string) get_option( self::OPT_EMAIL, '' ) ); if ( is_email( $e ) ) { return $e; } $r = trim( (string) get_option( 'aieo_rooster_client_email', '' ) ); return is_email( $r ) ? $r : (string) get_option( 'admin_email' ); } public static function threshold_usd() { $t = (float) get_option( self::OPT_THRESHOLD, 5 ); return $t > 0 ? $t : 5.0; } public static function fetch_status( $force = false ) { if ( ! $force ) { $c = get_transient( self::STATUS_TRANSIENT ); if ( is_array( $c ) ) { return $c; } } $key = trim( (string) get_option( 'aieo_rooster_api_key', '' ) ); $hub = trim( (string) get_option( 'aieo_rooster_hub_url', 'https://rooster.systems' ) ); if ( '' === $key ) { return new WP_Error( 'aieo_no_token', __( 'The Rooster Content Connector token is not configured.', 'ai-eshop-optimizer' ) ); } if ( '' === $hub ) { $hub = 'https://rooster.systems'; } $status_url = add_query_arg( array( 'site_url' => rawurlencode( home_url() ), 'fp' => class_exists( 'AIEO_Rooster_Signup' ) ? AIEO_Rooster_Signup::fp() : '', 'src' => class_exists( 'AIEO_Rooster_Signup' ) ? rawurlencode( AIEO_Rooster_Signup::source_string() ) : '', 'runtime' => class_exists( 'AIEO_Rooster_Signup' ) ? AIEO_Rooster_Signup::runtime_flag() : 0, ), rtrim( $hub, '/' ) . '/wp-json/om-portal/v1/anthropic/status' ); $r = wp_remote_get( $status_url, array( 'headers' => array( 'X-OM-API-Key' => $key, 'Accept' => 'application/json' ), 'timeout' => 15, 'sslverify' => (bool) apply_filters('aieo_portal_sslverify', true), ) ); if ( is_wp_error( $r ) ) { return $r; } $code = (int) wp_remote_retrieve_response_code( $r ); $body = json_decode( (string) wp_remote_retrieve_body( $r ), true ); if ( ! is_array( $body ) || $code < 200 || $code >= 300 ) { return new WP_Error( 'aieo_bad_status', sprintf( __( 'Rooster returned HTTP %d.', 'ai-eshop-optimizer' ), $code ) ); } foreach ( array( 'email', 'partner_email', 'client_email', 'contact_email' ) as $ek ) { if ( ! empty( $body[ $ek ] ) && is_email( $body[ $ek ] ) ) { if ( (string) get_option( 'aieo_rooster_client_email', '' ) !== $body[ $ek ] ) { update_option( 'aieo_rooster_client_email', $body[ $ek ], false ); } break; } } if ( isset( $body['entitlements']['content'] ) && is_array( $body['entitlements']['content'] ) ) { $ent = $body['entitlements']['content']; $save = array( 'tier' => (string) ( $ent['tier'] ?? '' ), 'expires_at' => (string) ( $ent['expires_at'] ?? '' ), 'trial' => ! empty( $ent['trial'] ), 'fetched_at' => time(), ); if ( ! empty( $ent['sig'] ) ) { $save['sig'] = (string) $ent['sig']; } if ( ! empty( $ent['site'] ) && is_array( $ent['site'] ) ) { $save['site'] = array( 'status' => sanitize_key( (string) ( $ent['site']['status'] ?? '' ) ), 'trial_expires_at' => sanitize_text_field( (string) ( $ent['site']['trial_expires_at'] ?? '' ) ), 'seats_used' => (int) ( $ent['site']['seats_used'] ?? 0 ), 'site_limit' => (int) ( $ent['site']['site_limit'] ?? 0 ), ); } if ( class_exists( 'AIEO_Entitlement_Binding' ) ) { $save = AIEO_Entitlement_Binding::stamp( $save ); } update_option( 'aieo_rooster_entitlement', $save, false ); } if ( isset( $body['entitlements']['documents'] ) && is_array( $body['entitlements']['documents'] ) ) { $docs = $body['entitlements']['documents']; update_option( 'aieo_rooster_entitlement_documents', array( 'enabled' => ! empty( $docs['enabled'] ), 'doc_rate_usd_micros' => (int) ( $docs['doc_rate_usd_micros'] ?? 0 ), 'line_rate_usd_micros' => (int) ( $docs['line_rate_usd_micros'] ?? 0 ), 'included_lines' => (int) ( $docs['included_lines'] ?? 0 ), 'fetched_at' => time(), ), false ); } if ( isset( $body['by_source'] ) && is_array( $body['by_source'] ) ) { update_option( self::OPT_USAGE, array( 'rows' => $body['by_source'], 'at' => time() ), false ); } set_transient( self::STATUS_TRANSIENT, $body, HOUR_IN_SECONDS ); return $body; } public static function usage_by_source( $fetch = true ) { if ( $fetch ) { $status = self::fetch_status(); } else { $status = get_transient( self::STATUS_TRANSIENT ); if ( ! is_array( $status ) || ! isset( $status['by_source'] ) ) { $kept = get_option( self::OPT_USAGE, array() ); $status = array( 'by_source' => is_array( $kept ) && isset( $kept['rows'] ) ? $kept['rows'] : array() ); } } if ( is_wp_error( $status ) || empty( $status['by_source'] ) || ! is_array( $status['by_source'] ) ) { return array(); } $out = array(); foreach ( $status['by_source'] as $row ) { $src = (string) ( $row['source'] ?? '' ); $out[] = array( 'source' => $src, 'label' => class_exists( 'AIEO_AI_Sources' ) ? AIEO_AI_Sources::label( $src ) : $src, 'calls' => (int) ( $row['calls'] ?? 0 ), 'usd' => round( (float) ( $row['cost_micros'] ?? 0 ) / 1000000, 2 ), ); } return $out; } public static function usage_at() { $kept = get_option( self::OPT_USAGE, array() ); return is_array( $kept ) ? (int) ( $kept['at'] ?? 0 ) : 0; } public static function remaining_usd( $status = null ) { if ( null === $status ) { $status = self::fetch_status(); } if ( is_wp_error( $status ) || ! is_array( $status ) ) { return null; } $cap = (float) ( $status['current_cap_usd_micros'] ?? 0 ); $used = (float) ( $status['used_this_month_usd_micros'] ?? 0 ); return max( 0.0, ( $cap - $used ) / 1000000 ); } public static function run_daily_check() { if ( ! self::is_enabled() ) { return; } $status = self::fetch_status( true ); $remaining = self::remaining_usd( $status ); if ( null === $remaining ) { return; } $threshold = self::threshold_usd(); if ( $remaining >= $threshold ) { if ( '' !== (string) get_option( self::OPT_LAST_SENT, '' ) ) { update_option( self::OPT_LAST_SENT, '', false ); } return; } $today = gmdate( 'Y-m-d' ); if ( (string) get_option( self::OPT_LAST_SENT, '' ) === $today ) { return; } if ( self::send_low_balance_email( $remaining, $threshold, $status ) ) { update_option( self::OPT_LAST_SENT, $today, false ); } } public static function send_low_balance_email( $remaining, $threshold, $status = null ) { $to = self::email(); if ( ! is_email( $to ) ) { return false; } $site = get_bloginfo( 'name' ); $subject = sprintf( __( '[%1$s] Low AI credit balance โ€” $%2$s left', 'ai-eshop-optimizer' ), $site, number_format( (float) $remaining, 2 ) ); $cap = is_array( $status ) ? (float) ( $status['current_cap_usd_micros'] ?? 0 ) / 1000000 : 0; $used = is_array( $status ) ? (float) ( $status['used_this_month_usd_micros'] ?? 0 ) / 1000000 : 0; $lines = array(); $lines[] = sprintf( __( 'Your Rooster One credit balance for %s is running low.', 'ai-eshop-optimizer' ), $site ); $lines[] = ''; $lines[] = sprintf( __( 'Remaining this cycle: $%s', 'ai-eshop-optimizer' ), number_format( (float) $remaining, 2 ) ); $lines[] = sprintf( __( 'Your alert threshold: $%s', 'ai-eshop-optimizer' ), number_format( (float) $threshold, 2 ) ); if ( $cap > 0 ) { $lines[] = sprintf( __( 'Monthly cap: $%1$s ยท used so far: $%2$s', 'ai-eshop-optimizer' ), number_format( $cap, 2 ), number_format( $used, 2 ) ); } $lines[] = ''; $hub_url = trim( (string) get_option( 'aieo_rooster_hub_url', 'https://rooster.systems' ) ); if ( '' === $hub_url ) { $hub_url = 'https://rooster.systems'; } $lines[] = sprintf( __( 'Top up or adjust your plan at: %s', 'ai-eshop-optimizer' ), $hub_url ); $lines[] = ''; $lines[] = __( 'You are receiving this because low-balance alerts are enabled in Rooster One โ†’ AI Chat โ†’ Step 1 (API & Credits). While the balance stays low you will get one reminder per day.', 'ai-eshop-optimizer' ); return AIEO_Email_Frame::send_text( $to, $subject, implode( "\n", $lines ), array( 'id' => 'aieo_credit_notice', 'customer' => false ) ); } public static function ajax_save() { if ( ! current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'Unauthorized', 'ai-eshop-optimizer' ) ), 403 ); } check_ajax_referer( 'aieo_save_credit_alert', 'nonce' ); $enabled = ! empty( $_POST['enabled'] ) ? '1' : '0'; $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; $threshold = isset( $_POST['threshold'] ) ? (float) $_POST['threshold'] : 5.0; if ( '' !== $email && ! is_email( $email ) ) { wp_send_json_error( array( 'message' => __( 'That billing email address is not valid.', 'ai-eshop-optimizer' ) ) ); } if ( $threshold < 0 ) { $threshold = 0.0; } update_option( self::OPT_ENABLED, $enabled, false ); update_option( self::OPT_EMAIL, $email, false ); update_option( self::OPT_THRESHOLD, $threshold, false ); if ( '1' === $enabled ) { self::maybe_schedule(); } wp_send_json_success( array( 'message' => __( 'Low-balance alert settings saved.', 'ai-eshop-optimizer' ), 'email' => self::email(), ) ); } }