ruạṛ
<?php /** * Innovative Equipment Theme Functions */ // ===== THEME SETUP ===== function ie_theme_setup() { add_theme_support('title-tag'); add_theme_support('post-thumbnails'); add_theme_support('custom-logo', array( 'height' => 80, 'width' => 250, 'flex-height' => true, 'flex-width' => true, )); add_theme_support('html5', array('search-form', 'comment-list', 'gallery', 'caption')); register_nav_menus(array( 'primary' => __('Primary Menu', 'innovative-equip'), 'footer' => __('Footer Menu', 'innovative-equip'), )); add_image_size('product-thumb', 400, 400, true); add_image_size('product-large', 800, 800, false); add_image_size('category-thumb', 600, 400, true); } add_action('after_setup_theme', 'ie_theme_setup'); // ===== ENQUEUE STYLES & SCRIPTS ===== function ie_enqueue_assets() { wp_enqueue_style('google-fonts', 'https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap', array(), null); wp_enqueue_style('ie-style', get_stylesheet_uri(), array('google-fonts'), '1.0.0'); wp_enqueue_script('ie-main', get_template_directory_uri() . '/js/main.js', array(), '1.0.0', true); wp_localize_script('ie-main', 'ieAjax', array( 'ajaxurl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('ie_form_nonce'), )); } add_action('wp_enqueue_scripts', 'ie_enqueue_assets'); // ===== CUSTOM POST TYPE: PRODUCT ===== function ie_register_product_cpt() { $labels = array( 'name' => 'Products', 'singular_name' => 'Product', 'menu_name' => 'Products', 'add_new' => 'Add New Product', 'add_new_item' => 'Add New Product', 'edit_item' => 'Edit Product', 'new_item' => 'New Product', 'view_item' => 'View Product', 'search_items' => 'Search Products', 'not_found' => 'No products found', 'not_found_in_trash' => 'No products found in trash', ); $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'show_in_menu' => true, 'show_in_rest' => true, 'query_var' => true, 'rewrite' => array('slug' => 'product'), 'capability_type' => 'post', 'has_archive' => true, 'hierarchical' => false, 'menu_position' => 5, 'menu_icon' => 'dashicons-products', 'supports' => array('title', 'editor', 'thumbnail', 'excerpt'), ); register_post_type('product', $args); } add_action('init', 'ie_register_product_cpt'); // ===== CUSTOM TAXONOMY: PRODUCT CATEGORY ===== function ie_register_product_taxonomy() { $labels = array( 'name' => 'Product Categories', 'singular_name' => 'Product Category', 'search_items' => 'Search Categories', 'all_items' => 'All Categories', 'parent_item' => 'Parent Category', 'parent_item_colon' => 'Parent Category:', 'edit_item' => 'Edit Category', 'update_item' => 'Update Category', 'add_new_item' => 'Add New Category', 'new_item_name' => 'New Category Name', 'menu_name' => 'Categories', ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_in_rest' => true, 'show_admin_column' => true, 'query_var' => true, 'rewrite' => array('slug' => 'product-category'), ); register_taxonomy('product_category', array('product'), $args); } add_action('init', 'ie_register_product_taxonomy'); // ===== PRODUCT META BOXES ===== function ie_add_product_meta_boxes() { add_meta_box( 'ie_product_details', 'Product Details', 'ie_product_details_callback', 'product', 'normal', 'high' ); } add_action('add_meta_boxes', 'ie_add_product_meta_boxes'); function ie_product_details_callback($post) { wp_nonce_field('ie_product_details', 'ie_product_details_nonce'); $part_number = get_post_meta($post->ID, '_ie_part_number', true); $short_desc = get_post_meta($post->ID, '_ie_short_description', true); ?> <table class="form-table"> <tr> <th><label for="ie_part_number">Part Number / SKU</label></th> <td><input type="text" id="ie_part_number" name="ie_part_number" value="<?php echo esc_attr($part_number); ?>" class="regular-text" placeholder="e.g. 200.30645"></td> </tr> <tr> <th><label for="ie_short_description">Short Description</label></th> <td><textarea id="ie_short_description" name="ie_short_description" rows="3" class="large-text" placeholder="Brief description for catalog cards"><?php echo esc_textarea($short_desc); ?></textarea></td> </tr> </table> <?php } function ie_save_product_details($post_id) { if (!isset($_POST['ie_product_details_nonce']) || !wp_verify_nonce($_POST['ie_product_details_nonce'], 'ie_product_details')) return; if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; if (!current_user_can('edit_post', $post_id)) return; if (isset($_POST['ie_part_number'])) { update_post_meta($post_id, '_ie_part_number', sanitize_text_field($_POST['ie_part_number'])); } if (isset($_POST['ie_short_description'])) { update_post_meta($post_id, '_ie_short_description', sanitize_textarea_field($_POST['ie_short_description'])); } } add_action('save_post_product', 'ie_save_product_details'); // ===== AJAX: SUPPLY ASAP FORM ===== function ie_handle_supply_asap() { check_ajax_referer('ie_form_nonce', 'nonce'); $name = sanitize_text_field($_POST['name'] ?? ''); $email = sanitize_email($_POST['email'] ?? ''); $phone = sanitize_text_field($_POST['phone'] ?? ''); $company = sanitize_text_field($_POST['company'] ?? ''); $product = sanitize_text_field($_POST['product_name'] ?? ''); $part_no = sanitize_text_field($_POST['part_number'] ?? ''); $quantity = sanitize_text_field($_POST['quantity'] ?? ''); $address = sanitize_textarea_field($_POST['address'] ?? ''); $notes = sanitize_textarea_field($_POST['notes'] ?? ''); if (empty($name) || empty($email) || empty($phone)) { wp_send_json_error('Please fill in all required fields.'); } $to = 'brad@thecomputerdoctor.co.nz'; $subject = 'Supply ASAP Request: ' . $product; $headers = array('Content-Type: text/html; charset=UTF-8', 'Reply-To: ' . $name . ' <' . $email . '>'); $body = " <h2>Supply ASAP Request</h2> <table style='border-collapse:collapse;width:100%'> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Product</td><td style='padding:8px;border:1px solid #ddd'>{$product}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Part Number</td><td style='padding:8px;border:1px solid #ddd'>{$part_no}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Name</td><td style='padding:8px;border:1px solid #ddd'>{$name}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Email</td><td style='padding:8px;border:1px solid #ddd'>{$email}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Phone</td><td style='padding:8px;border:1px solid #ddd'>{$phone}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Company</td><td style='padding:8px;border:1px solid #ddd'>{$company}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Quantity</td><td style='padding:8px;border:1px solid #ddd'>{$quantity}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Delivery Address</td><td style='padding:8px;border:1px solid #ddd'>{$address}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Notes</td><td style='padding:8px;border:1px solid #ddd'>{$notes}</td></tr> </table> "; $sent = wp_mail($to, $subject, $body, $headers); if ($sent) { wp_send_json_success('Your request has been submitted. We will process it ASAP!'); } else { wp_send_json_error('Failed to send. Please call us on 0274 321 966.'); } } add_action('wp_ajax_ie_supply_asap', 'ie_handle_supply_asap'); add_action('wp_ajax_nopriv_ie_supply_asap', 'ie_handle_supply_asap'); // ===== AJAX: GET A QUOTE FORM ===== function ie_handle_get_quote() { check_ajax_referer('ie_form_nonce', 'nonce'); $name = sanitize_text_field($_POST['name'] ?? ''); $email = sanitize_email($_POST['email'] ?? ''); $phone = sanitize_text_field($_POST['phone'] ?? ''); $company = sanitize_text_field($_POST['company'] ?? ''); $product = sanitize_text_field($_POST['product_name'] ?? ''); $part_no = sanitize_text_field($_POST['part_number'] ?? ''); $quantity = sanitize_text_field($_POST['quantity'] ?? ''); $specs = sanitize_textarea_field($_POST['specifications'] ?? ''); if (empty($name) || empty($email) || empty($phone)) { wp_send_json_error('Please fill in all required fields.'); } $to = 'brad@thecomputerdoctor.co.nz'; $subject = 'Quote Request: ' . $product; $headers = array('Content-Type: text/html; charset=UTF-8', 'Reply-To: ' . $name . ' <' . $email . '>'); $body = " <h2>Quote Request</h2> <table style='border-collapse:collapse;width:100%'> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Product</td><td style='padding:8px;border:1px solid #ddd'>{$product}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Part Number</td><td style='padding:8px;border:1px solid #ddd'>{$part_no}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Name</td><td style='padding:8px;border:1px solid #ddd'>{$name}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Email</td><td style='padding:8px;border:1px solid #ddd'>{$email}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Phone</td><td style='padding:8px;border:1px solid #ddd'>{$phone}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Company</td><td style='padding:8px;border:1px solid #ddd'>{$company}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Quantity</td><td style='padding:8px;border:1px solid #ddd'>{$quantity}</td></tr> <tr><td style='padding:8px;border:1px solid #ddd;font-weight:bold'>Specifications/Notes</td><td style='padding:8px;border:1px solid #ddd'>{$specs}</td></tr> </table> "; $sent = wp_mail($to, $subject, $body, $headers); if ($sent) { wp_send_json_success('Your quote request has been submitted. We\'ll be in touch shortly!'); } else { wp_send_json_error('Failed to send. Please call us on 0274 321 966.'); } } add_action('wp_ajax_ie_get_quote', 'ie_handle_get_quote'); add_action('wp_ajax_nopriv_ie_get_quote', 'ie_handle_get_quote'); // ===== AJAX: CONTACT FORM ===== function ie_handle_contact_form() { check_ajax_referer('ie_form_nonce', 'nonce'); $name = sanitize_text_field($_POST['name'] ?? ''); $email = sanitize_email($_POST['email'] ?? ''); $phone = sanitize_text_field($_POST['phone'] ?? ''); $message = sanitize_textarea_field($_POST['message'] ?? ''); if (empty($name) || empty($email) || empty($message)) { wp_send_json_error('Please fill in all required fields.'); } $to = 'brad@thecomputerdoctor.co.nz'; $subject = 'Website Contact: ' . $name; $headers = array('Content-Type: text/html; charset=UTF-8', 'Reply-To: ' . $name . ' <' . $email . '>'); $body = " <h2>Contact Form Inquiry</h2> <p><strong>Name:</strong> {$name}</p> <p><strong>Email:</strong> {$email}</p> <p><strong>Phone:</strong> {$phone}</p> <p><strong>Message:</strong></p> <p>{$message}</p> "; $sent = wp_mail($to, $subject, $body, $headers); if ($sent) { wp_send_json_success('Message sent! We\'ll get back to you shortly.'); } else { wp_send_json_error('Failed to send. Please call us on 0274 321 966.'); } } add_action('wp_ajax_ie_contact_form', 'ie_handle_contact_form'); add_action('wp_ajax_nopriv_ie_contact_form', 'ie_handle_contact_form'); // ===== FLUSH REWRITE RULES ON ACTIVATION ===== function ie_rewrite_flush() { ie_register_product_cpt(); ie_register_product_taxonomy(); flush_rewrite_rules(); } add_action('after_switch_theme', 'ie_rewrite_flush'); // ===== PRODUCT ARCHIVE SETTINGS ===== function ie_product_archive_settings($query) { if (!is_admin() && $query->is_main_query()) { if (is_post_type_archive('product') || is_tax('product_category')) { $query->set('posts_per_page', 12); $query->set('orderby', 'title'); $query->set('order', 'ASC'); } } } add_action('pre_get_posts', 'ie_product_archive_settings'); // ===== EXCERPT LENGTH ===== function ie_excerpt_length($length) { return 20; } add_filter('excerpt_length', 'ie_excerpt_length'); // ===== HELPER: GET PRODUCT CATEGORIES WITH COUNT ===== function ie_get_product_categories() { return get_terms(array( 'taxonomy' => 'product_category', 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC', )); }
cải xoăn