Files
Roman Pyrih 7483681901 first commit
2026-04-21 15:48:41 +02:00

265 lines
8.5 KiB
PHP

<?php
/**
* Inquiry Custom Post Type
*
* @package YachtBooking
*/
namespace YachtBooking;
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Inquiry CPT class
*/
class Inquiry {
/**
* Register custom post type
*/
public static function register() {
register_post_type(
'yacht_inquiry',
array(
'labels' => array(
'name' => __( 'Zapytania', 'yacht-booking' ),
'singular_name' => __( 'Zapytanie', 'yacht-booking' ),
),
'public' => false,
'publicly_queryable' => false,
'show_ui' => false,
'show_in_menu' => false,
'show_in_rest' => false,
'capability_type' => 'post',
'capabilities' => array(
'edit_post' => 'yacht_booking_manage_bookings',
'read_post' => 'yacht_booking_manage_bookings',
'delete_post' => 'yacht_booking_manage_bookings',
'edit_posts' => 'yacht_booking_manage_bookings',
'edit_others_posts' => 'yacht_booking_manage_bookings',
'publish_posts' => 'yacht_booking_manage_bookings',
'read_private_posts' => 'yacht_booking_manage_bookings',
'delete_posts' => 'yacht_booking_manage_bookings',
),
'has_archive' => false,
'supports' => array( 'title', 'custom-fields' ),
)
);
}
/**
* Get yacht ID.
*
* @param int $inquiry_id Inquiry post ID.
* @return int
*/
public static function get_yacht_id( $inquiry_id ) {
return (int) get_post_meta( $inquiry_id, '_inquiry_yacht_id', true );
}
/**
* Get customer name.
*
* @param int $inquiry_id Inquiry post ID.
* @return string
*/
public static function get_customer_name( $inquiry_id ) {
return get_post_meta( $inquiry_id, '_inquiry_customer_name', true );
}
/**
* Get customer email.
*
* @param int $inquiry_id Inquiry post ID.
* @return string
*/
public static function get_customer_email( $inquiry_id ) {
return get_post_meta( $inquiry_id, '_inquiry_customer_email', true );
}
/**
* Get customer phone.
*
* @param int $inquiry_id Inquiry post ID.
* @return string
*/
public static function get_customer_phone( $inquiry_id ) {
return get_post_meta( $inquiry_id, '_inquiry_customer_phone', true );
}
/**
* Get message.
*
* @param int $inquiry_id Inquiry post ID.
* @return string
*/
public static function get_message( $inquiry_id ) {
return get_post_meta( $inquiry_id, '_inquiry_message', true );
}
/**
* Get preferred dates.
*
* @param int $inquiry_id Inquiry post ID.
* @return string
*/
public static function get_preferred_dates( $inquiry_id ) {
return get_post_meta( $inquiry_id, '_inquiry_preferred_dates', true );
}
/**
* Get email sent to admin body.
*
* @param int $inquiry_id Inquiry post ID.
* @return string
*/
public static function get_admin_email_body( $inquiry_id ) {
return get_post_meta( $inquiry_id, '_inquiry_admin_email_body', true );
}
/**
* Get email sent to customer body.
*
* @param int $inquiry_id Inquiry post ID.
* @return string
*/
public static function get_customer_email_body( $inquiry_id ) {
return get_post_meta( $inquiry_id, '_inquiry_customer_email_body', true );
}
/**
* Create new inquiry.
*
* @param array $data Inquiry data.
* @return int|false Inquiry ID on success, false on failure.
*/
public static function create( $data ) {
$required = array( 'yacht_id', 'customer_name', 'customer_email', 'customer_phone' );
foreach ( $required as $field ) {
if ( empty( $data[ $field ] ) ) {
return false;
}
}
$yacht = get_post( $data['yacht_id'] );
if ( ! $yacht || 'yacht' !== $yacht->post_type ) {
return false;
}
$inquiry_id = wp_insert_post(
array(
'post_type' => 'yacht_inquiry',
'post_title' => sprintf(
__( 'Zapytanie #%s - %s', 'yacht-booking' ),
time(),
$yacht->post_title
),
'post_status' => 'publish',
)
);
if ( is_wp_error( $inquiry_id ) ) {
return false;
}
update_post_meta( $inquiry_id, '_inquiry_yacht_id', (int) $data['yacht_id'] );
update_post_meta( $inquiry_id, '_inquiry_customer_name', sanitize_text_field( $data['customer_name'] ) );
update_post_meta( $inquiry_id, '_inquiry_customer_email', sanitize_email( $data['customer_email'] ) );
update_post_meta( $inquiry_id, '_inquiry_customer_phone', sanitize_text_field( $data['customer_phone'] ) );
if ( ! empty( $data['message'] ) ) {
update_post_meta( $inquiry_id, '_inquiry_message', sanitize_textarea_field( $data['message'] ) );
}
if ( ! empty( $data['preferred_dates'] ) ) {
update_post_meta( $inquiry_id, '_inquiry_preferred_dates', sanitize_text_field( $data['preferred_dates'] ) );
}
return $inquiry_id;
}
/**
* Send inquiry emails (admin + customer) and store copies.
*
* @param int $inquiry_id Inquiry ID.
*/
public static function send_emails( $inquiry_id ) {
$inquiry_id = absint( $inquiry_id );
$yacht_id = self::get_yacht_id( $inquiry_id );
$yacht = get_post( $yacht_id );
$yacht_name = $yacht ? $yacht->post_title : __( 'Nieznany', 'yacht-booking' );
$customer_name = self::get_customer_name( $inquiry_id );
$customer_email = self::get_customer_email( $inquiry_id );
$customer_phone = self::get_customer_phone( $inquiry_id );
$message = self::get_message( $inquiry_id );
$preferred_dates = self::get_preferred_dates( $inquiry_id );
$site_name = get_bloginfo( 'name' );
$from_name = Settings::get_email_from_name();
$from_address = Settings::get_email_from_address();
$headers = array(
'Content-Type: text/html; charset=UTF-8',
'From: ' . $from_name . ' <' . $from_address . '>',
);
// --- Admin email ---
$admin_subject = sprintf(
/* translators: 1: inquiry ID, 2: site name */
__( '[Zapytanie o rezerwację] #%1$d - %2$s', 'yacht-booking' ),
$inquiry_id,
$site_name
);
$admin_body = '<p>' . __( 'Otrzymano nowe zapytanie o rezerwację jachtu.', 'yacht-booking' ) . '</p>';
$admin_body .= '<p><strong>' . __( 'Jacht:', 'yacht-booking' ) . '</strong> ' . esc_html( $yacht_name ) . '</p>';
if ( $preferred_dates ) {
$admin_body .= '<p><strong>' . __( 'Preferowane terminy:', 'yacht-booking' ) . '</strong> ' . esc_html( $preferred_dates ) . '</p>';
}
$admin_body .= '<p><strong>' . __( 'Dane klienta:', 'yacht-booking' ) . '</strong><br>';
$admin_body .= esc_html__( 'Imię i nazwisko:', 'yacht-booking' ) . ' ' . esc_html( $customer_name ) . '<br>';
$admin_body .= esc_html__( 'Email:', 'yacht-booking' ) . ' ' . esc_html( $customer_email ) . '<br>';
$admin_body .= esc_html__( 'Telefon:', 'yacht-booking' ) . ' ' . esc_html( $customer_phone ) . '</p>';
if ( $message ) {
$admin_body .= '<p><strong>' . __( 'Wiadomość:', 'yacht-booking' ) . '</strong><br>' . nl2br( esc_html( $message ) ) . '</p>';
}
$admin_body .= '<p><a href="' . esc_url( admin_url( 'admin.php?page=yacht-inquiries' ) ) . '">'
. __( 'Przejdź do listy zapytań', 'yacht-booking' ) . '</a></p>';
$admin_headers = $headers;
$admin_headers[] = 'Reply-To: ' . $customer_name . ' <' . $customer_email . '>';
wp_mail( get_option( 'admin_email' ), $admin_subject, $admin_body, $admin_headers );
update_post_meta( $inquiry_id, '_inquiry_admin_email_body', $admin_body );
// --- Customer confirmation email ---
$customer_subject = sprintf(
/* translators: 1: site name */
__( 'Potwierdzenie zapytania o rezerwację - %s', 'yacht-booking' ),
$site_name
);
$customer_body = '<p>' . sprintf(
/* translators: %s: customer name */
__( 'Witaj %s,', 'yacht-booking' ),
esc_html( $customer_name )
) . '</p>';
$customer_body .= '<p>' . __( 'Dziękujemy za przesłanie zapytania o rezerwację jachtu. Otrzymaliśmy Twoje zgłoszenie i skontaktujemy się z Tobą najszybciej jak to możliwe.', 'yacht-booking' ) . '</p>';
$customer_body .= '<p><strong>' . __( 'Szczegóły zapytania:', 'yacht-booking' ) . '</strong><br>';
$customer_body .= esc_html__( 'Jacht:', 'yacht-booking' ) . ' ' . esc_html( $yacht_name ) . '</p>';
if ( $preferred_dates ) {
$customer_body .= '<p>' . esc_html__( 'Preferowane terminy:', 'yacht-booking' ) . ' ' . esc_html( $preferred_dates ) . '</p>';
}
if ( $message ) {
$customer_body .= '<p>' . esc_html__( 'Twoja wiadomość:', 'yacht-booking' ) . '<br>' . nl2br( esc_html( $message ) ) . '</p>';
}
$customer_body .= '<p>' . __( 'Pozdrawiamy,', 'yacht-booking' ) . '<br>' . esc_html( $site_name ) . '</p>';
wp_mail( $customer_email, $customer_subject, $customer_body, $headers );
update_post_meta( $inquiry_id, '_inquiry_customer_email_body', $customer_body );
}
}