You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
170 lines
5.5 KiB
170 lines
5.5 KiB
<?php
|
|
/*
|
|
Plugin Name: Enterwell Prize
|
|
*/
|
|
|
|
function enterwell_prize_admin_assets() {
|
|
//wp_enqueue_script( 'enterwell-prize-admin', plugin_dir_url( __FILE__ ) . 'js/admin.js', array( 'jquery' ), '1.0.0', true );
|
|
}
|
|
add_action( 'admin_enqueue_scripts', 'enterwell_prize_admin_assets' );
|
|
|
|
function enterwell_prize_public_assets() {
|
|
wp_enqueue_script( 'jquery' );
|
|
wp_enqueue_script( 'enterwell-prize-form', plugin_dir_url( __FILE__ ) . 'js/form.js', array( 'jquery' ), '1.0.0', true );
|
|
wp_enqueue_script( 'enterwell-prize-image-upload', plugin_dir_url( __FILE__ ) . 'js/image-upload.js', array( 'jquery' ), '1.0.0', true );
|
|
wp_enqueue_script( 'country-select', plugin_dir_url( __FILE__ ) . 'js/countrySelect.min.js', array( 'jquery' ), '1.0.0', true );
|
|
wp_enqueue_style( 'country-select', plugin_dir_url( __FILE__ ) . 'css/countrySelect.min.css' );
|
|
wp_enqueue_style( 'enterwell-prize', plugin_dir_url( __FILE__ ) . 'css/enterwell-prize.css' );
|
|
}
|
|
add_action( 'wp_enqueue_scripts', 'enterwell_prize_public_assets' );
|
|
|
|
|
|
// Add menu item
|
|
add_action('admin_menu', 'enterwell_prize_menu');
|
|
function enterwell_prize_menu() {
|
|
add_menu_page('Enterwell Prize', 'Enterwell Prize', 'manage_options', 'enterwell-prize', 'enterwell_prize_page');
|
|
}
|
|
|
|
// Admin page content
|
|
function enterwell_prize_page() {
|
|
|
|
$form_entries = get_option('enterwell_prize_entries', array());
|
|
|
|
// Prepare data for the template
|
|
$entries = array();
|
|
foreach ($form_entries as $entry) {
|
|
$image_id = '';
|
|
if (!empty($entry['image_id'])) {
|
|
$image_id = wp_get_attachment_image_src($entry['image_id'], 'thumbnail')[0];
|
|
}
|
|
$entries[] = array(
|
|
'id' => uniqid(),
|
|
'image_id' => $image_id,
|
|
'receipt_number' => $entry['receipt_number'],
|
|
'first_name' => $entry['first_name'],
|
|
'last_name' => $entry['last_name'],
|
|
'address' => $entry['address'],
|
|
'house_number' => $entry['house_number'],
|
|
'city' => $entry['city'],
|
|
'zip' => $entry['zip'],
|
|
'country' => $entry['country'],
|
|
'phone' => $entry['phone'],
|
|
'email' => $entry['email']
|
|
);
|
|
}
|
|
|
|
ob_start();
|
|
include(plugin_dir_path(__FILE__) . 'templates/enterwell-prize-admin.php');
|
|
$output = ob_get_contents();
|
|
ob_end_clean();
|
|
|
|
echo $output;
|
|
}
|
|
|
|
// Add shortcode
|
|
add_shortcode('enterwell_prize', 'enterwell_prize_shortcode');
|
|
function enterwell_prize_shortcode() {
|
|
ob_start();
|
|
include plugin_dir_path( __FILE__ ) . 'templates/enterwell-prize-form.php';
|
|
$output = ob_get_contents();
|
|
ob_end_clean();
|
|
return $output;
|
|
}
|
|
|
|
// Add REST API endpoint
|
|
|
|
// Submit
|
|
add_action('rest_api_init', 'enterwell_prize_rest_api_init');
|
|
function enterwell_prize_rest_api_init() {
|
|
register_rest_route('enterwell-prize/v1', '/submit', array(
|
|
'methods' => 'POST',
|
|
'callback' => 'enterwell_prize_submit',
|
|
));
|
|
}
|
|
|
|
// Receipt number and email value check
|
|
add_action( 'rest_api_init', 'enterwell_prize_value_check_api' );
|
|
function enterwell_prize_value_check_api() {
|
|
register_rest_route( 'enterwell-prize/v1', '/value-check', array(
|
|
'methods' => 'POST',
|
|
'callback' => 'enterwell_prize_value_check',
|
|
) );
|
|
}
|
|
|
|
function enterwell_prize_value_check( WP_REST_Request $request ) {
|
|
$receipt_number = $request->get_param( 'receipt_number' );
|
|
$email = $request->get_param( 'email' );
|
|
$result = 'not_exists';
|
|
|
|
$option_value = get_option( 'enterwell_prize_entries' );
|
|
|
|
foreach ($option_value as $entry) {
|
|
if ( isset( $entry['receipt_number'] ) && ! empty( $entry['receipt_number'] ) && $entry['receipt_number'] === $receipt_number ) {
|
|
$result = 'exists';
|
|
break; // exit the loop if a matching entry is found for receipt number
|
|
} elseif ( isset( $entry['email'] ) && ! empty( $entry['email'] ) && $entry['email'] === $email ) {
|
|
$result = 'exists';
|
|
break; // exit the loop if a matching entry is found for email
|
|
}
|
|
}
|
|
|
|
return new WP_REST_Response( $result, 200 );
|
|
}
|
|
|
|
|
|
// Handle form submission
|
|
function enterwell_prize_submit($request) {
|
|
$image = $_FILES['image'];
|
|
$receipt_number = sanitize_text_field( $request['receipt_number'] );
|
|
$first_name = sanitize_text_field( $request['first_name'] );
|
|
$last_name = sanitize_text_field( $request['last_name'] );
|
|
$address = sanitize_text_field( $request['address'] );
|
|
$house_number = sanitize_text_field( $request['house_number'] );
|
|
$city = sanitize_text_field( $request['city'] );
|
|
$zip = sanitize_text_field( $request['zip'] );
|
|
$country = sanitize_text_field( $request['country'] );
|
|
$phone = sanitize_text_field( $request['phone'] );
|
|
$email = sanitize_email( $request['email'] );
|
|
|
|
|
|
// Save image
|
|
$image_id = '';
|
|
if ($image['size'] > 0) {
|
|
require_once ABSPATH . 'wp-admin/includes/image.php';
|
|
$image_id = media_handle_upload('image', 0);
|
|
}
|
|
|
|
// Save form entry
|
|
$form_entries = get_option('enterwell_prize_entries', array());
|
|
array_push($form_entries, array(
|
|
'id' => uniqid(),
|
|
'image_id' => $image_id,
|
|
'receipt_number' => $receipt_number,
|
|
'first_name' => $first_name,
|
|
'last_name' => $last_name,
|
|
'address' => $address,
|
|
'house_number' => $house_number,
|
|
'city' => $city,
|
|
'zip' => $zip,
|
|
'country' => $country,
|
|
'phone' => $phone,
|
|
'email' => $email
|
|
));
|
|
update_option('enterwell_prize_entries', $form_entries);
|
|
|
|
return 'Form submitted successfully!';
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|