Skip to content

Commit

Permalink
Internal: AI - Status check data shouldn't be cached [ED-14923]
Browse files Browse the repository at this point in the history
  • Loading branch information
matipojo committed May 23, 2024
1 parent 73bc390 commit 4fa8815
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 19 deletions.
2 changes: 1 addition & 1 deletion core/common/modules/connect/apps/base-app.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ protected function http_request( $method, $endpoint, $args = [], $options = [] )
$args
);

if ( is_wp_error( $response ) ) {
if ( is_wp_error( $response ) && empty( $options['with_error_data'] ) ) {
// PHPCS - the variable $response does not contain a user input value.
wp_die( $response, [ 'back_link' => true ] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
Expand Down
2 changes: 2 additions & 0 deletions modules/ai/assets/js/editor/api/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ const request = ( endpoint, data = {}, immediately = false, signal ) => {
data.context = window.elementorAiCurrentContext;
}

data.editor_session_id = window.EDITOR_SESSION_ID;

return new Promise( ( resolve, reject ) => {
const ajaxData = elementorCommon.ajax.addRequest(
endpoint,
Expand Down
2 changes: 1 addition & 1 deletion modules/ai/assets/js/editor/context/requests-ids.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export const getUniqueId = ( prefix ) => {
return prefix + '-' + Math.random().toString( 16 ).substr( 2, 7 );
};

window.EDITOR_SESSION_ID = window.EDITOR_SESSION_ID || getUniqueId( 'editor-session' );
window.EDITOR_SESSION_ID = window.EDITOR_SESSION_ID || window.ElementorAiConfig?.editor_session_id || getUniqueId( 'editor-session' );

export function generateIds( template ) {
template.id = getUniqueId().toString();
Expand Down
18 changes: 4 additions & 14 deletions modules/ai/connect/ai.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,20 @@ protected function get_api_url() {
return static::API_URL . '/';
}

public function get_usage() {
public function get_usage( $client_name, $client_session_id ) {
return $this->ai_request(
'POST',
'status/check',
[
'api_version' => ELEMENTOR_VERSION,
'site_lang' => get_bloginfo( 'language' ),
'client_name' => esc_attr( $client_name ),
'client_version' => ELEMENTOR_VERSION,
'client_session_id' => esc_attr( $client_session_id ),
]
);
}

public function get_cached_usage() {
$cache_key = 'elementor_ai_usage';
$cache_time = 24 * HOUR_IN_SECONDS;
$usage = get_site_transient( $cache_key );

if ( ! $usage ) {
$usage = $this->get_usage();
set_site_transient( $cache_key, $usage, $cache_time );
}

return $usage;
}

public function get_remote_config() {
return $this->ai_request(
'GET',
Expand Down
12 changes: 9 additions & 3 deletions modules/ai/module.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,20 @@ private function enqueue_main_script() {
true
);

$session_id = 'editor-session-' . Utils::generate_random_string();

$config = [
'is_get_started' => User::get_introduction_meta( 'ai_get_started' ),
'connect_url' => $this->get_ai_connect_url(),
'editor_session_id' => $session_id,
];

if ( $this->get_ai_app()->is_connected() ) {
// Use a cached version, don't call the API on every editor load.
$config['usage'] = $this->get_ai_app()->get_cached_usage();
$usage = $this->get_ai_app()->get_usage( 'elementor-loader', $session_id );

if ( ! is_wp_error( $usage ) ) {
$config['usage'] = $usage;
}
}

wp_localize_script(
Expand Down Expand Up @@ -250,7 +256,7 @@ public function ajax_ai_get_user_information( $data ) {
];
}

$user_usage = wp_parse_args( $app->get_usage(), [
$user_usage = wp_parse_args( $app->get_usage( 'elementor-editor', $data['editor_session_id'] ), [
'hasAiSubscription' => false,
'usedQuota' => 0,
'quota' => 100,
Expand Down

0 comments on commit 4fa8815

Please sign in to comment.