$advwl_opt_value) { update_option($advwl_opt_name, $advwl_opt_value); } advwl_success_message($advwl_success); add_action('admin_notices', 'advwl_success_message'); } /** * Upload file */ function advwl_upload_file() { // Verify nonce for security if ( ! isset( $_POST['security'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['security'] ) ), 'advwl-form' ) ) { wp_send_json_error( [ 'message' => __( 'Security check failed.', 'advwl-wishlist-for-woocommerce' ) ] ); } // Validate file input if ( ! isset( $_FILES['advwl_icon'] ) || ! isset( $_POST['advwl_option_name'] ) ) { wp_send_json_error( [ 'message' => __( 'Invalid request.', 'advwl-wishlist-for-woocommerce' ) ] ); } // Sanitize option name $advwl_opt_name = sanitize_text_field( wp_unslash( $_POST['advwl_option_name'] ) ); // Sanitize and validate file data $advwl_file_data = $_FILES['advwl_icon']; $advwl_file_name = sanitize_file_name( $advwl_file_data['name'] ); $advwl_file_type = sanitize_mime_type( $advwl_file_data['type'] ); $advwl_file_tmp_name = $advwl_file_data['tmp_name']; $advwl_file_error = absint( $advwl_file_data['error'] ); $advwl_file_size = absint( $advwl_file_data['size'] ); // Validate upload errors if ( $advwl_file_error !== UPLOAD_ERR_OK ) { wp_send_json_error( [ 'message' => __( 'File upload error.', 'advwl-wishlist-for-woocommerce' ) ] ); } // Validate file type $allowed_types = [ 'image/jpeg', 'image/png', 'image/gif', 'image/svg+xml' ]; if ( ! in_array( $advwl_file_type, $allowed_types, true ) ) { wp_send_json_error( [ 'message' => __( 'Invalid file type.', 'advwl-wishlist-for-woocommerce' ) ] ); } // Validate file size (2MB limit example) if ( $advwl_file_size > 2 * 1024 * 1024 ) { wp_send_json_error( [ 'message' => __( 'File size exceeds 2MB limit.', 'advwl-wishlist-for-woocommerce' ) ] ); } // Handle upload $advwl_upload = wp_upload_bits( $advwl_file_name, null, file_get_contents( $advwl_file_tmp_name ) ); if ( ! empty( $advwl_upload['error'] ) ) { wp_send_json_error( [ 'message' => __( 'Upload failed.', 'advwl-wishlist-for-woocommerce' ) ] ); } // Sanitize URL $advwl_file_url = esc_url_raw( $advwl_upload['url'] ); // Save option securely update_option( $advwl_opt_name, $advwl_file_url ); // Return escaped URL in JSON wp_send_json_success( [ 'file_url' => esc_url( $advwl_file_url ) ] ); } add_action( 'wp_ajax_advwl_upload_file', 'advwl_upload_file' ); /** * Display success message */ function advwl_success_message($advwl_success) { ?>