HEX
Server: Apache/2.4.52 (Ubuntu)
System: Linux spn-python 5.15.0-89-generic #99-Ubuntu SMP Mon Oct 30 20:42:41 UTC 2023 x86_64
User: arjun (1000)
PHP: 8.1.2-1ubuntu2.20
Disabled: NONE
Upload Files
File: /var/www/html/raya-health/wp-content/themes/raya-health/functions.php
<?php

function raya_contact_scripts() {
     wp_enqueue_style('parsley-custom-css', get_template_directory_uri() . '/css/parsley.css', array(), null);
    // Parsley for validation
    wp_enqueue_script('parsley-js', 'https://cdn.jsdelivr.net/npm/parsleyjs@2.9.2/dist/parsley.min.js', array('jquery'), null, true);

    // Custom contact form script
    wp_enqueue_script('raya-contact', get_template_directory_uri() . '/js/raya-contact.js', array('jquery', 'parsley-js'), null, true);

    wp_localize_script('raya-contact', 'raya_ajax_obj', array(
        'ajax_url' => admin_url('admin-ajax.php'),
        'contact_nonce' => wp_create_nonce('raya_contact_nonce'),
        'newsletter_nonce' => wp_create_nonce('raya_newsletter_nonce')
    ));
}
add_action('wp_enqueue_scripts', 'raya_contact_scripts');
// Handle "Send a Message"
function raya_send_message() {
    check_ajax_referer('raya_contact_nonce', 'nonce');
    parse_str($_POST['form_data'], $data);

    $first = sanitize_text_field($data['first_name']);
    $last = sanitize_text_field($data['last_name']);
    $company = sanitize_text_field($data['company']);
    $email = sanitize_text_field($data['email']);
    $message = sanitize_textarea_field($data['message']);
    

    if (empty($first) || empty($last) || empty($message) || empty($email)) {
        wp_send_json_error(['message' => 'Please fill in all required fields.']);
    }

    $admin_email = get_option('admin_email');
    $subject = "New Message from $first $last";
    $body = "Name: $first $last\nCompany: $company\nEmail: $email\nMessage:\n$message";
    $headers = ['Content-Type: text/plain; charset=UTF-8'];

    $sent = wp_mail($admin_email, $subject, $body, $headers);

    if ($sent) {
        wp_send_json_success(['message' => 'Your message has been sent successfully!']);
    } else {
        wp_send_json_error(['message' => 'Failed to send message.']);
    }
}
add_action('wp_ajax_raya_send_message', 'raya_send_message');
add_action('wp_ajax_nopriv_raya_send_message', 'raya_send_message');


// Handle "Schedule a Call"
function raya_schedule_call() {
    check_ajax_referer('raya_contact_nonce', 'nonce');
    parse_str($_POST['form_data'], $data);

    $first = sanitize_text_field($data['first_name']);
    $last = sanitize_text_field($data['last_name']);
    $email = sanitize_email($data['email']);

    if (empty($first) || empty($last) || empty($email)) {
        wp_send_json_error(['message' => 'Please fill in all required fields.']);
    }

    $admin_email = get_option('admin_email');
    $subject = "New Call Schedule Request from $first $last";
    $body = "Name: $first $last\nEmail: $email";
    $headers = ['Content-Type: text/plain; charset=UTF-8'];

    $sent = wp_mail($admin_email, $subject, $body, $headers);

    if ($sent) {
        wp_send_json_success(['message' => 'Your request has been submitted successfully!']);
    } else {
        wp_send_json_error(['message' => 'Failed to submit your request.']);
    }
}
add_action('wp_ajax_raya_schedule_call', 'raya_schedule_call');
add_action('wp_ajax_nopriv_raya_schedule_call', 'raya_schedule_call');

function raya_newsletter_submit() {
    // die('dd');
    check_ajax_referer('raya_newsletter_nonce', 'nonce');

    $email = sanitize_email($_POST['email']);

    if (!is_email($email)) {
        wp_send_json_error(array('message' => 'Please enter a valid email address.'));
    }

    // Example: Save to database or send email
    global $wpdb;
    $table = $wpdb->prefix . 'newsletter';
    $wpdb->query("CREATE TABLE IF NOT EXISTS $table (id INT AUTO_INCREMENT PRIMARY KEY, email VARCHAR(255), created_at DATETIME DEFAULT CURRENT_TIMESTAMP)");

    $exists = $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM $table WHERE email = %s", $email));
    if ($exists) {
        wp_send_json_error(array('message' => 'This email is already subscribed.'));
    }

    $insert = $wpdb->insert($table, array('email' => $email));
    if ($insert) {
        // Send confirmation email
        $subject = "Subscription Confirmation";
        $message = "Hello!\n\nThank you for subscribing to our newsletter.\n\n- Raya Health";
        $headers = array('Content-Type: text/plain; charset=UTF-8');

        wp_mail($email, $subject, $message, $headers);

        wp_send_json_success(array('message' => 'Thank you for subscribing! A confirmation email has been sent.'));
    } else {
        wp_send_json_error(array('message' => 'Unable to save your subscription. Please try again.'));
    }
}
add_action('wp_ajax_raya_newsletter_submit', 'raya_newsletter_submit');
add_action('wp_ajax_nopriv_raya_newsletter_submit', 'raya_newsletter_submit');

// Register Testimonials Custom Post Type
function raya_register_testimonials() {
    $labels = array(
        'name'               => 'Testimonials',
        'singular_name'      => 'Testimonial',
        'menu_name'          => 'Testimonials',
        'name_admin_bar'     => 'Testimonial',
        'add_new'            => 'Add New',
        'add_new_item'       => 'Add New Testimonial',
        'new_item'           => 'New Testimonial',
        'edit_item'          => 'Edit Testimonial',
        'view_item'          => 'View Testimonial',
        'all_items'          => 'All Testimonials',
        'search_items'       => 'Search Testimonials',
    );

    $args = array(
        'labels'             => $labels,
        'public'             => true,
        'show_in_rest'       => true, // Enables Gutenberg or ACF blocks
        'supports'           => array('title', 'editor', 'thumbnail'),
        'menu_icon'          => 'dashicons-testimonial',
        'has_archive'        => false,
        'rewrite'            => array('slug' => 'testimonials'),
    );

    register_post_type('testimonial', $args);
}
add_action('init', 'raya_register_testimonials');
function raya_testimonial_meta_box() {
    add_meta_box('testimonial_details', 'Testimonial Details', 'raya_testimonial_meta_callback', 'testimonial');
}
add_action('add_meta_boxes', 'raya_testimonial_meta_box');

function raya_testimonial_meta_callback($post) {
    $name = get_post_meta($post->ID, '_author_name', true);
    $location = get_post_meta($post->ID, '_author_location', true);
    ?>
    <p><label for="author_name">Author Name:</label></p>
    <input type="text" name="author_name" id="author_name" value="<?php echo esc_attr($name); ?>" style="width:100%;">
    <p><label for="author_location">Author Location:</label></p>
    <input type="text" name="author_location" id="author_location" value="<?php echo esc_attr($location); ?>" style="width:100%;">
    <?php
}

function raya_save_testimonial_meta($post_id) {
    if (array_key_exists('author_name', $_POST)) {
        update_post_meta($post_id, '_author_name', sanitize_text_field($_POST['author_name']));
    }
    if (array_key_exists('author_location', $_POST)) {
        update_post_meta($post_id, '_author_location', sanitize_text_field($_POST['author_location']));
    }
}
add_action('save_post', 'raya_save_testimonial_meta');


function raya_register_pricing_plan_cpt() {
    $labels = array(
        'name' => 'Pricing Plans',
        'singular_name' => 'Pricing Plan',
        'add_new' => 'Add New Plan',
        'add_new_item' => 'Add New Pricing Plan',
        'edit_item' => 'Edit Pricing Plan',
        'new_item' => 'New Pricing Plan',
        'view_item' => 'View Pricing Plan',
        'search_items' => 'Search Pricing Plans',
        'not_found' => 'No Pricing Plans found',
        'menu_name' => 'Pricing Plans',
    );

    $args = array(
        'labels' => $labels,
        'public' => true,
        'menu_icon' => 'dashicons-tag',
        'supports' => array('title', 'custom-fields'),
        'has_archive' => false,
    );

    register_post_type('pricing_plan', $args);
}
add_action('init', 'raya_register_pricing_plan_cpt');
function raya_add_pricing_plan_meta_box() {
    add_meta_box(
        'raya_pricing_plan_details',
        'Plan Details',
        'raya_pricing_plan_meta_box_callback',
        'pricing_plan',
        'normal',
        'high'
    );
}
add_action('add_meta_boxes', 'raya_add_pricing_plan_meta_box');

function raya_pricing_plan_meta_box_callback($post) {
    $yearly_price = get_post_meta($post->ID, 'yearly_price', true);
    $monthly_price = get_post_meta($post->ID, 'monthly_price', true);
    $trial_text = get_post_meta($post->ID, 'trial_text', true);
    $cancel_text = get_post_meta($post->ID, 'cancel_text', true);
    $yearly_features = get_post_meta($post->ID, 'yearly_features', true);
    $monthly_features = get_post_meta($post->ID, 'monthly_features', true);
    ?>
    <p>
        <label><strong>Yearly Price ($):</strong></label><br>
        <input type="text" name="yearly_price" value="<?php echo esc_attr($yearly_price); ?>" style="width:100%;">
    </p>
    <p>
        <label><strong>Monthly Price ($):</strong></label><br>
        <input type="text" name="monthly_price" value="<?php echo esc_attr($monthly_price); ?>" style="width:100%;">
    </p>
    <p>
        <label><strong>Trial Text:</strong></label><br>
        <input type="text" name="trial_text" value="<?php echo esc_attr($trial_text); ?>" style="width:100%;">
    </p>
    <p>
        <label><strong>Cancel Text:</strong></label><br>
        <input type="text" name="cancel_text" value="<?php echo esc_attr($cancel_text); ?>" style="width:100%;">
    </p>
    <p>
        <label><strong>Yearly Features (one per line):</strong></label><br>
        <textarea name="yearly_features" rows="6" style="width:100%;"><?php echo esc_textarea($yearly_features); ?></textarea>
    </p>
    <p>
        <label><strong>Monthly Features (one per line):</strong></label><br>
        <textarea name="monthly_features" rows="6" style="width:100%;"><?php echo esc_textarea($monthly_features); ?></textarea>
    </p>
    <?php
}

function raya_save_pricing_plan_meta_box($post_id) {
    if(array_key_exists('yearly_price', $_POST)) update_post_meta($post_id, 'yearly_price', sanitize_text_field($_POST['yearly_price']));
    if(array_key_exists('monthly_price', $_POST)) update_post_meta($post_id, 'monthly_price', sanitize_text_field($_POST['monthly_price']));
    if(array_key_exists('trial_text', $_POST)) update_post_meta($post_id, 'trial_text', sanitize_text_field($_POST['trial_text']));
    if(array_key_exists('cancel_text', $_POST)) update_post_meta($post_id, 'cancel_text', sanitize_text_field($_POST['cancel_text']));
    if(array_key_exists('yearly_features', $_POST)) update_post_meta($post_id, 'yearly_features', sanitize_textarea_field($_POST['yearly_features']));
    if(array_key_exists('monthly_features', $_POST)) update_post_meta($post_id, 'monthly_features', sanitize_textarea_field($_POST['monthly_features']));
}
add_action('save_post', 'raya_save_pricing_plan_meta_box');

add_action('admin_menu', 'raya_newsletter_admin_menu');

function raya_newsletter_admin_menu() {
    add_menu_page(
        'Newsletter Subscribers',    // Page title
        'Newsletter',                // Menu title
        'manage_options',            // Capability
        'raya-newsletter',           // Menu slug
        'raya_newsletter_list_page', // Callback function
        'dashicons-email-alt',       // Icon
        25                           // Position
    );
}
function raya_newsletter_list_page() {
    global $wpdb;
    $table = $wpdb->prefix . 'newsletter';

    $subscribers = $wpdb->get_results("SELECT * FROM $table ORDER BY id ASC"); 
    ?>

    <div class="wrap">
        <h1>Newsletter Subscribers</h1>
        <table class="wp-list-table widefat fixed striped">
            <thead>
                <tr>
                    <th>ID</th>
                    <th>Email</th>
                    <th>Subscribed On</th>
                </tr>
            </thead>
            <tbody>
                <?php if(!empty($subscribers)) : ?>
                    <?php foreach($subscribers as $sub) : ?>
                        <tr>
                            <td><?php echo esc_html($sub->id); ?></td>
                            <td><?php echo esc_html($sub->email); ?></td>
                            <td><?php echo esc_html($sub->created_at); ?></td>
                        </tr>
                    <?php endforeach; ?>
                <?php else: ?>
                    <tr><td colspan="3">No subscribers yet.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php
}