__( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } $response = ABPW_Api_Client::get( '/config' ); if ( is_wp_error( $response ) ) { wp_send_json_success( array( 'config' => null ) ); } wp_send_json_success( array( 'config' => isset( $response['config'] ) ? $response['config'] : null, ) ); } /** * Handle the abpw_save_config AJAX action. */ public function handle_save_config() { // Verify nonce. check_ajax_referer( 'abpw_onboarding_nonce', 'abpw_nonce' ); // Capability check. if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => __( 'You do not have permission to perform this action.', 'abp-writer-ai-blog-generator' ), ) ); } // Sanitize and validate business description. $business_desc = isset( $_POST['business_desc'] ) ? sanitize_textarea_field( wp_unslash( $_POST['business_desc'] ) ) : ''; if ( mb_strlen( $business_desc ) < 50 ) { wp_send_json_error( array( 'field' => 'business', 'message' => __( 'Business description must be at least 50 characters.', 'abp-writer-ai-blog-generator' ), ) ); } if ( mb_strlen( $business_desc ) > 1000 ) { wp_send_json_error( array( 'field' => 'business', 'message' => __( 'Business description cannot exceed 1000 characters.', 'abp-writer-ai-blog-generator' ), ) ); } // Sanitize location (optional). $location = isset( $_POST['location'] ) ? sanitize_text_field( wp_unslash( $_POST['location'] ) ) : ''; // Validate language. $language = isset( $_POST['language'] ) ? sanitize_text_field( wp_unslash( $_POST['language'] ) ) : ''; if ( ! in_array( $language, self::$allowed_languages, true ) ) { wp_send_json_error( array( 'field' => 'language', 'message' => __( 'Please select a valid language.', 'abp-writer-ai-blog-generator' ), ) ); } // Sanitize and validate keywords. $keywords_raw = isset( $_POST['keywords'] ) ? sanitize_textarea_field( wp_unslash( $_POST['keywords'] ) ) : ''; $keywords = array_values( array_filter( array_map( 'trim', explode( ',', $keywords_raw ) ) ) ); $keyword_count = count( $keywords ); if ( $keyword_count < 5 ) { wp_send_json_error( array( 'field' => 'keywords', 'message' => __( 'Please enter at least 5 keywords.', 'abp-writer-ai-blog-generator' ), ) ); } if ( $keyword_count > 50 ) { wp_send_json_error( array( 'field' => 'keywords', 'message' => __( 'Please enter no more than 50 keywords.', 'abp-writer-ai-blog-generator' ), ) ); } // Validate communication style. $comm_style = isset( $_POST['comm_style'] ) ? sanitize_text_field( wp_unslash( $_POST['comm_style'] ) ) : ''; if ( ! in_array( $comm_style, self::$allowed_styles, true ) ) { wp_send_json_error( array( 'field' => 'style', 'message' => __( 'Please select a valid communication style.', 'abp-writer-ai-blog-generator' ), ) ); } // Send config to AiBlogPress API. $result = ABPW_Api_Client::post( '/config', array( 'business_desc' => $business_desc, 'location' => $location, 'language' => $language, 'keywords' => $keywords, 'comm_style' => $comm_style, ) ); if ( is_wp_error( $result ) ) { wp_send_json_error( array( 'message' => $result->get_error_message(), ) ); } // Mark onboarding as complete. update_option( 'aiblogpress_onboarding_complete', true ); wp_send_json_success( array( 'message' => __( 'Configuration saved successfully.', 'abp-writer-ai-blog-generator' ), 'redirect' => admin_url( 'admin.php?page=abp-writer-topics' ), ) ); } }