PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / modules / ai-studio / REST / AiCredit.php

AiCredit.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/ai-studio/REST/AiCredit.php

83 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\AiStudio\REST;
4
5 use Templately\API\API;
6
7 class AiCredit extends API {
8
9 /**
10 * AI Studio — credit balance and usage history.
11 *
12 * Both upstream operations already accept `api_key` via the backend's
13 * `Helper::tokenOrApi()`, so unlike the workspace/subscription operations
14 * these needed no schema change to be callable from the plugin.
15 *
16 * Note this is the GraphQL surface, not the `v2/ai/available-credits` REST
17 * endpoint that `LogoGeneration` uses — that one returns only a single
18 * `available_credit` integer, while AI Studio needs the full stats payload
19 * (subscription vs purchased split, totals, plan interval).
20 */
21 public function register_routes() {
22 $this->get( 'ai-credit/stats', [ $this, 'get_stats' ] );
23 $this->get( 'ai-credit/history', [ $this, 'get_history' ] );
24 }
25
26 /**
27 * Credit balance.
28 *
29 * `stats` comes back as a JSON *string* which the client parses — same
30 * contract the web dashboard's `available-credit.tsx` works against. It is
31 * decoded here so the React side gets a real object and does not have to
32 * repeat the parse-and-guard dance.
33 */
34 public function get_stats() {
35 $response = $this->http()->query( 'getAvailableCredit', 'stats', [
36 'api_key' => $this->api_key,
37 ] )->post();
38
39 if ( is_wp_error( $response ) ) {
40 return $this->error( 'invalid_ai_credit_stats_response', $response->get_error_message(), 'ai-credit/stats', 400 );
41 }
42
43 $stats = [];
44
45 if ( ! empty( $response['stats'] ) ) {
46 $decoded = json_decode( $response['stats'], true );
47
48 if ( is_array( $decoded ) ) {
49 $stats = $decoded;
50 }
51 }
52
53 return $this->success( [ 'stats' => $stats ] );
54 }
55
56 /**
57 * Paginated credit usage history.
58 */
59 public function get_history() {
60 $page = $this->get_param( 'page', 1, 'intval' );
61 // Clamped: `per_page` reaches the upstream API verbatim, and an unbounded
62 // value lets any caller ask it for the whole table in one request.
63 $per_page = max( 1, min( 100, $this->get_param( 'per_page', 10, 'intval' ) ) );
64 $page = max( 1, $page );
65
66 $response = $this->http()->mutation(
67 'getAiCreditUseHistory',
68 'current_page, total_page, data{ id, template_name, template_type, credit_cost, created_at }',
69 [
70 'api_key' => $this->api_key,
71 'page' => $page,
72 'per_page' => $per_page,
73 ]
74 )->post();
75
76 if ( is_wp_error( $response ) ) {
77 return $this->error( 'invalid_ai_credit_history_response', $response->get_error_message(), 'ai-credit/history', 400 );
78 }
79
80 return $this->success( $response );
81 }
82 }
83