__( 'You do not have permission to perform this action.', 'ai-internal-links' ), ) ); } // Get and validate input. $content = isset( $_POST['content'] ) ? wp_kses_post( wp_unslash( $_POST['content'] ) ) : ''; $post_id = isset( $_POST['post_id'] ) ? intval( $_POST['post_id'] ) : 0; if ( empty( $content ) ) { wp_send_json_error( array( 'message' => __( 'Content is required.', 'ai-internal-links' ), ) ); } // Get link generator instance. $generator = new AIIL_Link_Generator(); // Generate suggestions. $result = $generator->generate( $content, $post_id ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message(), ) ); } wp_send_json_success( array( 'suggestions' => $result, 'count' => count( $result ), ) ); } /** * Test API credentials. */ public static function test_credentials() { check_ajax_referer( 'aiil_settings', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'ai-internal-links' ), ) ); } $provider_name = isset( $_POST['provider'] ) ? sanitize_text_field( wp_unslash( $_POST['provider'] ) ) : ''; $credentials = isset( $_POST['credentials'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_POST['credentials'] ) ) : array(); if ( empty( $provider_name ) ) { wp_send_json_error( array( 'message' => __( 'Provider is required.', 'ai-internal-links' ), ) ); } $provider = AIIL_LLM_Factory::get_provider( $provider_name ); if ( is_wp_error( $provider ) ) { wp_send_json_error( array( 'message' => $provider->get_error_message(), ) ); } $result = $provider->validate_credentials( $credentials ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message(), ) ); } wp_send_json_success( array( 'message' => __( 'Credentials validated successfully!', 'ai-internal-links' ), ) ); } }