key = $pro_key; $this->base_url = LLMSTXT_API_URL; } /** * Verify a Pro licence key against the API. * Returns array with 'valid', 'plan', 'expires' or WP_Error. */ public function verify_key(): array|WP_Error { $response = wp_remote_get( $this->base_url . 'license.php?action=verify&key=' . urlencode( $this->key ), [ 'timeout' => 15, 'sslverify' => true ] ); if ( is_wp_error( $response ) ) return $response; $code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code !== 200 || empty( $body ) ) { return new WP_Error( 'api_error', __( 'API unreachable', 'aiready-llms-txt-generator' ) ); } return $body; } /** * Generate llms.txt via API for a given URL. * Returns array ['llms.txt' => '...', 'llms-full.txt' => '...'] or WP_Error. */ public function generate( string $url ): array|WP_Error { $response = wp_remote_post( $this->base_url . 'wp-generate.php', [ 'timeout' => 60, 'sslverify' => true, 'headers' => [ 'Content-Type' => 'application/json', 'X-LLMSTXT-Key' => $this->key, 'X-LLMSTXT-Source' => 'wp-plugin/' . LLMSTXT_VERSION, ], 'body' => wp_json_encode( [ 'url' => $url, 'lang' => get_locale(), 'formats' => [ 'llms.txt', 'llms-full.txt' ], 'source' => 'wordpress', ] ), ] ); if ( is_wp_error( $response ) ) return $response; $code = wp_remote_retrieve_response_code( $response ); $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code === 401 ) { return new WP_Error( 'invalid_key', __( 'Invalid or expired Pro key.', 'aiready-llms-txt-generator' ) ); } if ( $code === 429 ) { return new WP_Error( 'rate_limit', __( 'Rate limit reached. Please try again later.', 'aiready-llms-txt-generator' ) ); } if ( $code !== 200 || empty( $body['files'] ) ) { $msg = $body['error'] ?? __( 'Unknown API error.', 'aiready-llms-txt-generator' ); return new WP_Error( 'api_error', $msg ); } return $body['files']; // ['llms.txt' => '...', 'llms-full.txt' => '...'] } /** * Get usage stats for the connected Pro account. */ public function get_usage(): array|WP_Error { $response = wp_remote_get( $this->base_url . 'license.php?action=usage&key=' . urlencode( $this->key ), [ 'timeout' => 10 ] ); if ( is_wp_error( $response ) ) return $response; $body = json_decode( wp_remote_retrieve_body( $response ), true ); return $body ?: new WP_Error( 'api_error', 'Could not fetch usage data.' ); } }