'Unauthorized' ) ); } $id = sanitize_text_field( wp_unslash( $_POST['id'] ?? '' ) ); $phrase = trim( str_replace( ',', ' ', sanitize_text_field( wp_unslash( $_POST['phrase'] ?? '' ) ) ) ); $entry = '' !== $id ? cwp_ai_knowledge()->get( $id ) : null; if ( ! $entry || '' === $phrase ) { wp_send_json_error( array( 'message' => 'Pick an entry and type a question first' ) ); } $keywords = (array) $entry['keywords']; if ( ! in_array( strtolower( $phrase ), array_map( 'strtolower', $keywords ), true ) ) { $keywords[] = mb_substr( $phrase, 0, 120 ); } if ( ! cwp_ai_knowledge()->update( $id, $entry['question'], $entry['answer'], $keywords ) ) { wp_send_json_error( array( 'message' => 'That entry could not be updated' ) ); } wp_send_json_success( array( 'message' => 'Added. Questions like this now find that entry.' ) ); } );