<?php
/*
Plugin Name: QR Code Email
Plugin URI: https://yourwebsite.com/qr-code-email
Description: Generates QR codes from JSON and sends them via email.
Version: 1.0
Author: Your Name
Author URI: https://yourwebsite.com
*/
// Register the REST API endpoint
add_action( 'rest_api_init', function() {
register_rest_route( 'qr-code-email/v1', '/generate', array(
'methods' => 'POST',
'callback' => 'qr_code_email_generate',
'permission_callback' => '__return_true', // Allow all users (adjust as needed)
) );
} );
// Endpoint callback function
function qr_code_email_generate( $request ) {
// 1. Verify the nonce
$nonce = $request->get_param( 'nonce' );
if ( ! wp_verify_nonce( $nonce, 'qr_code_email_generate' ) ) {
return new WP_Error( 'nonce_invalid', 'Invalid nonce.', array( 'status' => 400 ) );
}
// 2. Get JSON data
$json_data = $request->get_json_params();
// 3. Generate QR code
$qr_code_data = $json_data['data']; // Assuming 'data' is the key containing the QR code content
$qr_code_path = generate_qr_code( $qr_code_data );
// 4. Send email
$to_email = $json_data['to']; // Email address to send to
$subject = $json_data['subject']; // Email subject
$message = 'QR Code: ' . $qr_code_path; // Email message
$send_result = wp_mail( $to_email, $subject, $message, array( 'Content-Type' => 'text/html' ) );
if ( $send_result ) {
return new WP_REST_Response( array( 'message' => 'QR code generated and email sent successfully.' ), 200 );
} else {
return new WP_Error( 'email_failed', 'Failed to send email.', array( 'status' => 500 ) );
}
}
// Helper function to generate QR code (replace with your actual implementation)
function generate_qr_code( $data ) {
// ... Your QR code generation logic using phpqrcode or similar ...
// Return the path to the generated QR code image
}
// Add nonce to the endpoint request
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
add_action( 'rest_api_init', function() {
wp_enqueue_script( 'qr-code-email-script', plugins_url( '/assets/script.js', __FILE__ ), array( 'jquery' ), '1.0', true );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );
} );